ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/host/c++/SysMon.cpp
(Generate patch)

Comparing projects/cms/source/host/c++/SysMon.cpp (file contents):
Revision 1.2 by ab11, Mon Mar 5 12:38:35 2001 UTC vs.
Revision 1.10 by tdb, Tue May 21 16:47:11 2002 UTC

# Line 1 | Line 1
1 + /*
2 + * i-scream central monitoring system
3 + * http://www.i-scream.org.uk
4 + * 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   #include "SysMon.h"
22  
23   SysMon::SysMon( Config config, int printDebug){
# Line 11 | Line 31 | SysMon::SysMon( Config config, int printDebug){
31          // setup some standard variables
32          sequence = 0; // no packets sent yet
33          
14        // get our values from config
15        
16        
34          // setup our arrays
35          titlepointer = 0;
36 <        for ( int i=0; i < 40; i++ ){
36 >        for ( int i=0; i < max_titles; i++ ){
37                  titles[i] = "";
38                  values[i] = "";
39          }
# Line 28 | Line 45 | SysMon::SysMon( Config config, int printDebug){
45   int SysMon::collect(){
46          if ( debug == 1 ){
47                  std::cout << "SysMon::collect()\n";
48 <        }
48 >        } // if
49          
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 <        char input[2048];
55 >
56          if ( debug == 1 ){
57                  std::cout << "SysMon::collect::ipipestream\n";
58          }
59 <        ipipestream p("statgrab.pl");
59 >
60 >        p = new SubPipe("./statgrab.pl");
61          
62          // get the output into input from statgrab
63 <        while ( p.getline (input, 2048) ){
64 <                // find the first non-whitespace char in input
65 <                string sinput = input;
63 >        string sinput;
64 >        while ( p->getLine(&sinput) == 0 ){
65 >                
66 >                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                  // 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 <                        }
79 >                        } // if
80                  } else {
81                          int gotTitle = 0;
82                          // now use the array of titles and values
83 <                        for ( int i = 0; i < 60; i++ ){
83 >                        for ( int i = 0; i < max_titles; i++ ){
84                                  // check against the array value
85                                  if ( titles[i] == sinput.substr(0,found) ){
86                                          // is the one we are looking for.
87                                          if ( debug == 2 ){
88                                                  std::cout << "SysMon::collect::Found Value\n";
89 <                                                
90 <                                        }
89 >                                        } // if debug
90 >                                        
91                                          gotTitle = 1;
92                                          // now modify the data in the arrays if they are numbers
93 <                                        int * end;
69 <                                        char *buffer;
70 <                                        // buffer = sinput.substr(found+1, sinput.length());
71 <                                        double dbl = 0; // strtod((char *) sinput.substr(found+1, sinput.length()), &end);
72 <                                        if ( dbl == 0 ){
73 <                                                // error? we aren't so sure. check the position
74 <                                                // of end.
75 <                                                if ( *end == sinput.length()-1 ){
76 <                                                        // was the end.. ok
77 <                                                        
78 <                                                } // if
79 <                                        } else {
80 <                                                // must have worked!
81 <                                                double dbl2 = 0; //strtod(values[i], &end);
82 <                                                dbl2 += dbl;
83 <                                                char *buffer;
84 <                                                int precision = 5;
85 <                                                int decimal, sign;
86 <                                                buffer = ecvt (dbl2, precision, &decimal, &sign);
87 <                                        } // if
88 <                                } // if
93 >                            } // if (titles[i])
94                          } // for
95 +                                        
96                          // did we find this title? if not add it
97                          if ( gotTitle == 0 ){
98                                  // didnt
99                                  if ( debug == 1 ){
100 <                                                std::cout << "SysMon::collect::Adding New Value\n";
101 <                                                std::cout << "'" << sinput.substr(0,found) << "'\n";
102 <                                                std::cout << "'" <<  sinput.substr(found+1, sinput.length()) << "'\n";
103 <                                }
100 >                                        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                                  titles[titlepointer] = sinput.substr(0,found);
105                                  values[titlepointer] = sinput.substr(found+1, sinput.length());
106                                  titlepointer++;
# Line 104 | Line 110 | int SysMon::collect(){
110                  
111          } // while
112          
113 <        // delete p;
113 >        // delete the pointer to free up memory
114 >        delete p;
115          
116          if ( debug == 1 ){
117                  std::cout << "SysMon::collect::Parse from StatGrab finished\n";
118 <        }
118 >        } // if
119          
113        // alert?
114        // return 1;
115        
120          // return sucessful
121          return 0;
122          
123   } // collect
124  
125 + void SysMon::clearData(){
126 +    
127 +    titlepointer = 0;
128 +        for ( int i=0; i < max_titles; i++ ){
129 +                titles[i] = "";
130 +                values[i] = "";
131 +        } // for
132 +    
133 +    return;
134 +    
135 + } // clearData
136 +
137 +
138   string SysMon::getData(){
139          
140          if ( debug == 1 ){
141                  std::cout << "SysMon::getData()\n";
142 <        }
142 >        } // if
143          
144          // create an xml object
145          XMLFormatter xml = XMLFormatter();
146  
147 <        int i=0;
147 >    if ( debug == 1 ){
148 >                std::cout << "SysMon::getData::Sorting " << titlepointer << " elements\n";
149 >        } // if
150 >
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 >    /* used to check the array has sorted correctly
174 >    for ( int i=0; i < titlepointer; i++ ){
175 >        std::cout << titles[i] << endl;    
176 >    }
177 >    */
178 >      
179 >        // work through each one of the titles in turn and
180 >        // work out their levels.
181 >        
182          int count = 0;
183 <        int finished = 0;
184 <        string root = "packet.";
185 <        string level = root;
183 >        string currentLevel = "packet";
184 >        string currentLevels[10];
185 >        int currentLevelint = 0;
186 >        int retryCount = 0;
187          string attributes = "";
188 <        string attr = "";
189 <        // while (  i < titlepointer ){
138 <        // hard coding
139 <        while ( i < 10 ){
140 <                finished = 0;
141 <                count = 0;
142 <                attr = "";
143 <                while ( finished != 1 ){
144 <                        count++;
145 <                        if ( count > 10 ){
146 <                                // don't do more than 10 times.. its probably not gonna work!
147 <                                finished = 1;
148 <                        }
149 <                        
150 <                        // do for each item
151 <                        int where = titles[i].find(level,0);
152 <                        
153 <                        // where now contains the position of 'level'
154 <                        if ( where != string::npos ){
155 <                                // we found it, now check the size to see if this is the final level
156 <                                // grab the end bit a look for .'s
157 <                                string end = titles[i].substr(level.length(), titles[i].length()-level.length() );
158 <                                
159 <                                // now end should contain "something.something" etc
160 <                                // look to find a . in there that means more to traverse
161 <                                // but check for 'attributes' tags...
162 <                                
163 <                                where = end.find("attributes.",0);
164 <                                if ( where != string::npos ){
165 <                                        // is an attribute
166 <                                        // need to check that there are no more levels above this one
167 <                                        
168 <                                                                
169 <                                        
170 <                                        /*                                      
171 <                                        
172 <                                        // where points to the start of attributes
173 <                                        int start = 0;
174 <                                        if ( level.find(".",start) < where-1 ){
175 <                                                start = level.find(".", start);
176 <                                        } // if
177 <                                        
178 <                                        // start points to the thing to apply the attributes to.
179 <                                        // need to take the level upto before this point
180 <                                        string needLevel = end.substr(0,start-2);
181 <                                        std::cout << "needlevel " << needLevel << endl;                                
182 <                                        
183 <                                        // now we need to find the level we are at
184 <                                        while (( needLevel.length() > 0 ) && (needLevel.find(".",0) != string::npos ) ){
185 <                                                int got = needLevel.find(".",0);
186 <                                                if ( got != string::npos ){
187 <                                                        // we need to grab up until the .
188 <                                                        xml.addNest(needLevel.substr(0,got));
189 <                                                        level += needLevel.substr(0,got);
190 <                                                        level += ".";
191 <                                                        std::cout << "Adding Nest " << needLevel.substr(0,got) << endl;
192 <                                                        needLevel = needLevel.substr(got+1, needLevel.length()-(got+1));
193 <                                                } // if
194 <                                                std::cout << "needlevel " << needLevel << endl;
195 <                                        } // while
196 <                                        
197 <                                        // add the final nest
198 <                                        xml.addNest(needLevel);
199 <                                        level += needLevel;
200 <                                        level += ".";
201 <                                        std::cout << "Adding Nest " << needLevel << endl;
202 <                                        
203 <                                        */
204 <                                        
205 <                                        // now write out the variable
206 <                                        xml.addElement(end, values[i]);
207 <                                        finished = 1;                  
208 <                                        
209 <                                } else {
210 <                                        if ( where == 0 ){
211 <                                                // this is something we care about.
212 <                                                // it has an attribute tab, and thus we want to add it
213 <                                                // to the tag one lower than this (hopefully level!)
214 <                                                
215 <                                                // attributes = attributes + mount
216 <                                                attributes += end.substr(where+11, end.length()-(where+11));
217 <                                                // attributes = attributes + mount="
218 <                                                attributes += "=\"";
219 <                                                // attributes = attributes + mount="/dev
220 <                                                attributes += values[i];
221 <                                                // attributes = attributes + mount="/dev"
222 <                                                attributes += "\" ";
223 <                                        } else {
224 <                                                // need to check if we are the last element before
225 <                                                // an attribute tag, so we want to get up to this tag first
226 <                                                while ( end.find(".",0) != string::npos ){
227 <                                                
228 <                                                        where = end.find(".",0);
229 <                                                        string newNest = end.substr(0,where);
230 <                                                        xml.addNest(newNest);
231 <                                                        level += newNest;
232 <                                                        level += ".";
233 <                                                        // redefine end
234 <                                                        end = end.substr(where+1, end.length()-(where+1));
235 <                                                } // while      
236 <                                                
237 <                                                
238 <                                                xml.addElement(end, values[i]);
239 <                                                finished = 1;
240 <                                        } // if where == 0
241 <                                 } // if attribute tags
188 >        int isAttrib = 0;
189 >        string attributeLevel = "";
190          
191 <                                
191 >        // init that array quickly ;)
192 >        for ( int i=0; i< 10; i++ ){
193 >            currentLevels[i] = "";
194 >        } // for
195          
196 <                                        
197 <                        } else { // if doesn't contains level
198 <                        
248 <                                // we need to move one back and try again, until we are all the way
249 <                                // back to "packet."
250 <                                // go about it by closing nests
251 <                                
252 <                                while ( level.length() > root.length() ){
253 <                                        // need to close one nest and locate the next . from the end (hassle)
254 <                                        int start = 0;
255 <                                        if ( level.find(".",start) != string::npos ){
256 <                                                start = level.find(".", start);
257 <                                        } // if
258 <                                        // should now have the last .
259 <                                        xml.closeNest();
260 <                                        level = level.substr(0, start+1);
261 <                                }              
262 <                        
263 <                        } // if contains level
264 <                        
265 <                } // while
266 <                i++;
267 <        } // while
196 >        if ( debug == 1 ){
197 >                std::cout << "SysMon::getData::Starting while\n";
198 >        } // if
199          
200 <        
201 <        
202 <        
203 <        
204 <        
205 <                        
200 >        while ( count <= titlepointer ){
201 >
202 >            if ( debug == 1 ){
203 >                    std::cout << "Processing: " << titles[count] << endl;
204 >            } // if
205 >            
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 >                } // if
249 >                        
250 >            } // 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 >                         } // if
272 >                        
273 >                    } // 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 >                    } // if
283 >                } // 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 >                    } // if
296 >                } // 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 >                                        std::cout << "Adding Element + attributes1" << endl;
309 >                                } // if
310 >                           attributeLevel = "";
311 >                           attributes = "";
312 >                           count++;
313 >                        } // if
314 >                        if ( count == titlepointer ){
315 >                                xml.addElement(attributeLevel,attributes,"");
316 >                            if ( debug == 1 ){
317 >                                        std::cout << "Adding Element + attributes2" << endl;
318 >                                } // if
319 >                            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 >                                std::cout << "Adding Element + attributes3" << endl;
334 >                        } // if
335 >                    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 >                    } // if
347 >                } // 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 >                    attributes += "=\"";
356 >                    attributes += values[count];        
357 >                    attributes += "\" ";
358 >                            
359 >                } else {
360 >                xml.addElement(levels[level],  values[count]);
361 >                count++;            
362 >                if ( debug == 1 ){
363 >                    std::cout << "Adding Element" << endl;
364 >                } // if
365 >            } // if
366 >                    
367 >            } else {
368 >                // gotta change it    
369 >                
370 >                while( newLevelString.find(currentLevel) == string::npos ){
371 >                
372 >                if ( currentLevelint <= 0 ){
373 >                    break;
374 >                } // if
375 >                
376 >                // back up atleast one level...
377 >                xml.closeNest();
378 >                if ( debug == 1 ){
379 >                    std::cout << "Closing Nest" << endl;
380 >                } // if
381 >                // 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 >                } // if
405 >                
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 >        } // while
435 >
436 >    if ( debug == 1 ){
437 >                std::cout << "\n";
438 >        } // if
439 >
440          // return the string
441          return xml.returnXML();
442          

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines