psutil模块安装
sudo yum install gcc python3-devel
pip3 install psutil
#_*_coding=utf-8 _*
#__author__ = 'chubby_superman'
import psutil
import datetime
import time
import socket
import uuid
print('-----------------------------系统基本信息---------------------------------------')
# 当前时间
now_time = time.strftime('%Y-%m-%d-%H:%M:%S', time.localtime(time.time()))
print(now_time)
# 系统启动时间
print(u"系统启动时间: %s" % datetime.datetime.fromtimestamp(psutil.boot_time()).strftime("%Y-%m-%d %H:%M:%S"))
# 系统用户
users_count = len(psutil.users())
users_list = ",".join([u.name for u in psutil.users()])
print(u"当前有%s个用户,分别是 %s" % (users_count, users_list))
print('-----------------------------cpu信息---------------------------------------')
# 查看cpu物理个数的信息
print(u"物理CPU个数: %s" % psutil.cpu_count(logical=False))
print(u"逻辑CPU个数: %s" % psutil.cpu_count(logical=True))
print(u"CPU核心总数: %s" % psutil.cpu_count())
#CPU的使用率
cpu = (str(psutil.cpu_percent(1))) + '%'
print(u"cup使用率: %s" % cpu)
print('-----------------------------mem信息---------------------------------------')
#查看内存信息,剩余内存.free 总共.total
#round()函数方法为返回浮点数x的四舍五入值。
free = str(round(psutil.virtual_memory().free / (1024.0 * 1024.0 * 1024.0), 2))
total = str(round(psutil.virtual_memory().total / (1024.0 * 1024.0 * 1024.0), 2))
memory = int(psutil.virtual_memory().total - psutil.virtual_memory().free) / float(psutil.virtual_memory().total)
swap_free = str(round(psutil.swap_memory().free / (1024.0 * 1024.0 * 1024.0), 2))
swap_total = str(round(psutil.swap_memory().total / (1024.0 * 1024.0 * 1024.0), 2))
swap_memory = int(psutil.swap_memory().total - psutil.swap_memory().free) / float(psutil.swap_memory().total)
print(u"物理内存: %s G" % total)
print(u"剩余物理内存: %s G" % free)
print(u"物理内存使用率: %s %%" % int(memory * 100))
print(u"交换内存: %s G" % swap_total)
print(u"剩余交换内存: %s G" % swap_free)
print(u"交换内存使用率: %s %%" % int(swap_memory * 100))
print('-----------------------------网络信息---------------------------------------')
#网卡,可以得到网卡属性,连接数,当前流量等信息
hostname=socket.gethostname() #获取本机主机名
ip=socket.gethostbyname(hostname) #获取本机ip地址
mac=uuid.UUID(int=uuid.getnode()).hex[-12:]
mac=":".join([mac[e:e+2] for e in range(0,11,2)])
net = psutil.net_io_counters()
bytes_sent = '{0:.2f} Mb'.format(net.bytes_recv / 1024 / 1024)
bytes_rcvd = '{0:.2f} Mb'.format(net.bytes_sent / 1024 / 1024)
print("主机名称为:",hostname)
print("IP地址为:",ip)
print("MAC地址:",mac)
print(u"网卡接收流量 %s 网卡发送流量 %s" % (bytes_rcvd, bytes_sent))
print('-----------------------------磁盘信息---------------------------------------')
io = psutil.disk_partitions()
#print("系统磁盘信息:" + str(io))
o = psutil.disk_usage("/")
ioo=psutil.disk_io_counters()
print(ioo)
print("盘总容量:"+str(int(o.total / (1024.0 * 1024.0 * 1024.0))) + "G")
print("已用容量:" + str(int(o.used / (1024.0 * 1024.0 * 1024.0))) + "G")
print("可用容量:" + str(int(o.free / (1024.0 * 1024.0 * 1024.0))) + "G")
print('-----------------------------进程信息-------------------------------------')
# 查看系统全部进程
for pnum in psutil.pids():
p = psutil.Process(pnum)
print(u"进程名 %-20s 内存利用率 %-18s 进程状态 %-10s 创建时间 %-10s " \
% (p.name(), p.memory_percent(), p.status(), p.create_time()))
-----------------------------系统基本信息---------------------------------------
2019-08-15-18:53:04
系统启动时间: 2019-08-15 17:05:29
当前有1个用户,分别是 root
-----------------------------cpu信息---------------------------------------
物理CPU个数: 2
逻辑CPU个数: 2
CPU核心总数: 2
cup使用率: 0.0%
-----------------------------mem信息---------------------------------------
物理内存: 3.7 G
剩余物理内存: 3.47 G
物理内存使用率: 6 %
交换内存: 2.0 G
剩余交换内存: 2.0 G
交换内存使用率: 0 %
-----------------------------网络信息---------------------------------------
主机名称为: office-2-250
IP地址为: 192.168.2.250
MAC地址: fa:1f:c3:c0:6a:00
网卡接收流量 0.77 Mb 网卡发送流量 1.56 Mb
-----------------------------磁盘信息---------------------------------------
sdiskio(read_count=7802, write_count=5571, read_bytes=261302784, write_bytes=49298944, read_time=38007, write_time=315904)
盘总容量:97G
已用容量:6G
可用容量:90G
-----------------------------进程信息-------------------------------------
进程名 systemd 内存利用率 0.0942174795071828 进程状态 sleeping 创建时间 1565859929.02
进程名 kthreadd 内存利用率 0.0 进程状态 sleeping 创建时间 1565859929.02
进程名 ksoftirqd/0 内存利用率 0.0 进程状态 sleeping 创建时间 1565859929.03
进程名 kworker/0:0H 内存利用率 0.0 进程状态 sleeping 创建时间 1565859929.03
进程名 kworker/u4:0 内存利用率 0.0 进程状态 sleeping 创建时间 1565859929.03
进程名 migration/0 内存利用率 0.0 进程状态 sleeping 创建时间 1565859929.03
进程名 rcu_bh 内存利用率 0.0 进程状态 sleeping 创建时间 1565859929.03
进程名 rcu_sched 内存利用率 0.0 进程状态 sleeping 创建时间 1565859929.03
进程名 lru-add-drain 内存利用率 0.0 进程状态 sleeping 创建时间 1565859929.03
进程名 watchdog/0 内存利用率 0.0 进程状态 sleeping 创建时间 1565859929.03
进程名 watchdog/1 内存利用率 0.0 进程状态 sleeping 创建时间 1565859929.03
进程名 migration/1 内存利用率 0.0 进程状态 sleeping 创建时间 1565859929.03
进程名 ksoftirqd/1 内存利用率 0.0 进程状态 sleeping 创建时间 1565859929.03
进程名 kworker/1:0H 内存利用率 0.0 进程状态 sleeping 创建时间 1565859929.03
进程名 kdevtmpfs 内存利用率 0.0 进程状态 sleeping 创建时间 1565859929.03