这里写的是俩个模块,需要调用模块,但是俩个模块都必须在同一目录下
import subprocess
import mail
def use():a=subprocess.getoutput("""df -h | grep '^/dev/m' | awk '{print $5}'| tr -d "%" """)a= int(a)if a > 20:print("警告")mailelse:print("正常")
use()
import yagmail
yag = yagmail.SMTP(user="2996xxxxx@qq.com",password="xxxxxxxxxxxxx",host="smtp.qq.com",port=465,smtp_ssl=True,)
yag.send(to="12345678@qq.com", subject="提示", contents=f"你的根分区使用情况过高")
print("b")
监控cpu,内存
import subprocess
# 监控1 分钟的cpu负载
def cpuload():cpu_data = subprocess.getoutput('''uptime | awk '{print $(NF-2)}' | tr -d ","''')cpu_data = float(cpu_data) * 100cpus = subprocess.getoutput("""lscpu | grep '^CPU(s):' | awk '{print $2}'""")cpus = int(cpus)if cpu_data / cpus > 100:print("cpu 负载过高")# 监控内存使用百分比
def free():total,free = subprocess.getoutput("""free -m | awk 'NR==2{print $2,$4}'""").split(" ")total,free = int(total),int(free)if free / total * 100 < 98:print(free / total * 100)print("内存剩余空间不足")
# 监控当前/分的的剩余空间free()
