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