ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/host/c++/SysMon.cpp
Revision: 1.8
Committed: Tue Mar 27 00:04:30 2001 UTC (23 years, 8 months ago) by ab11
Branch: MAIN
CVS Tags: PROJECT_COMPLETION
Changes since 1.7: +46 -46 lines
Log Message:
tidied up and removed dead code.

File Contents

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