当前位置 博文首页 > 程序员石磊:python自动化运维-系统信息监控

    程序员石磊:python自动化运维-系统信息监控

    作者:[db:作者] 时间:2021-07-04 15:55

    最近负责的几个省的项目,问题频出,经常接到运维同事这样的反馈

    • jvm cpu%
    • ogg 断了
    • oracle 归档日志满了

    以上原因可能导致数据不能正常接收并传输,因此还要想办法补救,运维同学焦头烂额,因此半夜开发接到电话是很正常的。那么问题来了有没有解决办法,能及时提醒?答案是肯定的!

    系统信息监控

    运维离不开对系统信息的监控,如CPU的使用率、内存的占用情况、网络、进程等相关信息都需要被监控,虽然我们可以通过操作系统提供的任务管理器或命令查看相关信息,但仍不能简化这些日常的运维任务。如果我们通过编写程序获取以上信息,那么系统信息监控就是一件轻松而简单的工作。
    在Python中获取系统信息最便捷的模块是psutil(process and systemutilities)。通过简短的几行代码就可以获取系统相关信息,而且还是跨平台库。

    psutil

    psutil(进程和系统实用程序)是一个跨平台库,用于检索 Python 中有关正在运行的进程和系统利用率(CPU、内存、磁盘、网络、传感器)的信息。它主要用于系统监控、分析和限制进程资源以及运行过程的管理。它实现了经典UNIX命令行工具提供的许多功能_,如ps、顶部、iotop、lsof、netstat、ifconfig、free_和免费等。
    git地址https://github.com/giampaolo/psutil
    psutil不属于标准库,需要手动安装。安装psutil非常简单,执行以下命令即可。

    pip install psutil
    

    CPU监控

    >>> import psutil
    >>>
    >>> psutil.cpu_times()
    scputimes(user=3961.46, nice=169.729, system=2150.659, idle=16900.540, iowait=629.59, irq=0.0, softirq=19.42, steal=0.0, guest=0, nice=0.0)
    >>>
    >>> for x in range(3):
    ...     psutil.cpu_percent(interval=1)
    ...
    4.0
    5.9
    3.8
    >>>
    >>> for x in range(3):
    ...     psutil.cpu_percent(interval=1, percpu=True)
    ...
    [4.0, 6.9, 3.7, 9.2]
    [7.0, 8.5, 2.4, 2.1]
    [1.2, 9.0, 9.9, 7.2]
    >>>
    >>> for x in range(3):
    ...     psutil.cpu_times_percent(interval=1, percpu=False)
    ...
    scputimes(user=1.5, nice=0.0, system=0.5, idle=96.5, iowait=1.5, irq=0.0, softirq=0.0, steal=0.0, guest=0.0, guest_nice=0.0)
    scputimes(user=1.0, nice=0.0, system=0.0, idle=99.0, iowait=0.0, irq=0.0, softirq=0.0, steal=0.0, guest=0.0, guest_nice=0.0)
    scputimes(user=2.0, nice=0.0, system=0.0, idle=98.0, iowait=0.0, irq=0.0, softirq=0.0, steal=0.0, guest=0.0, guest_nice=0.0)
    >>>
    >>> psutil.cpu_count()
    4
    >>> psutil.cpu_count(logical=False)
    2
    >>>
    >>> psutil.cpu_stats()
    scpustats(ctx_switches=20455687, interrupts=6598984, soft_interrupts=2134212, syscalls=0)
    >>>
    >>> psutil.cpu_freq()
    scpufreq(current=931.42925, min=800.0, max=3500.0)
    >>>
    >>> psutil.getloadavg()  # also on Windows (emulated)
    (3.14, 3.89, 4.67)
    

    内存监控

    >>> psutil.virtual_memory()
    svmem(total=10367352832, available=6472179712, percent=37.6, used=8186245120, free=2181107712, active=4748992512, inactive=2758115328, buffers=790724608, cached=3500347392, shared=787554304)
    >>> psutil.swap_memory()
    sswap(total=2097147904, used=296128512, free=1801019392, percent=14.1, sin=304193536, sout=677842944)
    >>>
    

    硬盘监控

    >>> psutil.disk_partitions()
    [sdiskpart(device='/dev/sda1', mountpoint='/', fstype='ext4', opts='rw,nosuid', maxfile=255, maxpath=4096),
     sdiskpart(device='/dev/sda2', mountpoint='/home', fstype='ext, opts='rw', maxfile=255, maxpath=4096)]
    >>>
    >>> psutil.disk_usage('/')
    sdiskusage(total=21378641920, used=4809781248, free=15482871808, percent=22.5)
    >>>
    >>> psutil.disk_io_counters(perdisk=False)
    sdiskio(read_count=719566, write_count=1082197, read_bytes=18626220032, write_bytes=24081764352, read_time=5023392, write_time=63199568, read_merged_count=619166, write_merged_count=812396, busy_time=4523412)
    >>>
    

    进程监控

    >>> import psutil
    >>> psutil.pids()
    [1, 2, 3, 4, 5, 6, 7, 46, 48, 50, 51, 178, 182, 222, 223, 224, 268, 1215,
     1216, 1220, 1221, 1243, 1244, 1301, 1601, 2237, 2355, 2637, 2774, 3932,
     4176, 4177, 4185, 4187, 4189, 4225, 4243, 4245, 4263, 4282, 4306, 4311,
     4312, 4313, 4314, 4337, 4339, 4357, 4358, 4363, 4383, 4395, 4408, 4433,
     4443, 4445, 4446, 5167, 5234, 5235, 5252, 5318, 5424, 5644, 6987, 7054,
     7055, 7071]
    >>>
    >>> p = psutil.Process(7055)
    >>> p
    psutil.Process(pid=7055, name='python3', status='running', started='09:04:44')
    >>> p.name()
    'python'
    >>> p.exe()
    '/usr/bin/python'
    >>> p.cwd()
    '/home/giampaolo'
    >>> p.cmdline()
    ['/usr/bin/python', 'main.py']
    >>>
    >>> p.pid
    7055
    >>> p.ppid()
    7054
    >>> p.children(recursive=