用户名:小罗ge11 文章数:62 评论数:28
访问量:13570:1281:756:4 注册日期:2014-07-09
前几天看到朋友郑彦生分享的psutil模块的使用方法,感觉对于监控又多了一层想法,因为前期主要是通过saltstack,收集数据,但是有时候比较麻烦,在小型规模做监控的时候还是建议用psutil模块,方便快捷:首先介绍psutil模块的一些基本用法:
1、监控cpu:
>>> import psutil >>>psutil.cpu_count() #查看线程 >>>psutil.cpu_percent() #查看cpu负载 |
2、查看内存相关信息:
内存可以查看总共多少内存,使用了多少,还剩多少:
>>> mem = psutil.virtual_memory()
#统计所有的信息
svmem(total = 67502866432L ,available = 61983199232L ,percent = 8.1999999999999993 ,used = 7092293632L ,free = 60410572800L ,active = 5551243264 ,inactive = 410374144 ,buffers = 335728640L ,cached = 1236897792 ) >>>mem.total #内存总数 >>>mem.used #使用了多少内存 >>>mem.free #剩余内存 |
3、硬盘的IO和使用情况:
>>>psutil.disk_partitions() [sdiskpart(device = '/dev/mapper/VolGroup-lv_root' ,mountpoint = '/' ,fstype = 'ext4' ,opts = 'rw' ),sdiskpart(device = '/dev/sda3' ,mountpoint = '/boot' ,fstype = 'ext4' ,opts = 'rw' ),sdiskpart(device = '/dev/mapper/VolGroup-lv_home' ,mountpoint = '/home' ,fstype = 'ext4' ,opts = 'rw' )] >>>psutil.disk_partitions( '/' ) [sdiskpart(device = '/dev/mapper/VolGroup-lv_root' ,mountpoint = '/' ,fstype = 'ext4' ,opts = 'rw' ),sdiskpart(device = 'proc' ,mountpoint = '/proc' ,fstype = 'proc' ,opts = 'rw' ),sdiskpart(device = 'sysfs' ,mountpoint = '/sys' ,fstype = 'sysfs' ,opts = 'rw' ),sdiskpart(device = 'devpts' ,mountpoint = '/dev/pts' ,fstype = 'devpts' ,opts = 'rw,gid=5,mode=620' ),sdiskpart(device = 'tmpfs' ,mountpoint = '/dev/shm' ,fstype = 'tmpfs' ,opts = 'rw' ),sdiskpart(device = '/dev/sda3' ,mountpoint = '/boot' ,fstype = 'ext4' ,opts = 'rw' ),sdiskpart(device = '/dev/mapper/VolGroup-lv_home' ,mountpoint = '/home' ,fstype = 'ext4' ,opts = 'rw' ),sdiskpart(device = ' ',mountpoint=' / proc / sys / fs / binfmt_misc ',fstype=' binfmt_misc ',opts=' rw')] >>>psutil.disk_io_counters() sdiskio(read_count = 107158 ,write_count = 20666736 ,read_bytes = 1792485376 ,write_bytes = 133169932288 ,read_time = 130421 ,write_time = 337076882 ) >>>psutil.net_io_counters() snetio(bytes_sent = 984192973 ,bytes_recv = 1274031495 ,packets_sent = 3924134 ,packets_recv = 6024010 ,errin = 0 ,errout = 0 ,dropin = 0 ,dropout = 0 ) |
{ 'lo' :snetio(bytes_sent = 349343820 ,bytes_recv = 349343820 ,packets_sent = 1583789 ,packets_recv = 1583789 ,errin = 0 ,errout = 0 ,dropin = 0 ,dropout = 0 ), 'em1' :snetio(bytes_sent = 634794145 ,bytes_recv = 823789288 ,packets_sent = 2339457 ,packets_recv = 3623086 ,errin = 0 ,errout = 0 ,dropin = 0 ,dropout = 0 ), 'em4' :snetio(bytes_sent = 0 ,bytes_recv = 0 ,packets_sent = 0 ,packets_recv = 0 ,errin< |
没有评论:
发表评论