• python监控机器(第1版)


    # coding:utf-8
    import configparser
    import logging
    import os
    import psutil
    import ctypes
    import platform
    import schedule
    import time
    
    
    # 配置日志
    def configLog():
        logging.basicConfig(level=logging.INFO,  # 控制台打印的日志级别
                            filename='monitor.log',
                            filemode='a',  ##模式,有w和a,w就是写模式,每次都会重新写日志,覆盖之前的日志
                                                         # a是追加模式,默认如果不写的话,就是追加模式
                            format=
                            '%(asctime)s - %(pathname)s[line:%(lineno)d] - %(levelname)s: %(message)s'
                            # 日志格式
                            )
    
    
    # 初始化配置参数
    def init():
        configLog()
        cf = configparser.ConfigParser()
        cf.read("config.ini","UTF-8")
        secs = cf.sections()
        print(cf.get("backupFiles","valve"))
    
    
    # 备份文件监控任务
    def backupFilesMonitorJob(dir,files):
        for x in files:
            if not os.path.exists(dir+x):
                print("日常备份文件%s%s还未备份"%(dir,x))
    
    
    # 核心文件监控任务
    def coreFilesMonitorJob(dir,files):
        for x in files:
            if not os.path.exists(dir+x):
                print("core文件%s%s还未备份"%(dir,x))
    
    
    # 数据库备份文件监控任务
    def databaseBackupMonitorJob(dir,files):
        for x in files:
            if not os.path.exists(dir+x):
                print("core文件%s%s还未备份"%(dir,x))
    
    
    # 进程监控任务
    def processMonitorJob(processNames):
        for x in processNames:
            for p in psutil.process_iter():
                if p.name() == x:
                    print("进程状态",p.name)
                    break;
    
    
    
    # 磁盘监控任务
    def diskCapacityMonitorJob(folder):
        freeSpace = 0
        if platform.system() == 'Windows':
            free_bytes = ctypes.c_ulonglong(0)
            ctypes.windll.kernel32.GetDiskFreeSpaceExW(ctypes.c_wchar_p(folder), None, None, ctypes.pointer(free_bytes))
            freeSpace = free_bytes.value/1024/1024/1024
        else:
            st = os.statvfs(folder)
            freeSpace = st.f_bavail * st.f_frsize/1024/1024
        # print(freeSpace,"GB")
        if freeSpace <= 50:
            print("警告,磁盘空间不足50GB,当前仅剩%i GB,[当前时间:%s]"%(freeSpace,time.strftime('%Y.%m.%d %H:%M:%S',time.localtime(time.time()))))
    
    
    schedule.every(3).seconds.do(processMonitorJob,processNames = ['QQ.exe','chrome.exe'])
    schedule.every(10).seconds.do(diskCapacityMonitorJob,"C:\")
    schedule.every(10).seconds.do(backupFilesMonitorJob,"C:\",["demo.txt","demo2.txt"])
    schedule.every(10).seconds.do(coreFilesMonitorJob,"C:\",["demoa.txt","demob.txt"])
    schedule.every(10).seconds.do(databaseBackupMonitorJob,"C:\",["data1.txt","data2.txt"])
    
    if __name__ == "__main__":
        while True:
            schedule.run_pending()
    

      

  • 相关阅读:
    Android ViewPager动画切换
    Android 窗口全屏
    心情记忆
    C语言难点1之输入输出,链接,字符串操作
    SMR磁盘学习9---Classifying Data to Reduce Long Term Data Movement in Shingled Write Disks
    SMR磁盘学习12---drivemanaged SMR 磁盘总结
    SMR磁盘学习11---Data Management and Layout for Shingled Magnetic Recording
    SMR磁盘学习10---Data Handling Algorithms For Autonomous Shingled Magnetic Recording HDDs
    SMR磁盘学习8---Novel Address Mappings for Shingled Write Disks
    SMR磁盘学习7---Data Handling Algorithms For Autonomous Shingled Magnetic Recording HDDs
  • 原文地址:https://www.cnblogs.com/InformationGod/p/10697382.html
Copyright © 2020-2023  润新知