psutil模块简说

psutil可以检测计算机的进程网络信息以及计算机的相关信息:
psutil能够轻松实现系统运行的进程和系统利用率(CPU内存 磁盘 网络)
他实现了ps top lsof netstat ifocnfig who df kill free nice
ionice iostat iotop uptime pidof tty taskset pmap等linux的命令

import psutil
查看内存总容量与剩余容量

import psutil
def get_mem():
	mem = psutil.virtual_memory()
	#返回计算机的总内存,与使用内存
	#mem.total,mem.used
	return mem.total,mem.used
交换分区
psutil.swap_memory()

total,used = get_mem()
#total为总内存,used为使用的内存

psutil.cpu_times()
UserTime执行用户进程的时间百分比
SYstem Time执行内核中断的时间百分比
wait IO 由于IO等待是的CPU处于idle空闲状态的百分比
Idle:CPU处于idle状态的时间百分比

def cpu_time():
	psutil.cpu_times()获取cpu逻辑信息
	scputimes(user=3995.9, nice=3.74, system=3255.34, idle=760299.97,
	iowait=1091.67, irq=0.0, softirq=36.71, steal=0.0, guest=0.0, guest_nice=0.0)

查看当前执行进程的数目:
def get_pid():
	return len(psutil.pids()

查看磁盘信息:

psutil.disk_partions()  获取磁盘完整信息
返回列表
psutil.disk_partitions()
ps -ef |grep  sd* |wc -l

[sdiskpart(device=’/dev/vda1’, mountpoint=’/’, fstype=‘ext3’, opts=‘rw,noatime,data=ordered’),
sdiskpart(device=’/dev/vda1’, mountpoint=’/var/lib/docker-latest/containers’, fstype=‘ext3’, opts=‘rw,noatime,data=ordered’),
sdiskpart(device=’/dev/vda1’,
mountpoint=’/var/lib/docker-latest/overlay2’, fstype=‘ext3’, opts=‘rw,noatime,data=ordered’)]
列表中是一个个对象

获取网络信息

psutil.net_io_counters()
psutil.disk_io_counters(perdisk=True) #perdisk=True参数获取单个分区IO个数

获取当前登录的用户个数:
psutil.users():#返回一个列表,当前登录的用户是两个,pid分别如下
[suser(name=‘root’, terminal=‘pts/0’, host=‘223.167.30.218’, started=1563630208.0, pid=8667), suser(name=‘root’, terminal=‘pts/3’, host=‘223.167.30.218’, started=1563630720.0, pid=10072)]

查看系统全部进程
psutil.pids()返回进程号列表

psutil.pids()
[1, 2, 3, 5, 7, 8, 9, 10, 11, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 30, 31, 32, 33, 41, 43, 44, 45, 46, 59, 95, 253, 257, 258, 261, 263, 265, 271, 288, 289, 362, 387, 398, 549, 575, 576, 577, 579, 581, 584, 585, 812, 813, 866, 939, 941, 943, 1086, 1088, 1394, 1894, 1904, 2284, 2621, 2632, 2737, 5595, 5607, 7469, 7721, 7726, 7727, 8330, 8381, 8645, 8667, 9010, 9172, 9303, 9982, 10070, 10072, 10188, 10190, 10541, 10542, 10788, 12394, 26600, 27204, 29535, 29539]

查看单个进程

p = psutil.Process(2423) 
p.name()   #进程名
p.exe()    #进程的bin路径
p.cwd()    #进程的工作目录绝对路径
p.status()   #进程状态
p.create_time()  #进程创建时间
p.uids()    #进程uid信息
p.gids()    #进程的gid信息
p.cpu_times()   #进程的cpu时间信息,包括user,system两个cpu信息
p.cpu_affinity()  #get进程cpu亲和度,如果要设置cpu亲和度,将cpu号作为参考就好
p.memory_percent()  #进程内存利用率
p.memory_info()    #进程内存rss,vms信息
p.io_counters()    #进程的IO信息,包括读写IO数字及参数
p.connectios()   #返回进程列表
p.num_threads()  #进程开启的线程数
听过psutil的Popen方法启动应用程序,可以跟踪程序的相关信息
from subprocess import PIPE
p = psutil.Popen(["/usr/bin/python", "-c", "print('hello')"],stdout=PIPE)
p.name()
p.username()

有待更新。。。
psutil获取详细的磁盘信息:


def disk_info():

    from celery_task import celery

    res = psutil.disk_partitions()
    dic = {}
    for i in res:   #res为一个列表
        print(i.mountpoint, psutil.disk_usage(i.mountpoint).percent)    #i.mountpoint 是挂载点,
        #psutil.disk_usage(i.mountpoint).percent是挂载点的磁盘使用的百分比。
        # i.fstye 文件系统的类型,						
     #psutil.disk_usage(i.mountpoint).total  磁盘的总容量
        dic[i.mountpoint] = [psutil.disk_usage(i.mountpoint).percent,
                             i.fstype, int(psutil.disk_usage(i.mountpoint).total / 1024 / 1024 / 1024)]
    return dic

可以通过字典将值返回到前端,前端进行渲染:

<body>


{#{{  }}#}
<h1 class="text-center">磁盘使用情况</h1>
    <table class="table table-bordered table-condensed" border=" solid 1px green">
        <thead>
        <tr>
                <th>磁盘名</th>
                <th>磁盘使用百分比</th>
                <th>磁盘格式</th>

                <th>磁盘总容量</th>
                <th>操作</th>

        </tr>

        </thead>
        <tbody>
        <tr>
            {% for k,v in resdisk.items %}
                <tr>
                <td>{{ k }}</td>
{#                <td>{{ v }}</td>#}
                    <td>{{ v.0}}</td>
                    <td>{{ v.1}}</td>
                    <td>{{ v.2}} GB</td>
{#                    <td>{{ total.3}}</td>#}
                <td>
{#                    <a href="" class="btn btn-success">待写</a>#}
                    <button class="btn btn-success" id="buttong" {{ k }}>格式化</button>
                    <a href="" class="btn btn-danger">清空</a>
                </td>
</tr>
            {% endfor %}


        </tbody>
    </table>
    {% for foo in resdisk %}

    {% endfor %}


</body>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值