yum install python3
yum -y install python3-pip
yum -y install epel-release
yum -y install gcc python-devel
pip install --trusted-host pypi.tuna.tsinghua.edu.cn psutil
初版:
import os
import sys
import time
import platform
import subprocess
import psutil
def monitor_func():
print("---------------------------------------------------------")
#检查网卡,定义一个列表,如果第一个值为2并且ip不是127.0.0.1 将其加入列表里
def get_netcard():
netcard_info = []
info = psutil.net_if_addrs()
for netcard, ip in info.items():
for itme in ip:
if itme[0] == 2 and not itme[1] == '127.0.0.1':
netcard_info.append((netcard, itme[1]))
return netcard_info
netcard_info = get_netcard()
print("网络接口信息:")
for netcard, ip in netcard_info:
print(f"接口: {netcard}, IP: {ip}")
print("---------------------------------------------------------")
system_time = time.strftime("%Y-%m-%d %X",time.localtime())
#print(type(system_time))
print(f"当前系统时间是:{system_time}")
print("---------------------------------------------------------")
result = os.getcwd()
print(f"当前的工作目录:{result}")
print("---------------------------------------------------------")
#调用系统命令输出结果,输出内容为字节,需要decode解码
result = subprocess.run(["cat","/etc/os-release"], stdout=subprocess.PIPE)
stdout = result.stdout.decode("utf-8")
print("当前系统版本:")
print()
print(stdout)
print("---------------------------------------------------------")
#检查根分区的剩余容量:Avail
disk_info = psutil.disk_usage('/')
disk_info = disk_info.free / 1024 / 1024 / 1024
print("系统根分区剩余:""%.2f" % disk_info,"G")
print("---------------------------------------------------------")
#检查内存total和free的空间
memory_info = psutil.virtual_memory()
memory_total = memory_info.total / 1024 / 1024 / 1024
memory_free = memory_info.free / 1024 / 1024 / 1024
print("系统总内存:""%.2f" % memory_total,"G")
print("系统可用内存:""%.2f" % memory_free,"G")
print("---------------------------------------------------------")
if __name__ == '__main__':
monitor_func()
返回: