03 python 自动化运维~日常使用

1. 文本操作

2. 系统信息监控~psutil 模块

psutil 模块官方文档:https://psutil.readthedocs.io/en/latest/#

    psutil(python 系统和进程实用程序)是一个跨平台库,用于 在Python中检索有关正在运行的进程和系统利用率(CPU、内存、磁盘、网络、传感器)的信息。
它主要用于系统监控、分析、限制进程资源和管理正在运行的进程。它实现了 UNIX 命令行工具提供的许多功能,例如:ps、top、lsof、netstat、ifconfig、who、
df、kill、free、nice、ionice、iostat、iotop、uptime、pidof、tty、taskset、pmap。psutil 目前支持以下平台:
    Linux
    视窗
    苹果系统
    FreeBSD、OpenBSD、NetBSD
    太阳索拉里斯
    艾克斯

1. 获取/监控cpu 信息

>>> import psutil
#获取CPU逻辑数量
>>> psutil.cpu_count()
16
#获取CPU物理数量
>>> psutil.cpu_count(logical=False)
8
#获取CPU占比
>>> psutil.cpu_percent()
1.7
#获取每个CPU占比
>>> psutil.cpu_percent(percpu=True)
[5.5, 0.8, 4.5, 1.3, 2.9, 0.3, 1.2, 0.4, 0.9, 0.1, 4.1, 0.2, 3.9, 0.2, 1.6, 1.0]

2. 获取/监控内存信息

>>> import psutil
>>> psutil.virtual_memory()
svmem(total=16540291072, available=5063139328, percent=69.4, used=11477151744, free=5063139328)

3. 监控磁盘信息

>>> psutil.disk_partitions()
[sdiskpart(device='C:\\', mountpoint='C:\\', fstype='NTFS', opts='rw,fixed', maxfile=255, maxpath=260), sdiskpart(device='D:\\', mountpoint='D:\\', fstype='NTFS', opts='rw,fixed', maxfile=255, maxpath=260), sdiskpart(device='E:\\', mountpoint='E:\\', fstype='NTFS', opts='rw,fixed', maxfile=255, maxpath=260), sdiskpart(device='F:\\', mountpoint='F:\\', fstype='NTFS', opts='rw,fixed', maxfile=255, maxpath=260), sdiskpart(device='G:\\', mountpoint='G:\\', fstype='NTFS', opts='rw,fixed', maxfile=255, maxpath=260)]
>>> psutil.disk_partitions()
[sdiskpart(device='C:\\', mountpoint='C:\\', fstype='NTFS', opts='rw,fixed', maxfile=255, maxpath=260), sdiskpart(device='D:\\', mountpoint='D:\\', fstype='NTFS', opts='rw,fixed', maxfile=255, maxpath=260), sdiskpart(device='E:\\', mountpoint='E:\\', fstype='NTFS', opts='rw,fixed', maxfile=255, maxpath=260), sdiskpart(device='F:\\', mountpoint='F:\\', fstype='NTFS', opts='rw,fixed', maxfile=255, maxpath=260), sdiskpart(device='G:\\', mountpoint='G:\\', fstype='NTFS', opts='rw,fixed', maxfile=255, maxpath=260)]
>>> psutil.disk_usage('/')
sdiskusage(total=128849014784, used=77110710272, free=51738304512, percent=59.8)
>>> psutil.disk_io_counters()
sdiskio(read_count=484523, write_count=579985, read_bytes=26294988288, write_bytes=18608367616, read_time=1312, write_time=660)

4. 监控网络信息

#获取网络读写字节/包的个数
>>> psutil.net_io_counters()
snetio(bytes_sent=57260091, bytes_recv=1302594219, packets_sent=512236, packets_recv=935061, errin=0, errout=0, dropin=0, dropout=0)

