1 import turbogears as tg
2 from turbogears import controllers, expose, flash
3 # from monitorweb import model
5 # log = logging.getLogger("monitorweb.controllers")
7 from monitor.database.info.model import *
8 from monitor.database.zabbixapi.model import *
9 from monitor.database.dborm import zab_session as session
10 from monitor.database.dborm import zab_metadata as metadata
12 from pcucontrol import reboot
13 from monitor.wrapper.plccache import plcdb_id2lb as site_id2lb
14 from monitor.wrapper.plccache import plcdb_hn2lb as site_hn2lb
16 def format_ports(pcu):
18 if pcu.port_status and len(pcu.port_status.keys()) > 0 :
19 obj = reboot.model_to_object(pcu.plc_pcu_stats['model'])
20 for port in obj.supported_ports:
22 state = pcu.port_status[str(port)]
26 retval.append( (port, state) )
29 retval = [( "Closed/Filtered", "state" )]
33 def format_pcu_shortstatus(pcu):
35 if pcu.reboot_trial_status == str(0):
37 elif pcu.reboot_trial_status == "NetDown" or pcu.reboot_trial_status == "Not_Run":
38 status = pcu.reboot_trial_status
44 class Root(controllers.RootController):
45 @expose(template="monitorweb.templates.welcome")
48 # log.debug("Happy TurboGears Controller Responding For Duty")
49 flash("Your application is now running")
50 return dict(now=time.ctime())
52 @expose(template="monitorweb.templates.nodelist")
53 def node(self, filter='BOOT'):
55 fbquery = FindbadNodeRecord.get_all_latest()
57 filtercount = {'DOWN' : 0, 'BOOT': 0, 'DEBUG' : 0, 'neverboot' : 0, 'pending' : 0, 'all' : 0}
59 # NOTE: reformat some fields.
61 pcu = FindbadPCURecord.get_latest_by(plc_pcuid=node.plc_pcuid).first()
63 node.pcu_status = pcu.reboot_trial_status
65 node.pcu_status = "nodata"
66 node.pcu_short_status = format_pcu_shortstatus(pcu)
69 node.pcu_status = "nopcu"
70 node.pcu_short_status = "none"
72 if node.kernel_version:
73 node.kernel = node.kernel_version.split()[2]
78 node.loginbase = site_id2lb[node.plc_node_stats['site_id']]
80 node.loginbase = "unknown"
84 if node.observed_status != 'DOWN':
85 filtercount[node.observed_status] += 1
87 if node.plc_node_stats['last_contact'] != None:
88 filtercount[node.observed_status] += 1
90 filtercount['neverboot'] += 1
93 if filter == node.observed_status:
95 if node.plc_node_stats['last_contact'] != None:
99 elif filter == "neverboot":
100 if node.plc_node_stats['last_contact'] == None:
102 elif filter == "pending":
103 # TODO: look in message logs...
105 elif filter == "all":
108 return dict(now=time.ctime(), query=query, fc=filtercount)
110 @expose(template="monitorweb.templates.pculist")
111 def pcu(self, filter='all'):
113 fbquery = FindbadPCURecord.get_all_latest()
115 filtercount = {'ok' : 0, 'NetDown': 0, 'Not_Run' : 0, 'pending' : 0, 'all' : 0}
119 if node.reboot_trial_status == str(0):
120 filtercount['ok'] += 1
121 elif node.reboot_trial_status == 'NetDown' or node.reboot_trial_status == 'Not_Run':
122 filtercount[node.reboot_trial_status] += 1
124 filtercount['pending'] += 1
127 node.loginbase = site_id2lb[node.plc_pcu_stats['site_id']]
129 node.loginbase = "unknown"
131 node.ports = format_ports(node)
132 node.status = format_pcu_shortstatus(node)
137 elif filter == "ok" and node.reboot_trial_status == str(0):
139 elif filter == node.reboot_trial_status:
141 elif filter == "pending":
142 # TODO: look in message logs...
143 if node.reboot_trial_status != str(0) and \
144 node.reboot_trial_status != 'NetDown' and \
145 node.reboot_trial_status != 'Not_Run':
149 return dict(query=query, fc=filtercount)
151 @expose(template="monitorweb.templates.sitelist")
152 def site(self, filter='all'):
153 filtercount = {'good' : 0, 'down': 0, 'new' : 0, 'pending' : 0, 'all' : 0}
154 fbquery = HistorySiteRecord.query.all()
158 filtercount['all'] += 1
159 if site.new and site.slices_used == 0 and not site.enabled:
160 filtercount['new'] += 1
161 elif not site.enabled:
162 filtercount['pending'] += 1
164 filtercount[site.status] += 1
169 elif filter == 'new' and site.new and site.slices_used == 0 and not site.enabled:
171 elif filter == "pending" and not site.enabled:
173 elif filter == site.status:
176 return dict(query=query, fc=filtercount)
178 @expose(template="monitorweb.templates.actionlist")
179 def action(self, filter='all'):
180 session.bind = metadata.bind
181 filtercount = {'active' : 0, 'acknowledged': 0, 'all' : 0}
182 # With Acknowledgement
183 sql_ack = 'SELECT DISTINCT h.host,t.description,t.priority,t.lastchange,a.message '+ \
184 ' FROM triggers t,hosts h,items i,functions f, hosts_groups hg,escalations e,acknowledges a ' + \
185 ' WHERE f.itemid=i.itemid ' + \
186 ' AND h.hostid=i.hostid ' + \
187 ' AND hg.hostid=h.hostid ' + \
188 ' AND t.triggerid=f.triggerid ' + \
189 ' AND t.triggerid=e.triggerid ' + \
190 ' AND a.eventid=e.eventid ' + \
191 ' AND t.status=' + str(defines.TRIGGER_STATUS_ENABLED) + \
192 ' AND i.status=' + str(defines.ITEM_STATUS_ACTIVE) + \
193 ' AND h.status=' + str(defines.HOST_STATUS_MONITORED) + \
194 ' AND t.value=' + str(defines.TRIGGER_VALUE_TRUE) + \
195 ' ORDER BY t.lastchange DESC';
197 # WithOUT Acknowledgement
198 sql_noack = 'SELECT DISTINCT h.host,t.description,t.priority,t.lastchange,e.eventid ' + \
199 ' FROM triggers t,hosts h,items i,functions f, hosts_groups hg,escalations e,acknowledges a ' + \
200 ' WHERE f.itemid=i.itemid ' + \
201 ' AND h.hostid=i.hostid ' + \
202 ' AND hg.hostid=h.hostid ' + \
203 ' AND t.triggerid=f.triggerid ' + \
204 ' AND t.triggerid=e.triggerid ' + \
205 ' AND e.eventid not in (select eventid from acknowledges) ' + \
206 ' AND t.status=' + str(defines.TRIGGER_STATUS_ENABLED) + \
207 ' AND i.status=' + str(defines.ITEM_STATUS_ACTIVE) + \
208 ' AND h.status=' + str(defines.HOST_STATUS_MONITORED) + \
209 ' AND t.value=' + str(defines.TRIGGER_VALUE_TRUE) + \
210 ' ORDER BY t.lastchange DESC';
211 # for i in session.execute(sql): print i
214 replace = re.compile(' {.*}')
215 for sql,ack in [(sql_ack,True), (sql_noack,False)]:
216 result = session.execute(sql)
219 newrow = [ site_hn2lb[row[0].lower()] ] + [ r for r in row ]
221 print site_hn2lb.keys()
222 newrow = [ "unknown" ] + [ r for r in row ]
224 newrow[2] = replace.sub("", newrow[2]) # strip {.*} expressions
227 filtercount['all'] += 1
228 if not ack: # for unacknowledged
229 filtercount['active'] += 1
230 if filter == 'active':
233 filtercount['acknowledged'] += 1
234 if filter == 'acknowledged':
237 if filter != "acknowledged" and filter != "active":
240 return dict(query=query, fc=filtercount)