1 |
tdb |
1.30 |
/* |
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.25 |
package uk.org.iscream.cms.server.filter; |
22 |
tdb |
1.1 |
|
23 |
|
|
//---IMPORTS--- |
24 |
tdb |
1.25 |
import uk.org.iscream.cms.server.core.*; |
25 |
|
|
import uk.org.iscream.cms.server.filter.*; |
26 |
|
|
import uk.org.iscream.cms.server.componentmanager.*; |
27 |
tdb |
1.1 |
import java.net.Socket; |
28 |
|
|
import java.io.*; |
29 |
tdb |
1.27 |
import java.util.Random; |
30 |
tdb |
1.25 |
import uk.org.iscream.cms.server.util.*; |
31 |
tdb |
1.1 |
|
32 |
|
|
/** |
33 |
ajm |
1.10 |
* This provides Host heartbeat functionality |
34 |
tdb |
1.1 |
* |
35 |
tdb |
1.26 |
* @author $Author: tdb $ |
36 |
tdb |
1.30 |
* @version $Id: TCPReaderInit.java,v 1.29 2002/05/12 15:49:50 tdb Exp $ |
37 |
tdb |
1.1 |
*/ |
38 |
|
|
class TCPReaderInit extends Thread { |
39 |
|
|
|
40 |
|
|
//---FINAL ATTRIBUTES--- |
41 |
|
|
|
42 |
|
|
/** |
43 |
|
|
* The current CVS revision of this class |
44 |
|
|
*/ |
45 |
tdb |
1.30 |
public final String REVISION = "$Revision: 1.29 $"; |
46 |
tdb |
1.1 |
|
47 |
|
|
//---STATIC METHODS--- |
48 |
|
|
|
49 |
|
|
//---CONSTRUCTORS--- |
50 |
tdb |
1.13 |
|
51 |
|
|
/** |
52 |
|
|
* Construct a new TCPReaderInit. |
53 |
|
|
* |
54 |
|
|
* @param socket the Socket to which the host is connected |
55 |
|
|
* @param queue the Queue to which we'll add data |
56 |
|
|
* @throws IOException if something goes badly wrong |
57 |
|
|
*/ |
58 |
tdb |
1.11 |
public TCPReaderInit(Socket socket, Queue queue) throws IOException { |
59 |
tdb |
1.21 |
// set the Thread name |
60 |
|
|
setName("filter.TCPReaderInit"); |
61 |
|
|
|
62 |
tdb |
1.1 |
_socket = socket; |
63 |
tdb |
1.11 |
_queue = queue; |
64 |
tdb |
1.13 |
// setup the reader & writer |
65 |
tdb |
1.1 |
_socketIn = new BufferedReader(new InputStreamReader(_socket.getInputStream())); |
66 |
tdb |
1.15 |
_socketOut = new PrintWriter(_socket.getOutputStream(), true); |
67 |
tdb |
1.8 |
_logger.write(toString(), Logger.SYSINIT, "created"); |
68 |
tdb |
1.1 |
} |
69 |
|
|
|
70 |
|
|
//---PUBLIC METHODS--- |
71 |
tdb |
1.13 |
|
72 |
|
|
/** |
73 |
|
|
* Main run method. Will communicate with the host, inform it |
74 |
|
|
* if any updates to it's configuration are needed, and send |
75 |
|
|
* a heartbeat packet into the system. |
76 |
|
|
*/ |
77 |
tdb |
1.1 |
public void run() { |
78 |
|
|
try { |
79 |
tdb |
1.27 |
// get an instance of the KeyManager |
80 |
|
|
KeyManager keyman = KeyManager.getInstance(); |
81 |
|
|
|
82 |
|
|
// get some information about the host |
83 |
|
|
String hostname = _socket.getInetAddress().getHostName().toLowerCase(); |
84 |
|
|
String ipadd = _socket.getInetAddress().getHostAddress(); |
85 |
tdb |
1.2 |
|
86 |
tdb |
1.22 |
// try for HEARTBEAT |
87 |
tdb |
1.23 |
getInBound("HEARTBEAT"); |
88 |
tdb |
1.22 |
_socketOut.println("OK"); |
89 |
tdb |
1.3 |
|
90 |
tdb |
1.27 |
// look for a command: |
91 |
|
|
// CONFIG - to check config |
92 |
|
|
// KEY - to get the key |
93 |
|
|
// ENDHEARTBEAT - to finish |
94 |
|
|
String cmd = getInBound(); |
95 |
|
|
while(!cmd.equals("ENDHEARTBEAT")) { |
96 |
|
|
if(cmd.equals("CONFIG")) { |
97 |
|
|
// respond to CONFIG |
98 |
|
|
_socketOut.println("OK"); |
99 |
|
|
|
100 |
|
|
// try for {filelist} |
101 |
|
|
String filelist = getInBound(); |
102 |
|
|
_socketOut.println("OK"); |
103 |
|
|
|
104 |
|
|
// try for {lastModified} |
105 |
|
|
String lastModified = getInBound(); |
106 |
|
|
// check to see if a config update has happen |
107 |
|
|
boolean newConfig = _configManager.isModified(filelist, Long.parseLong(lastModified)); |
108 |
|
|
if(newConfig) { |
109 |
|
|
// new config ! |
110 |
|
|
_socketOut.println("ERROR"); |
111 |
|
|
} |
112 |
|
|
else { |
113 |
|
|
// nothing has changed |
114 |
|
|
_socketOut.println("OK"); |
115 |
|
|
} |
116 |
|
|
} |
117 |
|
|
else if(cmd.equals("KEY")) { |
118 |
|
|
// repsond to KEY |
119 |
|
|
// generate a key |
120 |
|
|
String key = keyman.genKey(); |
121 |
tdb |
1.29 |
// send key to host |
122 |
|
|
_socketOut.println(key); |
123 |
tdb |
1.27 |
// add it to the key manager |
124 |
|
|
keyman.addKey(hostname, key); |
125 |
|
|
} |
126 |
|
|
else { |
127 |
|
|
_socketOut.println("ERROR"); |
128 |
|
|
} |
129 |
|
|
// get the next command |
130 |
|
|
cmd = getInBound(); |
131 |
tdb |
1.3 |
} |
132 |
|
|
|
133 |
tdb |
1.27 |
// respond to ENDHEARTBEAT |
134 |
tdb |
1.22 |
_socketOut.println("OK"); |
135 |
|
|
|
136 |
|
|
// work out some information for our heartbeat packet |
137 |
|
|
String date = new Long(System.currentTimeMillis()/((long) 1000)).toString(); //seconds |
138 |
tdb |
1.6 |
|
139 |
ajm |
1.16 |
// run the service checks for this host |
140 |
ajm |
1.17 |
_logger.write(toString(), Logger.DEBUG, "Running service checks"); |
141 |
ajm |
1.16 |
String checks = PluginServiceCheckManager.getInstance().runServiceChecks(hostname); |
142 |
|
|
|
143 |
|
|
// build the heartbeat packet |
144 |
|
|
String xml = "<packet type=\"heartbeat\" machine_name=\""+hostname+"\" date=\""+date+"\" ip=\""+ipadd+"\">" + checks + "</packet>"; |
145 |
|
|
|
146 |
|
|
// get it to be sent on |
147 |
tdb |
1.11 |
_queue.add(xml); |
148 |
tdb |
1.1 |
|
149 |
|
|
} catch (Exception e) { |
150 |
tdb |
1.20 |
_logger.write(toString(), Logger.ERROR, "ERROR: " + e); |
151 |
tdb |
1.1 |
} |
152 |
|
|
|
153 |
|
|
// Disconnect streams & socket |
154 |
|
|
try { |
155 |
|
|
_socketIn.close(); |
156 |
|
|
_socketOut.close(); |
157 |
|
|
_socket.close(); |
158 |
|
|
} catch (IOException e) { |
159 |
tdb |
1.8 |
_logger.write(toString(), Logger.ERROR, "exception on socket close"); |
160 |
tdb |
1.1 |
} |
161 |
tdb |
1.14 |
_logger.write(toString(), Logger.DEBUG, "finished"); |
162 |
tdb |
1.1 |
} |
163 |
|
|
|
164 |
|
|
/** |
165 |
|
|
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
166 |
|
|
* method to provide clean logging (every class should have this). |
167 |
|
|
* |
168 |
tdb |
1.25 |
* This uses the uk.org.iscream.cms.server.util.NameFormat class |
169 |
ajm |
1.10 |
* to format the toString() |
170 |
|
|
* |
171 |
tdb |
1.1 |
* @return the name of this class and its CVS revision |
172 |
|
|
*/ |
173 |
|
|
public String toString() { |
174 |
ajm |
1.10 |
return FormatName.getName( |
175 |
|
|
_name, |
176 |
|
|
getClass().getName(), |
177 |
|
|
REVISION); |
178 |
tdb |
1.1 |
} |
179 |
|
|
|
180 |
|
|
//---PRIVATE METHODS--- |
181 |
tdb |
1.22 |
|
182 |
|
|
private String getInBound(String expected) throws IOException { |
183 |
|
|
// grab the input |
184 |
|
|
String inBound = getInBound(); |
185 |
|
|
// check if it's what we're expecting |
186 |
|
|
if(!inBound.equals(expected)) { |
187 |
tdb |
1.26 |
throw new IOException("protocol error from "+_socket.getInetAddress().getHostName()+" - expected:"+expected+" got:" + inBound); |
188 |
tdb |
1.22 |
} |
189 |
|
|
// it should be ok then |
190 |
|
|
return inBound; |
191 |
|
|
} |
192 |
|
|
|
193 |
|
|
private String getInBound() throws IOException { |
194 |
|
|
// grab the input |
195 |
|
|
String inBound = _socketIn.readLine(); |
196 |
|
|
// check for null's, likely disconnection |
197 |
|
|
if(inBound == null) { |
198 |
|
|
throw new IOException("got null from host, maybe it died"); |
199 |
|
|
} |
200 |
|
|
// it's a valid message it seems |
201 |
|
|
return inBound; |
202 |
|
|
} |
203 |
tdb |
1.27 |
|
204 |
tdb |
1.1 |
//---ACCESSOR/MUTATOR METHODS--- |
205 |
|
|
|
206 |
|
|
//---ATTRIBUTES--- |
207 |
|
|
|
208 |
ajm |
1.10 |
/** |
209 |
|
|
* A reference to the configuration manager |
210 |
|
|
*/ |
211 |
tdb |
1.8 |
ConfigurationManager _configManager = ReferenceManager.getInstance().getCM(); |
212 |
ajm |
1.10 |
|
213 |
|
|
/** |
214 |
|
|
* This is the friendly identifier of the |
215 |
|
|
* component this class is running in. |
216 |
|
|
* eg, a Filter may be called "filter1", |
217 |
|
|
* If this class does not have an owning |
218 |
|
|
* component, a name from the configuration |
219 |
|
|
* can be placed here. This name could also |
220 |
|
|
* be changed to null for utility classes. |
221 |
|
|
*/ |
222 |
|
|
private String _name = FilterMain.NAME; |
223 |
|
|
|
224 |
|
|
/** |
225 |
|
|
* This holds a reference to the |
226 |
|
|
* system logger that is being used. |
227 |
|
|
*/ |
228 |
|
|
private Logger _logger = ReferenceManager.getInstance().getLogger(); |
229 |
|
|
|
230 |
|
|
/** |
231 |
|
|
* The socket we are talking on |
232 |
|
|
*/ |
233 |
tdb |
1.1 |
Socket _socket; |
234 |
ajm |
1.10 |
|
235 |
|
|
/** |
236 |
|
|
* The input from the socket |
237 |
|
|
*/ |
238 |
tdb |
1.1 |
BufferedReader _socketIn; |
239 |
ajm |
1.10 |
|
240 |
|
|
/** |
241 |
|
|
* The output from the socket |
242 |
|
|
*/ |
243 |
tdb |
1.1 |
PrintWriter _socketOut; |
244 |
ajm |
1.10 |
|
245 |
|
|
/** |
246 |
tdb |
1.11 |
* A reference to our Queue |
247 |
ajm |
1.10 |
*/ |
248 |
tdb |
1.11 |
Queue _queue; |
249 |
tdb |
1.27 |
|
250 |
tdb |
1.1 |
//---STATIC ATTRIBUTES--- |
251 |
|
|
|
252 |
|
|
} |