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 setPenaltyPause(self):
66 self.db.penalty_pause = True
67 self.db.penalty_pause_time = datetime.now()
68 act = ActionRecord(loginbase=self.db.loginbase,
70 action_type='pause_penalty',)
72 def clearPenalty(self):
73 #act = ActionRecord(loginbase=self.db.loginbase, action='penalty', action_type='clear_penalty',)
74 self.db.penalty_level = 0
75 self.db.penalty_applied = False
76 self.clearPenaltyPause()
78 def clearPenaltyPause(self):
79 self.db.penalty_pause = False
80 self.db.penalty_pause_time = None
82 def getTicketStatus(self):
83 if self.db.message_id != 0:
84 rtstatus = mailer.getTicketStatus(self.db.message_id)
85 self.db.message_status = rtstatus['Status']
86 self.db.message_queue = rtstatus['Queue']
87 self.db.message_created = datetime.fromtimestamp(rtstatus['Created'])
88 #self.db.message_last_reply = datetime.fromtimestamp(rtstatus['Told'])
90 def setTicketStatus(self, status):
91 print 'SETTING status %s' % status
92 if self.db.message_id != 0:
93 rtstatus = mailer.setTicketStatus(self.db.message_id, status)
95 def getContacts(self):
97 if self.db.penalty_level >= 0:
98 contacts += plc.getTechEmails(self.db.loginbase)
100 if self.db.penalty_level >= 1:
101 contacts += plc.getPIEmails(self.db.loginbase)
103 if self.db.penalty_level >= 2:
104 contacts += plc.getSliceUserEmails(self.db.loginbase)
108 def sendMessage(self, type, **kwargs):
110 # NOTE: evidently changing an RT message's subject opens the ticket.
111 # the logic in this policy depends up a ticket only being 'open'
112 # if a user has replied to it.
113 # So, to preserve these semantics, we check the status before
114 # sending, then after sending, reset the status to the
116 # There is a very tiny race here, where a user sends a reply
117 # within the time it takes to check, send, and reset.
118 # This sucks. It's almost certainly fragile.
121 # TODO: catch any errors here, and add an ActionRecord that contains
124 args = {'loginbase' : self.db.loginbase,
125 'penalty_level' : -self.db.penalty_level,
126 'monitor_hostname' : config.MONITOR_HOSTNAME,
127 'support_email' : config.support_email,
128 'plc_name' : config.PLC_NAME,
129 'plc_hostname' : config.PLC_WWW_HOSTNAME}
133 if 'hostname' in args:
134 hostname = args['hostname']
136 if hasattr(mailtxt, type):
138 message = getattr(mailtxt, type)
143 if 'viart' in kwargs:
144 saveact = kwargs['viart']
145 viart = kwargs['viart']
147 if 'saveact' in kwargs:
148 saveact = kwargs['saveact']
150 if 'ccemail' in kwargs:
151 ccemail = kwargs['ccemail']
154 self.getTicketStatus() # get current message status
155 if self.db.message_status not in ['open', 'new']:
158 m = Message(message[0] % args, message[1] % args, viart, self.db.message_id)
161 contacts = [config.exception_email]
163 contacts = self.getContacts()
165 print "sending message: %s to site %s for host %s" % (type, self.db.loginbase, hostname)
167 ret = m.send(contacts)
169 self.db.message_id = ret
170 # reset to previous status, since a new subject 'opens' RT tickets.
171 self.setTicketStatus(self.db.message_status)
174 # NOTE: only make a record of it if it's in RT.
175 act = ActionRecord(loginbase=self.db.loginbase, hostname=hostname, action='notice',
176 action_type=type, message_id=self.db.message_id)
179 print "+-- WARNING! ------------------------------"
180 print "| No such message name in emailTxt.mailtxt: %s" % type
181 print "+------------------------------------------"
185 def closeTicket(self):
186 if self.db.message_id:
187 mailer.closeTicketViaRT(self.db.message_id, "Ticket Closed by Monitor")
188 act = ActionRecord(loginbase=self.db.loginbase, action='notice',
189 action_type='close_ticket', message_id=self.db.message_id)
190 self.db.message_id = 0
191 self.db.message_status = "new"
193 def runBootManager(self, hostname):
194 from monitor import bootman
195 print "attempting BM reboot of %s" % hostname
198 ret = bootman.restore(self, hostname)
201 err = traceback.format_exc()
204 # TODO: keep this record so that the policy.py can identify all
205 # bootmanager_* actions without explicitly listing every kind.
206 act = ActionRecord(loginbase=self.db.loginbase,
209 action_type='bootmanager_restore',
213 act = ActionRecord(loginbase=self.db.loginbase,
216 action_type='bootmanager_' + ret,
220 def attemptReboot(self, hostname):
221 print "attempting PCU reboot of %s" % hostname
224 ret = reboot.reboot_str(hostname)
226 err = traceback.format_exc()
229 if ret == 0 or ret == "0":
232 act = ActionRecord(loginbase=self.db.loginbase,
235 action_type='try_reboot',