1 |
/* |
2 |
* i-scream central monitoring system |
3 |
* http://www.i-scream.org.uk |
4 |
* 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 |
//---PACKAGE DECLARATION--- |
22 |
package uk.org.iscream.cms.server.filter; |
23 |
|
24 |
//---IMPORTS--- |
25 |
import java.net.*; |
26 |
import java.io.*; |
27 |
|
28 |
/** |
29 |
* A skeleton class for Service Checks to extend. |
30 |
* |
31 |
* @author $Author: tdb $ |
32 |
* @version $Id: ServiceCheckSkeleton.java,v 1.6 2002/05/18 18:16:02 tdb Exp $ |
33 |
*/ |
34 |
public abstract class ServiceCheckSkeleton implements PluginServiceCheck { |
35 |
|
36 |
//---FINAL ATTRIBUTES--- |
37 |
|
38 |
//---STATIC METHODS--- |
39 |
|
40 |
//---CONSTRUCTORS--- |
41 |
|
42 |
//---PUBLIC METHODS--- |
43 |
|
44 |
/** |
45 |
* Runs a service check on a given hostname. |
46 |
* |
47 |
* @param hostname The hostname to check |
48 |
*/ |
49 |
public abstract String runServiceCheck(String hostname); |
50 |
|
51 |
/** |
52 |
* Checks if a service is returning a valid response, and |
53 |
* constructs suitable XML for inclusion in a packet. |
54 |
* |
55 |
* @param hostname The hostname to query |
56 |
* @param port The port to query |
57 |
* @param expectedSubString The String to look for in the response |
58 |
* @param XMLTag The name of the tag to be put in the XML |
59 |
* @return The String of XML with the response information |
60 |
*/ |
61 |
protected String checkService(String hostname, int port, String expectedSubString, String XMLTag) { |
62 |
String message, status; |
63 |
try { |
64 |
// connect and get response |
65 |
String response = connectToService(hostname, port); |
66 |
// check if the expected text is in the response |
67 |
if(response.indexOf(expectedSubString) != -1) { |
68 |
status = "0"; |
69 |
message = "Good Header received: "+response; |
70 |
} |
71 |
else { |
72 |
status = "1"; |
73 |
message = "Bad Header received: "+response; |
74 |
} |
75 |
} catch (IOException e) { |
76 |
// connect failed |
77 |
status = "1"; |
78 |
message = "Service check failed to establish connection to host: " + e.getMessage(); |
79 |
} |
80 |
return "<"+XMLTag+" status=\"" + status + "\" message=\"" + message + "\"></"+XMLTag+">"; |
81 |
} |
82 |
|
83 |
/** |
84 |
* Connects to a service and returns the first line of text. |
85 |
* |
86 |
* @param hostname The hostname of the service to check |
87 |
* @param port The port of the service to check |
88 |
* @return The first line of text given |
89 |
* @throws IOException if the connection fails for some reason |
90 |
*/ |
91 |
protected String connectToService(String hostname, int port) throws IOException { |
92 |
// connect |
93 |
Socket socket = new Socket(hostname, port); |
94 |
BufferedReader socketIn = new BufferedReader(new InputStreamReader(socket.getInputStream())); |
95 |
// read first line |
96 |
String response = socketIn.readLine(); |
97 |
// close |
98 |
socketIn.close(); |
99 |
socket.close(); |
100 |
// return first line |
101 |
return response; |
102 |
} |
103 |
|
104 |
/** |
105 |
* Used to query the Service Check to see what it does. |
106 |
* |
107 |
* @return the String representation of what the Service Check does |
108 |
*/ |
109 |
public abstract String getDescription(); |
110 |
|
111 |
//---PRIVATE METHODS--- |
112 |
|
113 |
//---ACCESSOR/MUTATOR METHODS--- |
114 |
|
115 |
//---ATTRIBUTES--- |
116 |
|
117 |
//---STATIC ATTRIBUTES--- |
118 |
|
119 |
} |