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:
Dan Ballard 2012-04-10 23:49:33 -07:00
parent 03e10a093a
commit 4630be0b83
3 changed files with 28 additions and 19 deletions

View File

@ -22,6 +22,7 @@
import threading
from socket import *
import time
import json
#from sensors.cpu import *
#from sensors.memory import *
@ -56,14 +57,12 @@ class Stats():
time.sleep(0.1)
def getStats(self):
data = []
data = {}
self.acquire_read()
for s in self.sensors:
data.append(s.getFormatedData())
message = '{"Stats":['+','.join('%s'%x for x in data)+']}'
data.update(s.getFormatedData())
self.release_read()
return message
return json.dumps(data)
def acquire_read(self):
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.bind((ADDR)) #tuple with one param
serv_sock.listen(MAX_QUEUE)
try:
serv_sock.bind((ADDR)) #tuple with one param
serv_sock.listen(MAX_QUEUE)
except Exception as err:
print "Error: ", type(err), ': ', err
stats.stop.set()
exit(0)
while 1:

View File

@ -30,12 +30,15 @@ class cpu_monitor:
self.num_cpus = len(psutil.cpu_percent(0,percpu=True))
def update(self):
self.cpu_usage = psutil.cpu_percent(0.1, percpu=True)
self.cpu_times = psutil.cpu_times()
self.cpu = []
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):
data = '{"name": "Usage", "values": ['+','.join('{"value": %.1f}'%x for x in self.cpu_usage)+']}'
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
return {'cpu': self.cpu}

View File

@ -30,13 +30,15 @@ class mem_monitor:
self.virt_mem_usage = ''
def update(self):
self.mem_usage = psutil.phymem_usage()
#self.mem_usage['buffers'] = psutil.phymem_buffers()
#self.mem_usage['cached'] = psutil.cached_phymem()
self.virt_mem_usage = psutil.virtmem_usage()
phys_mem = psutil.phymem_usage()
self.mem_usage = {'total': phys_mem.total, 'used': phys_mem.used,
'free': phys_mem.free, 'percent': phys_mem.percent,
'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):
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 message
return {'memory': self.mem_usage, 'swap': self.swap_usage}