[Python monitoring] simple use of the psutil Module

Source: Internet
Author: User

[Python monitoring] simple use of the psutil Module
Easy to install pip install psutil

The official website address is https://pythonhosted.org/psutil/ (detailed api documentation) github address is https://github.com/giampaolo/psutil/

Psutil is better. One is cross-platform, and it is re-opened when you do not need to switch the platform. Another good tool is CPU, memory, disks, network, this information can be obtained. It can be used for system monitoring, performance analysis, and process management. Supported systems include Linux, Windows, OSX, FreeBSD and Sun Solaris, 32, and 64-bit systems, and pyhton2.4 to 3.4.
In order to see whether cross-platform services are useful

#-*-Coding: UTF-8-*-# python2.7x # author: orangleliu@gmail.com 2014-12-12 # psutiltest. py''' follow the tutorial to learn how to use psutil. In windows, try '''import psutilimport datetime # view cpu information print u "CPU count % s" % psutil. cpu_count () print u "physical CPU count % s" % psutil. cpu_count (logical = False) print u "CPU uptimes" print psutil. cpu_times () print "" # view memory information print u "total system memory % s M" % (psutil. TOTAL_PHYMEM/1024/1024) print u "system available memory % s M" % (psutil. avail_phymem ()/1024/1024) mem_rate = int (psutil. avail_phymem ()/float (psutil. TOTAL_PHYMEM) print u "system memory usage % s %" % int (mem_rate * 100) # system start time print u "system start time % s" % datetime. datetime. fromtimestamp (psutil. boot_time ()). strftime ("% Y-% m-% d % H: % M: % S") # system user users_count = len (psutil. users () users_list = ",". join ([u. name for u in psutil. users ()]) print u "there are currently % s users, respectively % s" % (users_count, users_list) # Nic, you can get Nic attributes, connections, current traffic and other information. net = psutil.net _ io_counters () bytes_sent = '{0 :. 2f} kb '. format (net. bytes_recv/1024) bytes_rcvd = '{0 :. 2f} kb '. format (net. bytes_sent/1024) print u "Nic Receiving Traffic % s Nic sending traffic % s" % (bytes_rcvd, bytes_sent) # detailed parameters of process processes # disk usage, etc.

In this simple case, we can see that the power of PstI is so powerful in Windows that it is very suitable for system data collection.
For more information, see the official documentation. There are some code snippets on gist can refer to the https://gist.github.com/search? Q = psutil

This article from the "orangleliu notebook" blog, reprint please be sure to keep this source http://blog.csdn.net/orangleliu/article/details/41891447

Author orangleliu adopts signature-non-commercial use-share protocol in the same way



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.