X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=unified_model.py;h=acc89d8a09dc90d69c6a30f03b172806eec0be93;hb=944d143a6528c4157b71f51ed480aec806cbaa06;hp=58c223be8e5172829600e8bae81e1b33692424fc;hpb=77f84f1e8242cdc45eb091ab65eef940a23493a6;p=monitor.git diff --git a/unified_model.py b/unified_model.py index 58c223b..acc89d8 100755 --- a/unified_model.py +++ b/unified_model.py @@ -1,20 +1,20 @@ #!/usr/bin/python -import soltesz +from monitor import database import plc -import auth -api = plc.PLC(auth.auth, auth.plc) +api = plc.getAuthAPI() import mailer import time -from nodecommon import * +from model import * from const import * +import util.file +import config def gethostlist(hostlist_file): - import config - return config.getListFromFile(hostlist_file) + return util.file.getListFromFile(hostlist_file) #nodes = api.GetNodes({'peer_id' : None}, ['hostname']) #return [ n['hostname'] for n in nodes ] @@ -70,7 +70,7 @@ class PenaltyMap: # condition/penalty is applied, move to the next phase. -fb = soltesz.dbLoad("findbad") +#fb = database.dbLoad("findbad") class RT(object): def __init__(self, ticket_id = None): @@ -150,10 +150,10 @@ class PersistFlags(Recent): db = "persistflags" try: - pm = soltesz.dbLoad(db) + pm = database.dbLoad(db) except: - soltesz.dbDump(db, {}) - pm = soltesz.dbLoad(db) + database.dbDump(db, {}) + pm = database.dbLoad(db) #print pm if id in pm: obj = pm[id] @@ -172,9 +172,9 @@ class PersistFlags(Recent): Recent.__init__(self, withintime) def save(self): - pm = soltesz.dbLoad(self.db) + pm = database.dbLoad(self.db) pm[self.id] = self - soltesz.dbDump(self.db, pm) + database.dbDump(self.db, pm) def resetFlag(self, name): self.__setattr__(name, False) @@ -222,10 +222,10 @@ class PersistMessage(Message): db = "persistmessages" try: - pm = soltesz.dbLoad(db) + pm = database.dbLoad(db) except: - soltesz.dbDump(db, {}) - pm = soltesz.dbLoad(db) + database.dbDump(db, {}) + pm = database.dbLoad(db) #print pm if id in pm: @@ -258,9 +258,9 @@ class PersistMessage(Message): self.actiontracker.setRecent() #print "recording object for persistance" - pm = soltesz.dbLoad(self.db) + pm = database.dbLoad(self.db) pm[self.id] = self - soltesz.dbDump(self.db, pm) + database.dbDump(self.db, pm) else: # NOTE: only send a new message every week, regardless. print "Not sending to host b/c not within window of %s days" % (self.actiontracker.withintime // 60*60*24) @@ -274,11 +274,11 @@ class MonitorMessage(object): try: if 'reset' in kwargs and kwargs['reset'] == True: - soltesz.dbDump(db, {}) - pm = soltesz.dbLoad(db) + database.dbDump(db, {}) + pm = database.dbLoad(db) except: - soltesz.dbDump(db, {}) - pm = soltesz.dbLoad(db) + database.dbDump(db, {}) + pm = database.dbLoad(db) #print pm if id in pm: @@ -346,11 +346,11 @@ class PersistSitePenalty(SitePenalty): try: if 'reset' in kwargs and kwargs['reset'] == True: - soltesz.dbDump(db, {}) - pm = soltesz.dbLoad(db) + database.dbDump(db, {}) + pm = database.dbLoad(db) except: - soltesz.dbDump(db, {}) - pm = soltesz.dbLoad(db) + database.dbDump(db, {}) + pm = database.dbLoad(db) #print pm if id in pm: @@ -369,9 +369,9 @@ class PersistSitePenalty(SitePenalty): self.id = id def save(self): - pm = soltesz.dbLoad(self.db) + pm = database.dbLoad(self.db) pm[self.id] = self - soltesz.dbDump(self.db, pm) + database.dbDump(self.db, pm) class Target: @@ -413,7 +413,7 @@ class Record(object): def __init__(self, hostname, data): self.hostname = hostname self.data = data - self.plcdb_hn2lb = soltesz.dbLoad("plcdb_hn2lb") + self.plcdb_hn2lb = database.dbLoad("plcdb_hn2lb") self.loginbase = self.plcdb_hn2lb[self.hostname] return @@ -449,12 +449,11 @@ class Record(object): def getDaysDown(cls, diag_record): daysdown = -1 if diag_record['comonstats']['uptime'] != "null": - #print "uptime %s" % (int(float(diag_record['comonstats']['uptime'])) // (60*60*24)) daysdown = - int(float(diag_record['comonstats']['uptime'])) // (60*60*24) - elif diag_record['comonstats']['sshstatus'] != "null": - daysdown = int(diag_record['comonstats']['sshstatus']) // (60*60*24) - elif diag_record['comonstats']['lastcotop'] != "null": - daysdown = int(diag_record['comonstats']['lastcotop']) // (60*60*24) + #elif diag_record['comonstats']['sshstatus'] != "null": + # daysdown = int(diag_record['comonstats']['sshstatus']) // (60*60*24) + #elif diag_record['comonstats']['lastcotop'] != "null": + # daysdown = int(diag_record['comonstats']['lastcotop']) // (60*60*24) else: now = time.time() last_contact = diag_record['plcnode']['last_contact'] @@ -468,15 +467,41 @@ class Record(object): getDaysDown = classmethod(getDaysDown) def getStrDaysDown(cls, diag_record): - daysdown = cls.getDaysDown(diag_record) - if daysdown > 0: - return "%d days down"%daysdown - elif daysdown == -1: - return "Unknown number of days" + daysdown = "unknown" + last_contact = diag_record['plcnode']['last_contact'] + date_created = diag_record['plcnode']['date_created'] + + if diag_record['comonstats']['uptime'] != "null" and \ + diag_record['comonstats']['uptime'] != "-1": + daysdown = int(float(diag_record['comonstats']['uptime'])) // (60*60*24) + daysdown = "%d days up" % daysdown + + elif last_contact is None: + if date_created is not None: + now = time.time() + diff = now - date_created + daysdown = diff // (60*60*24) + daysdown = "Never contacted PLC, created %s days ago" % daysdown + else: + daysdown = "Never contacted PLC" else: - return "%d days up"% -daysdown + now = time.time() + diff = now - last_contact + daysdown = diff // (60*60*24) + daysdown = "%s days down" % daysdown + return daysdown getStrDaysDown = classmethod(getStrDaysDown) + #def getStrDaysDown(cls, diag_record): + # daysdown = cls.getDaysDown(diag_record) + # if daysdown > 0: + # return "%d days down"%daysdown + # elif daysdown == -1: + # return "Never online" + # else: + # return "%d days up"% -daysdown + #getStrDaysDown = classmethod(getStrDaysDown) + def takeAction(self): pp = PersistSitePenalty(self.hostname, 0, db='persistpenalty_hostnames') if 'improvement' in self.data['stage'] or self.improved(): @@ -508,10 +533,6 @@ class Record(object): return message def getContacts(self): - from config import config - #print "policy" - config = config() - roles = self.data['email'] if not config.mail and not config.debug and config.bcc: @@ -544,10 +565,10 @@ class NodeRecord: self.hostname = hostname self.ticket = None self.target = target - if hostname in fb['nodes']: - self.data = fb['nodes'][hostname]['values'] - else: - raise Exception("Hostname not in scan database") + #if hostname in fb['nodes']: + # self.data = fb['nodes'][hostname]['values'] + #else: + # raise Exception("Hostname not in scan database") def stageIswaitforever(self): if 'waitforever' in self.data['stage']: @@ -587,7 +608,7 @@ class NodeRecord: self.ticket.closeTicket() def exempt_from_penalties(self): - bl = soltesz.dbLoad("l_blacklist") + bl = database.dbLoad("l_blacklist") return self.hostname in bl def penalties(self): @@ -617,12 +638,49 @@ class NodeRecord: def _get_contacts_for_condition(self): pass +class Action(MonRecord): + def __init__(self, host, data): + self.host = host + MonRecord.__init__(self, data) + return + + def deltaDays(self, delta): + t = datetime.fromtimestamp(self.__dict__['time']) + d = t + timedelta(delta) + self.__dict__['time'] = time.mktime(d.timetuple()) + +def node_end_record(node): + act_all = database.dbLoad("act_all") + if node not in act_all: + del act_all + return False + + if len(act_all[node]) == 0: + del act_all + return False + + a = Action(node, act_all[node][0]) + a.delField('rt') + a.delField('found_rt_ticket') + a.delField('second-mail-at-oneweek') + a.delField('second-mail-at-twoweeks') + a.delField('first-found') + rec = a.get() + rec['action'] = ["close_rt"] + rec['category'] = "UNKNOWN" + rec['stage'] = "monitor-end-record" + rec['time'] = time.time() - 7*60*60*24 + act_all[node].insert(0,rec) + database.dbDump("act_all", act_all) + del act_all + return True + if __name__ == "__main__": #r = RT() - #r.email("test", "body of test message", ['soltesz@cs.princeton.edu']) + #r.email("test", "body of test message", ['database@cs.princeton.edu']) #from emailTxt import mailtxt print "loaded" - #soltesz.dbDump("persistmessages", {}); + #database.dbDump("persistmessages", {}); #args = {'url_list': 'http://www.planet-lab.org/bootcds/planet1.usb\n','hostname': 'planet1','hostname_list': ' blahblah - days down\n'} #m = PersistMessage("blue", "test 1", mailtxt.newdown_one[1] % args, True) #m.send(['soltesz@cs.utk.edu'])