当前位置:   article > 正文

Linux常用系统性能监控运维脚本_+ '[' 1 -ne 0 ']' + echo 'usage: sh baseline-run.s

+ '[' 1 -ne 0 ']' + echo 'usage: sh baseline-run.sh ' usage: sh baseline-run

此脚本为上次发布版本的更新版,添加了些内容,增加了用户体验!

使用方法:1.根据自己的实际环境新建自己的目录
        2.将脚本’run.sh‘、'MonitorOS.py'、'alter_diskpath.sh'放到新建的目录下,并添加可执行权限
        3.执行'alter_diskpath.sh'脚本来修改要监控的磁盘挂载目录!(注意:建议一次性的正确输入,此脚本未添加异常处理,执行后输错需要手动进入py脚本中手动修改!参考命令:“df -hT”)
        4.执行'run.sh'脚本来启动监控程序,参考命令:./run.sh start | stop | status | restart
        5.查看当前目录下面的monitoros.log日志来获取您的Linux系统性能信息
        6.日志输出路径可自定义修改,在Crontab.sh脚本中修改即可 (默认路径为当前目录下,默认为每5分钟输出一次)
        7.关于日志变大的清理方法,使用Linux自带的crontab中加入定时清空日志即可
此脚本作者还在持续优化中...... (更新时间2021.11.23)

alter_diskpath.sh代码如下:

  1. #!/bin/bash
  2. read -p '请输入本地环境要监控的磁盘路径:' input
  3. path=$input
  4. eval sed -i 's#DiskPath#${path}#g' MonitorOS.py
  5. echo $?
  6. echo '输入成功!'

Crontab.sh代码如下:

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

