1 |
ab11 |
1.1 |
#include <iostream.h> |
2 |
|
|
#include <unistd.h> // linux header for the sleep function |
3 |
|
|
#include <time.h> |
4 |
|
|
#include "SysMon.h" |
5 |
|
|
// #include "Config.h" - redefinition error - turns out i don't need it |
6 |
|
|
#ifndef XMLFORMATTER_H /* Has the file been INCLUDE'd already? */ |
7 |
|
|
#define XMLFORMATTER_H yes |
8 |
|
|
#include "XMLFormatter.h" |
9 |
|
|
#endif |
10 |
|
|
|
11 |
|
|
using std::string; |
12 |
|
|
|
13 |
|
|
int main(){ |
14 |
|
|
|
15 |
|
|
// list of local variables |
16 |
|
|
int noErrors = 1; |
17 |
|
|
|
18 |
|
|
// print some extra info.. |
19 |
|
|
int debug = 0; |
20 |
|
|
|
21 |
|
|
// create a new connection with the Config Host |
22 |
|
|
// values hard coded for the moment |
23 |
|
|
std::cout << "C Host starting..\n"; |
24 |
|
|
|
25 |
|
|
std::cout << "Attemping to load config\n"; |
26 |
|
|
Config config("killigrew.ukc.ac.uk",4567,debug); // host, port, debug (0/1) |
27 |
|
|
|
28 |
|
|
std::cout << "Creating data collector\n"; |
29 |
|
|
SysMon sysMon(config, debug); |
30 |
|
|
|
31 |
|
|
std::cout << "Configuring UDPUpdateTime\n"; |
32 |
|
|
int UDPUpdateTime = config.getIntProperty("UDPUpdateTime"); |
33 |
|
|
if ( UDPUpdateTime == -1 ){ |
34 |
|
|
if ( debug == 1 ){ |
35 |
|
|
std::cout << "Using Default time!\n"; |
36 |
|
|
UDPUpdateTime = 5; |
37 |
|
|
} // if |
38 |
|
|
} // if |
39 |
|
|
|
40 |
|
|
std::cout << "Configuring TCPUpdateTime\n"; |
41 |
|
|
int TCPUpdateTime = config.getIntProperty("TCPUpdateTime"); |
42 |
|
|
if ( TCPUpdateTime == -1){ |
43 |
|
|
if ( debug == 1 ){ |
44 |
|
|
std::cout << "Using Default time!\n"; |
45 |
|
|
TCPUpdateTime = 10; |
46 |
|
|
} // if |
47 |
|
|
} // if |
48 |
|
|
|
49 |
|
|
std::cout << "Configuring AveragerUpdateTime\n"; |
50 |
|
|
int AveragerUpdateTime = config.getIntProperty("AVERAGERUpdateTime"); |
51 |
|
|
if ( AveragerUpdateTime == -1){ |
52 |
|
|
if ( debug == 1 ){ |
53 |
|
|
std::cout << "Using Default time!\n"; |
54 |
|
|
} // if |
55 |
|
|
AveragerUpdateTime = 2; |
56 |
|
|
} // if |
57 |
|
|
|
58 |
|
|
std::cout << "Configuring Alerter\n"; |
59 |
|
|
// Set the values at which the Alerter should kick in. and how many packets it should |
60 |
|
|
// send |
61 |
|
|
|
62 |
|
|
// now do the data collection |
63 |
|
|
|
64 |
|
|
// work out which is closer, the next heartbeat or the next data collection |
65 |
|
|
int localTime = time(NULL); |
66 |
|
|
std::cout << "#"; |
67 |
|
|
int nextUDPTime = localTime + UDPUpdateTime; |
68 |
|
|
int nextAveragerTime = localTime + AveragerUpdateTime; |
69 |
|
|
int nextTCPTime = localTime + TCPUpdateTime; |
70 |
|
|
int sequence = 0; |
71 |
|
|
|
72 |
|
|
int nextAction = 0; |
73 |
|
|
while ( noErrors == 1){ |
74 |
|
|
std::cout << "."; |
75 |
|
|
// keep going while no non-fatal errors have occurred |
76 |
|
|
int waitTime = 0; |
77 |
|
|
|
78 |
|
|
if ( nextTCPTime <= nextAveragerTime ){ |
79 |
|
|
std::cout << "-"; |
80 |
|
|
if ( nextTCPTime >= nextUDPTime ){ |
81 |
|
|
std::cout << "}"; |
82 |
|
|
nextAction = 3; |
83 |
|
|
waitTime = nextUDPTime - time(NULL); |
84 |
|
|
} else { |
85 |
|
|
// set the heartbeat to be the next item |
86 |
|
|
std::cout << "{"; |
87 |
|
|
nextAction = 1; |
88 |
|
|
waitTime = nextTCPTime - time(NULL); |
89 |
|
|
} // if |
90 |
|
|
} else { |
91 |
|
|
std::cout << "+"; |
92 |
|
|
if ( nextAveragerTime >= nextUDPTime ){ |
93 |
|
|
std::cout << "["; |
94 |
|
|
nextAction = 3; |
95 |
|
|
waitTime = nextUDPTime - time(NULL); |
96 |
|
|
} else { |
97 |
|
|
// set grabstats to be in the next action |
98 |
|
|
std::cout << "]"; |
99 |
|
|
nextAction = 2; |
100 |
|
|
waitTime = nextAveragerTime - time(NULL); |
101 |
|
|
} // if |
102 |
|
|
} // if |
103 |
|
|
|
104 |
|
|
// sleep until this time only if it is greater than 1! |
105 |
|
|
if ( waitTime >= 1 ){ |
106 |
|
|
// need to output \n to std::cout or the wait is infinate!!!! ????? |
107 |
|
|
std::cout << "_\n"; |
108 |
|
|
sleep(waitTime); /// crashes here |
109 |
|
|
std::cout << "_"; |
110 |
|
|
} |
111 |
|
|
|
112 |
|
|
std::cout << "~"; |
113 |
|
|
localTime = time(NULL); |
114 |
|
|
// do the action |
115 |
|
|
switch (nextAction){ |
116 |
|
|
case 1: std::cout << "T"; |
117 |
|
|
nextTCPTime = localTime + TCPUpdateTime; |
118 |
|
|
// SmallNet * net = config.getSmallNet(); |
119 |
|
|
// net->connectHeartBeat(config.getFilterName(),config.getFilterPort()); |
120 |
|
|
break; |
121 |
|
|
case 2: std::cout << "A"; |
122 |
|
|
nextAveragerTime = localTime + AveragerUpdateTime; |
123 |
|
|
if ( sysMon.collect() > 0 ){ |
124 |
|
|
// suggests that there should be an alert |
125 |
|
|
|
126 |
|
|
} |
127 |
|
|
break; |
128 |
|
|
default: std::cout << "U"; |
129 |
|
|
nextUDPTime = localTime + UDPUpdateTime; |
130 |
|
|
// package up the system data |
131 |
|
|
/// lovely HARD CODING!!!! |
132 |
|
|
string h1 = "raptor.ukc.ac.uk"; |
133 |
|
|
string ip1 = "129.12.4.232"; |
134 |
|
|
string date1 = "00000001"; |
135 |
|
|
string seq = "-64773929"; |
136 |
|
|
|
137 |
|
|
string attributes = "type=\"data\" machine_name=\""+h1+"\" ip=\""+ip1+"\" date=\""+date1+"\" seq_no=\""+seq+"\""; |
138 |
|
|
|
139 |
|
|
XMLFormatter xml("packet",attributes); |
140 |
|
|
// put in the data |
141 |
|
|
xml.addString(sysMon.getData()); |
142 |
|
|
(config.getSmallNet())->sendUPDPacket(config.getUDPFilterName(), config.getUDPFilterPort(), xml.returnXML()); |
143 |
|
|
|
144 |
|
|
break; |
145 |
|
|
} // switch |
146 |
|
|
|
147 |
|
|
} // while |
148 |
|
|
|
149 |
|
|
std::cout << "A fatal error occurred which caused this program to quit\n"; |
150 |
|
|
std::cout << "It is suggested that all belly button lint should be removed\n"; |
151 |
|
|
std::cout << "prior to use of this program"; |
152 |
|
|
std::cout << "Error Code: " << noErrors << "\n"; |
153 |
|
|
|
154 |
|
|
return 0; |
155 |
|
|
|
156 |
|
|
} // main |