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){ |
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 |
for ( int i=0; i < max_titles; i++ ){ |
37 |
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 |
} // 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 |
|
56 |
if ( debug == 1 ){ |
57 |
std::cout << "SysMon::collect::ipipestream\n"; |
58 |
} |
59 |
|
60 |
p = new SubPipe("./statgrab.pl"); |
61 |
|
62 |
// get the output into input from statgrab |
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 |
} // if |
80 |
} else { |
81 |
int gotTitle = 0; |
82 |
// now use the array of titles and values |
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 |
} // if debug |
90 |
|
91 |
gotTitle = 1; |
92 |
// now modify the data in the arrays if they are numbers |
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) << "' : "; |
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++; |
107 |
} // if |
108 |
|
109 |
} // if (not) found |
110 |
|
111 |
} // while |
112 |
|
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 |
} // if |
119 |
|
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 |
} // if |
143 |
|
144 |
// create an xml object |
145 |
XMLFormatter xml = XMLFormatter(); |
146 |
|
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 |
string currentLevel = "packet"; |
184 |
string currentLevels[10]; |
185 |
int currentLevelint = 0; |
186 |
int retryCount = 0; |
187 |
string attributes = ""; |
188 |
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 |
|
196 |
if ( debug == 1 ){ |
197 |
std::cout << "SysMon::getData::Starting while\n"; |
198 |
} // if |
199 |
|
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 |
|
443 |
} // getData |