run.sh代码如下:

  1. #!/usr/bin/bash
  2. APP_NAME=Crontab.sh
  3. #使用说明,用来提示输入参数
  4. usage() {
  5. echo "Usage: sh 执行脚本.sh [start|stop|restart|status]"
  6. exit 1
  7. }
  8. #检查程序是否在运行
  9. is_exist() {
  10. pid=$(ps -aux | grep ${APP_NAME} | grep -v grep | awk '{print $2}')
  11. #如果不存在返回1,存在返回0
  12. if [[ -z "${pid}" ]]; then
  13. return 1
  14. else
  15. return 0
  16. fi
  17. }
  18. #启动方法
  19. start() {
  20. is_exist
  21. if [[ $? -eq "0" ]]; then
  22. echo "${APP_NAME} is already running. pid is ${pid} ."
  23. else
  24. nohup sh Crontab.sh &
  25. # nohup /home/paas/jdk1.8.0_202/bin/java -jar ${PARAMS} ${APP_NAME} > /dev/null 2>&1 &
  26. status
  27. fi
  28. }
  29. #停止方法
  30. stop() {
  31. is_exist
  32. if [[ $? -eq "0" ]]; then
  33. kill -9 ${pid}
  34. echo "${APP_NAME} is shutdown."
  35. else
  36. echo "${APP_NAME} is not running"
  37. fi
  38. }
  39. #输出运行状态
  40. status() {
  41. is_exist
  42. if [[ $? -eq "0" ]]; then
  43. echo "${APP_NAME} is running. pid is ${pid}"
  44. else
  45. echo "${APP_NAME} is NOT running."
  46. fi
  47. }
  48. #重启
  49. restart() {
  50. stop
  51. start
  52. }
  53. #根据输入参数,选择执行对应方法,不输入则执行使用说明
  54. case "$1" in
  55. "start")
  56. start
  57. ;;
  58. "stop")
  59. stop
  60. ;;
  61. "status")
  62. status
  63. ;;
  64. "restart")
  65. restart
  66. ;;
  67. *)
  68. usage
  69. ;;
  70. esac

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 DiskPath | 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 DiskPath | 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 DiskPath | 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. disk_spaces = ' ['
  22. if use_percent > 80:
  23. print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()),"[WARING]:磁盘容量剩余不足20%,请及时清理空间!" + '\n' + disk_spaces + disk_Space + ' ' + disk_surplus + ']')
  24. else:
  25. print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()),"[INFO]:磁盘容量足够,请放心运行!" + '\n' + disk_spaces + disk_use + ']')
  26. def get_uscpu():
  27. #CPU使用率
  28. cpu = os.popen("iostat | awk 'NR==4{print $6}'")
  29. cpu = cpu.read().strip()
  30. cpu_Use = 100 - float(cpu)
  31. cpu_use = '当前CPU使用率为:' + str(cpu_Use)[:5] + '%'
  32. use_cpu = int(re.search("[0-9]+",cpu_use).group())
  33. cpu_spaces = ' ['
  34. if use_cpu > 80:
  35. print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()),"[WARING]:CPU使用率占用过高,请注意!" + '\n' + cpu_spaces + cpu_use + ']' + '\n' + cpu_spaces + get_cpuus() + ']')
  36. else:
  37. print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()),"[INFO]:CPU使用率占用正常,请放心运行!" + '\n' + cpu_spaces + cpu_use + ']' + '\n' + cpu_spaces + get_cpuus() + ']')
  38. def get_cpuus():
  39. #CPU每分钟使用值
  40. cpu_1 = os.popen("uptime | awk '{print $10}'")
  41. cpu_2 = os.popen("uptime | awk '{print $11}'")
  42. cpu_3 = os.popen("uptime | awk '{print $12}'")
  43. cpu_1 = cpu_1.read().strip()
  44. cpu_2 = cpu_2.read().strip()
  45. cpu_3 = cpu_3.read().strip()
  46. cpu_onemin = '每分钟:'
  47. cpu_fivemin = '5分钟/次:'
  48. cpu_fifteen = '15分钟/次:'
  49. cpu_uptime = 'CPU使用值:' + ' ' + cpu_onemin + ' ' + cpu_1 + ' ' + cpu_fivemin + ' ' + cpu_2 + ' ' + cpu_fifteen + ' ' +cpu_3
  50. return cpu_uptime
  51. def get_usmem():
  52. #内存总量
  53. mem_all = os.popen("free -mg | awk 'NR==2{print $2}'")
  54. mem_all = mem_all.read().strip()
  55. mem_all_use = '内存总大小:' + mem_all
  56. #物理内存
  57. mem = os.popen("free -mg | awk 'NR==2{print $3}'")
  58. mem = mem.read().strip()
  59. mem_use = '当前物理内存已使用:' + mem + 'G'
  60. #交换内存
  61. mem_swap = os.popen("free -mg | awk 'NR==3{print $3}'")
  62. mem_swap = mem_swap.read().strip()
  63. mem_swap_use = '当前交换内存已使用:' + mem_swap + 'G'
  64. #剩余内存
  65. mem_surplus = os.popen("free -mg | awk 'NR==2{print $7}'")
  66. mem_surplus = mem_surplus.read().strip()
  67. mem_surplus_use = '当前物理内存剩余:' + mem_surplus + 'G'
  68. #内存剩余率
  69. mem_surplus_int = mem_surplus
  70. mem_int = mem
  71. mem_mem = float(mem_surplus_int) / float(mem_int)
  72. mem_mem_float = float(mem_mem) * 100
  73. mem_mem_mem = '剩余百分率为:' + str(mem_mem_float)[:2] + '%'
  74. #内存使用率
  75. mem_all_int = mem_all
  76. mem_int_int = mem
  77. mem_all_use_mem = float(mem_int_int) / float(mem_all_int)
  78. mem_all_use_mem_float = float(mem_all_use_mem) * 100
  79. mem_all_use_mem_mem = '内存使用率:' + str(mem_all_use_mem_float)[:2] + '%'
  80. mem_spaces = ' ['
  81. if mem_mem_float < 10:
  82. print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()),'[WARING]:内存占用已超标,请检查!' + '\n' + mem_spaces + mem_all_use + ' ' + mem_surplus_use + ' ' + mem_mem_mem + ']')
  83. else:
  84. print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()),'[INFO]:内存使用正常,请放心运行!' + '\n' + mem_spaces + mem_all_use_mem_mem + ' ' + mem_use + ' ' + mem_swap_use + ']')
  85. if __name__ == '__main__':
  86. try:
  87. get_usage()
  88. except Exception as a:
  89. print('[ERROR]:main方法调用 “get_usage()” 函数失败,磁盘指标监控不可用!')
  90. try:
  91. get_uscpu()
  92. except Exception as b:
  93. print('[ERROR]:main方法调用 “get_uscpu()” 函数失败,cpu指标监控不可用!')
  94. try:
  95. get_usmem()
  96. except Exception as c:
  97. print('[ERROR]:main方法调用 “get_usmem()” 函数失败,内存指标监控不可用!')

感谢使用!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/710842
推荐阅读
相关标签
  

闽ICP备14008679号