ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/server/uk/org/iscream/cms/server/client/monitors/Heartbeat__Monitor.java
(Generate patch)

Comparing projects/cms/source/server/uk/org/iscream/cms/server/client/monitors/Heartbeat__Monitor.java (file contents):
Revision 1.3 by tdb, Mon Mar 5 23:14:30 2001 UTC vs.
Revision 1.7 by tdb, Tue Mar 6 22:35:01 2001 UTC

# Line 15 | Line 15 | import uk.ac.ukc.iscream.componentmanager.*;
15   * @author  $Author$
16   * @version $Id$
17   */
18 < public class Heartbeat__Monitor implements PluginMonitor, Runnable {
18 > public class Heartbeat__Monitor extends MonitorSkeleton implements Runnable {
19  
20   //---FINAL ATTRIBUTES---
21  
# Line 54 | Line 54 | public class Heartbeat__Monitor implements PluginMonit
54              }
55              
56              // perform the checks (use HB hash, although they *should* be the same)
57 <            Iterator i = _hostsHB.keySet().iterator();
57 >            Iterator i = _hosts.keySet().iterator();
58              while(i.hasNext()) {
59                  // get host
60                  String source = (String) i.next();
61                  // check it
62 <                analyseHB(source);
62 >                boolean remove = analyseHB(source);
63 >                if(remove) {
64 >                    i.remove();
65 >                }
66              }
67              
68              // wait a while
# Line 67 | Line 70 | public class Heartbeat__Monitor implements PluginMonit
70          }
71      }
72      
70    // only use attribute num 0 :)
71    public void analyseHB(String source) {
72        ConfigurationProxy cp = ConfigurationProxy.getInstance();
73        Register reg = (Register) _hostsReg.get(source);
74        
75        // get host's HB interval (seconds)
76        // this should always exist, thus we set to 0
77        int hostHBinterval = 0;
78        try {
79            hostHBinterval = Integer.parseInt(cp.getProperty("Host."+source, "TCPUpdateTime"));
80        } catch (PropertyNotFoundException e) {
81            hostHBinterval = 0;
82            _logger.write(toString(), Logger.WARNING, "TCPUpdateTime value unavailable using default of " + hostHBinterval + " seconds");
83        } catch (NumberFormatException e) {
84            hostHBinterval = 0;
85            _logger.write(toString(), Logger.WARNING, "Erronous TCPUpdateTime value in configuration using default of " + hostHBinterval + " seconds");
86        }
87
88        // get host's last HB time (seconds)
89        long lastHeartbeat = ((HeartbeatHolder) _hostsHB.get(source)).getLastHeartbeat();
90        // time since last heartbeat (seconds)
91        long timeSinceLastHB = (System.currentTimeMillis()/1000) - lastHeartbeat;
92        
93        // find out the threshold level we're at
94        int result = checkAttributeThreshold(timeSinceLastHB, reg);
95            
96        // decide what threshold level we're on, if we've changed, record that
97        if (result != reg.getLastThresholdLevel(0)) {
98            reg.setLastThresholdLevel(0, result);
99        }
100            
101        // as long as this isn't a normal level
102        if(reg.getLastThresholdLevel(0) != Alert.thresholdNORMAL) {
103            // if the time since the last alert is more than the time for
104            // its timeout, fire an alert, escalate the alert
105            long timeout = reg.getLastAlertTimeout(0);
106            if ((timeout > 0) && (reg.getTimeLastSent(0) > 0)) {
107                if((System.currentTimeMillis() - reg.getTimeLastSent(0)) > timeout) {
108                    int lastAlert = reg.getLastAlertLevel(0);
109                    reg.escalateAlert(0);
110                    reg.setTimeLastSent(0, System.currentTimeMillis());
111                    reg.setLastAlertTimeout(0, reg.getAlertTimeout(reg.getLastAlertLevel(0), 0));
112                    // -- SEND
113                    fireAlert(source, timeSinceLastHB, reg, lastAlert);
114                }
115            // if we don't have a timeout configured...we got STRAIGHT to the next level
116            } else {
117                int lastAlert = reg.getLastAlertLevel(0);
118                reg.escalateAlert(0);
119                reg.setTimeLastSent(0, System.currentTimeMillis());
120                reg.setLastAlertTimeout(0, reg.getAlertTimeout(reg.getLastAlertLevel(0), 0));
121                // -- SEND
122                fireAlert(source, timeSinceLastHB, reg, lastAlert);
123            }
124                
125        // we must be on ok, check the timeout value for this
126        } else {
127            // if we were on an OK alert before, then we don't do anything
128            // but if we weren't we only set OK, once the timeout of the last
129            // alert has occourd
130            if (reg.getLastAlertLevel(0) != Alert.alertOK) {
131                long timeout = reg.getLastAlertTimeout(0);
132                if ((timeout > 0) && (reg.getTimeLastSent(0) > 0)) {
133                    if ((System.currentTimeMillis() - reg.getTimeLastSent(0)) > timeout) {
134                        int lastAlert = reg.getLastAlertLevel(0);
135                        reg.setLastAlertLevel(0, Alert.alertOK);
136                        reg.setTimeLastSent(0, System.currentTimeMillis());
137                        reg.setLastAlertTimeout(0, timeout);
138                        // -- SEND
139                        fireAlert(source, timeSinceLastHB, reg, lastAlert);
140                    }
141                }
142            }
143        }
144    }
145
73      public void analysePacket(XMLPacket packet) {
74          if (packet.getParam("packet.attributes.type").equals("heartbeat")) {
75              String source = packet.getParam("packet.attributes.machine_name");
76 <            if (!_hostsHB.containsKey(source)) {
77 <                _hostsReg.put(source, new Register(source, _name, 1));
78 <                _hostsHB.put(source, new HeartbeatHolder());
76 >            if (!_hosts.containsKey(source)) {
77 >                HashMap registerHash = new HashMap();
78 >                registerHash.put(source, new Register(source, _name, 1));
79 >                _hosts.put(source, new HeartbeatHolder(registerHash));
80              }
81 <            HeartbeatHolder lastHeartbeat = (HeartbeatHolder) _hostsHB.get(source);
81 >            HeartbeatHolder lastHeartbeat = (HeartbeatHolder) _hosts.get(source);
82              lastHeartbeat.setLastHeartbeat(System.currentTimeMillis()/1000);
83          }
84      }
# Line 179 | Line 107 | public class Heartbeat__Monitor implements PluginMonit
107      }
108  
109   //---PRIVATE METHODS---
110 +
111 +    private boolean analyseHB(String source) {
112 +        ConfigurationProxy cp = ConfigurationProxy.getInstance();
113 +        HeartbeatHolder hbHolder = (HeartbeatHolder) _hosts.get(source);
114 +        Register reg = (Register) ((HashMap) hbHolder.getRegisterHash()).get(source);
115 +        
116 +        // get host's HB interval (seconds)
117 +        // this should always exist, thus we set to 0
118 +        int hostHBinterval = 0;
119 +        try {
120 +            hostHBinterval = Integer.parseInt(cp.getProperty("Host."+source, "Host.TCPUpdateTime"));
121 +        } catch (PropertyNotFoundException e) {
122 +            hostHBinterval = 0;
123 +            _logger.write(toString(), Logger.WARNING, "TCPUpdateTime value unavailable using default of " + hostHBinterval + " seconds");
124 +        } catch (NumberFormatException e) {
125 +            hostHBinterval = 0;
126 +            _logger.write(toString(), Logger.WARNING, "Erronous TCPUpdateTime value in configuration using default of " + hostHBinterval + " seconds");
127 +        }
128 +        
129 +        // get host's last HB time (seconds)
130 +        long lastHeartbeat = hbHolder.getLastHeartbeat();
131 +        // time since last heartbeat (seconds)
132 +        long timeSinceLastHB = (System.currentTimeMillis()/1000) - lastHeartbeat;
133 +        
134 +        // find out the threshold level we're at
135 +        int newThreshold = checkAttributeThreshold(timeSinceLastHB, reg);
136 +        
137 +        // process the alert
138 +        processAlert(newThreshold, 0, "Heartbeat", reg, source, String.valueOf(timeSinceLastHB));
139 +        
140 +        if(reg.getLastAlertLevel(0) == Alert.alertFINAL) {
141 +            return true;
142 +        }
143 +        return false;
144 +    }
145      
146      private int checkAttributeThreshold(long timeSinceLastHB, Register reg) {
147          for(int thresholdLevel = Alert.thresholdLevels.length - 1; thresholdLevel >= 0; thresholdLevel--) {
# Line 188 | Line 151 | public class Heartbeat__Monitor implements PluginMonit
151                  }
152              }
153          }
154 <        return 0;
154 >        return Alert.thresholdNORMAL;
155      }
193        
194    private void fireAlert(String source, long timeSinceLastHB, Register reg, int lastAlert) {
195        int alertLevel = reg.getLastAlertLevel(0);
196        int thresholdLevel = reg.getLastThresholdLevel(0);
197        String currentValue = String.valueOf(timeSinceLastHB);
198        String attributeName = "Heartbeat";
199        String thresholdValue = String.valueOf(reg.getThreshold(thresholdLevel));
200        String time = Long.toString(reg.getAlertTimeout(reg.getLastAlertLevel(0), 0) / 1000);
201        if (thresholdLevel == Alert.thresholdNORMAL) {
202            thresholdValue = "-";
203        }
204        if (alertLevel == Alert.alertOK) {
205            time = "0";
206        }
207        Alert alert = new Alert(alertLevel, lastAlert, thresholdLevel, source, thresholdValue, currentValue, attributeName, time);
208        _alerterQueue.add(alert);
209        _logger.write(toString(), Logger.DEBUG, "Fired alert for source:" + source + " at alert level:" + Alert.alertLevels[alertLevel] + " on:" + attributeName + " for threshold level:" + Alert.thresholdLevels[thresholdLevel] + " at:" +  currentValue + " exceeding threshold of:" +thresholdValue + " next alert sent in:" + time + "secs");
210    }
156  
157   //---ACCESSOR/MUTATOR METHODS---
158  
# Line 223 | Line 168 | public class Heartbeat__Monitor implements PluginMonit
168       * be changed to null for utility classes.
169       */
170      private String _name = "Heartbeat";
226
227    /**
228     * This holds a reference to the
229     * system logger that is being used.
230     */
231    private Logger _logger = ReferenceManager.getInstance().getLogger();
171      
233    private Queue _alerterQueue = ClientMain._alerterQueue;
234    
172      /**
173       * A reference to the configuration proxy in use
174       */
175      private ConfigurationProxy _cp = ConfigurationProxy.getInstance();
176  
177 <    private HashMap _hostsHB = new HashMap();
241 <    private HashMap _hostsReg = new HashMap();
177 >    private HashMap _hosts = new HashMap();
178  
179   //---STATIC ATTRIBUTES---
180  
# Line 246 | Line 182 | public class Heartbeat__Monitor implements PluginMonit
182  
183      private class HeartbeatHolder {
184          
185 +        public HeartbeatHolder(HashMap registerHash) {
186 +            _registerHash = registerHash;
187 +        }
188 +        
189          public void setLastHeartbeat(long lastHeartbeat) {
190              _lastHeartbeat = lastHeartbeat;
191          }
# Line 254 | Line 194 | public class Heartbeat__Monitor implements PluginMonit
194              return _lastHeartbeat;
195          }
196          
197 +        public HashMap getRegisterHash() {
198 +            return _registerHash;
199 +        }
200 +        
201          private long _lastHeartbeat;
202 +        private HashMap _registerHash;
203      }  
204  
205   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines