python3监控CentOS磁盘空间脚本
2019/7/15 0:37:17
本文主要是介绍python3监控CentOS磁盘空间脚本,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
Python脚本监控CentOS磁盘空间,任何一个分区空间使用大于80%即发邮件给到指定邮箱。
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()
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持找一找教程网。
这篇关于python3监控CentOS磁盘空间脚本的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-17在FastAPI项目中添加一个生产级别的数据库——本地环境搭建指南
- 2024-11-16`PyMuPDF4LLM`:提取PDF数据的神器
- 2024-11-16四种数据科学Web界面框架快速对比:Rio、Reflex、Streamlit和Plotly Dash
- 2024-11-14获取参数学习:Python编程入门教程
- 2024-11-14Python编程基础入门
- 2024-11-14Python编程入门指南
- 2024-11-13Python基础教程
- 2024-11-12Python编程基础指南
- 2024-11-12Python基础编程教程
- 2024-11-08Python编程基础与实践示例