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 |
for ( int i=0; i < maxTitles; i++ ){ |
20 |
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 |
// char input[2048]; |
39 |
if ( debug == 1 ){ |
40 |
std::cout << "SysMon::collect::ipipestream\n"; |
41 |
} |
42 |
// ipipestream p("statgrab.pl"); |
43 |
p = new SubPipe("statgrab.pl"); |
44 |
|
45 |
// get the output into input from statgrab |
46 |
string sinput; |
47 |
while ( p->getLine(&sinput) == 0 ){ |
48 |
|
49 |
// 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 |
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 |
} // if debug |
66 |
|
67 |
gotTitle = 1; |
68 |
// now modify the data in the arrays if they are numbers |
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) << "' : "; |
78 |
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 |
delete p; |
90 |
|
91 |
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 |
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 ){ |
116 |
std::cout << "SysMon::getData()\n"; |
117 |
} |
118 |
|
119 |
// create an xml object |
120 |
XMLFormatter xml = XMLFormatter(); |
121 |
|
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 |
string currentLevel = "packet"; |
161 |
string currentLevels[10]; |
162 |
int currentLevelint = 0; |
163 |
int retryCount = 0; |
164 |
string attributes = ""; |
165 |
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 |
|
173 |
if ( debug == 1 ){ |
174 |
std::cout << "SysMon::getData::Starting while\n"; |
175 |
} |
176 |
|
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 |
|
423 |
} // getData |