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