#
# Faiyaz Ahmed <faiyaza@cs.princeton.edu>
#
-# $Id: policy.py,v 1.16 2007/08/08 13:30:42 soltesz Exp $
+# $Id: policy.py,v 1.17 2007/08/29 17:26:50 soltesz Exp $
#
# Policy Engine.
import reboot
import soltesz
import string
-from printbadbysite import cmpCategoryVal
+from www.printbadnodes import cmpCategoryVal
from config import config
print "policy"
config = config()
PLCEMAIL="support@planet-lab.org"
#Thresholds (DAYS)
+SPERMIN = 60
+SPERHOUR = 60*60
SPERDAY = 86400
PITHRESH = 7 * SPERDAY
SLICETHRESH = 7 * SPERDAY
self.mergedb[loginbase][nodename]['ticket_id'] = ""
self.mergedb[loginbase][nodename]['prev_category'] = None
else:
+ if len(self.act_all[nodename]) == 0:
+ print "len(act_all[%s]) == 0, skipping %s %s" % (nodename, loginbase, nodename)
+ continue
+
y = self.act_all[nodename][0]
# skip if end-stage
if 'stage' in y and "monitor-end-record" in y['stage']:
+ # 1) ok, b/c it's a new problem. set ticket_id to null
+ self.mergedb[loginbase][nodename] = {}
+ self.mergedb[loginbase][nodename].update(x)
+ self.mergedb[loginbase][nodename]['ticket_id'] = ""
+ self.mergedb[loginbase][nodename]['prev_category'] = None
continue
- # for legacy actions
- if 'bucket' in y and y['bucket'][0] == 'dbg':
- # Only bootcd debugs made it to the act_all db.
- y['prev_category'] = "OLDBOOTCD"
- elif 'bucket' in y and y['bucket'][0] == 'down':
- y['prev_category'] = "ERROR"
- elif 'bucket' not in y:
- # for all other actions, just carry over the
- # previous category
- y['prev_category'] = y['category']
- else:
- print "UNKNOWN state for record: %s" % y
- sys.exit(1)
+ ## for legacy actions
+ #if 'bucket' in y and y['bucket'][0] == 'dbg':
+ # # Only bootcd debugs made it to the act_all db.
+ # y['prev_category'] = "OLDBOOTCD"
+ #elif 'bucket' in y and y['bucket'][0] == 'down':
+ # y['prev_category'] = "ERROR"
+ #elif 'bucket' not in y:
+ # # for all other actions, just carry over the
+ # # previous category
+ # y['prev_category'] = y['category']
+ #else:
+ # print "UNKNOWN state for record: %s" % y
+ # sys.exit(1)
+
# determine through translation, if the buckets match
#if 'category' in y and x['category'] == y['category']:
# b_match = True
d_diag_nodes are diagnose_in entries.
"""
d_diag_site = {loginbase : { 'config' :
- {'squeeze': False,
+ {'squeeze': False,
'email': False
},
'nodes': {}
if diag_record != None:
d_diag_site[loginbase]['nodes'][nodename] = diag_record
+
+ # NOTE: improvement means, we need to act/squeeze and email.
+ #print "DIAG_RECORD", diag_record
+ if 'monitor-end-record' in diag_record['stage'] or \
+ 'nmreset' in diag_record['stage']:
+ # print "resetting loginbase!"
+ d_diag_site[loginbase]['config']['squeeze'] = True
+ d_diag_site[loginbase]['config']['email'] = True
+ #else:
+ # print "NO IMPROVEMENT!!!!"
else:
pass # there is nothing to do for this node.
elif "BOOT" in state:
# no action needed.
# TODO: remove penalties, if any are applied.
+ now = time.time()
+ last_contact = node_record['plcnode']['last_contact']
+ if last_contact == None:
+ time_diff = 0
+ else:
+ time_diff = now - last_contact;
+
if 'improvement' in node_record['stage']:
# then we need to pass this on to 'action'
diag_record = {}
(loginbase, nodename, diag_record['stage'],
state, category, diag_record['ticket_id'])
return diag_record
+ elif time_diff >= 6*SPERHOUR:
+ # heartbeat is older than 30 min.
+ # then reset NM.
+ #print "Possible NM problem!! %s - %s = %s" % (now, last_contact, time_diff)
+ diag_record = {}
+ diag_record.update(node_record)
+ diag_record['message'] = emailTxt.mailtxt.NMReset
+ diag_record['args'] = {'nodename': nodename}
+ diag_record['stage'] = "nmreset"
+ diag_record['info'] = (nodename,
+ node_record['prev_category'],
+ node_record['category'])
+ if diag_record['ticket_id'] == "":
+ diag_record['log'] = "NM : %20s : %-40s == %20s %20s %s %s" % \
+ (loginbase, nodename, diag_record['stage'],
+ state, category, diag_record['found_rt_ticket'])
+ else:
+ diag_record['log'] = "NM : %20s : %-40s == %20s" % \
+ (loginbase, nodename, diag_record['stage'])
+
+ return diag_record
else:
return None
else:
# TODO: generalize delays at PLC, and prevent enforcement when there
# have been no emails.
# NOTE: 7*SPERDAY exists to offset the 'bad week'
- delta = current_time - diag_record['time'] - 7*SPERDAY
+ #delta = current_time - diag_record['time'] - 7*SPERDAY
+ delta = current_time - diag_record['time']
message = diag_record['message']
act_record = {}
act_record.update(diag_record)
#### DIAGNOSE STAGES
- #print "%s has stage %s" % (nodename, diag_record['stage'])
if 'findbad' in diag_record['stage']:
# The node is bad, and there's no previous record of it.
- act_record['email'] = TECH # addative emails
+ act_record['email'] = TECH
act_record['action'] = ['noop']
act_record['message'] = message[0]
act_record['stage'] = 'stage_actinoneweek'
+ elif 'nmreset' in diag_record['stage']:
+ act_record['email'] = ADMIN
+ act_record['action'] = ['reset_nodemanager']
+ act_record['message'] = message[0]
+ act_record['stage'] = 'nmreset'
+ return None
+
elif 'improvement' in diag_record['stage']:
# - backoff previous squeeze actions (slice suspend, nocreate)
# TODO: add a backoff_squeeze section... Needs to runthrough
act_record['stage'] = 'stage_actintwoweeks'
act_record['message'] = message[1]
act_record['action'] = ['nocreate' ]
+ act_record['time'] = current_time # reset clock for waitforever
elif delta >= 3* SPERDAY and not 'second-mail-at-oneweek' in act_record:
act_record['email'] = TECH
act_record['message'] = message[0]
return None # don't send if there's no action
elif 'actintwoweeks' in diag_record['stage']:
- if delta >= 14 * SPERDAY:
+ if delta >= 7 * SPERDAY:
act_record['email'] = TECH | PI | USER
act_record['stage'] = 'stage_waitforever'
act_record['message'] = message[2]
act_record['action'] = ['suspendslices']
act_record['time'] = current_time # reset clock for waitforever
- elif delta >= 10* SPERDAY and not 'second-mail-at-twoweeks' in act_record:
+ elif delta >= 3* SPERDAY and not 'second-mail-at-twoweeks' in act_record:
act_record['email'] = TECH | PI
act_record['message'] = message[1]
act_record['action'] = ['sendmailagain-waitfortwoweeksaction' ]
# 1. stage is unknown, or
# 2. delta is not big enough to bump it to the next stage.
# TODO: figure out which. for now assume 2.
- print "UNKNOWN!!? %s" % nodename
+ print "UNKNOWN!?!? %s" % nodename
act_record['action'] = ['unknown']
act_record['message'] = message[0]
print "Exiting..."
plc.enableSliceCreation(args['hostname'])
return
+def reboot_node(args):
+ host = args['hostname']
+ return reboot.reboot_new(host, True, config.debug)
+
+def reset_nodemanager(args):
+ os.system("ssh root@%s /sbin/service nm restart" % nodename)
+ return
+
class Action(Thread):
def __init__(self, l_action):
self.l_action = l_action
self.actions['close_rt'] = lambda args: close_rt_backoff(args)
self.actions['rins'] = lambda args: plc.nodeBootState(args['hostname'], "rins")
self.actions['noop'] = lambda args: args
+ self.actions['reboot_node'] = lambda args: reboot_node(args)
+ self.actions['reset_nodemanager'] = lambda args: args # reset_nodemanager(args)
+
self.actions['ticket_waitforever'] = lambda args: args
self.actions['waitforever'] = lambda args: args
self.actions['unknown'] = lambda args: args
mailer.email(subj, body, contacts)
ticket_id = args['ticket_id']
else:
- #if 'ticket_id' in args and 'ticket_id' != "":
- # # Reformat Subject to include Ticket_ID for RT
- # subj = "Re: [PL #%s] %s" % (args['ticket_id'], subject)
- # # RT remembers old contacts, so only add new users
- # mailer.email(subj, body, ['monitor@planet-lab.org'] + contacts)
- # ticket_id = args['ticket_id']
- #else:
- # ticket_id = mailer.emailViaRT(subject, body, contacts)
ticket_id = mailer.emailViaRT(subject, body, contacts, args['ticket_id'])
except Exception, err:
print "exception on message:"
for act_record in act_recordlist:
email_args['hostname_list'] += act_record['msg_format']
email_args['hostname'] = act_record['nodename']
+ if 'plcnode' in act_record and \
+ 'pcu_ids' in act_record['plcnode'] and \
+ len(act_record['plcnode']['pcu_ids']) > 0:
+ print "setting 'pcu_id' for email_args %s"%email_args['hostname']
+ email_args['pcu_id'] = act_record['plcnode']['pcu_ids'][0]
+ else:
+ email_args['pcu_id'] = "-1"
+
if 'ticket_id' in act_record:
email_args['ticket_id'] = act_record['ticket_id']
diag_record = site_record['nodes'][nodename]
act_record = self.__actOnNode(diag_record)
#print "nodename: %s %s" % (nodename, act_record)
- act_recordlist += [act_record]
+ if act_record is not None:
+ act_recordlist += [act_record]
unique_issues = self.get_unique_issues(act_recordlist)
print "\tworking on issue: %s" % issue
issue_record_list = unique_issues[issue]
email_args = self.get_email_args(issue_record_list)
+
+ # for each record.
+ for act_record in issue_record_list:
+ # if there's a pcu record and email config is set
+ if 'email_pcu' in act_record:
+ if act_record['email_pcu'] and \
+ site_record['config']['email']:
+
+ ticket_id = self.__emailSite(loginbase,
+ act_record['email'],
+ emailTxt.mailtxt.pcudown[0],
+ email_args)
+ email_args['ticket_id'] = ticket_id
+
act_record = issue_record_list[0]
# send message before squeezing
for act_record in issue_record_list:
nodename = act_record['nodename']
# update node record with RT ticket_id
- self.act_all[nodename][0]['ticket_id'] = "%s" % ticket_id
+ if nodename in self.act_all:
+ self.act_all[nodename][0]['ticket_id'] = "%s" % ticket_id
if config.mail: i_nodes_emailed += 1
print "\t\tconfig.squeeze: %s and %s" % (config.squeeze,
site_record['config']['squeeze'])
if config.squeeze and site_record['config']['squeeze']:
for act_key in act_record['action']:
- #act_key = act_record['action']
self.actions[act_key](email_args)
- i_nodes_actedon += 1
+ i_nodes_actedon += 1
if config.policysavedb:
print "Saving Databases... act_all, diagnose_out"
#print "sleeping for 1 sec"
#time.sleep(1)
- print "Hit enter to continue..."
+ #print "Hit enter to continue..."
sys.stdout.flush()
line = sys.stdin.readline()
act_record['nodename'] = nodename
act_record['msg_format'] = self._format_diaginfo(diag_record)
+
+ if "DOWN" in act_record['log']:
+ print "%s" % act_record['log'],
+ print "%15s" % (['reboot_node'],)
+ ret = reboot_node(act_record['nodename'])
+ if ret:
+ # Reboot Succeeded
+ act_record2 = {}
+ act_record2.update(act_record)
+ act_record2['action'] = ['reboot_node']
+ act_record2['reboot_node_failed'] = False
+ act_record2['email_pcu'] = False
+
+ if nodename not in self.act_all:
+ self.act_all[nodename] = []
+ self.act_all[nodename].insert(0,act_record2)
+
+ # return None to avoid further action
+ return None
+ else:
+ # set email_pcu to also send pcu notice for this record.
+ act_record['reboot_node_failed'] = True
+ act_record['email_pcu'] = True
+
print "%s" % act_record['log'],
print "%15s" % act_record['action']
- if act_record['stage'] is not 'monitor-end-record':
+ if act_record['stage'] is not 'monitor-end-record' and \
+ act_record['stage'] is not 'nmreset':
if nodename not in self.act_all:
self.act_all[nodename] = []