#获取网络接口信息
>>> psutil.net_if_addrs()
{'以太网': [snicaddr(family=<AddressFamily.AF_LINK: -1>, address='C0-18-50-33-47-92', netmask=None, broadcast=None, ptp=None), snicaddr(family=<AddressFamily.AF_INET: 2>, address='169.254.41.184', netmask='255.255.0.0', broadcast=None, ptp=None), snicaddr(family=<AddressFamily.AF_INET6: 23>, address='fe80::ed3e:e277:c004:29b8', netmask=None, broadcast=None, ptp=None)], 
'本地连接* 1': [snicaddr(family=<AddressFamily.AF_LINK: -1>, address='38-FC-98-68-71-D6', netmask=None, broadcast=None, ptp=None), snicaddr(family=<AddressFamily.AF_INET: 2>, address='169.254.49.178', netmask='255.255.0.0', broadcast=None, ptp=None), snicaddr(family=<AddressFamily.AF_INET6: 23>, address='fe80::b897:2c72:bc25:31b2', netmask=None, broadcast=None, ptp=None)], 
'本地连接* 10': [snicaddr(family=<AddressFamily.AF_LINK: -1>, address='3A-FC-98-68-71-D5', netmask=None, broadcast=None, ptp=None), snicaddr(family=<AddressFamily.AF_INET: 2>, address='169.254.229.245', netmask='255.255.0.0', broadcast=None, ptp=None), snicaddr(family=<AddressFamily.AF_INET6: 23>, address='fe80::2d54:bf70:f343:e5f5', netmask=None, broadcast=None, ptp=None)], 'VMware Network Adapter VMnet1': [snicaddr(family=<AddressFamily.AF_LINK: -1>, address='00-50-56-C0-00-01', netmask=None, broadcast=None, ptp=None), snicaddr(family=<AddressFamily.AF_INET: 2>, address='192.168.25.1', netmask='255.255.255.0', broadcast=None, ptp=None), snicaddr(family=<AddressFamily.AF_INET6: 23>, address='fe80::9083:b3ed:6b57:3', netmask=None, broadcast=None, ptp=None)], 'VMware Network Adapter VMnet8': [snicaddr(family=<AddressFamily.AF_LINK: -1>, address='00-50-56-C0-00-08', netmask=None, broadcast=None, ptp=None), snicaddr(family=<AddressFamily.AF_INET: 2>, address='192.168.221.1', netmask='255.255.255.0', broadcast=None, ptp=None), snicaddr(family=<AddressFamily.AF_INET6: 23>, address='fe80::ed21:4bcd:e9ef:564c', netmask=None, broadcast=None, ptp=None)], 'WLAN': [snicaddr(family=<AddressFamily.AF_LINK: -1>, address='38-FC-98-68-71-D5', netmask=None, broadcast=None, ptp=None), snicaddr(family=<AddressFamily.AF_INET: 2>, address='192.168.1.105', netmask='255.255.255.0', broadcast=None, ptp=None), snicaddr(family=<AddressFamily.AF_INET6: 23>, address='fe80::6968:10ec:a0c:4349', netmask=None, broadcast=None, ptp=None)], 
'蓝牙网络连接': [snicaddr(family=<AddressFamily.AF_LINK: -1>, address='38-FC-98-68-71-D9', netmask=None, broadcast=None, ptp=None), snicaddr(family=<AddressFamily.AF_INET: 2>, address='169.254.224.22', netmask='255.255.0.0', broadcast=None, ptp=None), snicaddr(family=<AddressFamily.AF_INET6: 23>, address='fe80::61f2:6f7c:8c36:e016', netmask=None, broadcast=None, ptp=None)], 'Loopback Pseudo-Interface 1': [snicaddr(family=<AddressFamily.AF_INET: 2>, address='127.0.0.1', netmask='255.0.0.0', broadcast=None, ptp=None), snicaddr(family=<AddressFamily.AF_INET6: 23>, address='::1', netmask=None, broadcast=None, ptp=None)]}

#获取网络接口状态
>>> psutil.net_if_stats()
{'以太网': snicstats(isup=False, duplex=<NicDuplex.NIC_DUPLEX_FULL: 2>, speed=0, mtu=1500), 
'蓝牙网络连接': snicstats(isup=False, duplex=<NicDuplex.NIC_DUPLEX_FULL: 2>, speed=3, mtu=1500), 'VMware Network Adapter VMnet1': snicstats(isup=True, duplex=<NicDuplex.NIC_DUPLEX_FULL: 2>, speed=100, mtu=1500), 'VMware Network Adapter VMnet8': snicstats(isup=True, duplex=<NicDuplex.NIC_DUPLEX_FULL: 2>, speed=100, mtu=1500), 'Loopback Pseudo-Interface 1': snicstats(isup=True, duplex=<NicDuplex.NIC_DUPLEX_FULL: 2>, speed=1073, mtu=1500), 'WLAN': snicstats(isup=True, duplex=<NicDuplex.NIC_DUPLEX_FULL: 2>, speed=54, mtu=1500), 
'本地连接* 1': snicstats(isup=False, duplex=<NicDuplex.NIC_DUPLEX_FULL: 2>, speed=0, mtu=1500), 
'本地连接* 10': snicstats(isup=False, duplex=<NicDuplex.NIC_DUPLEX_FULL: 2>, speed=0, mtu=1500)}

