Script Example
#!/usr/bin/env python # -*- coding:utf-8 -*-import psutil import timeimport datetimeimport sys import osimport socketimport Uuidimport platform def get_baseinfo (): nowtime= Datetime.datetime.now (). Strftime ('%y-%m-%d %h:%m:%s ') macs = uuid. UUID (Int = uuid.getnode ()) .hex[-12:] mac = ":". Join ([Macs[e:e+2] for e in range (0,11,2)]) # Mac=os.popen ("/usr/sbin/ip a|grep ether|awk -f ' ' ' {print $2} '). Read () # ipaddr=os.popen ("/usr/sbin/ip a|grep eno|grep inet| awk -f ' ' ' {print $2} ' |awk -f '/' ' {print $1} '). Read () &nBsp; hostname=socket.getfqdn (Socket.gethostname ()) ipaddr=socket.gethostbyname (Hostname) boot_ Start = time.strftime ("%y-%m-%d %h:%m:%s", Time.localtime (Psutil.boot_time ())) print ('---------------basic Information------------------') print (' Current time:%s ' % nowtime) print (' Host Domain name:%s ' % hostname) print (' IP address:%s ' % IPADDR) print (' Mac address:%s ' % mac) print (' Start time:%s ' % boot_start) print ('-----------------------------------------') def get_platinfo (): print ('---------------System Information-------------------') print (' calculation type :%s ' % platform.machine ()) print (' Host name :%s ' % platform.node ()) print (' OS type :%s ' % platform.system ()) print (' operating system name and version number : %s ' % platform.platform ()) print ('------------- -----------------------------') Def get_cpuinfo (): cpu_ Count=psutil.cpu_count () cpu_usage=psutil.cpu_percent () cpu_type=os.popen ("Cat /proc/cpuinfo | grep name |uniq -c| cut -f2 -d: "). Read () print ( '-----------------CPU Information------------------') print (' Cpu cores:%s cores ' % cpu_ count ) print (' Current CPU usage: %s%s ' % (CPU _usage, "%")) print (' Cpu model: %s ' % cpu_type) print ('------------------------------------------') def get_ Meminfo (): mem_total=int (Psutil.virtual_memory (). total/(1024* ) mem_free=int (Psutil.virtual_memory (). free/(1024*1024)) mem_percent=psutil.virtual_memory () .percent swap_total=int (Psutil.swap_memory (). total/(1024*1024)) swap_percent=psutil.swap_memory () .percent print ('-----------------Memory Information------------------') print (' Physical Memory:%DM ' % mem_ Total) print (' Free Memory:%DM ' % mem_free) print (' Memory utilization:%s%s ' % (mem_percent, "%")) print (' Swap Memory:%DM ' % swap_total) Print (' Swap utilization:%s%s ' % (swap_percent, "%")) print ('--- ----------------------------------------') Def get_diskinfo (): # for i in psutil.disk_partitions ():# print "drive letter: %s mount point: %s total space:%dm remaining space:%dm usage: %s%s " % (i[0],i[1), Psutil.disk_usage (i[1]) [0]/(1024*1024), Psutil.disk_usage (i[1]) [2]/(1024*1024), Psutil.disk_usage (I[1]) [3], "%") priNT ('----------------disk Information-------------------') num=int (Len (Psutil.disk_partitions ())) for j in range (0,num): Device=psutil.disk_partitions () [j].device mountpoint= Psutil.disk_partitions () [J].mountpoint total=psutil.disk_usage ( mountpoint). total/(1024*1024) free=psutil.disk_usage (mountpoint ). free/(1024*1024) percent=psutil.disk_usage (mountpoint). percent print "drive letter: %s mount point: %s Total space:%dm remaining space:%dm usage: %s%s " % (device,mountpoint,total,free,percent,"% ") print ('-------------------------------------------') def get_netinfo (): &nBsp;net_sent = psutil.net_io_counters () .bytes_sent net_recv = psutil.net_io_counters () .bytes_recv net_spkg = psutil.net_io_counters () .packets_sent net_rpkg = psutil.net_io_counters () .packets_recv print ('----------------Network Information-------------------') print (' send:%d byte number of packets:%d ' % (Net_ sent,net_spkg)) print (' Receive:%d byte Number of packets Received:%d ' % (net_recv,net_rpkg)) print ('------- ------------------------------------') if __name__ == "__main__": &Nbsp; get_baseinfo () get_platinfo () get_cpuinfo () get_meminfo () get_diskinfo () get_netinfo ()
Python test.py
Output Result:
---------------Basic Information------------------
Current time: 2018-05-09-18:23:58
Host Domain name: www.linuxtest01.com
IP Address: 192.168.1.16
MAC Address: 00:59:06:b0:55:62
Start time: 2017-02-12 7:23:50
-----------------------------------------
---------------System Information-------------------
Calculation type: x86_64
Host Name: linuxtest01
Operating system type: Linux
Operating system name and version number: Linux-3.10.0-327.el7.x86_64-x86_64-with-redhat-7.2-maipo
------------------------------------------
-----------------CPU Information------------------
Number of CPU cores: 4 cores
Current CPU Usage: 27.8%
CPU Model: Intel (R) Xeon (r) CPU e5-4640 0 @ 2.40GHz
------------------------------------------
-----------------Memory Information------------------
Physical Memory: 7823M
Free Memory: 1549M
Memory Utilization: 49.4%
Swap Memory: 4095M
Swap utilization: 17.6%
-------------------------------------------
----------------Disk Information-------------------
Drive letter:/dev/mapper/mount point:/Total space: 97752M remaining space: 47922M usage: 51%
Drive letter:/DEV/SDA1 mount point:/boot total space: 496M remaining space: 340M usage: 31.5%
-------------------------------------------
----------------Network Information-------------------
Send: 165857977619 bytes Sent: 718,789,512 packets
Receive: 688809440896 Byte received packets: 784,210,872
-------------------------------------------
Script for reference only
Python detection system Basic information example