用python監控服務器的cpu,磁盤空間,內存,超過郵件報警

  監控Linux服務器嘛,腳本邏輯基本上是用os.popen模塊,然後把獲取到的結果通過split切分成一個list,再拿目標list值和我閾值對比,超過就郵件報警;

  郵件是通過Linux的mailx發出去的,可自行搜索安裝該模塊,關鍵字:“Linux使用mailx發郵件”,腳本如下: 

一、cpu ideal值,不小於20%

#!/usr/bin/python
# -*- coding: utf-8 -*-
 
import datetime
import os
 
 
f = os.popen('vmstat').readlines()
cpu_ideall = str(f).split()[-3]
if int(cpuideall) > 20:
    mail_content = "echo 'ip:IP地址(vmstat)' | mailx -s '[Warning!]CPU ideal below 20%, please check!' 收件郵箱"
    os.popen(mail_content)
else:
    pass

二、磁盤空間,不大於95%

#!/usr/bin/python
# -*- coding: utf-8 -*-
 
import datetime
import os
 
 
f = os.popen('df -lh').readlines()
s = []
s.append(str(f).split()[11].split('%')[0])
s.append(str(f).split()[-8].split('%')[0])
s.append(str(f).split()[-2].split('%')[0])
print s
 
i = 0
while i < len(s):
    if int(s[i]) > 95:
    mail_content = "echo 'ip:ip地址(df -lh)' | mailx -s '[Warning!]Disk above 95%, please check!' 收件郵件"
        os.popen(mail_content)
    else:
    pass
    i = i + 1

三、內存利用率,不低於200

#!/usr/bin/python
# -*- coding: utf-8 -*-
 
import datetime
import os
 
 
f = os.popen('free -m').readlines()
memm = str(f).split()[10]
if int(memm) < 200:
    mail_content = "echo 'ip:ip地址(free -m)' | mailx -s '[Warning!]MEM below 200, please check!' 收件郵箱"
    os.popen(mail_content)
else:
    pass

以上就是用python監控服務器的cpu,磁盤空間,內存,超過郵件報警的詳細內容,更多關於python監控服務器的資料請關註WalkonNet其它相關文章!