Python获取服务器信息
采用python的psutil包进行服务器性能信息获取;
可获取:
- CPU信息
- 内存信息
- 服务器当前登录用户
- 网络信息
- 磁盘信息
- 运行进程信息
- 系统启动时间
- 系统名称
- 系统版本
- 计算机类型
- 处理器类型
统计结果以json格式保存在redis中,以计算机名称-ip为key,每个计算机的数据存储为hash类型,hash内以时间戳为key(YYYYmmddHHMMSS)
统计频率可以使用Linux自带的crontab进行脚本定时执行。
# !/usr/bin/env python
# -*-coding:utf-8 -*-
import platform
from dateutil.relativedelta import relativedelta
import socket
import redis
import psutil
import datetime
import time
import json
# 本脚本需要安装psutil、redis、python-dateutil依赖
# pip install j
# pip install redis
# pip install python-dateutil
# 获取系统信息组件文档 https://psutil.readthedocs.io/en/latest/
# 定时获取数据,可以通过crontab执行脚本,设置crontab开机启动
# 设置计算机名:hostnamectl set-hostname 计算机名称
# 数据存储在redis中以计算机名称-ip为key,每个计算机的数据存储为hash类型,hash内以时间戳为key(YYYYmmddHHMMSS)
r = redis.Redis(host='127.0.0.1', port=6379, password='', db=0)
# r = redis.Redis(host='172.17.8.122', port=6379, password='', db=0)
# 采集数据封装
result = {}
# 获取ip
def get_host_ip():
try:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(('10.0.0.1', 8080))
ip = s.getsockname()[0]
finally:
s.close()
return ip
# statisticalTime:统计时间
# sysStartTime:系统启动时间
# platform:操作系统及版本信息
# version:获取系统版本号
# system:获取系统名称(Windows、Linux、FreeBSD等)
# architecture:获取操作系统可执行程序的结构,,(’32bit’, ‘WindowsPE’)
# machine:计算机类型
# node:计算机名称
# processor:处理器类型
# print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
# time.strftime('%Y-%m-%d-%H:%M:%S', time.localtime(time.time()))
result["statisticalTime"] = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
result["sysStartTime"] = datetime.datetime.fromtimestamp(psutil.boot_time()).strftime("%Y-%m-%d %H:%M:%S")
result["platform"] = platform.platform()
result["version"] = platform.version()
result["system"] = platform.system()
result["architecture"] = platform.architecture()
result["machine"] = platform.machine()
result["node"] = platform.node()
result["processor"] = platform.processor()
# 查看cpu的信息
# CPU时间为秒数
# 所有逻辑CPU信息
# 以命名元组的形式返回系统CPU时间。每个属性表示CPU在给定模式下花费的秒数。可用性随平台的不同而不同:
# physics:物理cpu个数
# logic:逻辑cpu个数
# percent:cpu总利用率
# logicPercent:逻辑cpu利用率
# user:在用户模式下执行普通进程所花费的时间;在Linux上,这还包括访客时间
# system:在内核模式下执行的进程所花费的时间
# idle: 无所事事的时间
# nice(UNIX): niced(优先级)进程在用户模式下执行所花费的时间;在Linux上,这也包括了guest_nice时间
# iowait(Linux): 等待I/O完成所花费的时间
# irq(Linux, BSD): 用于服务硬件中断的时间
# softirq(Linux): 用于服务软件中断的时间
# steal(Linux 2.6.11+): 在虚拟环境中运行的其他操作系统所花费的时间
# guest(Linux 2.6.24+): 在Linux内核控制下运行来宾操作系统的虚拟CPU所花费的时间
# guest_nice (Linux 3.2.0+): 运行niced客户机(Linux内核控制下的客户机操作系统的虚拟CPU)的时间
# interrupt(Windows): 用于服务硬件中断的时间(类似于UNIX上的“irq”)
# dpc (Windows): 服务延迟程序调用(DPCs)的时间;DPCs是运行在低于标准中断优先级的中断。
cpu={}
# 物理CPU个数
cpu["physics"] = psutil.cpu_count(logical=False)
# 逻辑CPU个数
cpu["logic"] = psutil.cpu_count()
cpu["percent"] = psutil.cpu_percent(interval=None, percpu=False)
cpu["logicPercent"] = psutil.cpu_percent(interval=None, percpu=True)
# cpu整体情况
physicsInfo = psutil.cpu_times(percpu=False)
physicsInfos = {}
physicsInfos["user"] = physicsInfo.user
physicsInfos["system"] = physicsInfo.system
physicsInfos["idle"] = physicsInfo.idle
if(hasattr(physicsInfo, "nice")):
physicsInfos["nice"] = physicsInfo.nice
if(hasattr(physicsInfo, "iowait")):
physicsInfos["iowait"] = physicsInfo.iowait
if(hasattr(physicsInfo, "irq")):
physicsInfos["irq"] = physicsInfo.irq
if(hasattr(physicsInfo, "softirq")):
physicsInfos["softirq"] = physicsInfo.softirq
if(hasattr(physicsInfo, "steal")):
physicsInfos["steal"] = physicsInfo.steal
if(hasattr(physicsInfo, "guest")):
physicsInfos["guest"] = physicsInfo.guest
if(hasattr(physicsInfo, "guest_nice")):
physicsInfos["guest_nice"] = physicsInfo.guest_nice
if(hasattr(physicsInfo, "interrupt")):
physicsInfos["interrupt"] = physicsInfo.interrupt
if(hasattr(physicsInfo, "dpc")):
physicsInfos["dpc"] = physicsInfo.dpc
cpu["physicsInfo"] = physicsInfos
# 各个逻辑cpu情况
logicInfo = psutil.cpu_times(percpu=True)
logicInfoList = []
for i in range(0, len(logicInfo)):
logicInfos={}
logicInfos["user"] = logicInfo[i].user
logicInfos["system"] = logicInfo[i].system
logicInfos["idle"] = logicInfo[i].idle
if(hasattr(logicInfo[i], "nice")):
logicInfos["nice"] = logicInfo[i].nice
if(hasattr(logicInfo[i], "iowait")):
logicInfos["iowait"] = logicInfo[i].iowait
if(hasattr(logicInfo[i], "irq")):
logicInfos["irq"] = logicInfo[i].irq
if(hasattr(logicInfo[i], "softirq")):
logicInfos["softirq"] = logicInfo[i].softirq
if(hasattr(logicInfo[i], "steal")):
logicInfos["steal"] = logicInfo[i].steal
if(hasattr(logicInfo[i], "guest")):
logicInfos["guest"] = logicInfo[i].guest
if(hasattr(logicInfo[i], "guest_nice")):
logicInfos["guest_nice"] = logicInfo[i].guest_nice
if(hasattr(logicInfo[i], "interrupt")):
logicInfos["interrupt"] = logicInfo[i].interrupt
if(hasattr(logicInfo[i], "dpc")):
logicInfos["dpc"] = logicInfo[i].dpc
logicInfoList.append(logicInfos)
cpu["logicInfo"] = logicInfoList
result["cpu"]=cpu
# 查看内存信息
# 以元组信息返回系统内存使用情况,单位为字节。
# total: 总的物理内存
# available:不需要系统进入交换分区,可以立即分给线程的内存,这个值代表了系统可使用的内存情况
# percent: 系统内存使用率,percent = (total-available)/total*100
# used: 已使用的内存,注意:total- free不一定等于used.
# free: 已准备好,但是还一点都没被使用的内存,注意:total- used不一定等于free
# active (UNIX):当前正在使用或最近使用过的内存,因此它位于RAM中。
# inactive (UNIX):标记为未使用的内存。
# buffers (Linux, BSD):缓存文件系统元数据之类的东西。
# cached (Linux, BSD):缓存各种东西。
# shared (Linux, BSD):可以由多个进程同时访问的内存。
# slab (Linux):内核数据结构缓存。
# wired (BSD, macOS):标记为始终保留在RAM中的内存。它永远不会移动到磁盘。
mem = psutil.virtual_memory()
# 以元组信息返回swap系统内存使用情况,单位为字节。
memSwap = psutil.swap_memory()
memory={}
memory["total"] = mem.total
memory["available"] = mem.available
memory["percent"] = mem.percent
memory["used"] = mem.used
memory["free"] = mem.free
if(hasattr(mem, "active")):
memory["active"] = mem.active
if(hasattr(mem, "inactive")):
memory["inactive"] = mem.inactive
if(hasattr(mem, "buffers")):
memory["buffers"] = mem.buffers
if(hasattr(mem, "cached")):
memory["cached"] = mem.cached
if(hasattr(mem, "shared")):
memory["shared"] = mem.shared
if(hasattr(mem, "slab")):
memory["slab"] = mem.slab
if(hasattr(mem, "wired")):
memory["wired"] = mem.wired
# total: 总交换内存(以字节为单位)
# used: 使用的交换内存(以字节为单位)
# free: 可用字节交换内存
# percent: 使用率计算为 (total - available) / total * 100
# sin: 系统已从磁盘交换的字节数(累积)
# sout: 系统已从磁盘换出的字节数(累积)
memory["swap_total"] = memSwap.total
memory["swap_percent"] = memSwap.percent
memory["swap_used"] = memSwap.used
memory["swap_free"] = memSwap.free
memory["swap_sin"] = memSwap.sin
memory["swap_sout"] = memSwap.sout
result["memory"] = memory
# 系统用户,将当前连接到系统上的用户作为命名元组的列表返回,包括以下字段:
# name: 用户名。
# terminal: 与用户关联的tty或伪tty(如果有),否则为None。
# host: 与条目关联的主机名(如果有)。
# started: 创建时间,以浮点数为单位,以自纪元以来的秒数表示。
# pid: 登录过程的PID(如sshd,tmux,gdm-session-worker等)。在Windows和OpenBSD上,始终将其设置为None。
users=[]
# 返回登陆系统的用户数
user = psutil.users()
for i in range(0, len(user)):
isuser={}
isuser["name"]=user[i].name
isuser["terminal"] = user[i].terminal
isuser["host"] = user[i].host
isuser["started"] = user[i].started
isuser["pid"] = user[i].pid
users.append(isuser)
result["user"] = users
# 网卡,系统网络使用情况
# bytes_sent: 发送的字节数
# bytes_recv: 收到的字节数
# packets_sent: 发送的包数
# packets_recv: 收到的包数
# errin: 接收时的错误总数
# errout: 发送时的错误总数
# dropin: 被丢弃的入站数据包总数
# dropout: 丢弃的传出数据包总数(在macOS和BSD上始终为0)
netInfoAll = psutil.net_io_counters(pernic=False, nowrap=True)
netA={}
netA["bytes_sent"] = netInfoAll.bytes_sent
netA["bytes_recv"] = netInfoAll.bytes_recv
netA["packets_sent"] = netInfoAll.packets_sent
netA["packets_recv"] = netInfoAll.packets_recv
netA["errin"] = netInfoAll.errin
netA["errout"] = netInfoAll.errout
netA["dropin"] = netInfoAll.dropin
netA["dropout"] = netInfoAll.dropout
result["netInfoAll"] = netA
# print(netA)
netInfo = psutil.net_io_counters(pernic=True)
nets=[]
for v, k in netInfo.items():
net={}
net["name"]=v
netV={}
netV["bytes_sent"] = k.bytes_sent
netV["bytes_recv"] = k.bytes_recv
netV["packets_sent"] = k.packets_sent
netV["packets_recv"] = k.packets_recv
netV["errin"] = k.errin
netV["errout"] = k.errout
netV["dropin"] = k.dropin
netV["dropout"] = k.dropout
net["info"] = netV
nets.append(net)
result["netInfo"] = nets
# 网卡状态
# isup: 一个布尔值,指示NIC是否已启动并正在运行。
# duplex: 双工通信类型;可以是 NIC_DUPLEX_FULL, NIC_DUPLEX_HALF , NIC_DUPLEX_UNKNOWN.
# speed: NIC速度(以兆位(MB)表示),如果无法确定(例如“ localhost”),则会将其设置为0。
# mtu: NIC的最大传输单位,以字节为单位。
netStat=psutil.net_if_stats()
netStats = []
for v, k in netStat.items():
net = {}
net["name"] = v
netV = {}
netV["isup"] = k.isup
netV["duplex"] = k.duplex
netV["speed"] = k.speed
netV["mtu"] = k.mtu
net["info"] = netV
netStats.append(net)
result["netStat"] = netStats
# 磁盘
# print ("-----------------------------磁盘信息---------------------------------------")
disk={}
# 返回系统磁盘读写统计情况
# read_count: 读取次数
# write_count: 写入次数
# read_bytes:读取的字节数
# write_bytes: 写入的字节数
# read_time: (除NetBSD,OpenBSD以外的所有)从磁盘读取所花费的时间(以毫秒为单位)
# write_time:(除NetBSD,OpenBSD以外的所有)花费在写入磁盘上的时间(以毫秒为单位)
# busy_time: (Linux,FreeBSD)花费在执行实际I / O上的时间(以毫秒为单位)
# read_merged_count(Linux): 合并读取的数量
# write_merged_count(Linux): 合并写入数
diskRWA = psutil.disk_io_counters(perdisk=False,nowrap=True)
diskRWAll={}
diskRWAll["read_count"] = diskRWA.read_count
diskRWAll["write_count"] = diskRWA.write_count
diskRWAll["read_bytes"] = diskRWA.read_bytes
diskRWAll["write_bytes"] = diskRWA.write_bytes
if(hasattr(diskRWA, "read_time")):
diskRWAll["read_time"] = diskRWA.read_time
if(hasattr(diskRWA, "write_time")):
diskRWAll["write_time"] = diskRWA.write_time
if(hasattr(diskRWA, "busy_time")):
diskRWAll["busy_time"] = diskRWA.busy_time
if(hasattr(diskRWA, "read_merged_count")):
diskRWAll["read_merged_count"] = diskRWA.read_merged_count
if(hasattr(diskRWA, "write_merged_count")):
diskRWAll["write_merged_count"] = diskRWA.write_merged_count
disk["diskRWAll"] = diskRWAll
# print("磁盘IO(总):"+str(diskRWA))
partitionDiskRW = psutil.disk_io_counters(perdisk=True)
# print("磁盘IO:"+str(partitionDiskRW))
partitionDiskRWAll=[]
for v, k in partitionDiskRW.items():
partition={}
partition["name"]=v
partitionInfo={}
partitionInfo["read_count"] = k.read_count
partitionInfo["write_count"] = k.write_count
partitionInfo["read_bytes"] = k.read_bytes
partitionInfo["write_bytes"] = k.write_bytes
if(hasattr(diskRWA, "read_time")):
partitionInfo["read_time"] = k.read_time
if(hasattr(diskRWA, "write_time")):
partitionInfo["write_time"] = k.write_time
if(hasattr(diskRWA, "busy_time")):
partitionInfo["busy_time"] = k.busy_time
if(hasattr(diskRWA, "read_merged_count")):
partitionInfo["read_merged_count"] = k.read_merged_count
if(hasattr(diskRWA, "write_merged_count")):
partitionInfo["write_merged_count"] = k.write_merged_count
partition["info"] = partitionInfo
partitionDiskRWAll.append(partition)
disk["partitionDiskRWAll"] = partitionDiskRWAll
disk_usage=[]
# 以列表形式返回所有已挂载的分区情况
# device:设备
# mountpoint:挂载点
# fstype:文件系统类型
# opts:其它设置如 rw表示可读可写
# total:总数(字节)
# used:已使用(字节)
# free:已准备(字节)
# percent:使用率
io = psutil.disk_partitions(all=False)
# print(io)
for i in range(0, len(io)):
# 分区的磁盘使用情况统计信息
usage = psutil.disk_usage(io[i].device)
dinfo={}
dinfo["device"] = io[i].device
dinfo["mountpoint"] = io[i].mountpoint
dinfo["fstype"] = io[i].fstype
dinfo["opts"] = io[i].opts
dinfo["total"] = usage.total
dinfo["used"] = usage.used
dinfo["free"] = usage.free
dinfo["percent"] = usage.percent
disk_usage.append(dinfo)
disk["disk_usage"] = disk_usage
result["disk"]=disk
# 查看系统全部进程
# pid:进程PID。这是类的唯一(只读)属性。
# ppid:进程父PID。
# name:进程名称。
# username:拥有进程的用户名。在UNIX上,这是通过使用实际进程uid计算的。
# status:当前进程状态
# create_time:进程创建时间,以浮点数表示,从纪元开始以秒为单位,以UTC为单位。
# exe:该进程可执行文件为绝对路径。在某些系统上,这也可能是空字符串。
# num_threads:当前由该进程使用的线程数(非累积)。在某些系统上,这也可能是空字符串。
# num_handles:此进程当前使用的句柄数(非累积)。可用性:Windows
# cwd:进程当前工作目录作为绝对路径。在某些系统上,这也可能是空字符串。
# memory_percent:内存使用率在某些系统上,这也可能是空字符串。
progressAll=[]
# print(list(psutil.Process().as_dict().keys()))
# print(psutil.Process(440).memory_percent())
for pnum in psutil.pids():
p = psutil.Process(pnum)
# print(p.cpu_percent())
progress={}
# print("进程名 %-20s 内存利用率 %-18s 进程状态 %-10s pid %-10s 进程用户名 %-10s 进程的线程数量 %-10s 创建时间 %-10s " %
# (p.name(), p.memory_percent(), p.status(), p.pid, p.username(), p.num_threads(), p.create_time()))
progress["pid"] = p.pid
progress["ppid"] = p.ppid()
progress["name"] = p.name()
progress["username"] = p.username()
progress["status"] = p.status()
progress["create_time"] = p.create_time()
# progress["memory_percent"] = p.memory_percent()
# progress["num_threads"] = p.num_threads()
# progress["exe"] = p.exe()
try:
progress["exe"] = p.exe()
except Exception as e:
progress["exe"] = ""
try:
progress["memory_percent"] = p.memory_percent()
except Exception as e:
progress["memory_percent"] = ""
try:
progress["num_threads"] = p.num_threads()
except Exception as e:
progress["num_threads"] = ""
try:
progress["cwd"] = p.cwd()
except Exception as e:
progress["cwd"] = ""
try:
progress["num_handles"] = p.num_handles()
except Exception as e:
progress["num_handles"] = ""
# print(progress)
progressAll.append(progress)
# print(json.dumps(progressAll))
result["progress"] = progressAll
# 返回硬件风扇的速度。每个条目都是一个表示特定硬件传感器风扇的命名元组。风扇速度以RPM(每分钟转数)表示。如果操作系统不支持传感器,则返回空字典
# Linux, macOS
try:
result["fans"] = psutil.sensors_fans()
except Exception as e:
result["fans"] = {}
# 将电池状态信息作为包含以下值的命名元组返回。如果未安装电池或无法确定指标,None 则返回。
# percent:剩余电池电量,以百分比表示。
# secsleft:电池电量耗尽之前还剩下多少秒的大致估计。如果连接了交流电源线,则将其设置为 psutil.POWER_TIME_UNLIMITED。如果无法确定,则将其设置为 psutil.POWER_TIME_UNKNOWN。
# power_plugged:True是否连接了交流电源线,False 如果未连接或None无法确定
# Linux, Windows, FreeBSD,macOS
try:
result["battery"] = psutil.sensors_battery()
except Exception as e:
result["battery"] = {}
# 返回硬件温度。每个条目都是一个表示特定硬件温度传感器的命名元组(它可以是CPU,硬盘或其他东西,具体取决于操作系统及其配置)。如果操作系统不支持传感器,则返回空字典。
# Linux, FreeBSD
try:
result["temperatures"] = psutil.sensors_temperatures()
except Exception as e:
result["temperatures"] = {}
hostIP=get_host_ip()
result["ip"] = hostIP
resjson = json.dumps(result)
# print(len(resjson))
print(resjson)
# 获取本机计算机名称
hostname = socket.gethostname()
# print(hostname)
# print(get_host_ip())
hashkey = time.strftime('%Y%m%d%H%M%S',time.localtime(time.time()))
hashkey2 = datetime.datetime.now().strftime('%Y%m%d%H%M%S')
# print(hashkey2)
# print(hashkey)
# hash类型存储,以名称和ip为key,以日期(精确到秒YYYYmmddHHMMSS)为hashkey,value为统计结果json
# redis可存储2^32个key,hash内可存储2.5亿个key
r.hset(hostname+"-"+hostIP, hashkey, resjson)
# 删除hash中的key
# 模糊匹配,hk为key,pattern为hash内的key
def del_hkeys(hk,pattern):
# print(hk)
# print(pattern)
key_dict = {}
keys = r.keys(hk)
for key in keys:
subkeys = r.hkeys(key)
key_dict[key] = subkeys
# print(subkeys)
pipe = r.pipeline(transaction=False)
counter = 0
for key in key_dict.keys():
subkeys = key_dict[key]
for subkey in subkeys:
if(str(pattern) in str(subkey)):
r.hdel(key, subkey)
# print(subkey)
counter += 1
if counter > 10000:
pipe.execute()
pipe.execute()
# 获取去年时间
Last_year = str(datetime.date.today() - relativedelta(months=12)).replace("-", "")
# print(Last_year)
#调用删除
# del_hkeys(hostname+ip, "20200119173")
输出参数说明
根节点:
statisticalTime:脚本统计时间
sysStartTime:系统启动时间
platform:操作系统及版本信息
version:获取系统版本号
system:获取系统名称(Windows、Linux、FreeBSD等)
architecture:获取操作系统可执行程序的结构('32bit', 'WindowsPE')(数组)
machine:计算机类型
node:计算机名称
processor:处理器类型
ip:服务器ip地址
battery:电池信息(数组)(Linux, Windows, FreeBSD,macOS)
0 剩余电池电量,以百分比表示。
1 电池电量耗尽之前还剩下多少秒的大致估计。
2 True是否连接了交流电源线,False 如果未连接或None无法确定
fans:返回硬件风扇的速度。每个条目都是一个表示特定硬件传感器风扇的命名元组。风扇速度以RPM(每分钟转数)表示。如果操作系统不支持传感器,则返回空字典(Linux, macOS)
temperatures:返回硬件温度。每个条目都是一个表示特定硬件温度传感器的命名元组(它可以是CPU,硬盘或其他东西,具体取决于操作系统及其配置)。如果操作系统不支持传感器,则返回空字典。(Linux, FreeBSD)
cpu:cpu信息
physics:物理cpu个数
logic:逻辑cpu个数
percent:cpu总利用率
logicPercent:逻辑cpu利用率(数组)
physicsInfo:cpu整体情况
user:在用户模式下执行普通进程所花费的时间;在Linux上,这还包括访客时间
system:在内核模式下执行的进程所花费的时间
idle: 无所事事的时间
nice(UNIX): niced(优先级)进程在用户模式下执行所花费的时间;在Linux上,这也包括了guest_nice时间
iowait(Linux): 等待I/O完成所花费的时间
irq(Linux, BSD): 用于服务硬件中断的时间
softirq(Linux): 用于服务软件中断的时间
steal(Linux 2.6.11+): 在虚拟环境中运行的其他操作系统所花费的时间
guest(Linux 2.6.24+): 在Linux内核控制下运行来宾操作系统的虚拟CPU所花费的时间
guest_nice (Linux 3.2.0+): 运行niced客户机(Linux内核控制下的客户机操作系统的虚拟CPU)的时间
interrupt(Windows): 用于服务硬件中断的时间(类似于UNIX上的“irq”)
dpc (Windows): 服务延迟程序调用(DPCs)的时间;DPCs是运行在低于标准中断优先级的中断。
logicInfo:各个逻辑cpu信息(数组)
user:在用户模式下执行普通进程所花费的时间;在Linux上,这还包括访客时间
system:在内核模式下执行的进程所花费的时间
idle: 无所事事的时间
nice(UNIX): niced(优先级)进程在用户模式下执行所花费的时间;在Linux上,这也包括了guest_nice时间
iowait(Linux): 等待I/O完成所花费的时间
irq(Linux, BSD): 用于服务硬件中断的时间
softirq(Linux): 用于服务软件中断的时间
steal(Linux 2.6.11+): 在虚拟环境中运行的其他操作系统所花费的时间
guest(Linux 2.6.24+): 在Linux内核控制下运行来宾操作系统的虚拟CPU所花费的时间
guest_nice (Linux 3.2.0+): 运行niced客户机(Linux内核控制下的客户机操作系统的虚拟CPU)的时间
interrupt(Windows): 用于服务硬件中断的时间(类似于UNIX上的“irq”)
dpc (Windows): 服务延迟程序调用(DPCs)的时间;DPCs是运行在低于标准中断优先级的中断。
memory:内存信息
total: 总的物理内存
available:不需要系统进入交换分区,可以立即分给线程的内存,这个值代表了系统可使用的内存情况
percent: 系统内存使用率,percent = (total-available)/total*100
used: 已使用的内存,注意:total- free不一定等于used.
free: 已准备好,但是还一点都没被使用的内存,注意:total- used不一定等于free
active (UNIX):当前正在使用或最近使用过的内存,因此它位于RAM中。
inactive (UNIX):标记为未使用的内存。
buffers (Linux, BSD):缓存文件系统元数据之类的东西。
cached (Linux, BSD):缓存各种东西。
shared (Linux, BSD):可以由多个进程同时访问的内存。
slab (Linux):内核数据结构缓存。
wired (BSD, macOS):标记为始终保留在RAM中的内存。它永远不会移动到磁盘。
total: 总交换内存(以字节为单位)
used: 使用的交换内存(以字节为单位)
free: 可用字节交换内存
percent: 使用率计算为 (total - available) / total * 100
sin: 系统已从磁盘交换的字节数(累积)
sout: 系统已从磁盘换出的字节数(累积)
user:用户信息(数组)
name: 用户名。
terminal: 与用户关联的tty或伪tty(如果有),否则为None。
host: 与条目关联的主机名(如果有)。
started: 创建时间,以浮点数为单位,以自纪元以来的秒数表示。
pid: 登录过程的PID(如sshd,tmux,gdm-session-worker等)。在Windows和OpenBSD上,始终将其设置为None。
netInfoAll:网络流量信息(总)
bytes_sent: 发送的字节数
bytes_recv: 收到的字节数
packets_sent: 发送的包数
packets_recv: 收到的包数
errin: 接收时的错误总数
errout: 发送时的错误总数
dropin: 被丢弃的入站数据包总数
dropout: 丢弃的传出数据包总数(在macOS和BSD上始终为0)
netInfo:网络流量信息(各个网卡连接)(数组)
name:网卡名称
info:网卡信息
bytes_sent: 发送的字节数
bytes_recv: 收到的字节数
packets_sent: 发送的包数
packets_recv: 收到的包数
errin: 接收时的错误总数
errout: 发送时的错误总数
dropin: 被丢弃的入站数据包总数
dropout: 丢弃的传出数据包总数(在macOS和BSD上始终为0)
netStat:网卡状态(各个网卡)(数组)
name:网卡名称
info:网卡信息
isup: 一个布尔值,指示NIC是否已启动并正在运行。
duplex: 双工通信类型;可以是 NIC_DUPLEX_FULL, NIC_DUPLEX_HALF , NIC_DUPLEX_UNKNOWN.
speed: NIC速度(以兆位(MB)表示),如果无法确定(例如“ localhost”),则会将其设置为0。
mtu: NIC的最大传输单位,以字节为单位。
disk:磁盘信息
diskRWAll:磁盘总体信息
read_count: 读取次数
write_count: 写入次数
read_bytes:读取的字节数
write_bytes: 写入的字节数
read_time: (除NetBSD,OpenBSD以外的所有)从磁盘读取所花费的时间(以毫秒为单位)
write_time:(除NetBSD,OpenBSD以外的所有)花费在写入磁盘上的时间(以毫秒为单位)
busy_time: (Linux,FreeBSD)花费在执行实际I / O上的时间(以毫秒为单位)
read_merged_count(Linux): 合并读取的数量
write_merged_count(Linux): 合并写入数
partitionDiskRWAll:磁盘各个挂载磁盘信息(数组)
name:分区名(同通过此命令查看到的名称。cat /proc/partitions)
info:磁盘信息
read_count: 读取次数
write_count: 写入次数
read_bytes:读取的字节数
write_bytes: 写入的字节数
read_time: (除NetBSD,OpenBSD以外的所有)从磁盘读取所花费的时间(以毫秒为单位)
write_time:(除NetBSD,OpenBSD以外的所有)花费在写入磁盘上的时间(以毫秒为单位)
busy_time: (Linux,FreeBSD)花费在执行实际I / O上的时间(以毫秒为单位)
read_merged_count(Linux): 合并读取的数量
write_merged_count(Linux): 合并写入数
disk_usage:所有已挂载的分区情况(数组)
device:设备
mountpoint:挂载点
fstype:文件系统类型
opts:其它设置如 rw表示可读可写
total:总数(字节)
used:已使用(字节)
free:已准备(字节)
percent:使用率
progress:进程信息(数组)
pid:进程PID。这是类的唯一(只读)属性。
ppid:进程父PID。
name:进程名称。
username:拥有进程的用户名。在UNIX上,这是通过使用实际进程uid计算的。
status:当前进程状态
create_time:进程创建时间,以浮点数表示,从纪元开始以秒为单位,以UTC为单位。
exe:该进程可执行文件为绝对路径。在某些系统上,这也可能是空字符串。
num_threads:当前由该进程使用的线程数。在某些系统上,这也可能是空字符串。
num_handles:此进程当前使用的句柄数。可用性:Windows
cwd:进程当前工作目录作为绝对路径。在某些系统上,这也可能是空字符串。
memory_percent:内存使用率。在某些系统上,这也可能是空字符串。