fix a bug with pause_penalty.
[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 setPenaltyPause(self):
66                 self.db.penalty_pause = True
67                 self.db.penalty_pause_time = datetime.now()
68                 act = ActionRecord(loginbase=self.db.loginbase,
69                                                         action='penalty',
70                                                         action_type='pause_penalty',)
71         
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()
77
78         def clearPenaltyPause(self):
79                 self.db.penalty_pause = False
80                 self.db.penalty_pause_time = None
81         
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'])
89
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)
94
95         def getContacts(self):
96                 contacts = []
97                 if self.db.penalty_level >= 0:
98                         contacts += plc.getTechEmails(self.db.loginbase)
99
100                 if self.db.penalty_level >= 1:
101                         contacts += plc.getPIEmails(self.db.loginbase)
102
103                 if self.db.penalty_level >= 2:
104                         contacts += plc.getSliceUserEmails(self.db.loginbase)
105
106                 return contacts
107
108         def sendMessage(self, type, **kwargs):
109
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
115         #           previous status.
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.
119
120                 # 
121                 # TODO: catch any errors here, and add an ActionRecord that contains
122                 #       those errors.
123                 
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}
130                 args.update(kwargs)
131
132                 hostname = None
133                 if 'hostname' in args:
134                         hostname = args['hostname']
135
136                 if hasattr(mailtxt, type):
137
138                         message = getattr(mailtxt, type)
139
140                         ccemail = False
141                         saveact = True
142                         viart = True
143                         if 'viart' in kwargs: 
144                                 saveact = kwargs['viart']
145                                 viart = kwargs['viart']
146
147                         if 'saveact' in kwargs: 
148                                 saveact = kwargs['saveact']
149
150                         if 'ccemail' in kwargs: 
151                                 ccemail = kwargs['ccemail']
152
153                         if viart:
154                                 self.getTicketStatus()          # get current message status
155                                 if self.db.message_status not in ['open', 'new']:
156                                         self.closeTicket()
157
158                         m = Message(message[0] % args, message[1] % args, viart, self.db.message_id)
159
160                         if ccemail:
161                                 contacts = [config.exception_email]
162                         else:
163                                 contacts = self.getContacts()
164
165                         print "sending message: %s to site %s for host %s" % (type, self.db.loginbase, hostname)
166
167                         ret = m.send(contacts)
168                         if viart:
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) 
172
173                         if saveact:
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)
177
178                 else:
179                         print "+-- WARNING! ------------------------------"
180                         print "| No such message name in emailTxt.mailtxt: %s" % type
181                         print "+------------------------------------------"
182
183                 return
184
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"
192
193         def runBootManager(self, hostname):
194                 from monitor import bootman
195                 print "attempting BM reboot of %s" % hostname
196                 ret = "error"
197                 try:
198                         ret = bootman.restore(self, hostname)
199                         err = ""
200                 except:
201                         err = traceback.format_exc()
202                         print err
203
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,
207                                                         hostname=hostname,
208                                                         action='reboot',
209                                                         action_type='bootmanager_restore',
210                                                         error_string="")
211
212                 if ret:
213                         act = ActionRecord(loginbase=self.db.loginbase,
214                                                         hostname=hostname,
215                                                         action='reboot',
216                                                         action_type='bootmanager_' + ret,
217                                                         error_string=err)
218                 return ret
219
220         def attemptReboot(self, hostname):
221                 print "attempting PCU reboot of %s" % hostname
222                 err = ""
223                 try:
224                         ret = reboot.reboot_str(hostname)
225                 except Exception, e:
226                         err = traceback.format_exc()
227                         ret = str(e)
228
229                 if ret == 0 or ret == "0":
230                         ret = ""
231
232                 act = ActionRecord(loginbase=self.db.loginbase,
233                                                         hostname=hostname,
234                                                         action='reboot',
235                                                         action_type='try_reboot',
236                                                         error_string=err)
237