标签:
1、通过proc文件系统获取一些信息,用来监控
#coding:utf8 import os import json class SystemInfo(object): ‘‘‘ 监控的类 ‘‘‘ def __init__(self): self.proc = ‘/proc‘ self.loadavg = ‘/proc/loadavg‘ self.meminfo = ‘/proc/meminfo‘ self.interface = ‘/proc/net/dev‘ self.partitions = ‘/proc/partitions‘ self.reslut = {} def getLoadavg(self): ‘‘‘ 根据/proc/loadavg文件获取系统的1,5,15分钟负载,类型是浮点数 ‘‘‘ with open(self.loadavg) as f: loadavg = f.readline().split()[0:3] reslut = { "Loadavg":{ "loadavg_1": float(loadavg[0]), "loadavg_5": float(loadavg[1]), "loadavg_15": float(loadavg[2]) }, } return reslut def getPartitions(self): with open(self.partitions) as f: reslut = {"partition":{}} for line in f: if len(line.strip()) == 0 or line.startswith(‘major‘): continue reslut["partition"][line.split()[3]] = int(line.split()[2]) return reslut def getMemInfo(self): ‘‘‘ 根据/proc/meminfo文件获取内存的信息,类型是整型,单位是KB ‘‘‘ with open(self.meminfo) as f: MemTotal = int(f.readline().strip().split()[1]) MemFree = int(f.readline().strip().split()[1]) Buffers = int(f.readline().strip().split()[1]) Cached = int(f.readline().strip().split()[1]) reslut = { "MemInfo":{ "MemTotal":MemTotal, #总内存 "MemFree":MemFree, #空余内存 "Buffers":Buffers, #缓冲 "Cached":Cached, #缓存 "Available":Buffers + MemFree + Cached, #真正可用内存 "MemUse":MemTotal - (MemFree + Buffers + Cached) #真正的使用内存 } } return reslut def getTraffic(self): ‘‘‘ 根据/proc/net/dev文件获取系统所有接口的流量、发送与接收的包 计算公式: 所有接口发送与接收的总字节数、总包数 ‘‘‘ with open(self.interface) as f: T = 0 P = 0 lines = [lines.strip().split() for lines in f.readlines()[2:] if lines] for line in lines: T += int(line[1]) + int(line[9]) P += int(line[2]) + int(line[10]) else: reslut = { "Network": { "Traffic":T, #总流量 "Packet":P #总包数 } } return reslut def getSwapInfo(self): ‘‘‘ 通过/proc/meminfo文件获取当前系统的swap的信息 ‘‘‘ with open(self.meminfo) as f: for line in f: if line.startswith(‘SwapCached‘): SwapCached = int(line.split()[1]) if line.startswith(‘SwapTotal‘): SwapTotal = int(line.split()[1]) if line.startswith(‘SwapFree‘): SwapFree = int(line.split()[1]) reslut = { "SwapInfo": { "SwapFree":SwapFree, "SwapCached":SwapCached, "SwapTotal":SwapTotal } } return reslut def getPids(self): ‘‘‘ 获取当前系统所有的pid,返回一个列表 ‘‘‘ reslut = { "pids": [ pid for pid in os.listdir(self.proc) if pid.isdigit() ] } return reslut def getPidNum(self): ‘‘‘ 获取系统当前pid的个数 ‘‘‘ pids = self.getPids() reslut = { "pids": { "PidNum":len(pids["pids"]) } } return reslut def getAll(self): ‘‘‘ 得到信息,更新返回一个字典 ‘‘‘ for get in self.getLoadavg(),self.getMemInfo(),self.getTraffic(), self.getSwapInfo(),self.getPids(),self.getPidNum(),self.getPartitions(): self.reslut.update(get) return self.reslut if __name__ == ‘__main__‘: get = SystemInfo() getall = json.dumps(get.getAll(),indent=4) print(getall)
{ "Loadavg": { "loadavg_1": 0.0, "loadavg_5": 0.01, "loadavg_15": 0.05 }, "MemInfo": { "Available": 1347188, "MemTotal": 1914588, "Cached": 66592, "MemUse": 567400, "MemFree": 1204600, "Buffers": 75996 }, "Network": { "Traffic": 882943, "Packet": 11026 }, "pids": { "PidNum": 89 }, "partition": { "sda3": 18873344, "sda2": 1048576, "sda": 20971520, "sda1": 1048576 }, "SwapInfo": { "SwapTotal": 1048572, "SwapCached": 0, "SwapFree": 1048572 } }
2、通过proc文件系统获取当前的分区情况
with open(‘/proc/partitions‘) as f: reslut = {} for line in f: if len(line.strip()) == 0 or line.startswith(‘major‘): continue reslut[line.split()[3]] = line.split()[2] print reslut
结果:
{‘sda3‘: ‘18873344‘, ‘sda2‘: ‘1048576‘, ‘sda‘: ‘20971520‘, ‘sda1‘: ‘1048576‘}
标签:
原文地址:http://www.cnblogs.com/huangweimin/p/5781823.html