|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
小知识:CentOS已正式加入红帽公司。从事互联网技术的同学,CentOS应该是值得你深入了解学习研究的。
python写的抓取linux体系重要信息的剧本,重要就是内存,硬盘、CPU之类的信息。
- 内存信息/meminfo前往dict#!/usr/bin/envpythondefmemory_stat():mem={}f=open("/proc/meminfo")lines=f.readlines()f.close()forlineinlines:iflen(line)<2:continuename=line.split(:)[0]var=line.split(:)[1].split()[0]mem[name]=long(var)*1024.0mem[MemUsed]=mem[MemTotal]-mem[MemFree]-mem[Buffers]-mem[Cached]returnmemCPU信息/cpuinfo前往list,每焦点一dict#!/usr/bin/envpythondefcpu_stat():cpu=[]cpuinfo={}f=open("/proc/cpuinfo")lines=f.readlines()f.close()forlineinlines:ifline==n:cpu.append(cpuinfo)cpuinfo={}iflen(line)<2:continuename=line.split(:)[0].rstrip()var=line.split(:)[1]cpuinfo[name]=varreturncpu负载信息/loadavg前往dict#!/usr/bin/envpythondefload_stat():loadavg={}f=open("/proc/loadavg")con=f.read().split()f.close()loadavg[lavg_1]=con[0]loadavg[lavg_5]=con[1]loadavg[lavg_15]=con[2]loadavg[nr]=con[3]loadavg[last_pid]=con[4]returnloadavg运转时光/Uptime前往dict#!/usr/bin/envpythondefuptime_stat():uptime={}f=open("/proc/uptime")con=f.read().split()f.close()all_sec=float(con[0])MINUTE,HOUR,DAY=60,3600,86400uptime[day]=int(all_sec/DAY)uptime[hour]=int((all_sec%DAY)/HOUR)uptime[minute]=int((all_sec%HOUR)/MINUTE)uptime[second]=int(all_sec%MINUTE)uptime[Freerate]=float(con[1])/float(con[0])returnuptime获得网卡流量信息/proc/net/dev前往dict,单元byte#!/usr/bin/envpythondefnet_stat():net=[]f=open("/proc/net/dev")lines=f.readlines()f.close()forlineinlines[2:]:con=line.split()"""intf={}intf[interface]=con[0].lstrip(":")intf[ReceiveBytes]=int(con[1])intf[ReceivePackets]=int(con[2])intf[ReceiveErrs]=int(con[3])intf[ReceiveDrop]=int(con[4])intf[ReceiveFifo]=int(con[5])intf[ReceiveFrames]=int(con[6])intf[ReceiveCompressed]=int(con[7])intf[ReceiveMulticast]=int(con[8])intf[TransmitBytes]=int(con[9])intf[TransmitPackets]=int(con[10])intf[TransmitErrs]=int(con[11])intf[TransmitDrop]=int(con[12])intf[TransmitFifo]=int(con[13])intf[TransmitFrames]=int(con[14])intf[TransmitCompressed]=int(con[15])intf[TransmitMulticast]=int(con[16])"""intf=dict(zip((interface,ReceiveBytes,ReceivePackets,ReceiveErrs,ReceiveDrop,ReceiveFifo,ReceiveFrames,ReceiveCompressed,ReceiveMulticast,TransmitBytes,TransmitPackets,TransmitErrs,TransmitDrop,TransmitFifo,TransmitFrames,TransmitCompressed,TransmitMulticast),(con[0].rstrip(":"),int(con[1]),int(con[2]),int(con[3]),int(con[4]),int(con[5]),int(con[6]),int(con[7]),int(con[8]),int(con[9]),int(con[10]),int(con[11]),int(con[12]),int(con[13]),int(con[14]),int(con[15]),int(con[16]),)))net.append(intf)returnnet磁盘空间应用应用内置python内置函数,前往dict,单元byte#!/usr/bin/envpythondefdisk_stat():importoshd={}disk=os.statvfs("/")hd[available]=disk.f_bsize*disk.f_bavailhd[capacity]=disk.f_bsize*disk.f_blockshd[used]=disk.f_bsize*disk.f_bfreereturnhd
复制代码
小知识:CentOS完全遵守上游供应商的再发行政策,并且以百分之百的软件兼容性为目标。 |
|