In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
Psutil is a cross-platform, which can easily obtain the process and utilization (including CPU, memory, disk, network, etc.) information of the system running. The main application domain system monitoring, analysis and restriction of system resources and process management. It implements the same functions provided by command-line tools, such as ps/top/lsof/netstat/who/df/kill/nice/free/nice/ionstat/iotop/uptime/pidof/tty/taskset/pmap. Python versions from 2.4 to 3.4 are supported.
Psutil size units are generally in bytes.
Install psutil
Wget https://pypi.python.org/packages/source/p/psutil/psutil-2.0.0.tar.gz-- no-check-certificatetar-xzvf psutil-2.0.0.tar.gzcd psutil-2.0.0python setup.py install # psutil/_psutil_linux.c:12:20: fatal error: Python.h: no file or directory yum install python-devel.x86_64
1. Cpu information
The CPU utilization of the linux operating system has the following parts:
User time, the percentage of time spent executing the daemon
System time, percentage of time it takes to execute kernel processes and intermediate cases
Wait io, the percentage of time that the CPU was in the idle state due to IO waiting
Percentage of time that Idle,CPU spent in idle statu
We can get this information very easily using python's psutil.cpu_times () method, and colleagues can also get hardware-related information about CPU, such as hardware-related information about CPU, such as the logical number of physical number fields of CPU.
> psutil.cpu_times () # View all cpu information scputimes (user=762.76, nice=4.47, system=428.97, idle=4012472.08, iowait=207.27, irq=0.0, softirq=7.07, steal=0.0, guest=0.0) Guest_nice=0.0) > psutil.cpu_times (). User # View user's cpu time ratio 762.77 > > psutil.cpu_count () # View the number of cpu logics 2 > psutil.cpu_count (logical=False) # View the physical number of cpu 2
2. Memory information
The memory utilization information of the linux system involves total (total memory), used (memory used), free (free memory), buffers (buffer usage), cache (cache usage), swap (swap partition usage), and so on. Use the psutil.virtual_memory () and psutil.swap_memory () methods to get this information, respectively.
> import psutil > mem = psutil.virtual_memory () # use the psutil.virtual_memory method to get complete memory information > memsvmem (total=3975487488L, available=2716561408L, percent=31.7, used=2024685568L, free=1950801920L, active=1328680960, inactive=484872192, buffers=1433600L Cached=764325888) > mem.total # get total memory 3975487488L > > mem.free # get free memory 1950801920L > > psutil.swap_memory () # get SWAP partition information sswap (total=1073737728L, used=0L, free=1073737728L, percent=0.0, sin=0, sout=0) >
3. Disk information
Disk utilization is obtained by using the psutil.disk_usage method
Disk IO information includes read_count (number of read IO), write_count (number of write IO), read_bytes (number of IO write bytes), read_time (disk read time), and write_time (disk write time). These IO information is obtained by psutil_io_counters ().
Psutil.disk_io_counters () # get the total number of IO of the disk psutil.disk_partitions () # get the complete information of the disk psutil.disk_usage ('/') # get the usage status of the partition (parameter) psutil.disk_io_counters (perdisk=True) # perdisk=True parameter get the number of IO, read and write information of a single partition
4. Read network information
Network information is similar to disk IO information, which involves several key points, including byes_sent (number of bytes sent), byte_recv=xxx (number of bytes accepted), pack-ets_sent=xxx (number of bytes sent), and pack-ets_recv=xxx (number of packets received). These network information is obtained by the psutil.net_io_counters () method.
Psutil.net_io_counters () # get the total IO information of the network psutil.net_io_counters (pernic=True) # pernic=True output the IO information of each network interface
5. Get the login information of the current system user
Psutil.users () # get the user information of the current login system import psutil,datetimepsutil.boot_time () # get the boot time, return datetime.datetime.fromtimestamp (psutil.boot_time ()) in linux time format. Strftime ("% Y-%m-%d% H:% M:% S") # convert to natural time format
6. System process management method
Get the process information of the current system, get the running status of the current program, including the start time of the process, view the setting of CPU affinity, memory utilization, IO information, socket connections, the number of threads and so on.
(1) obtain process information
View all processes of the system
Psutil.pids () # list all processes ID p = psutil.Process (2423) # instantiate a process object Parameter is a process PID p.name () # process name p.exe () # process bin path p.cwd () # process working directory absolute path p.status () # process status p.create_time () # process creation time p.uids () # process uid information p.gids () # process gid information p.cpu_times () # process cpu time information, including user System two cpu information p.cpu_affinity () # get process cpu affinity. If you want to set the cpu affinity, just use the cpu number as a reference p.memory_percent () # process memory utilization p.memory_info () # process memory rss,vms information p.io_counters () # process IO information Including reading and writing IO numbers and parameter p.connectios () # return process list p.num_threads () # number of threads opened by the process
(2) the Popen class of psutil starts the application, which can track the relevant information of the program.
From subprocess import PIPEp = psutil.Popen (["/ usr/bin/python", "- c", "print ('hello')"], stdout=PIPE) p.name () p.username () p.communicate () p.cpu_times () # get the CPU time the process is running
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.