2 from monitor.common import *
3 from monitor.model import *
4 from monitor.wrapper import plc
5 from monitor.wrapper import plccache
6 from monitor.wrapper.emailTxt import mailtxt
7 from monitor.database.info.model import *
8 # NOTE: must import this after monitor.database.info.model b/c that imports
9 # pcucontro.reboot and blocks this version, if it comes last.
10 from monitor import reboot
12 class SiteInterface(HistorySiteRecord):
14 def get_or_make(cls, if_new_set={}, **kwargs):
15 if 'hostname' in kwargs:
16 kwargs['loginbase'] = plccache.plcdb_hn2lb[kwargs['hostname']]
17 del kwargs['hostname']
18 res = HistorySiteRecord.findby_or_create(if_new_set, **kwargs)
19 return SiteInterface(res)
21 def __init__(self, sitehist):
24 def getRecentActions(self, **kwargs):
25 # TODO: make query only return records within a certin time range,
26 # i.e. greater than 0.5 days ago. or 5 days, etc.
28 #print "kwargs: ", kwargs
31 if 'loginbase' in kwargs:
32 recent_actions = ActionRecord.query.filter_by(loginbase=kwargs['loginbase']).order_by(ActionRecord.date_created.desc())
33 elif 'hostname' in kwargs:
34 recent_actions = ActionRecord.query.filter_by(hostname=kwargs['hostname']).order_by(ActionRecord.date_created.desc())
37 def increasePenalty(self):
38 #act = ActionRecord(loginbase=self.db.loginbase, action='penalty', action_type='increase_penalty',)
39 self.db.penalty_level += 1
40 # NOTE: this is to prevent overflow or index errors in applyPenalty.
41 # there's probably a better approach to this.
42 if self.db.penalty_level >= 2:
43 self.db.penalty_level = 2
44 self.db.penalty_applied = True
46 def applyPenalty(self):
48 penalty_map.append( { 'name': 'noop', 'enable' : lambda site: None,
49 'disable' : lambda site: None } )
50 penalty_map.append( { 'name': 'nocreate', 'enable' : lambda site: plc.removeSiteSliceCreation(site),
51 'disable' : lambda site: plc.enableSiteSliceCreation(site) } )
52 penalty_map.append( { 'name': 'suspendslices', 'enable' : lambda site: plc.suspendSiteSlices(site),
53 'disable' : lambda site: plc.enableSiteSlices(site) } )
55 for i in range(len(penalty_map)-1,self.db.penalty_level,-1):
56 print "\tdisabling %s on %s" % (penalty_map[i]['name'], self.db.loginbase)
57 penalty_map[i]['disable'](self.db.loginbase)
59 for i in range(0,self.db.penalty_level+1):
60 print "\tapplying %s on %s" % (penalty_map[i]['name'], self.db.loginbase)
61 penalty_map[i]['enable'](self.db.loginbase)
65 def pausePenalty(self):
66 act = ActionRecord(loginbase=self.db.loginbase,
68 action_type='pause_penalty',)
70 def clearPenalty(self):
71 #act = ActionRecord(loginbase=self.db.loginbase, action='penalty', action_type='clear_penalty',)
72 self.db.penalty_level = 0
73 self.db.penalty_applied = False
75 def getTicketStatus(self):
76 if self.db.message_id != 0:
77 rtstatus = mailer.getTicketStatus(self.db.message_id)
78 self.db.message_status = rtstatus['Status']
79 self.db.message_queue = rtstatus['Queue']
80 self.db.message_created = datetime.fromtimestamp(rtstatus['Created'])
82 def setTicketStatus(self, status):
83 print 'SETTING status %s' % status
84 if self.db.message_id != 0:
85 rtstatus = mailer.setTicketStatus(self.db.message_id, status)
87 def getContacts(self):
89 if self.db.penalty_level >= 0:
90 contacts += plc.getTechEmails(self.db.loginbase)
92 if self.db.penalty_level >= 1:
93 contacts += plc.getPIEmails(self.db.loginbase)
95 if self.db.penalty_level >= 2:
96 contacts += plc.getSliceUserEmails(self.db.loginbase)
100 def sendMessage(self, type, **kwargs):
102 # NOTE: evidently changing an RT message's subject opens the ticket.
103 # the logic in this policy depends up a ticket only being 'open'
104 # if a user has replied to it.
105 # So, to preserve these semantics, we check the status before
106 # sending, then after sending, reset the status to the
108 # There is a very tiny race here, where a user sends a reply
109 # within the time it takes to check, send, and reset.
110 # This sucks. It's almost certainly fragile.
113 # TODO: catch any errors here, and add an ActionRecord that contains
116 args = {'loginbase' : self.db.loginbase,
117 'penalty_level' : self.db.penalty_level,
118 'monitor_hostname' : config.MONITOR_HOSTNAME,
119 'support_email' : config.support_email,
120 'plc_name' : config.PLC_NAME,
121 'plc_hostname' : config.PLC_WWW_HOSTNAME}
125 if 'hostname' in args:
126 hostname = args['hostname']
128 if hasattr(mailtxt, type):
130 message = getattr(mailtxt, type)
135 if 'viart' in kwargs:
136 saveact = kwargs['viart']
137 viart = kwargs['viart']
139 if 'saveact' in kwargs:
140 saveact = kwargs['saveact']
142 if 'ccemail' in kwargs:
143 ccemail = kwargs['ccemail']
146 self.getTicketStatus() # get current message status
147 if self.db.message_status not in ['open', 'new']:
150 m = Message(message[0] % args, message[1] % args, viart, self.db.message_id)
153 contacts = [config.exception_email]
155 contacts = self.getContacts()
157 print "sending message: %s to site %s for host %s" % (type, self.db.loginbase, hostname)
159 ret = m.send(contacts)
161 self.db.message_id = ret
162 # reset to previous status, since a new subject 'opens' RT tickets.
163 self.setTicketStatus(self.db.message_status)
166 # NOTE: only make a record of it if it's in RT.
167 act = ActionRecord(loginbase=self.db.loginbase, hostname=hostname, action='notice',
168 action_type=type, message_id=self.db.message_id)
171 print "+-- WARNING! ------------------------------"
172 print "| No such message name in emailTxt.mailtxt: %s" % type
173 print "+------------------------------------------"
177 def closeTicket(self):
178 if self.db.message_id:
179 mailer.closeTicketViaRT(self.db.message_id, "Ticket Closed by Monitor")
180 act = ActionRecord(loginbase=self.db.loginbase, action='notice',
181 action_type='close_ticket', message_id=self.db.message_id)
182 self.db.message_id = 0
183 self.db.message_status = "new"
185 def runBootManager(self, hostname):
186 from monitor import bootman
187 print "attempting BM reboot of %s" % hostname
190 ret = bootman.restore(self, hostname)
193 err = traceback.format_exc()
196 # TODO: keep this record so that the policy.py can identify all
197 # bootmanager_* actions without explicitly listing every kind.
198 act = ActionRecord(loginbase=self.db.loginbase,
201 action_type='bootmanager_restore',
204 act = ActionRecord(loginbase=self.db.loginbase,
207 action_type='bootmanager_' + ret,
211 def attemptReboot(self, hostname):
212 print "attempting PCU reboot of %s" % hostname
215 ret = reboot.reboot_str(hostname)
217 err = traceback.format_exc()
220 if ret == 0 or ret == "0":
223 act = ActionRecord(loginbase=self.db.loginbase,
226 action_type='try_reboot',