python监听服务器cpu,内存信息等

import json
import psutil
import time
import os
import re
import paho.mqtt.client as mqtt
from log4p import Logger
# 获取服务器CPU,内存,磁盘相关信息

import constant
import security
import utils

log = Logger('all.log', level='debug')


def getServerMetrics():
    # ------------------CPU-------------------------
    cpu = {}
    # 系统运行时长(秒)
    cpu['runningTime'] = int(time.time() - psutil.boot_time())
    # 逻辑CPU核数
    cpu['count'] = psutil.cpu_count()
    # CPU的总占用率
    cpu['percent'] = psutil.cpu_percent(1)  # 阻塞1秒
    # CPU的用户、系统、空闲时间,格式 :
    # scputimes(user=628790.15, nice=18.62, system=173299.3, idle=24341950.58, iowait=13951.41, irq=0.0, softirq=6954.63, steal=0.0, guest=0.0, guest_nice=0.0)
    # scputimes(user=46637.3125, system=8990.093750000058, idle=374547.70312499994, interrupt=569.484375, dpc=398.0625)
    cpu_times = psutil.cpu_times()
    total = cpu_times.system + cpu_times.idle + cpu_times.user
    cpu['system'] = float('%0.2f' % (cpu_times.system * 100 / total))
    cpu['idle'] = float('%0.2f' % (cpu_times.idle * 100 / total))
    cpu['userPercent'] = float('%0.2f' % (cpu_times.user * 100 / total))
    # print(cpu)
    # {'percent': 0.0, 'system': 0.022, 'idle': 0.879, 'user': 0.099}
    # -------------------内存----------------------
    memory = {}
    # svmem(total=17027571712, available=10754330624, percent=36.8, used=6273241088, free=10754330624)
    # svmem(total=16658386944, available=8768794624, percent=47.4, used=7446777856, free=2997215232, active=8725438464, inactive=3980324864, buffers=401440768, cached=5812953088, shared=96354304, slab=736215040)
    virtual_memory = psutil.virtual_memory()
    # 总内存(M)
    memory['total'] = virtual_memory.total / 1024 / 1024
    # 已用内存(M)
    memory['used'] = virtual_memory.used / 1024 / 1024
    # 可用内存
    memory['available'] = virtual_memory.available / 1024 / 1024
    # 内存使用率
    memory['percent'] = virtual_memory.percent
    # print(memory)
    # -------------------硬盘----------------------
    disk = []
    # [sdiskpart(device='/dev/vda1', mountpoint='/', fstype='ext4', opts='rw,relatime,data=ordered'), sdiskpart(device='/dev/vdb1', mountpoint='/data', fstype='ext3', opts='rw,relatime,data=ordered')]
    # [sdiskpart(device='C:\\', mountpoint='C:\\', fstype='NTFS', opts='rw,fixed', maxfile=255, maxpath=260)]
    # sdiskusage(total=254721126400, used=229651865600, free=25069260800, percent=90.2)}
    alltotal = 1  # 防止被除数为0,所以初始为1
    allused = 0
    for partition in psutil.disk_partitions():
        usage = psutil.disk_usage(partition.mountpoint)
        disk.append({'deviceName': partition.mountpoint, 'percent': usage.percent,
                     'total': usage.total / 1024 / 1024, 'used': usage.used / 1024 / 1024,
                     'free': usage.free / 1024 / 1024})
        alltotal = alltotal + usage.total / 1024 / 1024
        allused = allused + usage.used / 1024 / 1024

    # print(disk)
    server = {'cpu': cpu, 'memory': memory, 'disk': disk, 'diskPercent': allused / alltotal}
    # print(server)
    return server
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

胡家骏

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值