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, 4 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

# User Rev Content
1 tdb 1.6 /*
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 ab11 1.1 #include <iostream.h>
21     #include <unistd.h> // linux header for the sleep function
22 ab11 1.2 #include <stdlib.h> // itoa()
23 ab11 1.1 #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 ab11 1.2 int reloadConfig = 0;
38 ab11 1.1
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 ab11 1.2 Config config("raptor.ukc.ac.uk",4567,debug); // host, port, debug (0/1)
48 ab11 1.1
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 ab11 1.2
62 ab11 1.1 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 ab11 1.2 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 ab11 1.5 MaxFilterRetrys = 1;
87 ab11 1.2 } // if
88    
89    
90 ab11 1.5 // std::cout << "Configuring Alerter\n";
91 ab11 1.1 // 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 ab11 1.2 std::cout << "Starting Data Collection\n";
97 ab11 1.1 // work out which is closer, the next heartbeat or the next data collection
98 ab11 1.3 int localTime = time(NULL);
99 ab11 1.2 // std::cout << "#";
100 ab11 1.1 int nextUDPTime = localTime + UDPUpdateTime;
101     int nextAveragerTime = localTime + AveragerUpdateTime;
102     int nextTCPTime = localTime + TCPUpdateTime;
103 ab11 1.3 int sequence = 0;
104 ab11 1.2
105 ab11 1.1 int nextAction = 0;
106     while ( noErrors == 1){
107 ab11 1.2 // 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 ab11 1.3 // reloadConfig is done now
150     reloadConfig = 0;
151 ab11 1.2 } // if reloadconfig
152    
153    
154 ab11 1.3
155 ab11 1.2 localTime = time(NULL);
156 ab11 1.1 // 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 ab11 1.2 waitTime = nextUDPTime - localTime;
163 ab11 1.1 } else {
164     // set the heartbeat to be the next item
165     nextAction = 1;
166 ab11 1.2 waitTime = nextTCPTime - localTime;
167 ab11 1.1 } // if
168     } else {
169     if ( nextAveragerTime >= nextUDPTime ){
170     nextAction = 3;
171 ab11 1.2 waitTime = nextUDPTime - localTime;
172 ab11 1.1 } else {
173     // set grabstats to be in the next action
174     nextAction = 2;
175 ab11 1.2 waitTime = nextAveragerTime - localTime;
176 ab11 1.1 } // if
177     } // if
178    
179     // sleep until this time only if it is greater than 1!
180     if ( waitTime >= 1 ){
181 ab11 1.4 sleep(waitTime);
182 ab11 1.2
183 ab11 1.1 }
184    
185 ab11 1.2
186 ab11 1.1 localTime = time(NULL);
187 ab11 1.2 int con = -1;
188     int conCount = 0;
189    
190 ab11 1.1 // do the action
191     switch (nextAction){
192 ab11 1.2 case 1: std::cout << "Scheduled TCP Heartbeat Connection" << endl;
193 ab11 1.1 nextTCPTime = localTime + TCPUpdateTime;
194 ab11 1.2 // 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 ab11 1.1 break;
216 ab11 1.2 case 2: nextAveragerTime = localTime + AveragerUpdateTime;
217 ab11 1.4 /*
218 ab11 1.1 if ( sysMon.collect() > 0 ){
219     // suggests that there should be an alert
220 ab11 1.2 std::cout << "Alert Status" << endl;
221 ab11 1.4 }*/
222 ab11 1.1 break;
223 ab11 1.2 default: std::cout << "Scheduled UDP Connection" << endl;
224 ab11 1.4 sysMon.collect();
225 ab11 1.1 nextUDPTime = localTime + UDPUpdateTime;
226     // package up the system data
227 ab11 1.4 string host = config.getFQDN();
228 ab11 1.5 // string ip = (config.getSmallNet())->getHostIP();
229 ab11 1.2
230 ab11 1.3 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 ab11 1.5 string attributes = "type=\"data\" machine_name=\""+host+"\" ip=\"0.0.0.0\" date=\""+date+"\" seq_no=\""+seq+"\"";
239 ab11 1.1
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 ab11 1.4 // now clear the averaged data
245     sysMon.clearData();
246 ab11 1.3 sequence++;
247 ab11 1.1 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 ab11 1.2 std::cout << "prior to use of this program\n";
255     std::cout << "Oh and BTW, All you BASE are belong to us!\n";
256 ab11 1.1 std::cout << "Error Code: " << noErrors << "\n";
257    
258     return 0;
259    
260 ab11 1.3 } // main