Now with valid JSON format :D

This commit is contained in:
Robert Hausch 2012-04-07 17:46:42 -07:00
parent 144390e546
commit 17cadcd440
3 changed files with 8 additions and 8 deletions

View File

@ -43,10 +43,12 @@ class Stats:
self.sensors.append(mem_monitor())
def getStats(self):
message = ''
data = []
for s in self.sensors:
s.update()
message += '{'+s.getFormatedData()+'}'
data.append(s.getFormatedData())
message = '{"Stats":['+','.join('%s'%x for x in data)+']}'
return message

View File

@ -11,10 +11,8 @@ class cpu_monitor:
self.cpu_times = psutil.cpu_times()
def getFormatedData(self):
message = ''
message += 'Cpu usage: '
message += ', '.join('%.1f'%x for x in self.cpu_usage)
message += ' CPU times: '
message += ', '.join('%.3f'%x for x in self.cpu_times)
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

View File

@ -12,7 +12,7 @@ class mem_monitor:
self.virt_mem_usage = psutil.virtmem_usage()
def getFormatedData(self):
message = 'Memory- Phys: %.1f Virt: %.1f' % (self.mem_usage.percent, self.virt_mem_usage.percent)
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