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