updates to improve generalization and auto-installation.
[monitor.git] / unified_model.py
1 #!/usr/bin/python
2
3 from monitor import database
4
5 import plc
6 import mailer
7 import time
8
9 from model import *
10 from const import *
11 import util.file
12 import config
13
14 def gethostlist(hostlist_file):
15         return util.file.getListFromFile(hostlist_file)
16
17 def array_to_priority_map(array):
18         """ Create a mapping where each entry of array is given a priority equal
19         to its position in the array.  This is useful for subsequent use in the
20         cmpMap() function."""
21         map = {}
22         count = 0
23         for i in array:
24                 map[i] = count
25                 count += 1
26         return map
27
28 def cmpValMap(v1, v2, map):
29         if v1 in map and v2 in map and map[v1] < map[v2]:
30                 return 1
31         elif v1 in map and v2 in map and map[v1] > map[v2]:
32                 return -1
33         elif v1 in map and v2 in map:
34                 return 0
35         else:
36                 raise Exception("No index %s or %s in map" % (v1, v2))
37
38 def cmpCategoryVal(v1, v2):
39         # Terrible hack to manage migration to no more 'ALPHA' states.
40         if v1 == 'ALPHA': v1 = "PROD"
41         if v2 == 'ALPHA': v2 = "PROD"
42         #map = array_to_priority_map([ None, 'PROD', 'ALPHA', 'OLDBOOTCD', 'UNKNOWN', 'FORCED', 'ERROR', ])
43         map = array_to_priority_map([ None, 'ALPHA', 'PROD', 'OLDBOOTCD', 'UNKNOWN', 'FORCED', 'ERROR', ])
44         return cmpValMap(v1,v2,map)
45
46
47 class PCU:
48         def __init__(self, hostname):
49                 self.hostname = hostname
50
51         def reboot(self):
52                 return True
53         def available(self):
54                 return True
55         def previous_attempt(self):
56                 return True
57         def setValidMapping(self):
58                 pass
59
60 class Penalty:
61         def __init__(self, key, valuepattern, action):
62                 pass
63
64 class PenaltyMap:
65         def __init__(self):
66                 pass
67
68         # connect one penalty to another, in a FSM diagram.  After one
69         #       condition/penalty is applied, move to the next phase.
70
71
72 #fb = database.dbLoad("findbad")
73
74 class RT(object):
75         def __init__(self, ticket_id = None):
76                 self.ticket_id = ticket_id
77                 if self.ticket_id:
78                         print "getting ticket status",
79                         self.status = mailer.getTicketStatus(self.ticket_id)
80                         print self.status
81
82         def setTicketStatus(self, status):
83                 mailer.setTicketStatus(self.ticket_id, status)
84                 self.status = mailer.getTicketStatus(self.ticket_id)
85                 return True
86         
87         def getTicketStatus(self):
88                 if not self.status:
89                         self.status = mailer.getTicketStatus(self.ticket_id)
90                 return self.status
91
92         def closeTicket(self):
93                 mailer.closeTicketViaRT(self.ticket_id, "Ticket CLOSED automatically by SiteAssist.") 
94
95         def email(self, subject, body, to):
96                 self.ticket_id = mailer.emailViaRT(subject, body, to, self.ticket_id)
97                 return self.ticket_id
98
99 class Message(object):
100         def __init__(self, subject, message, via_rt=True, ticket_id=None, **kwargs):
101                 self.via_rt = via_rt
102                 self.subject = subject
103                 self.message = message
104                 self.rt = RT(ticket_id)
105
106         def send(self, to):
107                 if self.via_rt:
108                         return self.rt.email(self.subject, self.message, to)
109                 else:
110                         return mailer.email(self.subject, self.message, to)
111
112 class Recent(object):
113         def __init__(self, withintime):
114                 self.withintime = withintime
115
116                 try:
117                         self.time = self.__getattribute__('time')
118                 except:
119                         self.time = time.time()- 7*24*60*60
120
121                 #self.time = time.time()
122                 #self.action_taken = False
123
124         def isRecent(self):
125                 if self.time + self.withintime < time.time():
126                         self.action_taken = False
127
128                 if self.time + self.withintime > time.time() and self.action_taken:
129                         return True
130                 else:
131                         return False
132
133         def unsetRecent(self):
134                 self.action_taken = False
135                 self.time = time.time()
136                 return True
137
138         def setRecent(self):
139                 self.action_taken = True
140                 self.time = time.time()
141                 return True
142                 
143 class PersistFlags(Recent):
144         def __new__(typ, id, *args, **kwargs):
145                 if 'db' in kwargs:
146                         db = kwargs['db']
147                         del kwargs['db']
148                 else:
149                         db = "persistflags"
150
151                 try:
152                         pm = database.dbLoad(db)
153                 except:
154                         database.dbDump(db, {})
155                         pm = database.dbLoad(db)
156                 #print pm
157                 if id in pm:
158                         obj = pm[id]
159                 else:
160                         obj = super(PersistFlags, typ).__new__(typ, *args, **kwargs)
161                         for key in kwargs.keys():
162                                 obj.__setattr__(key, kwargs[key])
163                         obj.time = time.time()
164                         obj.action_taken = False
165
166                 obj.db = db
167                 return obj
168
169         def __init__(self, id, withintime, **kwargs):
170                 self.id = id
171                 Recent.__init__(self, withintime)
172
173         def save(self):
174                 pm = database.dbLoad(self.db)
175                 pm[self.id] = self
176                 database.dbDump(self.db, pm)
177
178         def resetFlag(self, name):
179                 self.__setattr__(name, False)
180
181         def setFlag(self, name):
182                 self.__setattr__(name, True)
183                 
184         def getFlag(self, name):
185                 try:
186                         return self.__getattribute__(name)
187                 except:
188                         self.__setattr__(name, False)
189                         return False
190
191         def resetRecentFlag(self, name):
192                 self.resetFlag(name)
193                 self.unsetRecent()
194
195         def setRecentFlag(self, name):
196                 self.setFlag(name)
197                 self.setRecent()
198
199         def getRecentFlag(self, name):
200                 # if recent and flag set -> true
201                 # else false
202                 try:
203                         return self.isRecent() & self.__getattribute__(name)
204                 except:
205                         self.__setattr__(name, False)
206                         return False
207
208         def checkattr(self, name):
209                 try:
210                         x = self.__getattribute__(name)
211                         return True
212                 except:
213                         return False
214                 
215
216 class PersistMessage(Message):
217         def __new__(typ, id, subject, message, via_rt, **kwargs):
218                 if 'db' in kwargs:
219                         db = kwargs['db']
220                 else:
221                         db = "persistmessages"
222
223                 try:
224                         pm = database.dbLoad(db)
225                 except:
226                         database.dbDump(db, {})
227                         pm = database.dbLoad(db)
228
229                 #print pm
230                 if id in pm:
231                         #print "Using existing object"
232                         obj = pm[id]
233                 else:
234                         #print "creating new object"
235                         obj = super(PersistMessage, typ).__new__(typ, [id, subject, message, via_rt], **kwargs)
236                         obj.id = id
237                         obj.actiontracker = Recent(1*60*60*24)
238                         obj.ticket_id = None
239
240                 if 'ticket_id' in kwargs and kwargs['ticket_id'] is not None:
241                         obj.ticket_id = kwargs['ticket_id']
242
243                 obj.db = db
244                 return obj
245
246         def __init__(self, id, subject, message, via_rt=True, **kwargs):
247                 print "initializing object: %s" % self.ticket_id
248                 self.id = id
249                 Message.__init__(self, subject, message, via_rt, self.ticket_id)
250
251         def reset(self):
252                 self.actiontracker.unsetRecent()
253
254         def save(self):
255                 pm = database.dbLoad(self.db)
256                 pm[self.id] = self
257                 database.dbDump(self.db, pm)
258
259         def send(self, to):
260                 if not self.actiontracker.isRecent():
261                         self.ticket_id = Message.send(self, to)
262                         self.actiontracker.setRecent()
263                         self.save()
264                 else:
265                         # NOTE: only send a new message every week, regardless.
266                         # NOTE: can cause thank-you messages to be lost, for instance when node comes back online within window.
267                         print "Not sending to host b/c not within window of %s days" % (self.actiontracker.withintime // (60*60*24))
268
269 class MonitorMessage(object):
270         def __new__(typ, id, *args, **kwargs):
271                 if 'db' in kwargs:
272                         db = kwargs['db']
273                 else:
274                         db = "monitormessages"
275
276                 try:
277                         if 'reset' in kwargs and kwargs['reset'] == True:
278                                 database.dbDump(db, {})
279                         pm = database.dbLoad(db)
280                 except:
281                         database.dbDump(db, {})
282                         pm = database.dbLoad(db)
283
284                 #print pm
285                 if id in pm:
286                         print "Using existing object"
287                         obj = pm[id]
288                 else:
289                         print "creating new object"
290                         obj = super(object, typ).__new__(typ, id, *args, **kwargs)
291                         obj.id = id
292                         obj.sp = PersistSitePenalty(id, 0)
293
294                 obj.db = db
295                 return obj
296
297         def __init__(self, id, message):
298                 pass
299                 
300
301 class SitePenalty(object):
302         penalty_map = [] 
303         penalty_map.append( { 'name': 'noop',                   'enable'   : lambda host: None,
304                                                                                                         'disable'  : lambda host: None } )
305         penalty_map.append( { 'name': 'nocreate',               'enable'   : lambda host: plc.removeSliceCreation(host),
306                                                                                                         'disable'  : lambda host: plc.enableSliceCreation(host) } )
307         penalty_map.append( { 'name': 'suspendslices',  'enable'   : lambda host: plc.suspendSlices(host),
308                                                                                                         'disable'  : lambda host: plc.enableSlices(host) } )
309
310         #def __init__(self, index=0, **kwargs):
311         #       self.index = index
312
313         def get_penalties(self):
314                 # TODO: get penalties actually applied to a node from PLC DB.
315                 return [ n['name'] for n in SitePenalty.penalty_map ] 
316
317         def increase(self):
318                 self.index = self.index + 1
319                 if self.index > len(SitePenalty.penalty_map)-1: self.index = len(SitePenalty.penalty_map)-1
320                 return True
321
322         def decrease(self):
323                 self.index = self.index - 1
324                 if self.index < 0: self.index = 0
325                 return True
326
327         def apply(self, host):
328
329                 for i in range(len(SitePenalty.penalty_map)-1,self.index,-1):
330                         print "\tdisabling %s on %s" % (SitePenalty.penalty_map[i]['name'], host)
331                         SitePenalty.penalty_map[i]['disable'](host)
332
333                 for i in range(0,self.index+1):
334                         print "\tapplying %s on %s" % (SitePenalty.penalty_map[i]['name'], host)
335                         SitePenalty.penalty_map[i]['enable'](host)
336
337                 return
338
339
340
341 class PersistSitePenalty(SitePenalty):
342         def __new__(typ, id, index, **kwargs):
343                 if 'db' in kwargs:
344                         db = kwargs['db']
345                 else:
346                         db = "persistpenalties"
347
348                 try:
349                         if 'reset' in kwargs and kwargs['reset'] == True:
350                                 database.dbDump(db, {})
351                         pm = database.dbLoad(db)
352                 except:
353                         database.dbDump(db, {})
354                         pm = database.dbLoad(db)
355
356                 #print pm
357                 if id in pm:
358                         print "Using existing object"
359                         obj = pm[id]
360                 else:
361                         print "creating new object"
362                         obj = super(PersistSitePenalty, typ).__new__(typ, [index], **kwargs)
363                         obj.id = id
364                         obj.index = index
365
366                 obj.db = db
367                 return obj
368
369         def __init__(self, id, index, **kwargs):
370                 self.id = id
371
372         def save(self):
373                 pm = database.dbLoad(self.db)
374                 pm[self.id] = self
375                 database.dbDump(self.db, pm)
376
377
378 class Target:
379         """
380                 Each host has a target set of attributes.  Some may be set manually,
381                 or others are set globally for the preferred target.
382
383                 For instance:
384                         All nodes in the Alpha or Beta group would have constraints like:
385                                 [ { 'state' : 'BOOT', 'kernel' : '2.6.22' } ]
386         """
387         def __init__(self, constraints):
388                 self.constraints = constraints
389
390         def verify(self, data):
391                 """
392                         self.constraints is a list of key, value pairs.
393                         # [ {... : ...}==AND , ... , ... , ] == OR
394                 """
395                 con_or_true = False
396                 for con in self.constraints:
397                         #print "con: %s" % con
398                         con_and_true = True
399                         for key in con.keys():
400                                 #print "looking at key: %s" % key
401                                 if key in data: 
402                                         #print "%s %s" % (con[key], data[key])
403                                         con_and_true = con_and_true & (con[key] in data[key])
404                                 elif key not in data:
405                                         print "missing key %s" % key
406                                         con_and_true = False
407
408                         con_or_true = con_or_true | con_and_true
409
410                 return con_or_true
411
412 class Record(object):
413
414         def __init__(self, hostname, data):
415                 self.hostname = hostname
416                 self.data = data
417                 self.plcdb_hn2lb = database.dbLoad("plcdb_hn2lb")
418                 self.loginbase = self.plcdb_hn2lb[self.hostname]
419                 return
420
421
422         def stageIswaitforever(self):
423                 if 'waitforever' in self.data['stage']:
424                         return True
425                 else:
426                         return False
427
428         def severity(self):
429                 category = self.data['category']
430                 prev_category = self.data['prev_category']
431                 #print "SEVERITY: ", category, prev_category
432                 val = cmpCategoryVal(category, prev_category)
433                 return val 
434
435         def improved(self):
436                 return self.severity() > 0
437         
438         def end_record(self):
439                 return node_end_record(self.hostname)
440
441         def reset_stage(self):
442                 self.data['stage'] = 'findbad'
443                 return True
444         
445         def getCategory(self):
446                 return self.data['category'].lower()
447
448         def getState(self):
449                 return self.data['state'].lower()
450
451         def getDaysDown(cls, diag_record):
452                 daysdown = -1
453                 if diag_record['comonstats']['uptime'] != "null" and diag_record['comonstats']['uptime'] != "-1":
454                         daysdown = - int(float(diag_record['comonstats']['uptime'])) // (60*60*24)
455                 #elif diag_record['comonstats']['sshstatus'] != "null":
456                 #       daysdown = int(diag_record['comonstats']['sshstatus']) // (60*60*24)
457                 #elif diag_record['comonstats']['lastcotop'] != "null":
458                 #       daysdown = int(diag_record['comonstats']['lastcotop']) // (60*60*24)
459                 else:
460                         now = time.time()
461                         last_contact = diag_record['plcnode']['last_contact']
462                         if last_contact == None:
463                                 # the node has never been up, so give it a break
464                                 daysdown = -1
465                         else:
466                                 diff = now - last_contact
467                                 daysdown = diff // (60*60*24)
468                 return daysdown
469         getDaysDown = classmethod(getDaysDown)
470
471         def getStrDaysDown(cls, diag_record):
472                 daysdown = "unknown"
473                 last_contact = diag_record['plcnode']['last_contact']
474                 date_created = diag_record['plcnode']['date_created']
475
476                 if      diag_record['comonstats']['uptime'] != "null" and \
477                         diag_record['comonstats']['uptime'] != "-1":
478                         daysdown = int(float(diag_record['comonstats']['uptime'])) // (60*60*24)
479                         daysdown = "%d days up" % daysdown
480
481                 elif last_contact is None:
482                         if date_created is not None:
483                                 now = time.time()
484                                 diff = now - date_created
485                                 daysdown = diff // (60*60*24)
486                                 daysdown = "Never contacted PLC, created %s days ago" % daysdown
487                         else:
488                                 daysdown = "Never contacted PLC"
489                 else:
490                         now = time.time()
491                         diff = now - last_contact
492                         daysdown = diff // (60*60*24)
493                         daysdown = "%s days down" % daysdown
494                 return daysdown
495         getStrDaysDown = classmethod(getStrDaysDown)
496
497         #def getStrDaysDown(cls, diag_record):
498         #       daysdown = cls.getDaysDown(diag_record)
499         #       if daysdown > 0:
500         #               return "%d days down"%daysdown
501         #       elif daysdown == -1:
502         #               return "Never online"
503         #       else:
504         #               return "%d days up"% -daysdown
505         #getStrDaysDown = classmethod(getStrDaysDown)
506
507         def takeAction(self, index=0):
508                 pp = PersistSitePenalty(self.hostname, 0, db='persistpenalty_hostnames')
509                 if 'improvement' in self.data['stage'] or self.improved() or \
510                         'monitor-end-record' in self.data['stage']:
511                         print "takeAction: decreasing penalty for %s"%self.hostname
512                         pp.decrease()
513                         pp.decrease()
514                 else:
515                         print "takeAction: increasing penalty for %s"%self.hostname
516                         pp.increase()
517                 pp.index = index
518                 pp.apply(self.hostname)
519                 pp.save()
520
521         def _format_diaginfo(self):
522                 info = self.data['info']
523                 print "FORMAT : STAGE: ", self.data['stage']
524                 if self.data['stage'] == 'monitor-end-record':
525                         if info[2] == "ALPHA": info = (info[0], info[1], "PROD")
526                         hlist = "    %s went from '%s' to '%s'\n" % (info[0], info[1], info[2]) 
527                 else:
528                         hlist = "    %s %s - %s\n" % (info[0], info[2], info[1]) #(node,ver,daysdn)
529                 return hlist
530         def saveAction(self):
531                 if 'save-act-all' in self.data and self.data['save-act-all'] == True:
532                         return True
533                 else:
534                         return False
535
536         def getMessage(self, ticket_id=None):
537                 self.data['args']['hostname'] = self.hostname
538                 self.data['args']['loginbase'] = self.loginbase
539                 self.data['args']['hostname_list'] = self._format_diaginfo()
540                 #print self.data['message']
541                 if self.data['message']:
542                         message = PersistMessage(self.hostname, 
543                                                                  self.data['message'][0] % self.data['args'],
544                                                                  self.data['message'][1] % self.data['args'],
545                                                                  True, db='monitor_persistmessages',
546                                                                  ticket_id=ticket_id)
547                         if self.data['stage'] == "improvement":
548                                 message.reset()
549                         return message
550                 else:
551                         return None
552         
553         def getContacts(self):
554                 roles = self.data['email']
555
556                 if not config.mail and not config.debug and config.bcc:
557                         roles = ADMIN
558                 if config.mail and config.debug:
559                         roles = ADMIN
560
561                 # build targets
562                 contacts = []
563                 if ADMIN & roles:
564                         contacts += [config.email]
565                 if TECH & roles:
566                         #contacts += [TECHEMAIL % self.loginbase]
567                         contacts += plc.getTechEmails(loginbase)
568                 if PI & roles:
569                         #contacts += [PIEMAIL % self.loginbase]
570                         contacts += plc.getSliceUserEmails(loginbase)
571                 if USER & roles:
572                         contacts += plc.getSliceUserEmails(loginbase)
573                         slices = plc.slices(self.loginbase)
574                         if len(slices) >= 1:
575                                 #for slice in slices:
576                                 #       contacts += [SLICEMAIL % slice]
577                                 print "SLIC: %20s : %d slices" % (self.loginbase, len(slices))
578                         else:
579                                 print "SLIC: %20s : 0 slices" % self.loginbase
580
581                 return contacts
582
583
584 class NodeRecord:
585         def __init__(self, hostname, target):
586                 self.hostname = hostname
587                 self.ticket = None
588                 self.target = target
589                 #if hostname in fb['nodes']:
590                 #       self.data = fb['nodes'][hostname]['values']
591                 #else:
592                 #       raise Exception("Hostname not in scan database")
593
594         def stageIswaitforever(self):
595                 if 'waitforever' in self.data['stage']:
596                         return True
597                 else:
598                         return False
599
600         def severity(self):
601                 category = self.data['category']
602                 prev_category = self.data['prev_category']
603                 print "IMPROVED: ", category, prev_category
604                 val = cmpCategoryVal(category, prev_category)
605                 return val 
606
607         def improved(self):
608                 return self.severity() > 0
609         
610         def end_record(self):
611                 return node_end_record(self.hostname)
612
613         def reset_stage(self):
614                 self.data['stage'] = 'findbad'
615                 return True
616
617         def open_tickets(self):
618                 if self.ticket and self.ticket.status['status'] == 'open':
619                         return 1
620                 return 0
621         def setIntrospect(self):
622                 pass
623
624         def email_notice(self):
625                 message = self._get_message_for_condition()
626                 message.send(self._get_contacts_for_condition())
627                 return True
628         def close_ticket(self):
629                 if self.ticket:
630                         self.ticket.closeTicket()
631
632         def exempt_from_penalties(self):
633                 bl = database.dbLoad("l_blacklist")
634                 return self.hostname in bl
635
636         def penalties(self):
637                 return []
638         def escellate_penalty(self):
639                 return True
640         def reduce_penalty(self):
641                 return True
642
643
644         def atTarget(self):
645                 return self.target.verify(self.data)
646
647         def _get_condition(self):
648                 return self.data['category'].lower()
649
650         def _get_stage(self):
651                 "improvement"
652                 "firstnotice_noop"
653                 "secondnotice_noslicecreation"
654                 "thirdnotice_disableslices"
655
656                 delta = current_time - self.data['time']
657
658         def _get_message_for_condition(self):
659                 pass
660         def _get_contacts_for_condition(self):
661                 pass
662
663 class Action(MonRecord):
664         def __init__(self, host, data):
665                 self.host = host
666                 MonRecord.__init__(self, data)
667                 return
668
669         def deltaDays(self, delta):
670                 t = datetime.fromtimestamp(self.__dict__['time'])
671                 d = t + timedelta(delta)
672                 self.__dict__['time'] = time.mktime(d.timetuple())
673                 
674 def node_end_record(node):
675         act_all = database.dbLoad("act_all")
676         if node not in act_all:
677                 del act_all
678                 return False
679
680         if len(act_all[node]) == 0:
681                 del act_all
682                 return False
683
684         pm = database.dbLoad("monitor_persistmessages")
685         if node not in pm:
686                 del pm
687                 return False
688         else:
689                 print "deleting node record"
690                 del pm[node]
691                 database.dbDump("monitor_persistmessages", pm)
692
693         a = Action(node, act_all[node][0])
694         a.delField('rt')
695         a.delField('found_rt_ticket')
696         a.delField('second-mail-at-oneweek')
697         a.delField('second-mail-at-twoweeks')
698         a.delField('first-found')
699         rec = a.get()
700         rec['action'] = ["close_rt"]
701         rec['category'] = "ALPHA"       # assume that it's up...
702         rec['stage'] = "monitor-end-record"
703         rec['ticket_id'] = None
704         rec['time'] = time.time() - 7*60*60*24
705         act_all[node].insert(0,rec)
706         database.dbDump("act_all", act_all)
707         del act_all
708         return True
709
710 if __name__ == "__main__":
711         #r = RT()
712         #r.email("test", "body of test message", ['database@cs.princeton.edu'])
713         #from emailTxt import mailtxt
714         print "loaded"
715         #database.dbDump("persistmessages", {});
716         #args = {'url_list': 'http://www.planet-lab.org/bootcds/planet1.usb\n','hostname': 'planet1','hostname_list': ' blahblah -  days down\n'}
717         #m = PersistMessage("blue", "test 1", mailtxt.newdown_one[1] % args, True)
718         #m.send(['soltesz@cs.utk.edu'])
719         #m = PersistMessage("blue", "test 1 - part 2", mailtxt.newalphacd_one[1] % args, True)
720         # TRICK timer to thinking some time has passed.
721         #m.actiontracker.time = time.time() - 6*60*60*24
722         #m.send(['soltesz@cs.utk.edu'])