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.7
Committed: Tue May 21 16:47:16 2002 UTC (22 years ago) by tdb
Branch: MAIN
Changes since 1.6: +3 -2 lines
Log Message:
Added URL to GPL headers.

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     import uk.org.iscream.cms.server.util.*;
33     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.7 * @version $Id: Services__Monitor.java,v 1.6 2002/05/18 18:16:00 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.7 public final String REVISION = "$Revision: 1.6 $";
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     if (!_hosts.containsKey(source)) {
70     _hosts.put(source, new HashMap());
71     }
72    
73     HashMap serviceRegisters = (HashMap) _hosts.get(source);
74    
75     // a tempory holder for all the disk attributes we find
76     ArrayList services = new ArrayList();
77 tdb 1.4
78     // the prefix for keys relating to service checks
79     String keyPrefix = "packet.services.";
80    
81 ajm 1.3 // unfortunatly we need to check the whole packet
82     // to find the disks, and then get the data attributes
83     Set packetSet = packet.getSet();
84     Iterator i = packetSet.iterator();
85     while (i.hasNext()) {
86     String dataKey = (String) i.next();
87 tdb 1.4 if(dataKey.startsWith(keyPrefix)) {
88 ajm 1.3 if(!services.contains(dataKey)) {
89     String serviceType = "";
90 tdb 1.4
91 ajm 1.3 // pos is after "packet.services."
92 tdb 1.4 int pos = keyPrefix.length();
93 ajm 1.3 while (dataKey.charAt(pos) != '.') {
94     serviceType = serviceType + dataKey.charAt(pos);
95     pos++;
96     }
97 tdb 1.4
98     // add the service to our list, with the packet data
99     services.add(keyPrefix + serviceType + ".attributes.status");
100     String status = packet.getParam(keyPrefix + serviceType + ".attributes.status");
101    
102     services.add(keyPrefix + serviceType + ".attributes.message");
103     String message = packet.getParam(keyPrefix + serviceType + ".attributes.message");
104    
105 ajm 1.3 // *** now process this service ***
106 tdb 1.4
107 ajm 1.3 // check if we've seen this service before on a previous run
108     // if not, we need to create a register for it
109     if(!serviceRegisters.containsKey(serviceType)) {
110     serviceRegisters.put(serviceType, new Register(source, _name));
111     }
112 tdb 1.4
113 ajm 1.3 // get the register for this service
114     Register reg = (Register) serviceRegisters.get(serviceType);
115 tdb 1.4
116    
117 ajm 1.3 // as we don't really have a threshold for services,
118     // a temporary fix is to use the status value as a threshold.
119     // after all, a 0 status is a NORMAL threshold ;)
120     int newThreshold = 0;
121     try {
122     newThreshold = Integer.parseInt(status);
123     } catch (NumberFormatException e) {
124     _logger.write(this.toString(), Logger.WARNING, "Received heartbeat from "+source+" with bad service information: "+e);
125     // don't try to continue and go try the next service
126     break;
127     }
128 tdb 1.4
129 ajm 1.3 // say which service it was
130     String attributeName = serviceType + " service";
131     String displayValue = "";
132     if (newThreshold == 0) {
133     displayValue = "RUNNING";
134     } else {
135     displayValue = "FAILED";
136 ajm 1.1 }
137 tdb 1.4
138 ajm 1.3 processAlert(newThreshold, attributeName, reg, source, displayValue);
139 ajm 1.1 }
140     }
141     }
142     }
143    
144     /**
145     * Overrides the {@link java.lang.Object#toString() Object.toString()}
146     * method to provide clean logging (every class should have this).
147     *
148 tdb 1.5 * This uses the uk.org.iscream.cms.server.util.NameFormat class
149 ajm 1.1 * to format the toString()
150     *
151     * @return the name of this class and its CVS revision
152     */
153     public String toString() {
154     return FormatName.getName(
155     _name,
156     getClass().getName(),
157     REVISION);
158     }
159    
160     /**
161     * return the String representation of what the monitor does
162     */
163     public String getDescription(){
164     return DESC;
165     }
166    
167     //---PRIVATE METHODS---
168    
169     //---ACCESSOR/MUTATOR METHODS---
170 tdb 1.4
171     /**
172     * Returns a reference to a specific Queue for this
173     * monitor. This Queue returns only the data packets
174     * (based on type) that we want too look at.
175     *
176     * @return a reference to a Queue
177     */
178 ajm 1.3 protected Queue getQueue() {
179     return MonitorManager.getInstance().getHeartbeatQueue();
180     }
181 ajm 1.1
182     //---ATTRIBUTES---
183    
184     /**
185     * This is the friendly identifier of the
186     * component this class is running in.
187     * eg, a Filter may be called "filter1",
188     * If this class does not have an owning
189     * component, a name from the configuration
190     * can be placed here. This name could also
191     * be changed to null for utility classes.
192     */
193     private String _name = "Services";
194    
195     /**
196 tdb 1.4 * A HashMap of Registers (or groups of Registers), one
197     * for each host we're monitoring.
198 ajm 1.1 */
199     private HashMap _hosts = new HashMap();
200    
201     //---STATIC ATTRIBUTES---
202    
203     }