How to Use the psutil Module to Retrieve Process and System Information on Devices Running Junos OS
The psutil
Python module is available on certain devices that
support Python automation scripts and that are running either Junos OS Evolved or
Junos OS with upgraded FreeBSD. You can use the psutil
module in
Python scripts to retrieve information about running processes and system
utilization on the device, for example, information about the CPU, memory, disks,
and processes. The module implements the functionality of many command-line tools
such as ps
and uptime
, among others.
Table 1 outlines the supported psutil
functions. For more information
about the psutil
module and its functions, see the official
documentation at https://psutil.readthedocs.io/en/latest/.
Function Category |
Supported Functions |
Supported Functions |
---|---|---|
CPU |
|
|
Disk |
|
|
Memory |
|
|
Network |
– |
|
Processes |
|
|
Sensors |
– |
– |
System Info |
|
|
The following sample Python op script demonstrates calls to the
psutil
functions to retrieve information about the system and
processes on the given device running Junos OS:
import psutil import datetime ### *** CPU FUNCTIONS *** # Number of logical CPUs in the system print ("psutil.cpu_count() = {0}".format(psutil.cpu_count())) ### *** DISK FUNCTIONS *** # List of named tuples containing all mounted disk partitions dparts = psutil.disk_partitions() print("psutil.disk_partitions() = {0}".format(dparts)) # Disk usage statistics du = psutil.disk_usage('/') print("psutil.disk_usage('/') = {0}".format(du)) ### *** MEMORY FUNCTIONS *** # System memory usage statistics mem = psutil.virtual_memory() print("psutil.virtual_memory() = {0}".format(mem)) THRESHOLD = 100 * 1024 * 1024 # 100MB if mem.available <= THRESHOLD: print("warning, available memory below threshold") ### *** PROCESS FUNCTIONS *** # List of current running process IDs. pids = psutil.pids() print("psutil.pids() = {0}".format(pids)) # Check whether the given PID exists in the current process list. for proc in psutil.process_iter(): try: pinfo = proc.as_dict(attrs=['pid', 'name']) except psutil.NoSuchProcess: pass else: print(pinfo) ### *** SYSTEM INFORMATION FUNCTIONS *** # System boot time expressed in seconds since the epoch boot_time = psutil.boot_time() print("psutil.boot_time() = {0}".format(boot_time)) # System boot time converted to human readable format print(datetime.datetime.fromtimestamp(psutil.boot_time()).strftime("%Y-%m-%d %H:%M:%S")) # Users currently connected on the system users = psutil.users() print("psutil.users() = {0}".format(users))
When you execute the op script, the script prints the requested information about the device. Some of the sample output has been truncated for brevity.
user@host> op psutil-test.py psutil.cpu_count() = 4 psutil.disk_partitions() = [sdiskpart(device='/dev/md0.uzip', mountpoint='/', fstype='cd9660', opts='ro'), sdiskpart(device='devfs', mountpoint='/dev', fstype='devfs', opts='rw,multilabel'), ...] psutil.disk_usage('/') = sdiskusage(total=20609945600L, used=12531843072L, free=6429306880L, percent=66.099999999999994) psutil.virtual_memory() = svmem(total=4230012928L, available=7632039936L, percent=-80.400000000000006, used=658825216L, free=4325273600L, active=59793408L, inactive=3306766336L, buffers=289771520L, cached=0L, shared=249659392L, wired=599031808L) psutil.pids() = [43521, 43134, 33616, 33610, 33609, 33608, 33605, 33604, 33603, 33602, 33599, 33598, 33597, 33596, 33593, 8356, 7893, 7871, 7870, 7869, 7868, 7867, 7866, 7865, 7864, 7863, 7862, 7861, 7860, 7859, 7858, 7857, 7856, 7854, 7853, 7851, 7850, 7849, 7848, 7847, 7846, 7845, 7844, 7842, 7841, 7840, 7839, 7838, 7837, 7836, 7835, 7834, 7833, 7832, 7831, 7830, 7829, 7828, 7826, 7825, 7824, 7823, 7822, 7821, 7820, 7819, 7817, 7807, 7627, 7560, 7410, 7370, 7362, 7359, 7345, 7344, 7343, 7342, 7340, 7336, 7335, 7328, 7327, 7322, 7320, 7319, 7318, 7314, 7313, 7312, 7308, 7307, 7304, 7303, 7301, 7299, 7296, 7295, 7293, 7282, 7267, 7266, 7262, 6278, 6276, 6275, 5886, 5493, 5492, 4015, 4014, 3954, 3953, 3895, 3894, 3835, 3834, 3776, 3775, 3717, 3716, 3660, 3659, 3601, 3600, 3541, 3540, 3481, 3480, 3423, 3422, 3364, 3363, 3304, 3303, 3160, 3159, 3091, 3090, 3032, 3031, 2973, 2972, 2916, 2915, 2857, 2856, 2798, 2797, 2707, 2650, 2649, 2591, 2590, 2532, 2531, 2464, 2463, 2407, 2406, 2348, 2347, 2289, 2220, 2219, 2161, 2160, 2102, 2101, 2043, 2042, 1984, 1983, 1925, 1924, 1865, 1782, 1781, 1671, 1670, 1564, 1563, 1089, 1088, 1032, 1031, 973, 972, 916, 915, 859, 858, 803, 802, 483, 482, 164, 163, 54, 53, 52, 51, 49, 48, 47, 46, 45, 44, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33, 32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 9, 8, 7, 6, 5, 4, 3, 2, 11, 1, 10, 0] {'pid': 0, 'name': 'kernel'} {'pid': 1, 'name': 'init'} {...} psutil.boot_time() = 1570456872.0 2019-10-07 07:01:12 psutil.users() = [suser(name='user', terminal='pts/0', host='198.51.100.1', started=1570552539.0)]