2 from monitor import reboot
3 from monitor.common import *
4 from monitor.model import *
5 from monitor.wrapper import plc
6 from monitor.wrapper import plccache
7 from monitor.wrapper.emailTxt import mailtxt
8 from monitor.database.info.model import *
10 class SiteInterface(HistorySiteRecord):
12 def get_or_make(cls, if_new_set={}, **kwargs):
13 if 'hostname' in kwargs:
14 kwargs['loginbase'] = plccache.plcdb_hn2lb[kwargs['hostname']]
15 del kwargs['hostname']
16 res = HistorySiteRecord.findby_or_create(if_new_set, **kwargs)
17 return SiteInterface(res)
19 def __init__(self, sitehist):
22 def getRecentActions(self, **kwargs):
23 # TODO: make query only return records within a certin time range,
24 # i.e. greater than 0.5 days ago. or 5 days, etc.
26 #print "kwargs: ", kwargs
29 if 'loginbase' in kwargs:
30 recent_actions = ActionRecord.query.filter_by(loginbase=kwargs['loginbase']).order_by(ActionRecord.date_created.desc())
31 elif 'hostname' in kwargs:
32 recent_actions = ActionRecord.query.filter_by(hostname=kwargs['hostname']).order_by(ActionRecord.date_created.desc())
35 def increasePenalty(self):
36 #act = ActionRecord(loginbase=self.db.loginbase, action='penalty', action_type='increase_penalty',)
37 self.db.penalty_level += 1
38 # NOTE: this is to prevent overflow or index errors in applyPenalty.
39 # there's probably a better approach to this.
40 if self.db.penalty_level >= 2:
41 self.db.penalty_level = 2
42 self.db.penalty_applied = True
44 def applyPenalty(self):
46 penalty_map.append( { 'name': 'noop', 'enable' : lambda site: None,
47 'disable' : lambda site: None } )
48 penalty_map.append( { 'name': 'nocreate', 'enable' : lambda site: plc.removeSiteSliceCreation(site),
49 'disable' : lambda site: plc.enableSiteSliceCreation(site) } )
50 penalty_map.append( { 'name': 'suspendslices', 'enable' : lambda site: plc.suspendSiteSlices(site),
51 'disable' : lambda site: plc.enableSiteSlices(site) } )
53 for i in range(len(penalty_map)-1,self.db.penalty_level,-1):
54 print "\tdisabling %s on %s" % (penalty_map[i]['name'], self.db.loginbase)
55 penalty_map[i]['disable'](self.db.loginbase)
57 for i in range(0,self.db.penalty_level+1):
58 print "\tapplying %s on %s" % (penalty_map[i]['name'], self.db.loginbase)
59 penalty_map[i]['enable'](self.db.loginbase)
63 def pausePenalty(self):
64 act = ActionRecord(loginbase=self.db.loginbase,
66 action_type='pause_penalty',)
68 def clearPenalty(self):
69 #act = ActionRecord(loginbase=self.db.loginbase, action='penalty', action_type='clear_penalty',)
70 self.db.penalty_level = 0
71 self.db.penalty_applied = False
73 def getTicketStatus(self):
74 if self.db.message_id != 0:
75 rtstatus = mailer.getTicketStatus(self.db.message_id)
76 self.db.message_status = rtstatus['Status']
77 self.db.message_queue = rtstatus['Queue']
78 self.db.message_created = datetime.fromtimestamp(rtstatus['Created'])
80 def setTicketStatus(self, status):
81 print 'SETTING status %s' % status
82 if self.db.message_id != 0:
83 rtstatus = mailer.setTicketStatus(self.db.message_id, status)
85 def getContacts(self):
87 if self.db.penalty_level >= 0:
88 contacts += plc.getTechEmails(self.db.loginbase)
90 if self.db.penalty_level >= 1:
91 contacts += plc.getPIEmails(self.db.loginbase)
93 if self.db.penalty_level >= 2:
94 contacts += plc.getSliceUserEmails(self.db.loginbase)
98 def sendMessage(self, type, **kwargs):
100 # NOTE: evidently changing an RT message's subject opens the ticket.
101 # the logic in this policy depends up a ticket only being 'open'
102 # if a user has replied to it.
103 # So, to preserve these semantics, we check the status before
104 # sending, then after sending, reset the status to the
106 # There is a very tiny race here, where a user sends a reply
107 # within the time it takes to check, send, and reset.
108 # This sucks. It's almost certainly fragile.
111 # TODO: catch any errors here, and add an ActionRecord that contains
114 args = {'loginbase' : self.db.loginbase,
115 'penalty_level' : self.db.penalty_level,
116 'monitor_hostname' : config.MONITOR_HOSTNAME,
117 'support_email' : config.support_email,
118 'plc_name' : config.PLC_NAME,
119 'plc_hostname' : config.PLC_WWW_HOSTNAME}
123 if 'hostname' in args:
124 hostname = args['hostname']
126 if hasattr(mailtxt, type):
128 message = getattr(mailtxt, type)
133 if 'viart' in kwargs:
134 saveact = kwargs['viart']
135 viart = kwargs['viart']
137 if 'saveact' in kwargs:
138 saveact = kwargs['saveact']
140 if 'ccemail' in kwargs:
141 ccemail = kwargs['ccemail']
144 self.getTicketStatus() # get current message status
145 if self.db.message_status not in ['open', 'new']:
148 m = Message(message[0] % args, message[1] % args, viart, self.db.message_id)
151 contacts = [config.cc_email]
153 contacts = self.getContacts()
155 print "sending message: %s to site %s for host %s" % (type, self.db.loginbase, hostname)
157 ret = m.send(contacts)
159 self.db.message_id = ret
160 # reset to previous status, since a new subject 'opens' RT tickets.
161 self.setTicketStatus(self.db.message_status)
164 # NOTE: only make a record of it if it's in RT.
165 act = ActionRecord(loginbase=self.db.loginbase, hostname=hostname, action='notice',
166 action_type=type, message_id=self.db.message_id)
169 print "+-- WARNING! ------------------------------"
170 print "| No such message name in emailTxt.mailtxt: %s" % type
171 print "+------------------------------------------"
175 def closeTicket(self):
176 if self.db.message_id:
177 mailer.closeTicketViaRT(self.db.message_id, "Ticket Closed by Monitor")
178 act = ActionRecord(loginbase=self.db.loginbase, action='notice',
179 action_type='close_ticket', message_id=self.db.message_id)
180 self.db.message_id = 0
181 self.db.message_status = "new"
183 def runBootManager(self, hostname):
184 from monitor import bootman
185 print "attempting BM reboot of %s" % hostname
188 ret = bootman.restore(self, hostname)
191 err = traceback.format_exc()
194 act = ActionRecord(loginbase=self.db.loginbase,
197 action_type='bootmanager_restore',
201 def attemptReboot(self, hostname):
202 print "attempting PCU reboot of %s" % hostname
205 ret = reboot.reboot_str(hostname)
207 err = traceback.format_exc()
210 if ret == 0 or ret == "0":
213 act = ActionRecord(loginbase=self.db.loginbase,
216 action_type='try_reboot',