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.3 by ab11, Tue Mar 6 00:38:32 2001 UTC vs.
Revision 1.7 by ab11, Fri Mar 23 16:57:03 2001 UTC

# Line 16 | Line 16 | SysMon::SysMon( Config config, int printDebug){
16          
17          // setup our arrays
18          titlepointer = 0;
19 <        for ( int i=0; i < 40; i++ ){
19 >        for ( int i=0; i < maxTitles; i++ ){
20                  titles[i] = "";
21                  values[i] = "";
22          }
# Line 35 | Line 35 | int SysMon::collect(){
35          
36          // collect the system data and place it in an array of strings.
37          // presume that the data is all formtted correctly.
38 <        char input[2048];
38 >        // char input[2048];
39          if ( debug == 1 ){
40                  std::cout << "SysMon::collect::ipipestream\n";
41          }
# Line 44 | Line 44 | int SysMon::collect(){
44          
45          // get the output into input from statgrab
46          string sinput;
47 <        while ( p->getLine(&sinput) ){
48 <                // find the first non-whitespace char in input
47 >        while ( p->getLine(&sinput) == 0 ){
48                  
49                  // locate the first white space
50                  int found = sinput.find(" ",0);
# Line 57 | Line 56 | int SysMon::collect(){
56                  } else {
57                          int gotTitle = 0;
58                          // now use the array of titles and values
59 <                        for ( int i = 0; i < 60; i++ ){
59 >                        for ( int i = 0; i < maxTitles; i++ ){
60                                  // check against the array value
61                                  if ( titles[i] == sinput.substr(0,found) ){
62                                          // is the one we are looking for.
63                                          if ( debug == 2 ){
64                                                  std::cout << "SysMon::collect::Found Value\n";
65 <                                                
66 <                                        }
65 >                                        } // if debug
66 >                                        
67                                          gotTitle = 1;
68                                          // now modify the data in the arrays if they are numbers
69 <                                        char * end;
71 <                                        char *buffer;
72 <                                        // buffer = sinput.substr(found+1, sinput.length());
73 <                                        double dbl = strtod( ((string) sinput.substr(found+1, sinput.length())).c_str(), &end);
74 <                                        if ( dbl == 0 ){
75 <                                                // error? we aren't so sure. check the position
76 <                                                // of end.
77 <                                                if ( *end == sinput.length()-1 ){
78 <                                                        // was the end.. ok
79 <                                                        // must have worked!
80 <                                                    double dbl2 = 0; //strtod(values[i], &end);
81 <                                                    dbl2 += dbl;
82 <                                                    char * buffer;
83 <                                                    int precision = 5;
84 <                                                    int decimal, sign;
85 <                                                    buffer = ecvt (dbl2, precision, &decimal, &sign);
86 <                                                } // if *end
87 <                                        } else {
88 <                                                // must have worked!
89 <                                                double dbl2 = 0; //strtod(values[i], &end);
90 <                                                dbl2 += dbl;
91 <                                                char * buffer;
92 <                                                int precision = 5;
93 <                                                int decimal, sign;
94 <                                                buffer = ecvt (dbl2, precision, &decimal, &sign);
95 <                                        } // if
96 <                                } // if
69 >                            } // if (titles[i])
70                          } // for
71 +                                        
72                          // 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 <                                                std::cout << "'" << sinput.substr(0,found) << "'\n";
77 >                                                std::cout << "'" << sinput.substr(0,found) << "' : ";
78                                                  std::cout << "'" <<  sinput.substr(found+1, sinput.length()) << "'\n";
79                                  }
80                                  titles[titlepointer] = sinput.substr(0,found);
# Line 118 | Line 92 | int SysMon::collect(){
92                  std::cout << "SysMon::collect::Parse from StatGrab finished\n";
93          }
94          
121        // alert?
122        // return 1;
123        
95          // return sucessful
96          return 0;
97          
98   } // collect
99  
100 + 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   string SysMon::getData(){
114          
115          if ( debug == 1 ){
# Line 135 | Line 119 | string SysMon::getData(){
119          // create an xml object
120          XMLFormatter xml = XMLFormatter();
121  
122 <        int i=0;
122 >    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          int count = 0;
160 <        int finished = 0;
161 <        string root = "packet.";
162 <        string level = root;
160 >        string currentLevel = "packet";
161 >        string currentLevels[10];
162 >        int currentLevelint = 0;
163 >        int retryCount = 0;
164          string attributes = "";
165 <        string attr = "";
166 <        while (  i < titlepointer ){
146 <            finished = 0;
147 <                count = 0;
148 <                attr = "";
149 <                while ( finished != 1 ){
150 <                        count++;
151 <                        if ( count > 10 ){
152 <                                // don't do more than 10 times.. its probably not gonna work!
153 <                                finished = 1;
154 <                        }
155 <                        
156 <                        // do for each item
157 <                        int where = titles[i].find(level,0);
158 <                        
159 <                        // where now contains the position of 'level'
160 <                        if ( where != string::npos ){
161 <                                // we found it, now check the size to see if this is the final level
162 <                                // grab the end bit a look for .'s
163 <                                string end = titles[i].substr(level.length(), titles[i].length()-level.length() );
164 <                                
165 <                                // now end should contain "something.something" etc
166 <                                // look to find a . in there that means more to traverse
167 <                                // but check for 'attributes' tags...
168 <                                
169 <                                where = end.find("attributes.",0);
170 <                                if ( where != string::npos ){
171 <                                        // is an attribute
172 <                                        // need to check that there are no more levels above this one
173 <                                        
174 <                                                                
175 <                                        
176 <                                        /*                                      
177 <                                        
178 <                                        // where points to the start of attributes
179 <                                        int start = 0;
180 <                                        if ( level.find(".",start) < where-1 ){
181 <                                                start = level.find(".", start);
182 <                                        } // if
183 <                                        
184 <                                        // start points to the thing to apply the attributes to.
185 <                                        // need to take the level upto before this point
186 <                                        string needLevel = end.substr(0,start-2);
187 <                                        std::cout << "needlevel " << needLevel << endl;                                
188 <                                        
189 <                                        // now we need to find the level we are at
190 <                                        while (( needLevel.length() > 0 ) && (needLevel.find(".",0) != string::npos ) ){
191 <                                                int got = needLevel.find(".",0);
192 <                                                if ( got != string::npos ){
193 <                                                        // we need to grab up until the .
194 <                                                        xml.addNest(needLevel.substr(0,got));
195 <                                                        level += needLevel.substr(0,got);
196 <                                                        level += ".";
197 <                                                        std::cout << "Adding Nest " << needLevel.substr(0,got) << endl;
198 <                                                        needLevel = needLevel.substr(got+1, needLevel.length()-(got+1));
199 <                                                } // if
200 <                                                std::cout << "needlevel " << needLevel << endl;
201 <                                        } // while
202 <                                        
203 <                                        // add the final nest
204 <                                        xml.addNest(needLevel);
205 <                                        level += needLevel;
206 <                                        level += ".";
207 <                                        std::cout << "Adding Nest " << needLevel << endl;
208 <                                        
209 <                                        */
210 <                                        
211 <                                        // now write out the variable
212 <                                        xml.addElement(end, values[i]);
213 <                                        finished = 1;                  
214 <                                        
215 <                                } else {
216 <                                        if ( where == 0 ){
217 <                                                // this is something we care about.
218 <                                                // it has an attribute tab, and thus we want to add it
219 <                                                // to the tag one lower than this (hopefully level!)
220 <                                                
221 <                                                // attributes = attributes + mount
222 <                                                attributes += end.substr(where+11, end.length()-(where+11));
223 <                                                // attributes = attributes + mount="
224 <                                                attributes += "=\"";
225 <                                                // attributes = attributes + mount="/dev
226 <                                                attributes += values[i];
227 <                                                // attributes = attributes + mount="/dev"
228 <                                                attributes += "\" ";
229 <                                        } else {
230 <                                                // need to check if we are the last element before
231 <                                                // an attribute tag, so we want to get up to this tag first
232 <                                                while ( end.find(".",0) != string::npos ){
233 <                                                
234 <                                                        where = end.find(".",0);
235 <                                                        string newNest = end.substr(0,where);
236 <                                                        xml.addNest(newNest);
237 <                                                        level += newNest;
238 <                                                        level += ".";
239 <                                                        // redefine end
240 <                                                        end = end.substr(where+1, end.length()-(where+1));
241 <                                                } // while      
242 <                                                
243 <                                                
244 <                                                xml.addElement(end, values[i]);
245 <                                                finished = 1;
246 <                                        } // if where == 0
247 <                                 } // if attribute tags
165 >        int isAttrib = 0;
166 >        string attributeLevel = "";
167          
168 <                                
168 >        // init that array quickly ;)
169 >        for ( int i=0; i< 10; i++ ){
170 >            currentLevels[i] = "";
171 >        } // for
172          
173 <                                        
174 <                        } else { // if doesn't contains level
175 <                        
254 <                                // we need to move one back and try again, until we are all the way
255 <                                // back to "packet."
256 <                                // go about it by closing nests
257 <                                
258 <                                while ( level.length() > root.length() ){
259 <                                        // need to close one nest and locate the next . from the end (hassle)
260 <                                        int start = 0;
261 <                                        if ( level.find(".",start) != string::npos ){
262 <                                                start = level.find(".", start);
263 <                                        } // if
264 <                                        // should now have the last .
265 <                                        xml.closeNest();
266 <                                        level = level.substr(0, start+1);
267 <                                }              
268 <                        
269 <                        } // if contains level
270 <                        
271 <                } // while
272 <                i++;
273 <        } // while
173 >        if ( debug == 1 ){
174 >                std::cout << "SysMon::getData::Starting while\n";
175 >        }
176          
177 <        
178 <        
179 <        
180 <        
181 <        
182 <                        
177 >        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 + attributes1" << 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 + attributes2" << 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 + attributes3" << 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 >                count++;            
342 >                if ( debug == 1 ){
343 >                    std::cout << "Adding Element" << endl;
344 >                }
345 >            }
346 >                    
347 >            } else {
348 >                // gotta change it    
349 >                
350 >                while( newLevelString.find(currentLevel) == string::npos ){
351 >                
352 >                if ( currentLevelint <= 0 ){
353 >                    break;
354 >                }
355 >                
356 >                // back up atleast one level...
357 >                xml.closeNest();
358 >                if ( debug == 1 ){
359 >                    std::cout << "Closing Nest" << endl;
360 >                }
361 >                // 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 >                }
385 >                
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 >        } // while
415 >
416 >    if ( debug == 1 ){
417 >                std::cout << "\n";
418 >        }              
419 >
420          // return the string
421          return xml.returnXML();
422          

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines