📄 boxstate.java
字号:
package no.ntnu.nav.eventengine.handlerplugins.BoxState;import java.util.*;import java.sql.*;import no.ntnu.nav.Database.*;import no.ntnu.nav.ConfigParser.*;import no.ntnu.nav.logger.*;import no.ntnu.nav.eventengine.*;import no.ntnu.nav.eventengine.deviceplugins.Box.*;import no.ntnu.nav.eventengine.deviceplugins.Netel.*;/** * BoxState plugin for eventengine. Handles all events dealing with * netboxes and their modules going up/down. */public class BoxState implements EventHandler, EventCallback{ public static final int SHADOW_SEVERITY_DEDUCTION = 20; public String[] handleEventTypes() { // FIXME: Temporarily disabled handling of linkState events, as it is broken // The INFO plugin will dispatch the alerts, but no update of the swport table is done. return new String[] { "boxState", "moduleState", "boxRestart" }; } public void handle(DeviceDB ddb, Event e, ConfigParser cp) { Log.setDefaultSubsystem("BOX_STATE_EVENTHANDLER"); Log.d("HANDLE", "Event: " + e); int warningWaitTime = 60; int alertWaitTime = 240; try { warningWaitTime = Integer.parseInt(cp.get("warningWaitTime")); } catch (Exception exp) { } try { alertWaitTime = Integer.parseInt(cp.get("alertWaitTime")); } catch (Exception exp) { } int moduleWarningWaitTime = 60; int moduleAlertWaitTime = 240; try { moduleWarningWaitTime = Integer.parseInt(cp.get("moduleWarningWaitTime")); } catch (Exception exp) { } try { moduleAlertWaitTime = Integer.parseInt(cp.get("moduleAlertWaitTime")); } catch (Exception exp) { } String eventtype = e.getEventtypeid(); if (eventtype.equals("boxState")) { Device d = ddb.getDevice(e.getDeviceid()); if (d == null) { Log.w("HANDLE", "Box with deviceid="+e.getDeviceid()+" not found! (boxState)"); return; } if (d instanceof Box) { Box b = (Box)d; if (e.getState() == Event.STATE_START) { if (!b.isUp() && isInQ(b, eventtype)) { Log.d("HANDLE", "Ignoring duplicate down event for Box"); e.dispose(); return; } Log.d("HANDLE", "Box going down: " + b); b.down(); addToQ(b, eventtype, warningWaitTime, alertWaitTime - warningWaitTime, e); } else if (e.getState() == Event.STATE_END) { // Get the down alert Alert a = ddb.getDownAlert(e); // Check if the deviceid has changed if (a == null && !isInQ(b, eventtype)) { try { ResultSet rs = Database.query("SELECT alerthistid,deviceid FROM alerthist WHERE netboxid='"+e.getNetboxid()+"' AND end_time='infinity' AND eventtypeid='boxState'"); if (rs.next()) { Alert oldevent = (Alert)e; oldevent.setDeviceid(rs.getInt("deviceid")); a = ddb.getDownAlert(e); // Close it and mark box up //Database.update("UPDATE alerthist SET end_time=NOW() WHERE alerthistid='"+rs.getString("alerthistid")+"'"); Log.d("HANDLE", "Deviceid changed for end event, deviceid="+rs.getString("deviceid") + " for netboxid: " + e.getNetboxid()); } else { Log.d("HANDLE", "Ignoring box up event as no down event was found!"); } } catch (SQLException exp) { Log.w("BOX_STATE_EVENTHANDLER", "SQLException when checking for open down event in alerthist, netboxid " + e.getNetboxid()); } } if (a == null) { // The down event could be in the queue SendAlertDescr sad = removeFromQ(b, eventtype); if (sad == null) { // No down alert, but we mark the box up just in case e.dispose(); Log.d("HANDLE", "No down event found, disposing up event"); } else { // For now ignore transient events sad.event.dispose(); e.dispose(); Log.d("HANDLE", "Ignoring transient boxState"); } } else { Log.d("HANDLE", "Box going up"); // Post alert a = ddb.alertFactory(e, "boxUp"); if (b.getStatus() == Box.STATUS_SHADOW) { a.setAlerttype("boxSunny"); a.setSeverity(Math.max(e.getSeverity()-SHADOW_SEVERITY_DEDUCTION,0)); } a.addEvent(e); if (b.onMaintenance()) { // Do not post to alertq if box is on maintenace Log.d("HANDLE", "Not posting alert to alertq as the box is on maintenance"); a.setPostAlertq(false); } try { ddb.postAlert(a); } catch (PostAlertException exp) { Log.w("HANDLE", "PostAlertException: " + exp.getMessage()); } // Clean up removeFromQ(b, eventtype); } b.up(); } } } else if (eventtype.equals("moduleState") || eventtype.equals("linkState")) { // Get parent netbox int parentDeviceid = Box.boxidToDeviceid(e.getNetboxid()); Device d = ddb.getDevice(parentDeviceid); if (d instanceof Netel) { if (!d.isUp()) { // Ignore event since box itself is down Log.d("HANDLE", "Box " + d + " is down, ignoring " + eventtype + " ("+e.getDeviceid()+")"); e.dispose(); return; } Netel parent = (Netel)d; Module m = parent.getModule(e.getDeviceid()); if (m == null) { Log.d("HANDLE", "Module " + e.getDeviceid() + " not found on box " + d); ddb.updateFromDB(); m = parent.getModule(e.getDeviceid()); if (m == null) { Log.d("HANDLE", "Module " + e.getDeviceid() + " not found on box after updateFromDB " + d); System.err.println("Module " + e.getDeviceid() + " not found on box after updateFromDB" + d); e.dispose(); return; } } if (eventtype.equals("linkState")) { Port p = m.getPort(Integer.parseInt(e.getSubid())); if (p == null) { Log.d("HANDLE", "Port ifindex="+e.getSubid()+" in module="+m.getModule()+" not found!"); return; } if (e.getState() == Event.STATE_START) p.down(); else if (e.getState() == Event.STATE_END) p.up(); e.dispose(); Log.d("HANDLE", "Port: " + p); } else { if (e.getState() == Event.STATE_START) { if (!m.isUp() && isInQ(m, eventtype)) { Log.d("HANDLE", "Ignoring duplicate down event for Module"); e.dispose(); return; } Log.d("HANDLE", "Module going down (" + m.getDeviceid()+")"); m.down(); addToQ(m, eventtype, moduleWarningWaitTime, moduleAlertWaitTime - moduleWarningWaitTime, e); } else if (e.getState() == Event.STATE_END) { // Get the down alert Alert a = ddb.getDownAlert(e); if (a == null) { // The down event could be in the queue SendAlertDescr sad = removeFromQ(m, eventtype); if (sad == null) { Log.d("HANDLE", "Ignoring module up event as no down event was found!"); e.dispose(); return; } // For now ignore transient events sad.event.dispose(); e.dispose(); Log.d("HANDLE", "Ignoring transient moduleState"); } else { Log.d("HANDLE", "Module going up"); // Post alert a = ddb.alertFactory(e, "moduleUp"); a.addEvent(e); if (parent != null && parent.onMaintenance()) { // Do not post to alertq if box is on maintenace a.setPostAlertq(false); } try { ddb.postAlert(a); } catch (PostAlertException exp) { Log.w("HANDLE", "PostAlertException: " + exp.getMessage()); } // Clean up removeFromQ(m, eventtype); } m.up(); } //outld("BoxState Module: " + m); } } } else if (eventtype.equals("boxRestart")) { // Simply post on alertq Device d = ddb.getDevice(e.getDeviceid()); if (d == null) { Log.w("HANDLE", "Box with deviceid="+e.getDeviceid()+" not found (boxRestart)!"); e.dispose(); return; } String alerttype = e.getVar("alerttype"); Alert a = ddb.alertFactory(e, alerttype); a.addEvent(e); if (d instanceof Box) { Box b = (Box)d; if (b.onMaintenance()) { // Do not post to alertq if box is on maintenace a.setPostAlertq(false); } } Log.d("HANDLE", "Posting boxRestart ("+alerttype+") alert"); try { ddb.postAlert(a);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -