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/Services__Monitor.java
Revision: 1.4
Committed: Fri Mar 23 02:32:49 2001 UTC (23 years, 2 months ago) by tdb
Branch: MAIN
CVS Tags: PROJECT_COMPLETION
Changes since 1.3: +44 -26 lines
Log Message:
Fully javadoc'd all the monitors. Also made a few little changes here and there,
removing code that had been duplicated by copying other monitors, and tidying
up any silly little things (such has hardcoded integer values).

File Contents

# User Rev Content
1 ajm 1.1 //---PACKAGE DECLARATION---
2 tdb 1.2 package uk.org.iscream.client.monitors;
3 ajm 1.1
4     //---IMPORTS---
5     import java.util.HashMap;
6     import java.util.ArrayList;
7     import java.util.Set;
8     import java.util.Iterator;
9     import java.text.NumberFormat;
10 tdb 1.2 import uk.org.iscream.client.*;
11     import uk.org.iscream.core.*;
12     import uk.org.iscream.util.*;
13     import uk.org.iscream.componentmanager.*;
14 ajm 1.1
15     /**
16     * This Monitor watches the Service checks on hosts
17     *
18 tdb 1.4 * @author $Author: ajm4 $
19     * @version $Id: Services__Monitor.java,v 1.3 2001/03/22 17:57:06 ajm4 Exp $
20 ajm 1.1 */
21     public class Services__Monitor extends MonitorSkeleton {
22    
23     //---FINAL ATTRIBUTES---
24    
25     /**
26     * The current CVS revision of this class
27     */
28 tdb 1.4 public final String REVISION = "$Revision: 1.3 $";
29 ajm 1.1
30 tdb 1.4 /**
31     * A description of this monitor
32     */
33 ajm 1.1 public final String DESC = "Monitors a hosts services.";
34    
35     //---STATIC METHODS---
36    
37     //---CONSTRUCTORS---
38    
39     //---PUBLIC METHODS---
40 tdb 1.4
41     /**
42     * Analyse a packet of data, and generate an alert if
43     * necessary.
44     *
45     * @param packet the XMLPacket to analyse
46     */
47 ajm 1.1 public void analysePacket(XMLPacket packet) {
48 ajm 1.3 String source = packet.getParam("packet.attributes.machine_name");
49     if (!_hosts.containsKey(source)) {
50     _hosts.put(source, new HashMap());
51     }
52    
53     HashMap serviceRegisters = (HashMap) _hosts.get(source);
54    
55     // a tempory holder for all the disk attributes we find
56     ArrayList services = new ArrayList();
57 tdb 1.4
58     // the prefix for keys relating to service checks
59     String keyPrefix = "packet.services.";
60    
61 ajm 1.3 // unfortunatly we need to check the whole packet
62     // to find the disks, and then get the data attributes
63     Set packetSet = packet.getSet();
64     Iterator i = packetSet.iterator();
65     while (i.hasNext()) {
66     String dataKey = (String) i.next();
67 tdb 1.4 if(dataKey.startsWith(keyPrefix)) {
68 ajm 1.3 if(!services.contains(dataKey)) {
69     String serviceType = "";
70 tdb 1.4
71 ajm 1.3 // pos is after "packet.services."
72 tdb 1.4 int pos = keyPrefix.length();
73 ajm 1.3 while (dataKey.charAt(pos) != '.') {
74     serviceType = serviceType + dataKey.charAt(pos);
75     pos++;
76     }
77 tdb 1.4
78     // add the service to our list, with the packet data
79     services.add(keyPrefix + serviceType + ".attributes.status");
80     String status = packet.getParam(keyPrefix + serviceType + ".attributes.status");
81    
82     services.add(keyPrefix + serviceType + ".attributes.message");
83     String message = packet.getParam(keyPrefix + serviceType + ".attributes.message");
84    
85 ajm 1.3 // *** now process this service ***
86 tdb 1.4
87 ajm 1.3 // check if we've seen this service before on a previous run
88     // if not, we need to create a register for it
89     if(!serviceRegisters.containsKey(serviceType)) {
90     serviceRegisters.put(serviceType, new Register(source, _name));
91     }
92 tdb 1.4
93 ajm 1.3 // get the register for this service
94     Register reg = (Register) serviceRegisters.get(serviceType);
95 tdb 1.4
96    
97 ajm 1.3 // as we don't really have a threshold for services,
98     // a temporary fix is to use the status value as a threshold.
99     // after all, a 0 status is a NORMAL threshold ;)
100     int newThreshold = 0;
101     try {
102     newThreshold = Integer.parseInt(status);
103     } catch (NumberFormatException e) {
104     _logger.write(this.toString(), Logger.WARNING, "Received heartbeat from "+source+" with bad service information: "+e);
105     // don't try to continue and go try the next service
106     break;
107     }
108 tdb 1.4
109 ajm 1.3 // say which service it was
110     String attributeName = serviceType + " service";
111     String displayValue = "";
112     if (newThreshold == 0) {
113     displayValue = "RUNNING";
114     } else {
115     displayValue = "FAILED";
116 ajm 1.1 }
117 tdb 1.4
118 ajm 1.3 processAlert(newThreshold, attributeName, reg, source, displayValue);
119 ajm 1.1 }
120     }
121     }
122     }
123    
124     /**
125     * Overrides the {@link java.lang.Object#toString() Object.toString()}
126     * method to provide clean logging (every class should have this).
127     *
128 tdb 1.2 * This uses the uk.org.iscream.util.NameFormat class
129 ajm 1.1 * to format the toString()
130     *
131     * @return the name of this class and its CVS revision
132     */
133     public String toString() {
134     return FormatName.getName(
135     _name,
136     getClass().getName(),
137     REVISION);
138     }
139    
140     /**
141     * return the String representation of what the monitor does
142     */
143     public String getDescription(){
144     return DESC;
145     }
146    
147     //---PRIVATE METHODS---
148    
149     //---ACCESSOR/MUTATOR METHODS---
150 tdb 1.4
151     /**
152     * Returns a reference to a specific Queue for this
153     * monitor. This Queue returns only the data packets
154     * (based on type) that we want too look at.
155     *
156     * @return a reference to a Queue
157     */
158 ajm 1.3 protected Queue getQueue() {
159     return MonitorManager.getInstance().getHeartbeatQueue();
160     }
161 ajm 1.1
162     //---ATTRIBUTES---
163    
164     /**
165     * This is the friendly identifier of the
166     * component this class is running in.
167     * eg, a Filter may be called "filter1",
168     * If this class does not have an owning
169     * component, a name from the configuration
170     * can be placed here. This name could also
171     * be changed to null for utility classes.
172     */
173     private String _name = "Services";
174    
175     /**
176 tdb 1.4 * A HashMap of Registers (or groups of Registers), one
177     * for each host we're monitoring.
178 ajm 1.1 */
179     private HashMap _hosts = new HashMap();
180    
181     //---STATIC ATTRIBUTES---
182    
183     }