自学内容网 自学内容网

python邮件告警

这里写的是俩个模块,需要调用模块,但是俩个模块都必须在同一目录下

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("警告")
        mail
    else:
        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) * 100
    cpus = 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()


原文地址:https://blog.csdn.net/2401_83945639/article/details/140722772

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!