| 1 |
/* |
| 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 |
#include "Config.h" |
| 21 |
|
| 22 |
Config::Config( string serverName, int serverPort, int printDebug ){ |
| 23 |
// the constructor for the class |
| 24 |
|
| 25 |
configConnectionRetryTime = 1; // one second |
| 26 |
debug = printDebug; |
| 27 |
MAX_CONNECTION_ATTEMPTS = 10; |
| 28 |
|
| 29 |
configName = serverName; |
| 30 |
configPort = serverPort; |
| 31 |
|
| 32 |
if ( debug == 1 ){ |
| 33 |
std::cout << "Config Constructor\n"; |
| 34 |
} // if |
| 35 |
|
| 36 |
|
| 37 |
// make a reference to smallnet |
| 38 |
if ( debug == 1 ){ |
| 39 |
std::cout << "Config::Constructor::Constructing SmallNet\n"; |
| 40 |
} |
| 41 |
net = new SmallNet(debug); |
| 42 |
|
| 43 |
// sod that we are using defined values now.. |
| 44 |
propertyNames[0] = "UDPUpdateTime"; |
| 45 |
propertyNames[1] = "TCPUpdateTime"; |
| 46 |
propertyNames[2] = "FilterRetryTime"; |
| 47 |
propertyNames[3] = "ConfigRetryTime"; |
| 48 |
propertyNames[4] = "AVERAGERUpdateTime"; |
| 49 |
propertyNames[5] = "MAXTCPFilterRetries"; |
| 50 |
numProperties = 5; |
| 51 |
|
| 52 |
if ( loadConfig() == 0 ){ |
| 53 |
// successful just continue |
| 54 |
if ( debug == 1 ){ |
| 55 |
std::cout << "Config::Constructor::loadConfig() Successful\n"; |
| 56 |
} // if |
| 57 |
} else { |
| 58 |
if ( debug == 1 ){ |
| 59 |
std::cout << "Config::Constructor::loadConfig() failed-retrying\n"; |
| 60 |
} // if |
| 61 |
|
| 62 |
// woops something went wrong. we'll just keep retrying |
| 63 |
// and increasing the time between trys |
| 64 |
int loadConfigResult = -1; |
| 65 |
|
| 66 |
// the following could cause problems if it is fed the |
| 67 |
// incorrect filter name and/or port it'll never stop trying |
| 68 |
int retrys = 1; // # of times we have tried to connect but failed.. |
| 69 |
while ( loadConfigResult != 0 ){ |
| 70 |
if ( retrys > MAX_CONNECTION_ATTEMPTS ){ |
| 71 |
std::cout << "Maxium number of retries reached, Quitting"; |
| 72 |
} // if |
| 73 |
// make the connection retry time greater |
| 74 |
configConnectionRetryTime = configConnectionRetryTime * 2; |
| 75 |
std::cout << "Waiting " << configConnectionRetryTime << " seconds before retrying\n"; |
| 76 |
sleep(configConnectionRetryTime); // seconds |
| 77 |
// increment the number of times we have tried to connect |
| 78 |
retrys++; |
| 79 |
loadConfigResult = loadConfig(); |
| 80 |
} // while |
| 81 |
} // if |
| 82 |
|
| 83 |
if ( debug == 1 ){ |
| 84 |
std::cout << "Config::Constructor - Finished!\n"; |
| 85 |
} // if |
| 86 |
|
| 87 |
} // Config |
| 88 |
|
| 89 |
int Config::loadConfig(){ |
| 90 |
// establishes a connection to the config manager (filter manager?) and |
| 91 |
// tries to load all the info which it needs, if it fails returns !0 |
| 92 |
|
| 93 |
if ( debug == 1 ){ |
| 94 |
std::cout << "Config::loadConfig()\n"; |
| 95 |
} // if |
| 96 |
|
| 97 |
// make a connection |
| 98 |
|
| 99 |
if ( debug == 1 ){ |
| 100 |
std::cout << "Config::loadConfig::establishing config connection\n"; |
| 101 |
} // if |
| 102 |
|
| 103 |
int established = establishConfig(); |
| 104 |
if ( established != 0 ){ |
| 105 |
destroyConfig(); |
| 106 |
return established; |
| 107 |
} // if |
| 108 |
|
| 109 |
// do the talk ;) |
| 110 |
|
| 111 |
if ( debug == 1 ){ |
| 112 |
std::cout << "Config::loadConfig::Processing chat script\n"; |
| 113 |
} // if |
| 114 |
|
| 115 |
int chat = chatConfig(); |
| 116 |
if ( chat != 0 ){ |
| 117 |
// and close the connection |
| 118 |
destroyConfig(); |
| 119 |
} // if |
| 120 |
|
| 121 |
// and close the connection |
| 122 |
if ( debug == 1 ){ |
| 123 |
std::cout << "Config::loadConfig::closing config connection\n"; |
| 124 |
} // if |
| 125 |
destroyConfig(); |
| 126 |
|
| 127 |
// return everything ok |
| 128 |
return 0; |
| 129 |
|
| 130 |
} // loadConfig |
| 131 |
|
| 132 |
int Config::establishHeartbeat(){ |
| 133 |
// just an interface onto smallnet |
| 134 |
if ( debug == 1 ){ |
| 135 |
std::cout << "Config::establishHeartbeat()\n"; |
| 136 |
} // if |
| 137 |
|
| 138 |
int establish = net->connectHeartBeat(configName, configPort); |
| 139 |
|
| 140 |
if ( debug == 1 ){ |
| 141 |
std::cout << "->" << establish << "\n"; |
| 142 |
} // if |
| 143 |
|
| 144 |
return establish; |
| 145 |
|
| 146 |
} // establishHeartBeat |
| 147 |
|
| 148 |
int Config::destroyHeartbeat(){ |
| 149 |
// just hooks into the smallnet interface |
| 150 |
if ( debug == 1 ){ |
| 151 |
std::cout << "Config::destroyHeartbeat()\n"; |
| 152 |
} // if |
| 153 |
|
| 154 |
net->closeHeartBeatConnection(); |
| 155 |
|
| 156 |
} // destroyHeartBeat |
| 157 |
|
| 158 |
int Config::chatHeartBeat(){ |
| 159 |
|
| 160 |
string response; |
| 161 |
|
| 162 |
response = net->heartBeatSend("HEARTBEAT"); |
| 163 |
if ( response != "OK" ){ |
| 164 |
// something went wrong |
| 165 |
if ( debug == 1 ){ |
| 166 |
std::cout << "Config::chatHeartBeat::ERROR in HEARTBEAT\n"; |
| 167 |
} // if |
| 168 |
return -1; |
| 169 |
} // if |
| 170 |
|
| 171 |
response = net->heartBeatSend("CONFIG"); |
| 172 |
if ( response != "OK" ){ |
| 173 |
// something went wrong |
| 174 |
if ( debug == 1 ){ |
| 175 |
std::cout << "Config::chatHeartBeat::ERROR in CONFIG\n"; |
| 176 |
} // if |
| 177 |
return -1; |
| 178 |
} // if |
| 179 |
|
| 180 |
response = net->heartBeatSend(fileList); |
| 181 |
if ( response != "OK" ){ |
| 182 |
// something went wrong |
| 183 |
if ( debug == 1 ){ |
| 184 |
std::cout << "Config::chatHeartBeat::ERROR in fileList\n"; |
| 185 |
} // if |
| 186 |
return -1; |
| 187 |
} // if |
| 188 |
|
| 189 |
response = net->heartBeatSend(lastModified); |
| 190 |
if ( response != "OK" ){ |
| 191 |
// something went wrong |
| 192 |
if ( debug == 1 ){ |
| 193 |
std::cout << "Config::chatHeartBeat::ERROR in lastModified\n"; |
| 194 |
} // if |
| 195 |
return -1; |
| 196 |
} // if |
| 197 |
|
| 198 |
response = net->heartBeatSend("ENDHEARTBEAT"); |
| 199 |
if ( response != "OK" ){ |
| 200 |
// something went wrong |
| 201 |
if ( debug == 1 ){ |
| 202 |
std::cout << "Config::chatHeartBeat::ERROR in ENDHEARTBEAT\n"; |
| 203 |
} // if |
| 204 |
return -1; |
| 205 |
} // if |
| 206 |
|
| 207 |
// seems ok to me |
| 208 |
return 0; |
| 209 |
|
| 210 |
} // chat Heart Beat |
| 211 |
|
| 212 |
|
| 213 |
int Config::establishConfig(){ |
| 214 |
// ask smallnet to make a connection to the filtermanager so we can get some config |
| 215 |
// details |
| 216 |
if ( debug == 1 ){ |
| 217 |
std::cout << "Config::establishConfig()\n"; |
| 218 |
} |
| 219 |
|
| 220 |
int response = net->connectConfig(configName, configPort); |
| 221 |
|
| 222 |
if ( debug == 1 ){ |
| 223 |
std::cout << "->" << response << "\n"; |
| 224 |
} // if |
| 225 |
|
| 226 |
return response; |
| 227 |
|
| 228 |
} // establishConfig |
| 229 |
|
| 230 |
|
| 231 |
int Config::chatConfig(){ |
| 232 |
// do some funky jibble.. ;) |
| 233 |
if ( debug == 1 ){ |
| 234 |
std::cout << "Config::chatConfig()\n"; |
| 235 |
} // if |
| 236 |
|
| 237 |
string response; |
| 238 |
|
| 239 |
// hard coeded for the moment |
| 240 |
if ( debug == 1 ){ |
| 241 |
std::cout << "Config::chatConfig::STARTCONFIG\n"; |
| 242 |
} // if |
| 243 |
|
| 244 |
response = net->heartBeatSend("STARTCONFIG"); |
| 245 |
if ( response != "OK" ){ |
| 246 |
// something went wrong |
| 247 |
if ( debug == 1 ){ |
| 248 |
std::cout << "Config::chatConfig::ERROR\n"; |
| 249 |
} // if |
| 250 |
return -1; |
| 251 |
} // if |
| 252 |
|
| 253 |
if ( debug == 1 ){ |
| 254 |
std::cout << "Config::chatConfig::LASTMODIFIED\n"; |
| 255 |
} // if |
| 256 |
lastModified = net->heartBeatSend("LASTMODIFIED"); |
| 257 |
if ( debug == 1 ){ |
| 258 |
std::cout << "Config::chatConfig::FILELIST\n"; |
| 259 |
} // if |
| 260 |
fileList = net->heartBeatSend("FILELIST"); |
| 261 |
|
| 262 |
fQDN = net->heartBeatSend("FQDN"); |
| 263 |
|
| 264 |
// now send the properties we want |
| 265 |
|
| 266 |
if ( debug == 1 ){ |
| 267 |
std::cout << "Config::chatConfig::requesting properties\n"; |
| 268 |
} // if |
| 269 |
if ( numProperties > 0 ){ |
| 270 |
// we actually need to look for some properties |
| 271 |
for ( int i=0; i < numProperties; i++ ){ |
| 272 |
// lets hope that this works! ;) |
| 273 |
if ( debug == 1 ){ |
| 274 |
std::cout << "Config::chatConfig::" << propertyNames[i] << "\n"; |
| 275 |
} // if |
| 276 |
propertyValues[i] = net->heartBeatSend(propertyNames[i]); |
| 277 |
} // for |
| 278 |
} // if |
| 279 |
|
| 280 |
if ( debug == 1 ){ |
| 281 |
std::cout << "Config::chatConfig::ENDCONFIG\n"; |
| 282 |
} // if |
| 283 |
response = net->heartBeatSend("ENDCONFIG"); |
| 284 |
// doesn't matter what it returns |
| 285 |
|
| 286 |
if ( debug == 1 ){ |
| 287 |
std::cout << "Config::chatConfig::FILTER\n"; |
| 288 |
} // if |
| 289 |
|
| 290 |
response = net->heartBeatSend("FILTER"); |
| 291 |
// now we need to pull repsonse apart to get the filter information out |
| 292 |
int first = response.find(";",0); |
| 293 |
int second = response.find(";",first+1); |
| 294 |
UDPFilterName = response.substr(0,first); |
| 295 |
UDPFilterPort = atoi( response.substr( first+1 ,second ).c_str()); |
| 296 |
TCPFilterPort = atoi(response.substr(second+1).c_str()); |
| 297 |
|
| 298 |
if ( debug == 1 ){ |
| 299 |
std::cout << "Config::chatConfig::END\n"; |
| 300 |
} // if |
| 301 |
response = net->heartBeatSend("END"); |
| 302 |
|
| 303 |
return 0; |
| 304 |
|
| 305 |
} // chatConfig |
| 306 |
|
| 307 |
int Config::destroyConfig(){ |
| 308 |
if ( debug == 1 ){ |
| 309 |
std::cout << "Config::destroyConfig()\n"; |
| 310 |
} // if |
| 311 |
|
| 312 |
net->closeConfigConnection(); |
| 313 |
|
| 314 |
} // destroyConfig |
| 315 |
|
| 316 |
string Config::getStrProperty(string propertyName){ |
| 317 |
|
| 318 |
|
| 319 |
if ( debug == 1 ){ |
| 320 |
std::cout << "Config::getProperty()\n"; |
| 321 |
} // if |
| 322 |
|
| 323 |
for ( int i=0; i<20; i++ ){ |
| 324 |
if ( propertyName == propertyNames[i] ){ |
| 325 |
if ( propertyValues[i] != "" ){ |
| 326 |
return propertyValues[i]; |
| 327 |
} // if |
| 328 |
} // if |
| 329 |
} // for |
| 330 |
|
| 331 |
// otherwise return ERROR |
| 332 |
return "ERROR"; |
| 333 |
|
| 334 |
} // getStrProperty |
| 335 |
|
| 336 |
int Config::getIntProperty( string propertyName ){ |
| 337 |
|
| 338 |
if ( debug == 1 ){ |
| 339 |
std::cout << "Config::getProperty()\n"; |
| 340 |
} // if |
| 341 |
|
| 342 |
string response = getStrProperty( propertyName ); |
| 343 |
if ( response == "ERROR" ){ |
| 344 |
return -1; |
| 345 |
} // if |
| 346 |
|
| 347 |
int i = atoi(response.c_str()); |
| 348 |
|
| 349 |
if ( i > 0 ){ |
| 350 |
// valid |
| 351 |
return i; |
| 352 |
} // if |
| 353 |
|
| 354 |
// otherwise return ERROR |
| 355 |
return -1; |
| 356 |
|
| 357 |
} // getIntProperty |