Python Monitoring Server Sharp--psutil

Source: Internet
Author: User

Server monitoring by installing some commonly used monitoring software, sometimes you need to run some shell or Python scripts, and the shell can use the system's own PS/FREE/TOP/DF and other shell commands, Python can invoke modules such as subprocess to run shell commands, but it is cumbersome to do so. Here is a more useful third-party module: Psutil.
Psutil is a cross-platform library for retrieving information about running processes and system utilization (CPU, memory, disk, network, sensor) in Python. It is mainly used for system monitoring, analysis, restricting the management of process resources and running processes. It implements many of the features provided by the UNIX command-line tool, such as: Ps,top,lsof,netstat,ifconfig,who,df,kill,free,nice,ionice,iostat,iotop,uptime,pidof, Tty,taskset,pmap. Psutil currently supports the following platforms:

Linux  WindowsOSX,FreeBSD, OpenBSD, NetBSDSun SolarisAIX...

32-bit and 64-bit architectures with Python2.6 to 3.6. You can also run on PyPy.
Installation
Examples in this article are in Python version 3.6 environment;
# pip3 install psutil
Common modules
Get Psutil Version Information

In [1]: import psutilIn [2]: psutil.version_infoOut[2]: (5, 4, 3

Get CPU Information

In [3]: psutil.cpu_count() # 逻辑CPU核数Out[3]: 4In [4]: psutil.cpu_count(logical=False) # 物理CPU核数Out[4]: 2In [5]: psutil.cpu_times() # CPU的用户、系统、空闲时间Out[5]: scputimes(user=240773.0, nice=0.0, system=96416.32, idle=1161930.41)In [9]: psutil.cpu_percent(percpu=True) # 获取每个CPU的使用率,类似TOP命令Out[9]: [43.3, 22.0, 42.0, 23.0]In [10]: top = [psutil.cpu_percent(interval=i, percpu=True) for i in range(10)] #设置每秒刷新时间间隔,统计十次的结果In [11]: topOut[11]:[[40.8, 19.7, 38.5, 20.7],[25.7, 5.9, 13.0, 5.0],[35.0, 15.6, 30.0, 14.4],[23.7, 7.0, 18.3, 7.4],[38.5, 17.0, 34.2, 17.5],[37.2, 19.6, 36.3, 20.0],[29.6, 16.6, 28.8, 16.8],[37.7, 19.0, 35.4, 18.7],[30.8, 16.3, 26.9, 16.5],[44.2, 27.9, 41.5, 28.6]]

Get memory information

In [13]: psutil.virtual_memory() #获取内存统计数据,单位bytes,我这里8G内存Out[13]: svmem(total=8589934592, available=1891045376, percent=78.0, used=6053986304, free=15130624, active=1878392832, inactive=1875914752, wired=2299678720)In [14]: psutil.swap_memory() # 获取swap的统计数据Out[14]: sswap(total=2147483648, used=1340866560, free=806617088, percent=62.4, sin=126090076160, sout=3524710400)

Get disk information

In [17]: psutil.disk_partitions() #获取磁盘分区信息Out[17]: [sdiskpart(device=‘/dev/disk1‘, mountpoint=‘/‘, fstype=‘hfs‘, opts=‘rw,local,rootfs,dovolfs,journaled,multilabel‘)]In [20]: psutil.disk_usage(‘/‘) # 获取分区使用情况,这里使用了25.4%Out[20]: sdiskusage(total=499055067136, used=126482944000, free=372309979136, percent=25.4)In [22]: psutil.disk_io_counters() #磁盘IO情况Out[22]: sdiskio(read_count=7364142, write_count=6510641, read_bytes=282106464256, write_bytes=261763244544, read_time=2608778, write_time=1095259)

Get Network Information

In [All]: Psutil.net_if_stats () # Get NIC interface status out[23]:{' awdl0 ': Snicstats (Isup=true, duplex=<nicduplex.nic_duplex_ Unknown:0&gt, Speed=0, mtu=1484), ' Bridge0 ': Snicstats (Isup=true, Duplex=<nicduplex.nic_duplex_unknown:0>, Speed=0, mtu=1500), ' En0 ': Snicstats (Isup=true, Duplex=<nicduplex.nic_duplex_unknown:0>, Speed=0, mtu=1500), ' En1 ': Snicstats (Isup=true, Duplex=<nicduplex.nic_duplex_full:2>, Speed=0, mtu=1500), ' en2 ': Snicstats (isup= True, Duplex=<nicduplex.nic_duplex_full:2>, Speed=0, mtu=1500), ' gif0 ': Snicstats (Isup=false, duplex=< Nicduplex.nic_duplex_unknown:0&gt, Speed=0, mtu=1280), ' Lo0 ': Snicstats (Isup=true, Duplex=<nicduplex.nic_duplex _unknown:0&gt, Speed=0, mtu=16384), ' p2p0 ': Snicstats (Isup=true, Duplex=<nicduplex.nic_duplex_unknown:0>, Speed=0, mtu=2304), ' stf0 ': Snicstats (Isup=false, Duplex=<nicduplex.nic_duplex_unknown:0>, Speed=0, mtu=1280), ' Utun0 ': Snicstats (Isup=true, Duplex=<nicduplex.nic_duplex_unknown:0>, Speed=0, MTu=2000), ' utun1 ': Snicstats (Isup=true, Duplex=<nicduplex.nic_duplex_unknown:0>, Speed=0, mtu=1352)}In [25]: Psutil.net_if_stats (). Get ("En0") #获取单个网卡en0的状态Out [+]: Snicstats (isup=true, duplex=<nicduplex.nic_duplex_ Unknown:0&gt, Speed=0, mtu=1500) in []: Psutil.net_if_addrs () # Get address information for all network cards out[26]:{' awdl0 ': [Snic (family=< Addressfamily.af_link:18&gt, address= ' 36:7d:f3:80:6e:4e ', Netmask=none, Broadcast=none, Ptp=None), Snic (family= <addressfamily.af_inet6:30&gt, address= ' fe80::347d:f3ff:fe80:6e4e%awdl0 ', netmask= ' ffff:ffff:ffff:ffff:: ', Broadcast=none, Ptp=none)], ' bridge0 ': [Snic (Family=<addressfamily.af_link:18>, address= ' 4a:00:02:c0:33:70 ', Netmask=none, Broadcast=none, Ptp=none)], ' en0 ': [Snic (Family=<addressfamily.af_inet:2>, address= ' 192.168.0.101 ', netmask= ' 255.255.255.0 ', broadcast= ' 192.168.0.255 ', Ptp=none), Snic (family=<addressfamily.af_ Link:18&gt, address= ' ac:bc:32:91:32:8b ', Netmask=none, Broadcast=none, Ptp=none), Snic (family=<addressfamilY.af_inet6:30&gt, address= ' fe80::1476:ce7e:210a:2e32%en0 ', netmask= ' ffff:ffff:ffff:ffff:: ', Broadcast=none, ptp= None)], ' en1 ': [Snic (Family=<addressfamily.af_link:18>, address= ' 4a:00:02:c0:33:70 ', Netmask=none, broadcast= None, Ptp=none)], ' en2 ': [Snic (Family=<addressfamily.af_link:18>, address= ' 4a:00:02:c0:33:71 ', Netmask=none, Broadcast=none, Ptp=none)], ' lo0 ': [Snic (Family=<addressfamily.af_inet:2>, address= ' 127.0.0.1 ', netmask= ' 255.0.0.0 ', Broadcast=none, Ptp=none), Snic (family=<addressfamily.af_inet6:30>, address= ':: 1 ', netmask= ' FFFF: Ffff:ffff:ffff:ffff:ffff:ffff:ffff ', Broadcast=none, Ptp=none), Snic (FAMILY=&LT;ADDRESSFAMILY.AF_INET6:30&GT;, Address= ' Fe80::1%lo0 ', netmask= ' ffff:ffff:ffff:ffff:: ', Broadcast=none, Ptp=none], ' p2p0 ': [Snic (family=< Addressfamily.af_link:18&gt, address= ' 0e:bc:32:91:32:8b ', Netmask=none, Broadcast=none, Ptp=None)], ' utun0 ': [Snic ( Family=<addressfamily.af_inet6:30&gt, address= ' fe80::583c:77a0:6b93:b045%utun0 ', netmask= 'FFFF:FFFF:FFFF:FFFF:: ', Broadcast=none, Ptp=none)], ' utun1 ': [Snic (Family=<addressfamily.af_inet:2>, address= ' 10.5.200.244 ', Netmask=none, Broadcast=none, ptp= ' 10.5.200.244 ')]}# get the address of the En0 NIC, which includes Mac and IPv6 address in [Max]: for addr in Psutil.net_if_addrs (). Get ("En0"): ...: print (addr.address) 192.168.0.101ac:bc:32:91:32:8bfe80::1476:ce7e:210a : 2e32%en0in []: Psutil.net_io_counters () # Gets the number of bytes/packets read from the network out[43]: Snetio (bytes_sent=174614221, bytes_recv=586279725 , packets_sent=863903, packets_recv=873583, errin=0, errout=0, Dropin=0, dropout=0) in []: Psutil.net_connections () # For network connection information, note that root privileges are required here.

Get process information:

In [46]: psutil.pids() # 获取所有进程IDIn [47]: psutil.Process(61) # 获取指定PID的进程信息Out[47]: psutil.Process(pid=61, name=‘dsAccessService‘, started=‘2018-02-26 09:57:04‘)In [49]: psutil.Process(45573).exe() # 获取进程的exe路径Out[49]: ‘/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/Resources/Python.app/Contents/MacOS/Python‘In [50]: psutil.Process(45573).name() # 获取进程名称Out[50]: ‘Python‘In [52]: psutil.Process(45573).cmdline() # 获取进程启动的命令Out[52]:[‘/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/Resources/Python.app/Contents/MacOS/Python‘,‘/usr/local/bin/ptipython‘]In [56]: psutil.Process(45573).num_threads() # 获取进程的线程数量Out[56]: 3In [57]: psutil.Process(45573).environ() # 获取进程的环境变量信息

Summarize:
Using the Psutil module can be a more comprehensive monitoring of the system, if you are considering using Python as a monitoring system or script tool, you can consider this module.

Python Monitoring Server Sharp--psutil

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.