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


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

[python] view plain copy
  1. #! /usr/bin/env python  
  2. import smtplib   
  3. import sys  
  4. from email.mime.text import MIMEText   
  5.    
  6. mailto_list=[""]   
  7.    
  8. mail_host = "smtp.126.com"   
  9. mail_user = "monitor_algo"   
  10. mail_pass = ""   
  11. mail_postfix="126.com"   
  12.    
  13. def send_mail(to_list, sub, context):   
  14.     me = mail_user + "<"+mail_user+"@"+mail_postfix+">"   
  15.     msg = MIMEText(context)   
  16.     msg['Subject'] = sub   
  17.     msg['From'] = me   
  18.     msg['To'] = ";".join(to_list)   
  19.     try:   
  20.         send_smtp = smtplib.SMTP()   
  21.         send_smtp.connect(mail_host)   
  22.         send_smtp.login(mail_user, mail_pass)   
  23.         send_smtp.sendmail(me, to_list, msg.as_string())   
  24.         send_smtp.close()   
  25.         return True   
  26.     except (Exception, e):   
  27.         print(str(e))   
  28.         return False   
  29.   
  30. if __name__=="__main__":  
  31. #    print ("start")   
  32. #    for a in range(1, len(sys.argv)):  
  33. #        print sys.argv[a]  
  34.     if (True == send_mail(mailto_list,sys.argv[1],sys.argv[2])):   
  35.     pass  
  36. #        print ("sucess")  
  37.     else:   
  38.     pass  
  39. #        print ("failed")   


3. 加入crontab定时任务

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

*/1 * * * * . /etc/profile; sh /application/search/monitor.sh
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值