add pcu_name to pcufailed_notice
[monitor.git] / monitor / database / info / interface.py
1
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
11
12 class SiteInterface(HistorySiteRecord):
13         @classmethod
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)
20         
21         def __init__(self, sitehist):
22                 self.db = sitehist
23
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.
27
28                 #print "kwargs: ", kwargs
29
30                 recent_actions = []
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())
35                 return recent_actions
36         
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
45         
46         def applyPenalty(self):
47                 penalty_map = [] 
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) } )
54
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) 
58
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)
62
63                 return
64
65         def pausePenalty(self):
66                 act = ActionRecord(loginbase=self.db.loginbase,
67                                                         action='penalty',
68                                                         action_type='pause_penalty',)
69         
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
74         
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'])
81
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)
86
87         def getContacts(self):
88                 contacts = []
89                 if self.db.penalty_level >= 0:
90                         contacts += plc.getTechEmails(self.db.loginbase)
91
92                 if self.db.penalty_level >= 1:
93                         contacts += plc.getPIEmails(self.db.loginbase)
94
95                 if self.db.penalty_level >= 2:
96                         contacts += plc.getSliceUserEmails(self.db.loginbase)
97
98                 return contacts
99
100         def sendMessage(self, type, **kwargs):
101
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
107         #           previous status.
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.
111
112                 # 
113                 # TODO: catch any errors here, and add an ActionRecord that contains
114                 #       those errors.
115                 
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}
122                 args.update(kwargs)
123
124                 hostname = None
125                 if 'hostname' in args:
126                         hostname = args['hostname']
127
128                 if hasattr(mailtxt, type):
129
130                         message = getattr(mailtxt, type)
131
132                         ccemail = False
133                         saveact = True
134                         viart = True
135                         if 'viart' in kwargs: 
136                                 saveact = kwargs['viart']
137                                 viart = kwargs['viart']
138
139                         if 'saveact' in kwargs: 
140                                 saveact = kwargs['saveact']
141
142                         if 'ccemail' in kwargs: 
143                                 ccemail = kwargs['ccemail']
144
145                         if viart:
146                                 self.getTicketStatus()          # get current message status
147                                 if self.db.message_status not in ['open', 'new']:
148                                         self.closeTicket()
149
150                         m = Message(message[0] % args, message[1] % args, viart, self.db.message_id)
151
152                         if ccemail:
153                                 contacts = [config.exception_email]
154                         else:
155                                 contacts = self.getContacts()
156
157                         print "sending message: %s to site %s for host %s" % (type, self.db.loginbase, hostname)
158
159                         ret = m.send(contacts)
160                         if viart:
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) 
164
165                         if saveact:
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)
169
170                 else:
171                         print "+-- WARNING! ------------------------------"
172                         print "| No such message name in emailTxt.mailtxt: %s" % type
173                         print "+------------------------------------------"
174
175                 return
176
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"
184
185         def runBootManager(self, hostname):
186                 from monitor import bootman
187                 print "attempting BM reboot of %s" % hostname
188                 ret = "error"
189                 try:
190                         ret = bootman.restore(self, hostname)
191                         err = ""
192                 except:
193                         err = traceback.format_exc()
194                         print err
195
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,
199                                                         hostname=hostname,
200                                                         action='reboot',
201                                                         action_type='bootmanager_restore',
202                                                         error_string="")
203
204                 act = ActionRecord(loginbase=self.db.loginbase,
205                                                         hostname=hostname,
206                                                         action='reboot',
207                                                         action_type='bootmanager_' + ret,
208                                                         error_string=err)
209                 return ret
210
211         def attemptReboot(self, hostname):
212                 print "attempting PCU reboot of %s" % hostname
213                 err = ""
214                 try:
215                         ret = reboot.reboot_str(hostname)
216                 except Exception, e:
217                         err = traceback.format_exc()
218                         ret = str(e)
219
220                 if ret == 0 or ret == "0":
221                         ret = ""
222
223                 act = ActionRecord(loginbase=self.db.loginbase,
224                                                         hostname=hostname,
225                                                         action='reboot',
226                                                         action_type='try_reboot',
227                                                         error_string=err)
228