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 |
#include <iostream.h> |
22 |
#include <unistd.h> // linux header for the sleep function |
23 |
#include <stdlib.h> // itoa() |
24 |
#include <time.h> |
25 |
#include "SysMon.h" |
26 |
// #include "Config.h" - redefinition error - turns out i don't need it |
27 |
#ifndef XMLFORMATTER_H /* Has the file been INCLUDE'd already? */ |
28 |
#define XMLFORMATTER_H yes |
29 |
#include "XMLFormatter.h" |
30 |
#endif |
31 |
|
32 |
using std::string; |
33 |
|
34 |
int main(){ |
35 |
|
36 |
// list of local variables |
37 |
int noErrors = 1; |
38 |
int reloadConfig = 0; |
39 |
|
40 |
// print some extra info.. |
41 |
int debug = 0; |
42 |
|
43 |
// create a new connection with the Config Host |
44 |
// values hard coded for the moment |
45 |
std::cout << "C Host starting..\n"; |
46 |
|
47 |
std::cout << "Attemping to load config\n"; |
48 |
Config config("raptor.ukc.ac.uk",4567,debug); // host, port, debug (0/1) |
49 |
|
50 |
std::cout << "Creating data collector\n"; |
51 |
SysMon sysMon(config, debug); |
52 |
|
53 |
std::cout << "Configuring UDPUpdateTime\n"; |
54 |
int UDPUpdateTime = config.getIntProperty("UDPUpdateTime"); |
55 |
if ( UDPUpdateTime == -1 ){ |
56 |
if ( debug == 1 ){ |
57 |
std::cout << "Using Default time!\n"; |
58 |
UDPUpdateTime = 5; |
59 |
} // if |
60 |
} // if |
61 |
|
62 |
|
63 |
std::cout << "Configuring TCPUpdateTime\n"; |
64 |
int TCPUpdateTime = config.getIntProperty("TCPUpdateTime"); |
65 |
if ( TCPUpdateTime == -1){ |
66 |
if ( debug == 1 ){ |
67 |
std::cout << "Using Default time!\n"; |
68 |
TCPUpdateTime = 10; |
69 |
} // if |
70 |
} // if |
71 |
|
72 |
std::cout << "Configuring AveragerUpdateTime\n"; |
73 |
int AveragerUpdateTime = config.getIntProperty("AVERAGERUpdateTime"); |
74 |
if ( AveragerUpdateTime == -1){ |
75 |
if ( debug == 1 ){ |
76 |
std::cout << "Using Default time!\n"; |
77 |
} // if |
78 |
AveragerUpdateTime = 2; |
79 |
} // if |
80 |
|
81 |
std::cout << "Configuring Filter Retry Tolerances\n"; |
82 |
int MaxFilterRetrys = config.getIntProperty("MAXTCPFilterRetries"); |
83 |
if ( MaxFilterRetrys == -1){ |
84 |
if ( debug == 1 ){ |
85 |
std::cout << "Using Default Tolerance\n"; |
86 |
} // if |
87 |
MaxFilterRetrys = 1; |
88 |
} // if |
89 |
|
90 |
|
91 |
// std::cout << "Configuring Alerter\n"; |
92 |
// Set the values at which the Alerter should kick in. and how many packets it should |
93 |
// send |
94 |
|
95 |
// now do the data collection |
96 |
|
97 |
std::cout << "Starting Data Collection\n"; |
98 |
// work out which is closer, the next heartbeat or the next data collection |
99 |
int localTime = time(NULL); |
100 |
// std::cout << "#"; |
101 |
int nextUDPTime = localTime + UDPUpdateTime; |
102 |
int nextAveragerTime = localTime + AveragerUpdateTime; |
103 |
int nextTCPTime = localTime + TCPUpdateTime; |
104 |
int sequence = 0; |
105 |
|
106 |
int nextAction = 0; |
107 |
while ( noErrors == 1){ |
108 |
// firstly check we haven't got to reload our config |
109 |
if ( reloadConfig == 1 ){ |
110 |
// reload the config by starting a new |
111 |
std::cout << "Forcing reload of Configuration\n"; |
112 |
config = Config("raptor.ukc.ac.uk",4567,debug); |
113 |
|
114 |
std::cout << "Configuring UDPUpdateTime\n"; |
115 |
int UDPUpdateTime = config.getIntProperty("UDPUpdateTime"); |
116 |
if ( UDPUpdateTime == -1 ){ |
117 |
if ( debug == 1 ){ |
118 |
std::cout << "Using Default time!\n"; |
119 |
UDPUpdateTime = 5; |
120 |
} // if |
121 |
} // if |
122 |
|
123 |
|
124 |
std::cout << "Configuring TCPUpdateTime\n"; |
125 |
int TCPUpdateTime = config.getIntProperty("TCPUpdateTime"); |
126 |
if ( TCPUpdateTime == -1){ |
127 |
if ( debug == 1 ){ |
128 |
std::cout << "Using Default time!\n"; |
129 |
TCPUpdateTime = 10; |
130 |
} // if |
131 |
} // if |
132 |
|
133 |
std::cout << "Configuring AveragerUpdateTime\n"; |
134 |
int AveragerUpdateTime = config.getIntProperty("AVERAGERUpdateTime"); |
135 |
if ( AveragerUpdateTime == -1){ |
136 |
if ( debug == 1 ){ |
137 |
std::cout << "Using Default time!\n"; |
138 |
} // if |
139 |
AveragerUpdateTime = 2; |
140 |
} // if |
141 |
|
142 |
std::cout << "Configuring Filter Retry Tolerances\n"; |
143 |
int MaxFilterRetrys = config.getIntProperty("MAXTCPFilterRetries"); |
144 |
if ( MaxFilterRetrys == -1){ |
145 |
if ( debug == 1 ){ |
146 |
std::cout << "Using Default Tolerance\n"; |
147 |
} // if |
148 |
MaxFilterRetrys = 5; |
149 |
} // if |
150 |
// reloadConfig is done now |
151 |
reloadConfig = 0; |
152 |
} // if reloadconfig |
153 |
|
154 |
|
155 |
|
156 |
localTime = time(NULL); |
157 |
// keep going while no non-fatal errors have occurred |
158 |
int waitTime = 0; |
159 |
|
160 |
if ( nextTCPTime <= nextAveragerTime ){ |
161 |
if ( nextTCPTime >= nextUDPTime ){ |
162 |
nextAction = 3; |
163 |
waitTime = nextUDPTime - localTime; |
164 |
} else { |
165 |
// set the heartbeat to be the next item |
166 |
nextAction = 1; |
167 |
waitTime = nextTCPTime - localTime; |
168 |
} // if |
169 |
} else { |
170 |
if ( nextAveragerTime >= nextUDPTime ){ |
171 |
nextAction = 3; |
172 |
waitTime = nextUDPTime - localTime; |
173 |
} else { |
174 |
// set grabstats to be in the next action |
175 |
nextAction = 2; |
176 |
waitTime = nextAveragerTime - localTime; |
177 |
} // if |
178 |
} // if |
179 |
|
180 |
// sleep until this time only if it is greater than 1! |
181 |
if ( waitTime >= 1 ){ |
182 |
sleep(waitTime); |
183 |
|
184 |
} |
185 |
|
186 |
|
187 |
localTime = time(NULL); |
188 |
int con = -1; |
189 |
int conCount = 0; |
190 |
|
191 |
// do the action |
192 |
switch (nextAction){ |
193 |
case 1: std::cout << "Scheduled TCP Heartbeat Connection" << endl; |
194 |
nextTCPTime = localTime + TCPUpdateTime; |
195 |
// connect to the filter - should do all the talking within config. |
196 |
con = (config.getSmallNet())->connectHeartBeat(config.getUDPFilterName(), config.getFilterPort()); |
197 |
conCount = 0; |
198 |
while (( con != 0 ) && ( conCount < MaxFilterRetrys )){ |
199 |
int con = (config.getSmallNet())->connectHeartBeat(config.getUDPFilterName(), config.getFilterPort()); |
200 |
conCount++; |
201 |
} |
202 |
// if con != 0, then we must reconnect to the main filter manager and |
203 |
// reload our config! |
204 |
if ( con == 0 ){ |
205 |
// run the chat script |
206 |
int result = config.chatHeartBeat(); |
207 |
if ( result != 0 ){ |
208 |
// gotta reload our config |
209 |
reloadConfig = 1; |
210 |
} // if |
211 |
} else { |
212 |
// set the config to be reloaded next time |
213 |
reloadConfig = 1; |
214 |
} // if con =!= 0 |
215 |
|
216 |
break; |
217 |
case 2: nextAveragerTime = localTime + AveragerUpdateTime; |
218 |
/* |
219 |
if ( sysMon.collect() > 0 ){ |
220 |
// suggests that there should be an alert |
221 |
std::cout << "Alert Status" << endl; |
222 |
}*/ |
223 |
break; |
224 |
default: std::cout << "Scheduled UDP Connection" << endl; |
225 |
sysMon.collect(); |
226 |
nextUDPTime = localTime + UDPUpdateTime; |
227 |
// package up the system data |
228 |
string host = config.getFQDN(); |
229 |
// string ip = (config.getSmallNet())->getHostIP(); |
230 |
|
231 |
string seq = ""; |
232 |
// char * buffer; |
233 |
char buffer[32]; |
234 |
sprintf(buffer,"%u", sequence); |
235 |
seq = buffer; |
236 |
string date = ""; |
237 |
sprintf(buffer,"%u", localTime); |
238 |
date = buffer; |
239 |
string attributes = "type=\"data\" machine_name=\""+host+"\" ip=\"0.0.0.0\" date=\""+date+"\" seq_no=\""+seq+"\""; |
240 |
|
241 |
XMLFormatter xml("packet",attributes); |
242 |
// put in the data |
243 |
xml.addString(sysMon.getData()); |
244 |
(config.getSmallNet())->sendUPDPacket(config.getUDPFilterName(), config.getUDPFilterPort(), xml.returnXML()); |
245 |
// now clear the averaged data |
246 |
sysMon.clearData(); |
247 |
sequence++; |
248 |
break; |
249 |
} // switch |
250 |
|
251 |
} // while |
252 |
|
253 |
std::cout << "A fatal error occurred which caused this program to quit\n"; |
254 |
std::cout << "It is suggested that all belly button lint should be removed\n"; |
255 |
std::cout << "prior to use of this program\n"; |
256 |
std::cout << "Oh and BTW, All you BASE are belong to us!\n"; |
257 |
std::cout << "Error Code: " << noErrors << "\n"; |
258 |
|
259 |
return 0; |
260 |
|
261 |
} // main |