More data gathered and new data structure structure
- memory returns more data now - both sensors have had their data reordered and the CPU sensor now returns data for all cpus - sensors now return a dict with named sub dicts - monitor.py merges and converts it all to json with built in python json
This commit is contained in:
parent
03e10a093a
commit
4630be0b83
14
monitor.py
14
monitor.py
|
@ -22,6 +22,7 @@
|
||||||
import threading
|
import threading
|
||||||
from socket import *
|
from socket import *
|
||||||
import time
|
import time
|
||||||
|
import json
|
||||||
|
|
||||||
#from sensors.cpu import *
|
#from sensors.cpu import *
|
||||||
#from sensors.memory import *
|
#from sensors.memory import *
|
||||||
|
@ -56,14 +57,12 @@ class Stats():
|
||||||
time.sleep(0.1)
|
time.sleep(0.1)
|
||||||
|
|
||||||
def getStats(self):
|
def getStats(self):
|
||||||
data = []
|
data = {}
|
||||||
self.acquire_read()
|
self.acquire_read()
|
||||||
for s in self.sensors:
|
for s in self.sensors:
|
||||||
data.append(s.getFormatedData())
|
data.update(s.getFormatedData())
|
||||||
message = '{"Stats":['+','.join('%s'%x for x in data)+']}'
|
|
||||||
|
|
||||||
self.release_read()
|
self.release_read()
|
||||||
return message
|
return json.dumps(data)
|
||||||
|
|
||||||
def acquire_read(self):
|
def acquire_read(self):
|
||||||
self.read_lock.acquire()
|
self.read_lock.acquire()
|
||||||
|
@ -98,8 +97,13 @@ serv_sock = socket(AF_INET, SOCK_STREAM)
|
||||||
serv_sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
|
serv_sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
|
||||||
|
|
||||||
|
|
||||||
|
try:
|
||||||
serv_sock.bind((ADDR)) #tuple with one param
|
serv_sock.bind((ADDR)) #tuple with one param
|
||||||
serv_sock.listen(MAX_QUEUE)
|
serv_sock.listen(MAX_QUEUE)
|
||||||
|
except Exception as err:
|
||||||
|
print "Error: ", type(err), ': ', err
|
||||||
|
stats.stop.set()
|
||||||
|
exit(0)
|
||||||
|
|
||||||
|
|
||||||
while 1:
|
while 1:
|
||||||
|
|
|
@ -30,12 +30,15 @@ class cpu_monitor:
|
||||||
self.num_cpus = len(psutil.cpu_percent(0,percpu=True))
|
self.num_cpus = len(psutil.cpu_percent(0,percpu=True))
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
self.cpu_usage = psutil.cpu_percent(0.1, percpu=True)
|
self.cpu = []
|
||||||
self.cpu_times = psutil.cpu_times()
|
cpu_usage = psutil.cpu_percent(0.1, percpu=True)
|
||||||
|
cpu_times = psutil.cpu_times(True)
|
||||||
|
for i in range(0,len(cpu_usage)):
|
||||||
|
self.cpu.append( {'percent': cpu_usage[i], 'user': cpu_times[i].user,
|
||||||
|
'nice': cpu_times[i].nice, 'system': cpu_times[i].system,
|
||||||
|
'idle': cpu_times[i].idle, 'iowait': cpu_times[i].iowait,
|
||||||
|
'irq': cpu_times[i].irq, 'softirq': cpu_times[i].softirq})
|
||||||
|
|
||||||
def getFormatedData(self):
|
def getFormatedData(self):
|
||||||
data = '{"name": "Usage", "values": ['+','.join('{"value": %.1f}'%x for x in self.cpu_usage)+']}'
|
return {'cpu': self.cpu}
|
||||||
data += ',{"name": "Times", "values": [{"name": "user", "value": %.2f},{"name": "system", "value": %.2f},{"name": "idle", "value": %.2f},{"name":"nice", "value": %.2f},{"name": "iowait", "value": %.2f},{"name": "irq", "value": %.2f},{"name": "softirq", "value": %.2f}]}' % (self.cpu_times.user, self.cpu_times.system, self.cpu_times.idle, self.cpu_times.nice, self.cpu_times.iowait, self.cpu_times.irq, self.cpu_times.softirq)
|
|
||||||
message = '{"type": "Cpu", "value": %.1f, "data": [%s]}' % ( sum(self.cpu_usage)/self.num_cpus, data )
|
|
||||||
return message
|
|
||||||
|
|
||||||
|
|
|
@ -30,13 +30,15 @@ class mem_monitor:
|
||||||
self.virt_mem_usage = ''
|
self.virt_mem_usage = ''
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
self.mem_usage = psutil.phymem_usage()
|
phys_mem = psutil.phymem_usage()
|
||||||
#self.mem_usage['buffers'] = psutil.phymem_buffers()
|
self.mem_usage = {'total': phys_mem.total, 'used': phys_mem.used,
|
||||||
#self.mem_usage['cached'] = psutil.cached_phymem()
|
'free': phys_mem.free, 'percent': phys_mem.percent,
|
||||||
self.virt_mem_usage = psutil.virtmem_usage()
|
'buffers': psutil.phymem_buffers(),
|
||||||
|
'cached': psutil.cached_phymem()}
|
||||||
|
virt_mem = psutil.virtmem_usage()
|
||||||
|
self.swap_usage = {'total': virt_mem.total, 'used': virt_mem.used, 'free': virt_mem.free, 'percent': virt_mem.percent}
|
||||||
|
|
||||||
def getFormatedData(self):
|
def getFormatedData(self):
|
||||||
message = '{"type": "Memory", "value": %.1f, "data": [{"name": "Physical", "value": "%.1f"},{"name": "Virtual", "value": "%.1f"}]}' % (self.mem_usage.percent, self.mem_usage.percent, self.virt_mem_usage.percent)
|
return {'memory': self.mem_usage, 'swap': self.swap_usage}
|
||||||
return message
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue