ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/host/c++/SysMon.cpp
Revision: 1.5
Committed: Fri Mar 23 15:02:12 2001 UTC (23 years, 6 months ago) by ab11
Branch: MAIN
Changes since 1.4: +0 -1 lines
Log Message:
Tidied up the debug level so it is more informative.

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