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
15 from monitor.wrapper.plccache import plcdb_lb2hn as site_lb2hn
17 def format_ports(pcu):
19 if pcu.port_status and len(pcu.port_status.keys()) > 0 :
20 obj = reboot.model_to_object(pcu.plc_pcu_stats['model'])
21 for port in obj.supported_ports:
23 state = pcu.port_status[str(port)]
27 retval.append( (port, state) )
30 retval = [( "Closed/Filtered", "state" )]
34 def format_pcu_shortstatus(pcu):
36 if pcu.reboot_trial_status == str(0):
38 elif pcu.reboot_trial_status == "NetDown" or pcu.reboot_trial_status == "Not_Run":
39 status = pcu.reboot_trial_status
45 def prep_pcu_for_display(pcu):
48 pcu.loginbase = site_id2lb[pcu.plc_pcu_stats['site_id']]
50 pcu.loginbase = "unknown"
52 pcu.ports = format_ports(pcu)
53 pcu.status = format_pcu_shortstatus(pcu)
55 def prep_node_for_display(node):
57 pcu = FindbadPCURecord.get_latest_by(plc_pcuid=node.plc_pcuid).first()
59 node.pcu_status = pcu.reboot_trial_status
61 node.pcu_status = "nodata"
62 node.pcu_short_status = format_pcu_shortstatus(pcu)
65 node.pcu_status = "nopcu"
66 node.pcu_short_status = "none"
68 if node.kernel_version:
69 node.kernel = node.kernel_version.split()[2]
74 node.loginbase = site_id2lb[node.plc_node_stats['site_id']]
76 node.loginbase = "unknown"
79 class Root(controllers.RootController):
80 @expose(template="monitorweb.templates.welcome")
83 # log.debug("Happy TurboGears Controller Responding For Duty")
84 flash("Your application is now running")
85 return dict(now=time.ctime())
87 @expose(template="monitorweb.templates.nodeview")
88 def nodeview(self, hostname=None):
91 for node in FindbadNodeRecord.get_latest_by(hostname=hostname):
92 # NOTE: reformat some fields.
93 prep_node_for_display(node)
96 return dict(nodequery=nodequery)
98 @expose(template="monitorweb.templates.nodelist")
99 def node(self, filter='BOOT'):
101 fbquery = FindbadNodeRecord.get_all_latest()
103 filtercount = {'DOWN' : 0, 'BOOT': 0, 'DEBUG' : 0, 'neverboot' : 0, 'pending' : 0, 'all' : 0}
105 # NOTE: reformat some fields.
106 prep_node_for_display(node)
108 # NOTE: count filters
109 if node.observed_status != 'DOWN':
110 filtercount[node.observed_status] += 1
112 if node.plc_node_stats['last_contact'] != None:
113 filtercount[node.observed_status] += 1
115 filtercount['neverboot'] += 1
118 if filter == node.observed_status:
120 if node.plc_node_stats['last_contact'] != None:
124 elif filter == "neverboot":
125 if node.plc_node_stats['last_contact'] == None:
127 elif filter == "pending":
128 # TODO: look in message logs...
130 elif filter == "all":
133 return dict(now=time.ctime(), query=query, fc=filtercount)
135 @expose(template="monitorweb.templates.pcuview")
136 def pcuview(self, pcuid=None):
139 for pcu in FindbadPCURecord.get_latest_by(plc_pcuid=pcuid):
141 prep_pcu_for_display(pcu)
144 return dict(pcuquery=pcuquery)
146 @expose(template="monitorweb.templates.pculist")
147 def pcu(self, filter='all'):
149 fbquery = FindbadPCURecord.get_all_latest()
151 filtercount = {'ok' : 0, 'NetDown': 0, 'Not_Run' : 0, 'pending' : 0, 'all' : 0}
155 if pcu.reboot_trial_status == str(0):
156 filtercount['ok'] += 1
157 elif pcu.reboot_trial_status == 'NetDown' or pcu.reboot_trial_status == 'Not_Run':
158 filtercount[pcu.reboot_trial_status] += 1
160 filtercount['pending'] += 1
162 prep_pcu_for_display(node)
167 elif filter == "ok" and node.reboot_trial_status == str(0):
169 elif filter == node.reboot_trial_status:
171 elif filter == "pending":
172 # TODO: look in message logs...
173 if node.reboot_trial_status != str(0) and \
174 node.reboot_trial_status != 'NetDown' and \
175 node.reboot_trial_status != 'Not_Run':
179 return dict(query=query, fc=filtercount)
181 @expose(template="monitorweb.templates.siteview")
182 def siteview(self, loginbase='pl'):
184 sitequery = [HistorySiteRecord.by_loginbase(loginbase)]
186 for plcnode in site_lb2hn[loginbase]:
187 for node in FindbadNodeRecord.get_latest_by(hostname=plcnode['hostname']):
188 # NOTE: reformat some fields.
189 prep_node_for_display(node)
191 return dict(sitequery=sitequery, nodequery=nodequery, fc={})
193 @expose(template="monitorweb.templates.sitelist")
194 def site(self, filter='all'):
195 filtercount = {'good' : 0, 'down': 0, 'new' : 0, 'pending' : 0, 'all' : 0}
196 fbquery = HistorySiteRecord.query.all()
200 filtercount['all'] += 1
201 if site.new and site.slices_used == 0 and not site.enabled:
202 filtercount['new'] += 1
203 elif not site.enabled:
204 filtercount['pending'] += 1
206 filtercount[site.status] += 1
211 elif filter == 'new' and site.new and site.slices_used == 0 and not site.enabled:
213 elif filter == "pending" and not site.enabled:
215 elif filter == site.status:
218 return dict(query=query, fc=filtercount)
220 @expose(template="monitorweb.templates.actionlist")
221 def action(self, filter='all'):
222 session.bind = metadata.bind
223 filtercount = {'active' : 0, 'acknowledged': 0, 'all' : 0}
224 # With Acknowledgement
225 sql_ack = 'SELECT DISTINCT h.host,t.description,t.priority,t.lastchange,a.message '+ \
226 ' FROM triggers t,hosts h,items i,functions f, hosts_groups hg,escalations e,acknowledges a ' + \
227 ' WHERE f.itemid=i.itemid ' + \
228 ' AND h.hostid=i.hostid ' + \
229 ' AND hg.hostid=h.hostid ' + \
230 ' AND t.triggerid=f.triggerid ' + \
231 ' AND t.triggerid=e.triggerid ' + \
232 ' AND a.eventid=e.eventid ' + \
233 ' AND t.status=' + str(defines.TRIGGER_STATUS_ENABLED) + \
234 ' AND i.status=' + str(defines.ITEM_STATUS_ACTIVE) + \
235 ' AND h.status=' + str(defines.HOST_STATUS_MONITORED) + \
236 ' AND t.value=' + str(defines.TRIGGER_VALUE_TRUE) + \
237 ' ORDER BY t.lastchange DESC';
239 # WithOUT Acknowledgement
240 sql_noack = 'SELECT DISTINCT h.host,t.description,t.priority,t.lastchange,e.eventid ' + \
241 ' FROM triggers t,hosts h,items i,functions f, hosts_groups hg,escalations e,acknowledges a ' + \
242 ' WHERE f.itemid=i.itemid ' + \
243 ' AND h.hostid=i.hostid ' + \
244 ' AND hg.hostid=h.hostid ' + \
245 ' AND t.triggerid=f.triggerid ' + \
246 ' AND t.triggerid=e.triggerid ' + \
247 ' AND e.eventid not in (select eventid from acknowledges) ' + \
248 ' AND t.status=' + str(defines.TRIGGER_STATUS_ENABLED) + \
249 ' AND i.status=' + str(defines.ITEM_STATUS_ACTIVE) + \
250 ' AND h.status=' + str(defines.HOST_STATUS_MONITORED) + \
251 ' AND t.value=' + str(defines.TRIGGER_VALUE_TRUE) + \
252 ' ORDER BY t.lastchange DESC';
253 # for i in session.execute(sql): print i
256 replace = re.compile(' {.*}')
257 for sql,ack in [(sql_ack,True), (sql_noack,False)]:
258 result = session.execute(sql)
261 newrow = [ site_hn2lb[row[0].lower()] ] + [ r for r in row ]
263 print site_hn2lb.keys()
264 newrow = [ "unknown" ] + [ r for r in row ]
266 newrow[2] = replace.sub("", newrow[2]) # strip {.*} expressions
269 filtercount['all'] += 1
270 if not ack: # for unacknowledged
271 filtercount['active'] += 1
272 if filter == 'active':
275 filtercount['acknowledged'] += 1
276 if filter == 'acknowledged':
279 if filter != "acknowledged" and filter != "active":
282 return dict(query=query, fc=filtercount)