python3監(jiān)控CentOS磁盤空間腳本
Python腳本監(jiān)控CentOS磁盤空間,任何一個(gè)分區(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()
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 用python監(jiān)控服務(wù)器的cpu,磁盤空間,內(nèi)存,超過(guò)郵件報(bào)警
- python實(shí)現(xiàn)磁盤日志清理的示例
- python獲取磁盤號(hào)下盤符步驟詳解
- 使用python獲取電腦的磁盤信息方法
- python3實(shí)現(xiàn)磁盤空間監(jiān)控
- 磁盤垃圾文件清理器python代碼實(shí)現(xiàn)
- Python實(shí)現(xiàn)獲取磁盤剩余空間的2種方法
- Linux 發(fā)郵件磁盤空間監(jiān)控(python)
- Python實(shí)現(xiàn)測(cè)試磁盤性能的方法
- python 遍歷磁盤目錄的三種方法
相關(guān)文章
python實(shí)現(xiàn)整數(shù)的二進(jìn)制循環(huán)移位
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)整數(shù)的二進(jìn)制循環(huán)移位,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-03-03pycharm 2021.3最新激活碼有效期至2100年(親測(cè)可用)
這篇文章主要介紹了pycharm 2021.3最新激活碼有效期至2100年(親測(cè)可用)2021-02-02python實(shí)現(xiàn)字典(dict)和字符串(string)的相互轉(zhuǎn)換方法
這篇文章主要介紹了python實(shí)現(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,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-08-08java虛擬機(jī)中棧的運(yùn)行知識(shí)點(diǎn)總結(jié)
在本篇文章里小編給大家整理的是一篇關(guān)于java虛擬機(jī)中棧的運(yùn)行知識(shí)點(diǎn)總結(jié),有需要的朋友們可以學(xué)習(xí)參考下。2021-06-06Python通過(guò)串口實(shí)現(xiàn)收發(fā)文件
這篇文章主要為大家詳細(xì)介紹了Python如何通過(guò)串口實(shí)現(xiàn)收發(fā)文件功能,文中的示例代碼簡(jiǎn)潔易懂,具有一定的借鑒價(jià)值,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-11-11已安裝Pytorch卻提示no?moudle?named?'torch'(沒(méi)有名稱為torch
這篇文章主要給大家介紹了關(guān)于已安裝Pytorch卻提示no?moudle?named?'torch'(沒(méi)有名稱為torch的模塊)的相關(guān)資料,當(dāng)提示"No module named 'torch'"時(shí),可能是由于安裝的Pytorch版本與當(dāng)前環(huán)境不匹配導(dǎo)致的,需要的朋友可以參考下2023-11-11