import json
import psutil
import time
import os
import re
import paho.mqtt.client as mqtt
from log4p import Logger
# Get server CPU, Memory , Disk related information
import constant
import security
import utils
log = Logger('all.log', level='debug')
def getServerMetrics():
# ------------------CPU-------------------------
cpu = {
}
# The system runs for a long time ( second )
cpu['runningTime'] = int(time.time() - psutil.boot_time())
# Logic CPU Check the number
cpu['count'] = psutil.cpu_count()
# CPU Total occupancy of
cpu['percent'] = psutil.cpu_percent(1) # Blocking 1 second
# CPU Users of 、 System 、 free time , Format :
# 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 ----------------------
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()
# Total memory (M)
memory['total'] = virtual_memory.total / 1024 / 1024
# Used memory (M)
memory['used'] = virtual_memory.used / 1024 / 1024
# Available memory
memory['available'] = virtual_memory.available / 1024 / 1024
# Memory usage
memory['percent'] = virtual_memory.percent
# print(memory)
# ------------------- Hard disk ----------------------
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 # Prevent dividend from being 0, So the initial is 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