Python日常运维脚本
Posted Wolf_Dreams
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python日常运维脚本相关的知识,希望对你有一定的参考价值。
1 扫描Windows系统CPU、内存、磁盘利用率
1.1 实现代码
#!usr/bin/env python #-*- coding:utf-8 _*- """ @author:T-guoh @file: server_monitor.py @time: 2019/03/18 """ import psutil import os import socket cpu = {\'user\':0,\'system\':0,\'idle\':0,\'percent\':0} mem = {\'total\' : 0, \'avaiable\' : 0, \'percent\' : 0, \'used\' : 0, \'free\' : 0} disk_id = [] disk_total = [] disk_used = [] disk_free = [] disk_percent = [] #获取CPU信息 def get_cpu_info(): cpu_times = psutil.cpu_times() cpu[\'user\'] = cpu_times.user cpu[\'system\'] = cpu_times.system cpu[\'idle\'] = cpu_times.idle cpu[\'percent\'] = psutil.cpu_percent(interval=2) #获取Memory信息 def get_mem_info(): mem_info = psutil.virtual_memory() mem[\'total\'] = mem_info.total mem[\'avaiable\'] = mem_info.available mem[\'percent\'] = mem_info.percent mem[\'used\'] = mem_info.percent mem[\'free\'] = mem_info.free #获取Disk信息 def get_disk_info(): for id in psutil.disk_partitions(): if \'cdrom\' in id.opts or id.fstype == \'\': continue disk_name = id.device.split(\':\') s = disk_name[0] disk_id.append(s) disk_info = psutil.disk_usage(id.device) disk_total.append(disk_info.total) disk_used.append(disk_info.used) disk_percent.append(disk_info.percent) disk_free.append(disk_info.free) #定义主函数 def main(): get_cpu_info() cpu_status = cpu[\'percent\'] get_mem_info() mem_status = mem[\'percent\'] hostname = os.environ[\'COMPUTERNAME\'] username = os.environ[\'USERNAME\'] ip_addr = socket.gethostbyname(hostname) print(\'======基本信息======\') print(\'主机名:\'+ hostname+ \'\\nIP地址:\' + ip_addr + \'\\n用户名:\' + username) print(\'====================\') print(\'\\n=====资源使用率=====\') print(\'CPU使用率:%s %%\' % cpu_status) print(\'Mem使用率:%s %%\' % mem_status) get_disk_info() for i in range(len(disk_id)): print(\'%s盘空闲率:%s %%\' % (disk_id[i], round(100 - disk_percent[i], 2))) print(\'====================\') os.system(\'pause\') if __name__ == \'__main__\': main()
1.2 运行结果
2 扫描Windows系统Top目录与文件大小
2.1 实现代码
#!usr/bin/env python #-*- coding:utf-8 _*- """ @author:T-guoh @file: server_monitor.py @time: 2019/03/18 """ import os #统计指定目录下各个文件夹的大小,方便观察具体目录的大小 def CountDirSize(srcDir): dirSizeDict = {} fileSizeDict = {} for fileList in os.listdir(srcDir): filePath = os.path.join(srcDir,fileList) if os.path.isdir(filePath): size = 0 for roots,dirs,files in os.walk(filePath): for fileName in files: fileNamePath = os.path.join(roots,fileName) sizetemp = 0 try: sizetemp = os.path.getsize(fileNamePath) if sizetemp > 1024*1024: fileSizeDict[fileNamePath] = sizetemp except: sizetemp = 0 size += sizetemp dirSizeDict[fileList] = size #按照从大到小的顺序降序排列 dirSizeDict = sorted(dirSizeDict.items(),key = lambda dirSizeDict:dirSizeDict[1],reverse = True) Sum = 0 #各个目录占用空间 print("Each directory takes up space:") for ele in dirSizeDict: temp = ele[1]/1024/1024 Sum += temp #以单位为M,输出大小 print(ele[0] + ":" + str(temp) + " M") #以单位为G,输出大小 print("Total use:" + str(Sum / 1024) +" G") print("") #前20个大文件占用空间 print("Top 20 large file takes up space:") fileSizeDict = sorted(fileSizeDict.items(),key = lambda fileSizeDict:fileSizeDict[1],reverse = True) for ele in fileSizeDict[:20]: print(ele[0] + ":" +str(ele[1]/1024/1024) + " M") if __name__ == "__main__": srcDir = "c:\\\\" CountDirSize(srcDir)
2.2 运行结果
以上是关于Python日常运维脚本的主要内容,如果未能解决你的问题,请参考以下文章