ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/host/c++/Host.cpp
Revision: 1.6
Committed: Sat May 18 18:15:56 2002 UTC (22 years, 5 months ago) by tdb
Branch: MAIN
Changes since 1.5: +19 -0 lines
Log Message:
i-scream is now licensed under the GPL. I've added the GPL headers to every
source file, and put a full copy of the license in the appropriate places.
I think I've covered everything. This is going to be a mad commit ;)

File Contents

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