1 |
tdb |
1.6 |
/* |
2 |
|
|
* i-scream central monitoring system |
3 |
|
|
* Copyright (C) 2000-2002 i-scream |
4 |
|
|
* |
5 |
|
|
* This program is free software; you can redistribute it and/or |
6 |
|
|
* modify it under the terms of the GNU General Public License |
7 |
|
|
* as published by the Free Software Foundation; either version 2 |
8 |
|
|
* of the License, or (at your option) any later version. |
9 |
|
|
* |
10 |
|
|
* This program is distributed in the hope that it will be useful, |
11 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 |
|
|
* GNU General Public License for more details. |
14 |
|
|
* |
15 |
|
|
* You should have received a copy of the GNU General Public License |
16 |
|
|
* along with this program; if not, write to the Free Software |
17 |
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
18 |
|
|
*/ |
19 |
|
|
|
20 |
tdb |
1.1 |
//---PACKAGE DECLARATION--- |
21 |
tdb |
1.5 |
package uk.org.iscream.cms.server.filter; |
22 |
tdb |
1.1 |
|
23 |
|
|
//---IMPORTS--- |
24 |
|
|
import java.net.*; |
25 |
|
|
import java.io.*; |
26 |
|
|
|
27 |
|
|
/** |
28 |
|
|
* A skeleton class for Service Checks to extend. |
29 |
|
|
* |
30 |
tdb |
1.6 |
* @author $Author: tdb $ |
31 |
|
|
* @version $Id: ServiceCheckSkeleton.java,v 1.5 2001/05/29 17:02:35 tdb Exp $ |
32 |
tdb |
1.1 |
*/ |
33 |
|
|
public abstract class ServiceCheckSkeleton implements PluginServiceCheck { |
34 |
|
|
|
35 |
|
|
//---FINAL ATTRIBUTES--- |
36 |
|
|
|
37 |
|
|
//---STATIC METHODS--- |
38 |
|
|
|
39 |
|
|
//---CONSTRUCTORS--- |
40 |
|
|
|
41 |
|
|
//---PUBLIC METHODS--- |
42 |
|
|
|
43 |
|
|
/** |
44 |
|
|
* Runs a service check on a given hostname. |
45 |
|
|
* |
46 |
|
|
* @param hostname The hostname to check |
47 |
|
|
*/ |
48 |
|
|
public abstract String runServiceCheck(String hostname); |
49 |
|
|
|
50 |
|
|
/** |
51 |
|
|
* Checks if a service is returning a valid response, and |
52 |
|
|
* constructs suitable XML for inclusion in a packet. |
53 |
|
|
* |
54 |
|
|
* @param hostname The hostname to query |
55 |
|
|
* @param port The port to query |
56 |
|
|
* @param expectedSubString The String to look for in the response |
57 |
|
|
* @param XMLTag The name of the tag to be put in the XML |
58 |
|
|
* @return The String of XML with the response information |
59 |
|
|
*/ |
60 |
|
|
protected String checkService(String hostname, int port, String expectedSubString, String XMLTag) { |
61 |
|
|
String message, status; |
62 |
|
|
try { |
63 |
|
|
// connect and get response |
64 |
|
|
String response = connectToService(hostname, port); |
65 |
|
|
// check if the expected text is in the response |
66 |
|
|
if(response.indexOf(expectedSubString) != -1) { |
67 |
|
|
status = "0"; |
68 |
|
|
message = "Good Header received: "+response; |
69 |
|
|
} |
70 |
|
|
else { |
71 |
|
|
status = "1"; |
72 |
|
|
message = "Bad Header received: "+response; |
73 |
|
|
} |
74 |
|
|
} catch (IOException e) { |
75 |
|
|
// connect failed |
76 |
|
|
status = "1"; |
77 |
|
|
message = "Service check failed to establish connection to host: " + e.getMessage(); |
78 |
|
|
} |
79 |
|
|
return "<"+XMLTag+" status=\"" + status + "\" message=\"" + message + "\"></"+XMLTag+">"; |
80 |
|
|
} |
81 |
|
|
|
82 |
|
|
/** |
83 |
|
|
* Connects to a service and returns the first line of text. |
84 |
|
|
* |
85 |
|
|
* @param hostname The hostname of the service to check |
86 |
|
|
* @param port The port of the service to check |
87 |
|
|
* @return The first line of text given |
88 |
|
|
* @throws IOException if the connection fails for some reason |
89 |
|
|
*/ |
90 |
|
|
protected String connectToService(String hostname, int port) throws IOException { |
91 |
|
|
// connect |
92 |
tdb |
1.2 |
Socket socket = new Socket(hostname, port); |
93 |
tdb |
1.1 |
BufferedReader socketIn = new BufferedReader(new InputStreamReader(socket.getInputStream())); |
94 |
|
|
// read first line |
95 |
|
|
String response = socketIn.readLine(); |
96 |
|
|
// close |
97 |
|
|
socketIn.close(); |
98 |
|
|
socket.close(); |
99 |
|
|
// return first line |
100 |
|
|
return response; |
101 |
|
|
} |
102 |
|
|
|
103 |
|
|
/** |
104 |
|
|
* Used to query the Service Check to see what it does. |
105 |
|
|
* |
106 |
|
|
* @return the String representation of what the Service Check does |
107 |
|
|
*/ |
108 |
tdb |
1.3 |
public abstract String getDescription(); |
109 |
tdb |
1.1 |
|
110 |
|
|
//---PRIVATE METHODS--- |
111 |
|
|
|
112 |
|
|
//---ACCESSOR/MUTATOR METHODS--- |
113 |
|
|
|
114 |
|
|
//---ATTRIBUTES--- |
115 |
|
|
|
116 |
|
|
//---STATIC ATTRIBUTES--- |
117 |
|
|
|
118 |
|
|
} |