用python脚本监控阿里云或者linux,并触发报警维护机制

文章目录


提示:只需要创建一个py文件用来30s监控计划,可以搭配我之前写的钉钉py文件,导入之后,触发钉钉报警

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# author:fly
# datetime:2022/8/16 10:25
# software: PyCharm
 
import os, time
import logging
 
logging.basicConfig(level=logging.DEBUG,#控制台打印的日志级别
                    filename='monitor.log',
                    filemode='a',##模式,有w和a,w就是写模式,每次都会重新写日志,覆盖之前的日志
                    #a是追加模式,默认如果不写的话,就是追加模式
                    format=
                    '%(asctime)s - %(pathname)s[line:%(lineno)d] - %(levelname)s: %(message)s'
                    #日志格式
                    )
last_worktime = 0
last_idletime = 0
 
 
def get_cpu():
    global last_worktime, last_idletime
    f = open("/proc/stat", "r")
    line = ""
    while not "cpu " in line: line = f.readline()
    f.close()
    spl = line.split(" ")
    worktime = int(spl[2]) + int(spl<

你可能感兴趣的:(python,linux)