[Python monitoring]psutil module simple to use

Source: Internet
Author: User

installation is simplePip Install Psutil

Website address is https://pythonhosted.org/psutil/(detailed API on document) GitHub address is https://github.com/giampaolo/psutil/

Psutil better Place, one is cross-platform, do not need to switch the platform when the re-open,Another benefit of the toolset is the CPU, memory, disks, network, all of which can be obtained. Can be used to do system monitoring, performance analysis, process management. The systems that can be supported areLinux, Windows, OSX, FreeBSD and Sun solaris,32 and 64-bit systems all support, while supporting pyhton2.4 to 3.4.
To see how good cross-platform is, under Windows Labs
#-*-coding:utf-8-*-#python2.7x#author: [email protected] 2014-12-12#psutiltest.py "Follow the tutorial for a simple study of the use of Psutil, Windows Try "Import psutilimport datetime# view CPU Information print U" CPU number%s "%psutil.cpu_count () print U" physical CPU number%s "%psutil.cpu _count (logical=false) print U "CPU uptimes" Print psutil.cpu_times () print "" #查看内存信息print U "System total 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 utilization%s percent"%int (mem_rate*100) #系统启动时间print u "System boot time%s"%datetime.datetime.fromtimestamp ( Psutil.boot_time ()). Strftime ("%y-%m-%d%h:%m:%s") #系统用户users_count = Len (Psutil.users ()) Users_list = ",". Join ([ U.name for u in Psutil.users ()]) print U "currently has%s users, respectively%s"% (Users_count, users_list) #网卡, can get network card properties, 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 "network card receive traffic%s network card send traffic%s"% (BYTES_RCVD, bytes_sent) #进程 the various detailed parameters of the process # magneticDisk usage, and so on. 

from this simple case, it can be seen that Psuti is powerful, so it works well on window, and it is very suitable for system data collection.
Please refer to the official documentation for details if you need to use it. There are also some code snippets on gist to refer to Https://gist.github.com/search?q=psutil

This article is from the "Orangleliu Notebook" blog, reproduced please be sure to keep this source http://blog.csdn.net/orangleliu/article/details/41891447

Author Orangleliu using Attribution-NonCommercial-sharing protocol in the same way



[Python monitoring]psutil module simple to use

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.