#!/usr/bin/python
#coding: Utf-8
‘‘‘
# -------------------------------------------------------------------------------
# Filename:hardwareinfo.py
# revision:0.1
# date:2018/04/03
# Author:stivee
# email: [Email protected]
# Description: Server, python2.7
# Notes: Start: nohup python hardwareinfo.py >/dev/null 2>&1 &
# -------------------------------------------------------------------------------
‘‘‘
#先下载psutil库:p IP Install psutil
Import Psutil
Import Sys,os,datetime,time,subprocess,re,json,shutil
Try
Import Psutil
Except Importerror as E:
With open ('/home/logs/hardwareinfoerror.log ', ' a ') as FE:
Fe.write (Time.strftime ("%y-%m-%d%h:%m:%s", Time.localtime ()) + repr (e) + ' \ n ')
Sys.exit ()
Totalinfo = {}
Def getload ():
K = 1.0
Loadavg = {}
If Os.path.exists ('/proc/loadavg '):
Load = [Float ('/proc/loadavg '). Read (). split () [x]) * k for X in range (3)]
Else
tokens = Subprocess.check_output ([' Uptime ']). Split ()
Load = [Float (str (x). Strip (' [b,\ ',] ')) * k for X in tokens[-3:]]
loadavg[' loadavg ' = load
Return Loadavg
Def getmem ():
Mem = {}
Allinfo = Psutil.virtual_memory ()
Percent = ('%.3f '% (float (allinfo.used)/float (allinfo.total)))
Total = Allinfo.total
used = allinfo.used
Free = Allinfo.free
mem[' mem_percent '] = percent
mem[' mem_total '] = Total
mem[' mem_used '] = used
mem[' mem_free ' = Free
Return mem
Def getusers ():
Alluserinfo = {}
AllUsers = Psutil.users ()
Userscount = Len (allusers)
UserInfo = []
For I in AllUsers:
Userinfo.append (I.name + "-" + I.host + "-" + time.strftime ("%y/%m/%d-%h:%m:%s", Time.localtime (int ('%d '%i.started)))
UserInfo = ". Join (UserInfo)
Uptime = Subprocess.check_output ([' Uptime ']). Split ()
Uptime = str (uptime[2]). Strip (' [b,\ ',] ') + ' days ' + str (uptime[4]). Strip (' [b,\ ',] ')
Pidsnum = Len (Psutil.pids ())
alluserinfo[' userinfo '] = userinfo
alluserinfo[' uptime '] = uptime
alluserinfo[' pidsnum '] = Pidsnum
Return Alluserinfo
Def getcpu ():
Cpuinfo = {}
Cpucount = Psutil.cpu_count (logical=false)
cpuused = Psutil.cpu_percent ()
cpuinfo[' cpucount '] = Cpucount
cpuinfo[' cpuused '] = cpuused
Return Cpuinfo
Def getdisk ():
DiskInfo = {}
Diskroot = Psutil.disk_usage ('/'). Percent
Diskhome = Psutil.disk_usage ('/home/'). percent
diskinfo[' diskroot '] = Diskroot
diskinfo[' diskhome '] = Diskhome
Return DiskInfo
Def getnet ():
Netlist = [x for x in Psutil.net_io_counters (pernic=true). Keys () If Re.search (' ^e ', x)]
NetFlow = {}
netflow_in = {}
Netflow_out = {}
UT = {}
recv = []
Sent = []
For net in netlist:
Recv.append (Psutil.net_io_counters (pernic=true). Get (NET). Bytes_recv)
Sent.append (Psutil.net_io_counters (pernic=true). Get (NET). Bytes_sent)
Time.sleep (1)
Recv.append (Psutil.net_io_counters (pernic=true). Get (NET). Bytes_recv)
Sent.append (Psutil.net_io_counters (pernic=true). Get (NET). Bytes_sent)
netflow_in[net+ ' _in ' = (recv[1]-recv[0])/1024 * 8
netflow_out[net+ ' _out ' = (sent[1]-sent[0])/1024 * 8
recv = []
Sent = []
Netflow.update (netflow_in)
Netflow.update (Netflow_out)
Return NetFlow
Try
if __name__ = = "__main__":
While True:
totalinfo[' loadavg '] = Getload ()
totalinfo[' mem '] = Getmem ()
totalinfo[' users '] = Getusers ()
totalinfo[' CPU ' = GETCPU ()
totalinfo[' disk ' = Getdisk ()
totalinfo[' net '] = Getnet ()
FileSize = os.path.getsize ('/home/logs/totalinfo.log ')
If FileSize > 1024000000:
Shutil.move ('/home/logs/totalinfo.log ', '/home/logs/totalinfo.logbak ')
With open ('/home/logs/totalinfo.log ', ' a ') as F:
F.write ("::" + time.strftime ("%y-%m-%d%h:%m:%s", Time.localtime ()) + "::" + json.dumps (totalinfo) + ' \ n ')
Time.sleep (5)
Except Exception as E:
With open ('/home/logs/hardwareinfoerror.log ', ' a ') as F:
F.write (Time.strftime ("%y-%m-%d%h:%m:%s", Time.localtime ()) + repr (e) + ' \ n ')
Else
With open ('/home/logs/hardwareinfostatus.log ', ' a ') as F:
F.write (Time.strftime ("%y-%m-%d%h:%m:%s", Time.localtime ()) + ': OK ' + ' \ n ')
Server hardware information capture Python version