如何使用 psutil 模块检索运行 Junos OS 的设备上的进程和系统信息
psutil
Python 模块可在支持 Python 自动化脚本且运行 Junos OS Evolved 或 Junos OS 及升级版 FreeBSD 的设备上提供。您可以使用 psutil
Python 脚本中的模块检索有关设备上运行进程和系统利用情况的信息,例如有关 CPU、内存、磁盘和进程的信息。该模块可实施多种命令行工具的功能,例如ps
,uptime
等等。
表 1 概述了支持的 psutil
功能。有关该模块及其功能的 psutil
详细信息,请参阅 https://psutil.readthedocs.io/en/latest/ 的官方文档。
功能类别 |
支持的功能 |
支持的功能 |
---|---|---|
Cpu |
|
|
磁盘 |
|
|
记忆 |
|
|
网络 |
– |
|
过程 |
|
|
传感器 |
– |
– |
系统信息 |
|
|
以下示例 Python op 脚本演示了有关功能的 psutil
呼叫,以便检索有关运行 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))
执行操作脚本时,脚本将打印有关设备的请求信息。部分样本输出已截断以实现简洁性。
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)]