socialnerds/hive
Archived
1
0
This repository has been archived on 2020-12-06. You can view files and clone it, but cannot push or open issues or pull requests.
hive/app.py

128 lines
3.7 KiB
Python
Executable File

#!/usr/bin/python
from time import time
from os import getloadavg
import logging #TODO be more precise
#import pdb
class probe(object):
""""
An atomic measurment
Consists of name, value and unit. Unit is optional, althought when
available it is highly recommended to state it; bd default it is set
to "".
"""
def __init__(self, name, value, unit=""):
self.name = name
self.time = time()
self.value = value
self.unit = unit
class survey(object):
"""
A survey skeleton - a survey is a probe container.
Inherit from this class if you want to create your on Survey.
"Implement" update by any means.
"""
def __init__(self):
self.probe = [] #just to let you know
self.update()
self.name = 'Generic Survey'
def update(self):
"""
Updates the list of probes
Empty list of probes first and fill it with new ones afterwards
"""
pass
class display(object):
"""
A display skeleton - a display updates and displays surveys
Inherit from this class if you want to create your own display
Implement display.
"""
def __init__(self):
self.survey = []
def update(self):
for s in self.survey:
s.update()
def display(self):
"""
Displays self.survey items
Don't forget to add some surveys first and update them as needed
"""
pass
class surveyLoad(survey):
""" Surveys unix load average """
def __init__(self):
super(surveyLoad,self).__init__() #call superconstructor
self.name = 'Load Survey'
def update(self):
self.probe = []
try:
self.load = getloadavg()
self.probe.append(probe('lastminute',self.load[0]))
self.probe.append(probe('last5minute',self.load[1]))
self.probe.append(probe('last15minute',self.load[2]))
except Exception, ex:
logging.exception("Error ocurred while probing %s" % self.__class__.__name__)
pass
class surveyFree(survey):
#TODO it's just a quick hack; might not work too well
""" Primitive memory survey"""
def __init__(self):
super(surveyFree,self).__init__()
self.name = 'Simple Memory Survey'
def update(self):
self.probe = []
try:
f = open('/proc/meminfo', "r")
self.probe.append(self.parseline(f.next()))
self.probe.append(self.parseline(f.next()))
f.next() #skip Buffer
self.probe.append(self.parseline(f.next()))
f.close()
self.probe.append(probe("Effective free memory",
self.probe[1].value + self.probe[2].value, 'kb'))
except Exception, ex:
logging.exception("Error ocurred while probing %s" % self.__class__.__name__)
pass
def parseline(self, memline):
tmp = memline.partition(' ')
name = tmp[0].rstrip(':')
tmp = tmp[2].lstrip(' ').partition(' ')
value = int(tmp[0])
unit = tmp[2].rstrip('\n')
return probe(name,value,unit)
class displaycli(display):
""" "Dumps" survey to standard out """
def display(self):
for s in self.survey:
s.update()
print "Name: %s" % s.name
for p in s.probe:
print "%s \t%s %s" % (p.name,p.value,p.unit)
print ""
if __name__ == "__main__":
dis = displaycli()
dis.survey.append(surveyLoad())
dis.survey.append(surveyLoad())
dis.survey.append(surveyFree())
dis.display()
#pdb.set_trace()