#获取当前网络链接连接信息
>>> psutil.net_connections()
[sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type=<SocketKind.SOCK_DGRAM: 2>, laddr=addr(ip='0.0.0.0', port=5355), raddr=(), status='NONE', pid=2932), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type=<SocketKind.SOCK_DGRAM: 2>, laddr=addr(ip='192.168.25.1', port=137), raddr=(), status='NONE', pid=4), sconn(fd=-1, family=<AddressFamily.AF_INET6: 23>, type=<SocketKind.SOCK_DGRAM: 2>, laddr=addr(ip='::', port=4500), raddr=(), status='NONE', pid=5360), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type=<SocketKind.SOCK_STREAM: 1>, laddr=addr(ip='127.0.0.1', port=9080), raddr=(), status='LISTEN', pid=5656), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type=<SocketKind.SOCK_STREAM: 1>, laddr=addr(ip='192.168.1.105', port=58115), raddr=addr(ip='184.28.223.33', port=80), status='CLOSE_WAIT', pid=13492), sconn(fd=-1, family=<AddressFamily.AF_INET6: 23>, type=<SocketKind.SOCK_STREAM: 1>, laddr=addr(ip='::', port=49668), raddr=(), status='LISTEN', pid=5092), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type=<SocketKind.SOCK_STREAM: 1>, laddr=addr(ip='0.0.0.0', port=7680), raddr=(), status='LISTEN', pid=13796), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type=<SocketKind.SOCK_STREAM: 1>, laddr=addr(ip='0.0.0.0', port=902), raddr=(), status='LISTEN', pid=1052), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type=<SocketKind.SOCK_STREAM: 1>,
......

5. 获取进程信息

#获取所有进程的进程号
>>> psutil.pids()
[0, 4, 172, 464, 520, 528, 604, 676, 716, 740, 1052, 1076, 1216, 1220, 1292, 1312, 1340, 1352, 1436, 1460, 1500, 1528, 1576, 1584, 1688, 1816, 1820, 1916, 1924, 1932, 1940, 1948, 1992, 2148, 2160, 2228, 2264, 2268, 2332, 2356, 2364, 2492, 2544, 2568, 2656, 2688, 2696, 2792, 2836, 2932, 2980, 2988, 2996, 3004, 3012, 3020, 3168, 3184, 3340, 3412, 3444, 3492, 3524, 3532, 3832, 3840, 3892, 3920, 3960, 3964, 3976, 4044, 4048, 4056, 4068, 4120, 4252, 4280, 4484, 4572, 4584, 4600, 4676, 4680, 4720, 4800, 4840, 4952, 5032, 5040, 5060, 5092, 5140, 5360, 5368, 5384, 5428, 5556, 5560, 5568, 5584, 5592, 5600, 5608, 5616, 5624, 5632, 5640, 5648, 5656, 5664, 5672, 5680, 5688, 5696, 5716, 5764, 5780, 6028, 6072, 6604, 6696, 7032, 7052, 7100, 7152, 7188, 7336, 7360, 7380, 7408, 7436, 7488, 7512, 7536, 7576, 7744, 7800, 7832, 7928, 7960, 8068, 8084, 8164, 8356, 8408, 8556, 8648, 8672, 8696, 8740, 8748, 8776, 9072, 9316, 9320, 9344, 9568, 9588, 9600, 9688, 9700, 9752, 9848, 9864, 9992, 10280, 10460, 10496, 10548, 10664, 10672, 10680, 10696, 10764, 10992, 11020, 11180, 11432, 11464, 11468, 11480, 11484, 11508, 11612, 11640, 11748, 11760, 11832, 11948, 12068, 12084, 12112, 12184, 12356, 12476, 12516, 12528, 13020, 13092, 13120, 13248, 13284, 13336, 13468, 13492, 13548, 13672, 13708, 13724, 13796, 13896, 14024, 14088, 14140, 14196, 14308, 14316, 14596, 14700, 14872, 14892, 15028, 15352]

6. 有关进程的其他信息

#获取该进程(号)内存占用,rss 就是实际占用的内存
>>> psutil.Process(15352).memory_info()
pmem(rss=93106176, vms=61927424, num_page_faults=303743, peak_wset=174489600, wset=93106176, peak_paged_pool=484376, paged_pool=470408, peak_nonpaged_pool=54640, nonpaged_pool=47112, pagefile=61927424, peak_pagefile=159944704, private=61927424)

#获取该进程(号)CPU占用
>>> psutil.Process(15352).cpu_times()
pcputimes(user=6.171875, system=2.21875, children_user=0.0, children_system=0.0)

#获取该进程(号)的线程数
>>> psutil.Process(15352).num_threads()
24

#获取该进程(号)内存占比
>>> psutil.Process(15352).memory_percent()
0.5631529432857613
posted @ 2022-06-15 21:02  潜龙~勿用  阅读(157)  评论(0编辑  收藏  举报