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

File Contents

# User Rev Content
1 tdb 1.9 /*
2     * i-scream central monitoring system
3 tdb 1.10 * http://www.i-scream.org.uk
4 tdb 1.9 * 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 "SysMon.h"
22    
23     SysMon::SysMon( Config config, int printDebug){
24     // debugging on?
25     debug = printDebug;
26    
27     if ( debug == 1 ){
28     std::cout << "SysMon::Constructor\n";
29     }
30    
31     // setup some standard variables
32     sequence = 0; // no packets sent yet
33    
34     // setup our arrays
35     titlepointer = 0;
36 ab11 1.8 for ( int i=0; i < max_titles; i++ ){
37 ab11 1.1 titles[i] = "";
38     values[i] = "";
39     }
40    
41     checks = 0;
42    
43     } // constructor
44    
45     int SysMon::collect(){
46     if ( debug == 1 ){
47     std::cout << "SysMon::collect()\n";
48 ab11 1.8 } // if
49 ab11 1.1
50     // say we have checked again
51     checks++;
52    
53     // collect the system data and place it in an array of strings.
54     // presume that the data is all formtted correctly.
55 ab11 1.8
56 ab11 1.1 if ( debug == 1 ){
57     std::cout << "SysMon::collect::ipipestream\n";
58     }
59 ab11 1.8
60     p = new SubPipe("./statgrab.pl");
61 ab11 1.1
62     // get the output into input from statgrab
63 ab11 1.3 string sinput;
64 ab11 1.4 while ( p->getLine(&sinput) == 0 ){
65 ab11 1.3
66 ab11 1.8 if ( titlepointer == max_titles ){
67     std::cout << "WARNING: Max Titles reached, skipping remaining" << endl;
68     std::cout << "Try recompiling with a value greater than the current (" << max_titles << ")" << endl;
69     titlepointer--;
70     break;
71     } // if
72    
73 ab11 1.1 // locate the first white space
74     int found = sinput.find(" ",0);
75     if ( found == string::npos ){
76     // error
77     if ( debug == 1 ){
78     std::cout << "SysMon::collect::Parse error from statgrab.pl\n";
79 ab11 1.8 } // if
80 ab11 1.1 } else {
81     int gotTitle = 0;
82     // now use the array of titles and values
83 ab11 1.8 for ( int i = 0; i < max_titles; i++ ){
84 ab11 1.1 // check against the array value
85     if ( titles[i] == sinput.substr(0,found) ){
86     // is the one we are looking for.
87 ab11 1.2 if ( debug == 2 ){
88 ab11 1.1 std::cout << "SysMon::collect::Found Value\n";
89 ab11 1.4 } // if debug
90    
91 ab11 1.2 gotTitle = 1;
92 ab11 1.1 // now modify the data in the arrays if they are numbers
93 ab11 1.4 } // if (titles[i])
94 ab11 1.1 } // for
95 ab11 1.4
96 ab11 1.1 // did we find this title? if not add it
97     if ( gotTitle == 0 ){
98     // didnt
99     if ( debug == 1 ){
100 ab11 1.8 std::cout << "SysMon::collect::Adding New Value\n";
101     std::cout << "'" << sinput.substr(0,found) << "' : ";
102     std::cout << "'" << sinput.substr(found+1, sinput.length()) << "'\n";
103     } // if
104 ab11 1.1 titles[titlepointer] = sinput.substr(0,found);
105     values[titlepointer] = sinput.substr(found+1, sinput.length());
106     titlepointer++;
107     } // if
108    
109     } // if (not) found
110    
111     } // while
112    
113 ab11 1.8 // delete the pointer to free up memory
114 ab11 1.3 delete p;
115 ab11 1.2
116 ab11 1.1 if ( debug == 1 ){
117     std::cout << "SysMon::collect::Parse from StatGrab finished\n";
118 ab11 1.8 } // if
119 ab11 1.1
120     // return sucessful
121     return 0;
122    
123     } // collect
124    
125 ab11 1.4 void SysMon::clearData(){
126    
127     titlepointer = 0;
128 ab11 1.8 for ( int i=0; i < max_titles; i++ ){
129 ab11 1.4 titles[i] = "";
130     values[i] = "";
131 ab11 1.8 } // for
132 ab11 1.4
133     return;
134    
135     } // clearData
136    
137    
138 ab11 1.1 string SysMon::getData(){
139    
140     if ( debug == 1 ){
141     std::cout << "SysMon::getData()\n";
142 ab11 1.8 } // if
143 ab11 1.1
144     // create an xml object
145     XMLFormatter xml = XMLFormatter();
146 ab11 1.2
147 ab11 1.4 if ( debug == 1 ){
148     std::cout << "SysMon::getData::Sorting " << titlepointer << " elements\n";
149 ab11 1.8 } // if
150 ab11 1.4
151     // firstly sort the data.
152     // simple bubble sort
153     int flag = 1;
154     while ( flag == 1 ){
155     flag = 0;
156     for ( int i=1; i<=titlepointer; i++ ){
157     if (titles[i-1].compare(titles[i]) > 0 ){
158     // swap them
159     string temp;
160     temp = titles[i-1];
161     titles[i-1] = titles[i];
162     titles[i] = temp;
163     // now do the values
164     temp = values[i-1];
165     values[i-1] = values[i];
166     values[i] = temp;
167     // say we have changed something
168     flag = 1;
169     } // if
170     } // for
171     } // while
172    
173 ab11 1.8 /* used to check the array has sorted correctly
174 ab11 1.4 for ( int i=0; i < titlepointer; i++ ){
175     std::cout << titles[i] << endl;
176     }
177 ab11 1.8 */
178 ab11 1.4
179     // work through each one of the titles in turn and
180     // work out their levels.
181    
182 ab11 1.2 int count = 0;
183 ab11 1.4 string currentLevel = "packet";
184     string currentLevels[10];
185     int currentLevelint = 0;
186     int retryCount = 0;
187 ab11 1.2 string attributes = "";
188 ab11 1.4 int isAttrib = 0;
189     string attributeLevel = "";
190    
191     // init that array quickly ;)
192     for ( int i=0; i< 10; i++ ){
193     currentLevels[i] = "";
194     } // for
195 ab11 1.2
196 ab11 1.4 if ( debug == 1 ){
197     std::cout << "SysMon::getData::Starting while\n";
198 ab11 1.8 } // if
199 ab11 1.2
200 ab11 1.4 while ( count <= titlepointer ){
201 ab11 1.8
202 ab11 1.4 if ( debug == 1 ){
203     std::cout << "Processing: " << titles[count] << endl;
204 ab11 1.8 } // if
205 ab11 1.4
206     retryCount++;
207    
208     currentLevel = "";
209     for ( int i=0; i<currentLevelint; i++ ){
210     if (( currentLevel != "" ) && ( i != currentLevelint )){
211     currentLevel += ".";
212     } // if
213     currentLevel += currentLevels[i];
214     } // for
215    
216     // set the title into a variable
217     string currentTitle = titles[count];
218    
219     // make an array of strings to hold the levels
220     string levels[10];
221     for ( int i=0; i< 10; i++ ){
222     levels[i] = "";
223     } // for
224     int level = 0;
225    
226    
227     // firstly find the first .
228     int dotPosition = currentTitle.find(".");
229    
230     while ( dotPosition != string::npos ){
231    
232     levels[level] = currentTitle.substr(0,dotPosition);
233    
234     // now remove this bit
235     currentTitle = currentTitle.substr(dotPosition+1);
236    
237     // now find the position of the next dot
238     dotPosition = currentTitle.find(".");
239    
240     // increment level etc
241     level++;
242    
243     // we already are at 'packet' level because of
244     // the outer packet tags, get rif of it , only
245     // first one mind!
246     if ( levels[0] == "packet" ){
247     level--;
248 ab11 1.8 } // if
249    
250 ab11 1.4 } // while dotPosition
251    
252     // now grab the value title
253     levels[level] = currentTitle;
254    
255     // now have the levels and the level depth.
256     // check it against the last one, reconstruct a
257     // level string
258    
259     // only do this if the 2nd to last string is not
260     // atrribute
261     string newLevelString = "";
262    
263     if ( level > 0 ){
264     if ( levels[level-1] != "attributes" ){
265     // say it isn't an attribute, but write out
266     // the attribute first if the last one was
267     if ( attributeLevel != "" ){
268     xml.addElement(attributeLevel,attributes,"");
269     if ( debug == 1 ){
270     std::cout << "Adding Element + attributes" << endl;
271 ab11 1.8 } // if
272    
273 ab11 1.4 } // if
274     isAttrib = 0;
275     attributeLevel = "";
276     attributes = "";
277    
278     for ( int i=0; i < level; i++ ){
279     newLevelString += levels[i];
280     if ( i != level-1 ){
281     newLevelString += ".";
282 ab11 1.8 } // if
283 ab11 1.4 } // for
284     } // if levels[level]
285    
286     else
287    
288     {
289     // we need to add to the attributes
290     level -= 2;
291     for ( int i=0; i < level; i++ ){
292     newLevelString += levels[i];
293     if ( i != level-1 ){
294     newLevelString += ".";
295 ab11 1.8 } // if
296 ab11 1.4 } // for
297    
298     isAttrib = 1;
299     if ( attributeLevel == "" ){
300     // set this attribute to be the level
301     attributeLevel = levels[level];
302     } else {
303     if ( levels[level] != attributeLevel ){
304     // arrg! its a different level!
305     if ( attributeLevel != levels[level] ){
306     xml.addElement(attributeLevel,attributes,"");
307     if ( debug == 1 ){
308 ab11 1.7 std::cout << "Adding Element + attributes1" << endl;
309 ab11 1.8 } // if
310 ab11 1.4 attributeLevel = "";
311     attributes = "";
312     count++;
313     } // if
314     if ( count == titlepointer ){
315     xml.addElement(attributeLevel,attributes,"");
316     if ( debug == 1 ){
317 ab11 1.7 std::cout << "Adding Element + attributes2" << endl;
318 ab11 1.8 } // if
319 ab11 1.4 attributeLevel = "";
320     attributes = "";
321     count++;
322     } // if
323     } // if
324     } // if else
325    
326     } // else attributes
327     } // if level > 1
328     else {
329     if ( count == titlepointer ){
330     if ( attributeLevel != "" ){
331     xml.addElement(attributeLevel,attributes,"");
332     if ( debug == 1 ){
333 ab11 1.7 std::cout << "Adding Element + attributes3" << endl;
334 ab11 1.8 } // if
335 ab11 1.4 attributeLevel = "";
336     attributes = "";
337     count++;
338     } // if
339     } // if
340    
341    
342     for ( int i=0; i < level; i++ ){
343     newLevelString += levels[i];
344     if ( i != level-1 ){
345     newLevelString += ".";
346 ab11 1.8 } // if
347 ab11 1.4 } // for
348     } // if level == 1
349    
350     // check if it is the same
351     if ( newLevelString == currentLevel ){
352     // it is the same level, just add this as another element
353     if ( isAttrib ){
354     attributes += levels[level+2];
355 ab11 1.6 attributes += "=\"";
356 ab11 1.4 attributes += values[count];
357 ab11 1.6 attributes += "\" ";
358 ab11 1.8
359 ab11 1.4 } else {
360     xml.addElement(levels[level], values[count]);
361 ab11 1.7 count++;
362 ab11 1.4 if ( debug == 1 ){
363     std::cout << "Adding Element" << endl;
364 ab11 1.8 } // if
365     } // if
366 ab11 1.7
367 ab11 1.4 } else {
368     // gotta change it
369    
370     while( newLevelString.find(currentLevel) == string::npos ){
371    
372     if ( currentLevelint <= 0 ){
373     break;
374 ab11 1.8 } // if
375 ab11 1.4
376     // back up atleast one level...
377     xml.closeNest();
378     if ( debug == 1 ){
379     std::cout << "Closing Nest" << endl;
380 ab11 1.8 } // if
381 ab11 1.4 // remove the last item
382     currentLevelint--;
383    
384     currentLevel = "";
385     for ( int i=0; i<currentLevelint; i++ ){
386     currentLevel += currentLevels[i];
387     if ( i != currentLevelint-1 ){
388     currentLevel += ".";
389     } // if
390     } // for
391    
392     } // while
393    
394    
395     while ( newLevelString != currentLevel ){
396    
397     // now got to a level which we like.
398     currentLevels[currentLevelint] = levels[currentLevelint];
399    
400     // add in one more level and restart the loop
401     xml.addNest(levels[currentLevelint]);
402     if ( debug == 1 ){
403     std::cout << "Adding nest " << levels[currentLevelint] << endl;
404 ab11 1.8 } // if
405 ab11 1.4
406     // set this to be the top level of the new level
407     currentLevelint++;
408    
409     currentLevel = "";
410     for ( int i=0; i<currentLevelint; i++ ){
411     currentLevel += currentLevels[i];
412     if ( i != currentLevelint-1 ){
413     currentLevel += ".";
414     } // if
415     } // for
416    
417     } // while
418    
419    
420     } // if
421    
422     if ( debug == 1 ){
423     std::cout << "Retrying Main While" << endl;
424     } // if
425    
426     if ( retryCount > 10 ){
427     count++;
428     if ( debug == 1 ){
429     std::cout << "Tried too many times" << endl;
430     } // if
431     retryCount = 0;
432     } // if
433    
434 ab11 1.2 } // while
435 ab11 1.4
436     if ( debug == 1 ){
437     std::cout << "\n";
438 ab11 1.8 } // if
439 ab11 1.4
440 ab11 1.1 // return the string
441     return xml.returnXML();
442    
443     } // getData