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.11
Committed: Thu Jan 15 14:10:13 2004 UTC (20 years, 4 months ago) by tdb
Branch: MAIN
Changes since 1.10: +3 -3 lines
Log Message:
OK - I can still program Java, I just can't remember how this works :-)

File Contents

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