python3監(jiān)控CentOS磁盤空間腳本
Python腳本監(jiān)控CentOS磁盤空間,任何一個分區(qū)空間使用大于80%即發(fā)郵件給到指定郵箱。
monitor.py
#-*- coding: utf-8 -*- import socket import subprocess import smtplib from email.mime.text import MIMEText sender="xxx.xx@xxx.com" receiver= ["xxx.xx@xxx.com"] smtpHost="10.134.xxx.xxx" smtpPort="587" def get_ip(): hostname = socket.getfqdn(socket.gethostname()) ip = socket.gethostbyname(hostname) return ip def send_mail(receiver,subject,content): ip = get_ip() msg = MIMEText(content,_subtype='plain',_charset='utf-8') msg['Subject'] = subject msg['From'] = 'CLOUD SERVER ' + ip msg['To'] = ",".join(receiver) try: smtp = smtplib.SMTP(smtpHost,smtpPort) #smtp.set_debuglevel(1) smtp.docmd("HELO Server") smtp.ehlo("ismetoad") smtp.starttls() smtp.helo("ismetoad") smtp.sendmail(sender,receiver,msg.as_string()) smtp.close() except Exception as error: print(error) def run_cmd(cmd): process = subprocess.Popen(cmd,shell=True, stdout=subprocess.PIPE,stderr=subprocess.PIPE) result_f,error_f = process.stdout,process.stderr errors = error_f.read() if errors: pass result = result_f.read().decode() if result_f: result_f.close() if error_f: error_f.close() return result def disk_check(): subject = '' result = run_cmd(cmd) content = '[root@vm-vc02-SR910 ~]# ' + cmd + '\n' + result result = result.split('\n') for line in result: if 'G ' in line or 'M ' in line: line = line.split() for i in line: if '%' in i and int(i.strip('%')) > 80: subject = '[WARNING] SERVER FILESYSTEM USE% OVER ' + i + ', PLEASE CHECK!' if subject: send_mail(receiver,subject,content) print('email sended') else: print('Everything is ok, keep on monitor.') if __name__ == '__main__': cmd = 'df -h' disk_check()
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
python實現(xiàn)整數(shù)的二進(jìn)制循環(huán)移位
這篇文章主要為大家詳細(xì)介紹了python實現(xiàn)整數(shù)的二進(jìn)制循環(huán)移位,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-03-03pycharm 2021.3最新激活碼有效期至2100年(親測可用)
這篇文章主要介紹了pycharm 2021.3最新激活碼有效期至2100年(親測可用)2021-02-02python實現(xiàn)字典(dict)和字符串(string)的相互轉(zhuǎn)換方法
這篇文章主要介紹了python實現(xiàn)字典(dict)和字符串(string)的相互轉(zhuǎn)換方法,涉及Python字典dict的遍歷與字符串轉(zhuǎn)換相關(guān)操作技巧,需要的朋友可以參考下2017-03-03django foreignkey外鍵使用的例子 相當(dāng)于left join
今天小編就為大家分享一篇django foreignkey外鍵使用的例子 相當(dāng)于left join,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-08-08java虛擬機(jī)中棧的運(yùn)行知識點(diǎn)總結(jié)
在本篇文章里小編給大家整理的是一篇關(guān)于java虛擬機(jī)中棧的運(yùn)行知識點(diǎn)總結(jié),有需要的朋友們可以學(xué)習(xí)參考下。2021-06-06已安裝Pytorch卻提示no?moudle?named?'torch'(沒有名稱為torch
這篇文章主要給大家介紹了關(guān)于已安裝Pytorch卻提示no?moudle?named?'torch'(沒有名稱為torch的模塊)的相關(guān)資料,當(dāng)提示"No module named 'torch'"時,可能是由于安裝的Pytorch版本與當(dāng)前環(huán)境不匹配導(dǎo)致的,需要的朋友可以參考下2023-11-11