• python自动化运维-基础篇之psutil


    系统基础信息

    系统基础模块主要就是监控linux服务器的资源使用情况。比如网卡,内存,cpu等资源使用率

    psutil模块

    psutil是一个跨平台的模块,可以获取系统运行的进程和资源的使用率

    安装psutil
    wget https://pypi.org/project/psutil/#files/psutil-5.7.0.tar.gz
    tar xf psutil-5.7.0.tar.gz
    cd psutil-5.7.0
    python setup.py install
    
    系统性能信息
    CPU
    user time 执行用户进程时间占比
    system time 内核进程和中断进程
    iowait IO等待导致cpu处于idel空闲状态时间占比
    Idle  cpu处于空闲的占比
    
    >>> psutil.cpu_times(percpu=True)
    [scputimes(user=441.55, nice=0.04, system=192.4, idle=25082.39, iowait=7.05, irq=0.0, softirq=3.16, steal=0.0, guest=0.0, guest_nice=0.0)]
    逻辑cpu个数
    >>> psutil.cpu_count()
    1
    物理个数
    >>> psutil.cpu_count(logical=False)
    1
    获取用户进程时间占比
    >>> psutil.cpu_times().user
    441.77
    
    内存
    获取linux内存的total(内存总数),userd(使用),free(空闲),buffers(缓冲使用数),swap(交换区使用数),cache(缓存使用数)
    >>> type(psutil.virtual_memory())
    <class 'psutil._pslinux.svmem'>
    >>> psutil.virtual_memory()
    svmem(total=1019645952, available=563720192, percent=44.7, used=279293952, free=91193344, active=372215808, inactive=318390272, buffers=4096, cached=649154560, shared=7311360, slab=166547456)
    >>> mem = psutil.virtual_memory()
    >>> mem.total
    1019645952
    swap就是磁盘做内存使用,说明内存已经溢出,要注意了
    >>> psutil.swap_memory()
    sswap(total=2147479552, used=3428352, free=2144051200, percent=0.2, sin=344064, sout=3624960)
    
    DISK磁盘
    >>> psutil.disk_partitions()
    [sdiskpart(device='/dev/mapper/centos-root', mountpoint='/', fstype='xfs', opts='rw,seclabel,relatime,attr2,inode64,noquota'), sdiskpart(device='/dev/sda1', mountpoint='/boot', fstype='xfs', opts='rw,seclabel,relatime,attr2,inode64,noquota')]
    具体分区的使用率
    >>> psutil.disk_usage('/') 
    sdiskusage(total=18238930944, used=4890099712, free=13348831232, percent=26.8)
    硬盘总的IO数
    >>> psutil.disk_io_counters()
    sdiskio(read_count=11900, write_count=395, read_bytes=451097600, write_bytes=9930752, read_time=25756, write_time=826, read_merged_count=1, write_merged_count=11, busy_time=15498)
    具体到分区的IO数
    >>> psutil.disk_io_counters(perdisk=True)
    {'sda': sdiskio(read_count=6252, write_count=210, read_bytes=229998592, write_bytes=6129152, read_time=12991, write_time=452, read_merged_count=1, write_merged_count=14, busy_time=7853), 'sda1': sdiskio(read_count=511, write_count=4, read_bytes=6200320, write_bytes=2097152, read_time=220, write_time=7, read_merged_count=0, write_merged_count=0, busy_time=216), 'sda2': sdiskio(read_count=5711, write_count=206, read_bytes=222217216, write_bytes=4032000, read_time=12766, write_time=445, read_merged_count=1, write_merged_count=14, busy_time=7666), 'sr0': sdiskio(read_count=18, write_count=0, read_bytes=1052672, write_bytes=0, read_time=37, write_time=0, read_merged_count=0, write_merged_count=0, busy_time=27), 'dm-0': sdiskio(read_count=5542, write_count=220, read_bytes=217789440, write_bytes=4032000, read_time=12711, write_time=505, read_merged_count=0, write_merged_count=0, busy_time=7621), 'dm-1': sdiskio(read_count=88, write_count=0, read_bytes=2256896, write_bytes=0, read_time=17, write_time=0, read_merged_count=0, write_merged_count=0, busy_time=11)}
    
    网卡
    主要是bytes_sent,bytes_recv,packets_sent, packets_recv
    总的数据包发送接收信息
    >>> psutil.net_io_counters()
    snetio(bytes_sent=46168, bytes_recv=105921, packets_sent=385, packets_recv=1020, errin=0, errout=0, dropin=136, dropout=0)
    每个网络接口的数据包发送接收信息
    >>> psutil.net_io_counters(pernic=True)
    {'ens32': snetio(bytes_sent=49652, bytes_recv=116123, packets_sent=419, packets_recv=1120, errin=0, errout=0, dropin=151, dropout=0), 'lo': snetio(bytes_sent=0, bytes_recv=0, packets_sent=0, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0), 'docker0': snetio(bytes_sent=0, bytes_recv=0, packets_sent=0, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0)}
    
    获取主机的登录信息
    >>> psutil.users()
    [suser(name='root', terminal='pts/0', host='192.168.1.147', started=1584152320.0, pid=1231), suser(name='root', terminal='pts/1', host='192.168.1.147', started=1584152448.0, pid=1253)]
    
    系统进程的管理
    >>> psutil.pids()     #获取进程的pid
    [1, 2, 4, 6, 7, 8, 9, 10, 11, 13, 14, 15, 。。。。]
    >>> p = psutil.Process(680) #实例化某进程
    >>> p.exe()  #bin路径
    '/usr/bin/vmtoolsd'
    >>> p.name()  #获取进程名
    'vmtoolsd'
    >>> p.cwd() #工作的路径
    '/'
    >>> p.status()  #状态
    'sleeping'
    >>> p.create_time( ) #创建时间-时间戳
    1584152128.61
    >>> p.uids()
    puids(real=0, effective=0, saved=0)
    >>> p.gids()
    pgids(real=0, effective=0, saved=0)
    还可以获取cpu mem,具体查看help(模块)...
    
    from subprocess import PIPE 管道
    > p = psutil.Popen(["/usr/bin/python", "-c", "print('mark')"], stdout=PIPE)
    >>> p.name()
    'python'
    >>> p.create_time()
    1584155239.31
    >>> p.username()
    'root'
    
  • 相关阅读:
    node.js中常用的fs文件系统
    秒懂 this
    Filter 过滤器
    Ubuntu 安装zookeeper
    Vmware 设置NAT模式
    TreeMap
    ArrayList扩容
    Java 面试题收集
    SwitchyOmega 设置修改代理
    Jedis操作Redis
  • 原文地址:https://www.cnblogs.com/linux-error/p/12483887.html
Copyright © 2020-2023  润新知