WHCSRL 技术网

Linux性能常用监控,自动化运维

作者是小白菜鸡,嘴下留情!
===使用者必读===
此脚本仅供运维人员使用!
在平时的运维过程当中,可使得我们清晰、可视化、自动化来监控Linux系统的性能,进而提升效率和质量!
主要监控的内容:CPU、内存、磁盘、IO
使用方法:1.根据自己的实际环境新建自己的目录
        2.将Shell脚本’run.sh‘和Python脚本'MonitorOS.py'放到新建的目录下,并添加可执行权限
        3.打开MonitorOS.py脚本,修改disk、disk_int、disk_space后面的磁盘路径(根据自己的实际环境修改!否则运行失败)
        4.执行Shell脚本'run.sh'并将起加入后台进程实现定时执行,参考命令“nohup sh run.sh &”
        5.查看当前目录下面的monitoros.log日志来获取您的Linux系统性能信息
        6.日志输出路径可自定义修改,在Shell脚本中修改即可
        7.关于日志变大的清理方法,使用Linux自带的crontab中加入定时清空日志即可

MonitorOS.py代码如下:

  1. #!/usr/bin/python3
  2. #!_*_ coding:utf-8 _*_
  3. #导入使用的模块
  4. import os,re
  5. import time
  6. def get_usage():
  7. disk = os.popen("df -hT /dev/mapper/rhel-home | awk 'NR==2{print $6}'")
  8. disk = disk.read().strip()
  9. #print(time.strftime('%%Y-%%m-%%d %%H:%%M:%%S', time.localtime()),'[INFO]:当前磁盘使用:',disk)
  10. disk_use = '当前磁盘使用:' + disk
  11. disk_int = "df -hT /dev/mapper/rhel-home | awk 'NR==2{print $6}' | awk -F%% '{print $1}'"
  12. disk_shell = os.popen(disk_int)
  13. disk_shell = disk_shell.read().strip()
  14. disk_Surplus = 100 - int(disk_shell)
  15. disk_surplus = '当前磁盘剩余:' + str(disk_Surplus) + '%%'
  16. disk_space = "df -hT | grep /dev/mapper/rhel-home | awk '{print $3}'"
  17. disk_shell_2 = os.popen(disk_space)
  18. disk_shell_2 = disk_shell_2.read().strip()
  19. disk_Space = '总容量:' + disk_shell_2
  20. use_percent = int(re.search("[0-9]+",disk).group()) #分组截取取判断值
  21. if use_percent > 80:
  22. print(time.strftime('%%Y-%%m-%%d %%H:%%M:%%S', time.localtime()),"[WARING]:磁盘容量剩余不足20%%,请及时清理空间!" + ' ' + ' [' + disk_Space + ' ' + disk_surplus + ']')
  23. else:
  24. print(time.strftime('%%Y-%%m-%%d %%H:%%M:%%S', time.localtime()),"[INFO]:磁盘容量足够,请放心运行!" + ' ' + ' [' + disk_use + ']')
  25. def get_uscpu():
  26. #CPU使用率
  27. cpu = os.popen("iostat | awk 'NR==4{print $6}'")
  28. cpu = cpu.read().strip()
  29. cpu_Use = 100 - float(cpu)
  30. cpu_use = '当前CPU使用率为:' + str(cpu_Use)[:5] + '%%'
  31. use_cpu = int(re.search("[0-9]+",cpu_use).group())
  32. if use_cpu > 80:
  33. print(time.strftime('%%Y-%%m-%%d %%H:%%M:%%S', time.localtime()),"[WARING]:CPU使用率占用过高,请注意!" + ' ' + ' [' + cpu_use + ']' + ' ' + ' [' + get_cpuus() + ']')
  34. else:
  35. print(time.strftime('%%Y-%%m-%%d %%H:%%M:%%S', time.localtime()),"[INFO]:CPU使用率占用正常,请放心运行!" + ' ' + ' [' + cpu_use + ']' + ' ' + ' [' + get_cpuus() + ']')
  36. def get_cpuus():
  37. #CPU每分钟使用值
  38. cpu_1 = os.popen("uptime | awk '{print $10}'")
  39. cpu_2 = os.popen("uptime | awk '{print $11}'")
  40. cpu_3 = os.popen("uptime | awk '{print $12}'")
  41. cpu_1 = cpu_1.read().strip()
  42. cpu_2 = cpu_2.read().strip()
  43. cpu_3 = cpu_3.read().strip()
  44. cpu_onemin = '每分钟:'
  45. cpu_fivemin = '5分钟/次:'
  46. cpu_fifteen = '15分钟/次:'
  47. cpu_uptime = 'CPU使用值:' + ' ' + cpu_onemin + ' ' + cpu_1 + ' ' + cpu_fivemin + ' ' + cpu_2 + ' ' + cpu_fifteen + ' ' +cpu_3
  48. return cpu_uptime
  49. def get_usmem():
  50. #内存总量
  51. mem_all = os.popen("free -mh | awk 'NR==2{print $2}'")
  52. mem_all = mem_all.read().strip()
  53. mem_all_use = '内存总大小:' + mem_all
  54. #物理内存
  55. mem = os.popen("free -mh | awk 'NR==2{print $3}'")
  56. mem = mem.read().strip()
  57. mem_use = '当前物理内存已使用:' + mem
  58. #交换内存
  59. mem_swap = os.popen("free -mh | awk 'NR==3{print $3}'")
  60. mem_swap = mem_swap.read().strip()
  61. mem_swap_use = '当前交换内存已使用:' + mem_swap
  62. #剩余内存
  63. mem_surplus = os.popen("free -mh | awk 'NR==2{print $7}'")
  64. mem_surplus = mem_surplus.read().strip()
  65. mem_surplus_use = '当前物理内存剩余:' + mem_surplus
  66. #内存剩余率
  67. mem_surplus_int = mem_surplus[:2]
  68. mem_int = mem[:2]
  69. mem_mem = int(mem_surplus_int) / int(mem_int)
  70. mem_mem_float = float(mem_mem) * 100
  71. mem_mem_mem = '剩余百分率为:' + str(mem_mem_float)[:2] + '%%'
  72. #内存使用率
  73. mem_all_int = mem_all[:2]
  74. mem_int_int = mem[:2]
  75. mem_all_use_mem = int(mem_int_int) / int(mem_all_int)
  76. mem_all_use_mem_float = float(mem_all_use_mem) * 100
  77. mem_all_use_mem_mem = '内存使用率:' + str(mem_all_use_mem_float)[:2] + '%%'
  78. if mem_mem_float < 10:
  79. print(time.strftime('%%Y-%%m-%%d %%H:%%M:%%S', time.localtime()),'[WARING]:内存占用已超标,请检查!' + ' ' + ' [' + mem_all_use + ' ' + mem_surplus_use + ' ' + mem_mem_mem + ']')
  80. else:
  81. print(time.strftime('%%Y-%%m-%%d %%H:%%M:%%S', time.localtime()),'[INFO]:内存使用正常,请放心运行!' + ' ' + ' [' + mem_all_use_mem_mem + ' ' + mem_use + ' ' + mem_swap_use + ']')
  82. if __name__ == '__main__':
  83. get_usage(),
  84. get_uscpu(),
  85. get_usmem()

run.sh代码如下: 

  1. #!/bin/bash
  2. #执行py脚本启动监控
  3. while true
  4. do
  5. python3 MonitorOS.py >> ./monitoros.log
  6. sleep 600
  7. done

 此脚本作者还在持续优化中......

推荐阅读