Skip to content
Browse files

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
  • Loading branch information...
1 parent 03e10a0 commit 4630be0b83c2bbbb92d59971e9c4ed9c0f58a2c3 @dballard committed Apr 10, 2012
Showing with 28 additions and 19 deletions.
  1. +11 −7 monitor.py
  2. +9 −6 sensors/psutil_sensors/cpu.py
  3. +8 −6 sensors/psutil_sensors/memory.py
View
18 monitor.py
@@ -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 @@ def update_loop(self):
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 @@ def handle(self, sock):
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
15 sensors/psutil_sensors/cpu.py
@@ -30,12 +30,15 @@ def __init__(self):
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
14 sensors/psutil_sensors/memory.py
@@ -30,13 +30,15 @@ def __init__(self):
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}

0 comments on commit 4630be0

Please sign in to comment.
Something went wrong with that request. Please try again.