python 自动化运维-psutil 模块

# _*_ coding: utf-8 _*_
__author__ = 'pythonwu'
__date__ = "2018/5/10 21:14"

import psutil
import datetime
from subprocess import PIPE

mem = psutil.virtual_memory()

print(mem.total,mem.used)

cpu = psutil.cpu_times()
print(cpu)

cpu_new = psutil.cpu_count()

print(cpu_new)
print(mem.free/1024/1024/1024)
print(psutil.disk_partitions())
print(psutil.disk_io_counters())
print(psutil.disk_io_counters(perdisk=True))
print(psutil.users())
print(psutil.boot_time())
print(datetime.datetime.fromtimestamp(psutil.boot_time()).strftime("%Y-%m-%d %H:%M:%S"))

print(psutil.pids())
p = psutil.Process(1748)
print(p.name())
# print(p.exe())
#注释window中无法使用此命令获取进程bin路径

# print(p.cwd())
#windows进程工作目录绝对路径

print(p.status()) #进程状态
print(datetime.datetime.fromtimestamp(p.create_time()).strftime("%Y-%m-%d %H:%M:%S")) #进程创建时间

print(p.cpu_times()) #进程cpu时间信息

print(p.memory_percent()) #进程利用率

print(p.memory_info()) #进程rss,vms信息

print(p.io_counters()) #进程IO信息,包括读写IO数和字节数

print(p.connections())

print(p.num_threads()) #进程开启的线程数

ps = psutil.Popen(["python","-c","print('hello word')"],stdout = PIPE)
print(ps.name())
print(ps.username())
print(ps.communicate()) #(b'hello word\r\n', None)

posted @ 2018-05-10 22:19  五等码农  阅读(167)  评论(0编辑  收藏  举报