2018-02-20 11:57:39   Visit  2038

psutil是python的一个扩展库,能够轻松获取服务器状态信息,用于系统监控。

#encoding:utf-8
__author__ = \\\'leechg\\\'

import psutil
import datetime

#内存
def getMem():
    mem = psutil.virtual_memory()
    return mem
#内存总量
def getMemTotal():
    mem = getMem()
    total = mem.total
    return total
#已用内存数量
def getMemUsed():
    mem = getMem()
    used = mem.used
    return used
#空闲内存
def getMemFree():
    mem = getMem()
    return mem.free
#内存占用率
def getMemUsedPercent():
    return int(getMemUsed()/float(getMemTotal())*100)
#逻辑处理器数量
def getCpuCount():
    return psutil.cpu_count()
#物理处理器数量
def getLogicalCpuCount():
    return psutil.cpu_count()
#cpu时间
def getCpuTime():
    return psutil.cpu_times().user
#磁盘信息
def getDisk():
    return psutil.disk_partitions()
#磁盘使用状态
def getDiskUsage(device = \\\"C:\\\\\\\\\\\"):
    return  psutil.disk_usage(device)
#网络状态
def getNetState():
    return psutil.net_io_counters(pernic=True)
#用户信息
def getUserInfo():
    return psutil.users()
#系统启动时间
def getBoottime():
    return psutil.boot_time()
#时间戳转为自然时间
def timestapToLocalStr(tt):
    return  datetime.datetime.fromtimestamp(tt).strftime(\\\"%Y-%m-%d %H:%M:%S\\\")


if __name__==\\\"__main__\\\":
    print \\\"Leechg\\\"
    print getMem()
    print getMemTotal()/1024/1024,\\\"M\\\"
    print getMemUsed()/1024/1024,\\\"M\\\"
    print getMemFree()/1024/1024,\\\"M\\\"
    print getMemUsedPercent()
    print getCpuCount()
    print getLogicalCpuCount()
    print getCpuTime()
    print getDisk()
    print getDiskUsage(\\\"d:\\\\\\\\\\\")
    print getNetState()
    print getUserInfo()
    print timestapToLocalStr(getBoottime())
Leechg
svmem(total=8504565760L, available=3029106688L, percent=64.4, used=5475459072L, free=3029106688L)
8110 M
5221 M
2888 M
64
8
8
587414.125
[sdiskpart(device=\'C:\\\\\', mountpoint=\'C:\\\\\', fstype=\'NTFS\', opts=\'rw,fixed\'), sdiskpart(device=\'D:\\\\\', mountpoint=\'D:\\\\\', fstype=\'NTFS\', opts=\'rw,fixed\'), sdiskpart(device=\'E:\\\\\', mountpoint=\'E:\\\\\', fstype=\'NTFS\', opts=\'rw,fixed\'), sdiskpart(device=\'F:\\\\\', mountpoint=\'F:\\\\\', fstype=\'NTFS\', opts=\'rw,fixed\'), sdiskpart(device=\'G:\\\\\', mountpoint=\'G:\\\\\', fstype=\'NTFS\', opts=\'rw,fixed\'), sdiskpart(device=\'H:\\\\\', mountpoint=\'H:\\\\\', fstype=\'NTFS\', opts=\'rw,fixed\'), sdiskpart(device=\'I:\\\\\', mountpoint=\'I:\\\\\', fstype=\'NTFS\', opts=\'rw,fixed\'), sdiskpart(device=\'K:\\\\\', mountpoint=\'K:\\\\\', fstype=\'NTFS\', opts=\'rw,fixed\'), sdiskpart(device=\'M:\\\\\', mountpoint=\'M:\\\\\', fstype=\'\', opts=\'cdrom\')]
sdiskusage(total=107374178304L, used=34099097600L, free=73275080704L, percent=31.8)
{\'\\xd2\\xd4\\xcc\\xab\\xcd\\xf8 2\': snetio(bytes_sent=0L, bytes_recv=0L, packets_sent=0L, packets_recv=0L, errin=0L, errout=0L, dropin=0L, dropout=0L), \'\\xd2\\xd4\\xcc\\xab\\xcd\\xf8\': snetio(bytes_sent=0L, bytes_recv=0L, packets_sent=0L, packets_recv=0L, errin=0L, errout=0L, dropin=0L, dropout=0L), \'\\xc0\\xb6\\xd1\\xc0\\xcd\\xf8\\xc2\\xe7\\xc1\\xac\\xbd\\xd3\': snetio(bytes_sent=0L, bytes_recv=0L, packets_sent=0L, packets_recv=0L, errin=0L, errout=0L, dropin=0L, dropout=0L), \'Loopback Pseudo-Interface 1\': snetio(bytes_sent=0L, bytes_recv=0L, packets_sent=0L, packets_recv=0L, errin=0L, errout=0L, dropin=0L, dropout=0L), \'WLAN\': snetio(bytes_sent=10181550L, bytes_recv=106462649L, packets_sent=62924L, packets_recv=253769L, errin=0L, errout=0L, dropin=0L, dropout=0L), \'\\xb1\\xbe\\xb5\\xd8\\xc1\\xac\\xbd\\xd3* 3\': snetio(bytes_sent=0L, bytes_recv=0L, packets_sent=0L, packets_recv=0L, errin=0L, errout=0L, dropin=0L, dropout=0L), \'\\xb1\\xbe\\xb5\\xd8\\xc1\\xac\\xbd\\xd3* 1\': snetio(bytes_sent=0L, bytes_recv=0L, packets_sent=0L, packets_recv=0L, errin=0L, errout=0L, dropin=0L, dropout=0L)}
[suser(name=\'licha\', terminal=None, host=\'0.0.0.0\', started=1518789595.0, pid=None)]
2018-02-03 09:25:00
©2017 Leechg.com