Linux中使用shell+python实现监控系统负载+邮件报警


1. 获取ip、cpu、内存信息、loadAverage

ip使用ifconfig获得

localip=$(ifconfig eth0 | grep 'inet addr' | awk '{print $2}' | cut -f2 -d:)

空闲cpu由mpstat获得

cpuidle=$(mpstat | grep all | awk '{print $11}')

内存使用情况由free获得

freemem=$(free | grep Mem | awk '{print $4}')

totalmem=$(free | grep Mem | awk '{print $2}')

pcent=$(free | grep Mem | awk '{print $4/$2}')

loadAverage由uptime获得

loadavg1=$(uptime | awk '{print $10}' | cut -f1 -d,)
loadavg5=$(uptime | awk '{print $11}' | cut -f1 -d,)
loadavg15=$(uptime | awk '{print $12}')

[python] view plain copy
  1. ./etc/profile
  2. AlertNginxCpu=50
  3. AlertNginxMem=50
  4. AlertPcent=0.5
  5. AlertCpu=50
  6. AlertLoad=1
  7. msg=
  8. Subject='服务器超载警报'
  9. mailto(){
  10. /application/search/sendmail.py"$Subject""$msg"
  11. msg=
  12. Subject='服务器超载警报'
  13. }
  14. date=$(date)
  15. localip=$(/sbin/ifconfigeth0|grep'inetaddr'|awk'{print$2}'|cut-f2-d:)
  16. nginxcpu=$(psaxu|grepnginx|grepsearch|grepworker|awk'{print$3}')
  17. nginxmempcent=$(psaxu|grepnginx|grepsearch|grepworker|awk'{print$4}')
  18. nginxmem=$(psaxu|grepnginx|grepsearch|grepworker|awk'{print$6}')
  19. cpuidle=$(mpstat|grepall|awk'{print$11}')
  20. freemem=$(free|grepMem|awk'{print$4}')
  21. totalmem=$(free|grepMem|awk'{print$2}')
  22. freemempcent=$(free|grepMem|awk'{print$4/$2}')
  23. freeswap=$(free|grepSwap|awk'{print$4}')
  24. totalswap=$(free|grepSwap|awk'{print$2}')
  25. freeswappcent=$(free|grepSwap|awk'{print$4/$2}')
  26. loadavg1=$(uptime|awk'{print$10}'|cut-f1-d,)
  27. loadavg5=$(uptime|awk'{print$11}'|cut-f1-d,)
  28. loadavg15=$(uptime|awk'{print$12}')
  29. echo$date
  30. echo$localip
  31. echo'空闲cpu:'$cpuidle
  32. echo'空闲内存:'$freemem
  33. echo'总内存:'$totalmem
  34. echo'空闲内存比:'$freemempcent
  35. echo'空闲交换内存:'$freeswap
  36. echo'总交换内存:'$totalswap
  37. echo'空闲交换内存比:'$freeswappcent
  38. echo$loadavg1
  39. echo$loadavg5
  40. echo$loadavg15
  41. alldata=''$date'|'$localip'|nginx:'$nginxcpu','$nginxmempcent'('$nginxmem')|cpu_idle:'$cpuidle'|Mem:'$freemem'/'$totalmem'='$freemempcent'|Swap:'$freeswap'/'$totalswap'='$freeswappcent'|loadavg:'$loadavg1','$loadavg5','$loadavg15''
  42. echo$alldata>>monitor.log
  43. if[$(echo"$nginxcpu>=$AlertNginxCpu"|bc)=1];then
  44. msg=''$alldata'|nginxcpu'$nginxcpu':高于'$AlertNginxCpu''
  45. Subject=''$Subject':'$localip':nginxcpu'$nginxcpu'高于'$AlertNginxCpu''
  46. echo$msg>>monitor.log
  47. mailto
  48. fi
  49. if[$(echo"$nginxmempcent>=$AlertNginxMem"|bc)=1];then
  50. msg=''$alldata'|nginxmem'$nginxmempcent':高于'$AlertNginxMem''
  51. Subject=''$Subject':'$localip':nginxmem'$nginxmempcent'高于'$AlertNginxMem''
  52. echo$msg>>monitor.log
  53. mailto
  54. fi
  55. if[$(echo"$freeswappcent<=$AlertPcent"|bc)=1];then
  56. msg=''$alldata'|剩余虚拟内存占比'$freeswappcent':低于'$AlertPcent''
  57. Subject=''$Subject':'$localip':剩余虚拟内存占比'$freeswappcent'低于'$AlertPcent''
  58. echo$msg>>monitor.log
  59. mailto
  60. fi
  61. if[$(echo"$cpuidle<=$AlertCpu"|bc)=1];then
  62. msg=''$alldata'|cpu空闲时间'$cpuidle':低于'$AlertCpu''
  63. Subject=''$Subject':'$localip':cpu空闲时间'$cpuidle'低于'$AlertCpu''
  64. echo$msg>>monitor.log
  65. mailto
  66. fi
  67. if[$(echo"$loadavg1>=$AlertLoad"|bc)=1];then
  68. msg=''$alldata'|load'$loadavg1':高于'$AlertLoad''
  69. Subject=''$Subject':'$localip':load'$loadavg1'高于'$AlertLoad''
  70. echo$msg>>monitor.log
  71. mailto
  72. fi
  73. if[-n$msg];then
  74. echo'系统运行正常'
  75. fi


2. 空闲cpu小于cpu报警阈值或空闲内存比例低于内存报警阈值或loadAverage超过阈值时发送邮件报警

[python] view plain copy
  1. #!/usr/bin/envpython
  2. importsmtplib
  3. importsys
  4. fromemail.mime.textimportMIMEText
  5. mailto_list=[""]
  6. mail_host="smtp.126.com"
  7. mail_user="monitor_algo"
  8. mail_pass=""
  9. mail_postfix="126.com"
  10. defsend_mail(to_list,sub,context):
  11. me=mail_user+"<"+mail_user+"@"+mail_postfix+">"
  12. msg=MIMEText(context)
  13. msg['Subject']=sub
  14. msg['From']=me
  15. msg['To']=";".join(to_list)
  16. try:
  17. send_smtp=smtplib.SMTP()
  18. send_smtp.connect(mail_host)
  19. send_smtp.login(mail_user,mail_pass)
  20. send_smtp.sendmail(me,to_list,msg.as_string())
  21. send_smtp.close()
  22. returnTrue
  23. except(Exception,e):
  24. print(str(e))
  25. returnFalse
  26. if__name__=="__main__":
  27. #print("start")
  28. #forainrange(1,len(sys.argv)):
  29. #printsys.argv[a]
  30. if(True==send_mail(mailto_list,sys.argv[1],sys.argv[2])):
  31. pass
  32. #print("sucess")
  33. else:
  34. pass
  35. #print("failed")


3. 加入crontab定时任务

注意一点,crontan中的环境变量与用户环境的环境变量不一样,因其不会从缺省的用户profile文件中读入环境变量参数,最简单的方法是用source命令(.)

*/1 * * * * . /etc/profile; sh /application/search/monitor.sh

你可能感兴趣的:(python)