/
python.sys.info.py
64 lines (60 loc) · 2.65 KB
/
python.sys.info.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env python
#coding:utf-8
import psutil
import time
import sys
from optparse import OptionParser
import platform
parser = OptionParser()
parser.add_option("-t", "--time", dest="time",
help="此参数可查看当前下载占的带宽,-t是测试时间", metavar="10")
parser.add_option("-d", "--deamon",action="store_false", dest="deamon", default=True,
help="后台运行此脚本")
def Sysinfo():
Hostname = platform.node()
Sys_version = platform.platform()
Boot_Start = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(psutil.boot_time()))
time.sleep(0.5)
Cpu_usage = psutil.cpu_percent()
RAM = int(psutil.virtual_memory().total/(1027*1024))
RAM_percent = psutil.virtual_memory().percent
Swap = int(psutil.swap_memory().total/(1027*1024))
Swap_percent = psutil.swap_memory().percent
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
BFH = r'%'
print " \033[1;32m主机名: %s\033[1;m" % Hostname
print " \033[1;32m系统版本: %s\033[1;m" % Sys_version
print " \033[1;32m开机时间:%s\033[1;m" % Boot_Start
print " \033[1;32m当前CPU使用率:%s%s\033[1;m" % (Cpu_usage,BFH)
print " \033[1;32m物理内存:%dM\t使用率:%s%s\033[1;m" % (RAM,RAM_percent,BFH)
print " \033[1;32mSwap内存:%dM\t使用率:%s%s\033[1;m" % (Swap,Swap_percent,BFH)
print " \033[1;32m发送:%d Byte\t发送包数:%d个\033[1;m" % (Net_sent,Net_spkg)
print " \033[1;32m接收:%d Byte\t接收包数:%d个\033[1;m" % (Net_recv,Net_rpkg)
for i in psutil.disk_partitions():
print " \033[1;32m盘符: %s 挂载点: %s 使用率: %s%s\033[1;m" % (i[0],i[1],psutil.disk_usage(i[1])[3],BFH)
for key in psutil.net_if_addrs().keys():
print " \033[1;32m网卡: %s IP: %s\033[1;m" % (key,psutil.net_if_addrs()[key][0][1])
def Net_io(s):
x = 0
sum = 0
while True:
if x >= s:
break
r1 = psutil.net_io_counters().bytes_recv
time.sleep(1)
r2 = psutil.net_io_counters().bytes_recv
y = r2 - r1
print "%.2f Kb/s" % (y / 1024.0)
sum += y
x += 1
result = sum / x
print "\033[1;32m%s秒内平均速度:%.2f Kb/s \033[1;m" % (x,result / 1024.0)
if __name__ == "__main__":
(options, args) = parser.parse_args()
if options.time:
Net_io(int(options.time))
else:
Sysinfo()