ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/host/c++/Host.cpp
Revision: 1.7
Committed: Tue May 21 16:47:11 2002 UTC (22 years, 6 months ago) by tdb
Branch: MAIN
Changes since 1.6: +1 -0 lines
Log Message:
Added URL to GPL headers.

File Contents

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