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 |
|
} |
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 |
|
} |
42 |
< |
ipipestream p("statgrab.pl"); |
42 |
> |
// ipipestream p("statgrab.pl"); |
43 |
> |
p = new SubPipe("statgrab.pl"); |
44 |
|
|
45 |
|
// get the output into input from statgrab |
46 |
< |
while ( p.getline (input, 2048) ){ |
47 |
< |
// find the first non-whitespace char in input |
48 |
< |
string sinput = input; |
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 ){ |
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 == 1 ){ |
63 |
> |
if ( debug == 2 ){ |
64 |
|
std::cout << "SysMon::collect::Found Value\n"; |
65 |
< |
gotTitle = 1; |
66 |
< |
} |
65 |
> |
} // if debug |
66 |
> |
|
67 |
> |
gotTitle = 1; |
68 |
|
// now modify the data in the arrays if they are numbers |
69 |
< |
int * end; |
68 |
< |
char *buffer; |
69 |
< |
// buffer = sinput.substr(found+1, sinput.length()); |
70 |
< |
double dbl = 0; // strtod((char *) sinput.substr(found+1, sinput.length()), &end); |
71 |
< |
if ( dbl == 0 ){ |
72 |
< |
// error? we aren't so sure. check the position |
73 |
< |
// of end. |
74 |
< |
if ( *end == sinput.length()-1 ){ |
75 |
< |
// was the end.. ok |
76 |
< |
|
77 |
< |
} // if |
78 |
< |
} else { |
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 |
87 |
< |
} // 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); |
86 |
|
|
87 |
|
} // while |
88 |
|
|
89 |
+ |
delete p; |
90 |
+ |
|
91 |
|
if ( debug == 1 ){ |
92 |
|
std::cout << "SysMon::collect::Parse from StatGrab finished\n"; |
93 |
|
} |
94 |
|
|
110 |
– |
// alert? |
111 |
– |
// return 1; |
112 |
– |
|
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 ){ |
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 |
< |
// spammage |
160 |
< |
// xml.addElement(titles[0],values[0]); // version |
161 |
< |
xml.addNest("os"); |
162 |
< |
xml.addElement("name",values[1]); // packet.os.name |
163 |
< |
xml.addElement("release",values[2]); // packet.os.release |
164 |
< |
xml.addElement("platform",values[3]); // packet.os.platform |
165 |
< |
xml.addElement("sysname",values[4]); // packet.os.sysname |
166 |
< |
xml.addElement("version",values[5]); // packet.os.version |
135 |
< |
xml.addElement("uptime",values[9]); // packet.os.uptime |
136 |
< |
xml.closeNest(); |
137 |
< |
xml.addNest("load"); |
138 |
< |
xml.addElement("load1",values[6]); // packet.load.load1 |
139 |
< |
xml.addElement("load5",values[7]); // packet.load.load5 |
140 |
< |
xml.addElement("load15",values[8]); // packet.load.load15 |
141 |
< |
xml.closeNest(); |
142 |
< |
xml.addNest("users"); |
143 |
< |
xml.addElement("count",values[10]); // packet.users.count |
144 |
< |
// xml.addElement("list",values[11]); // packet.users.list |
145 |
< |
// xml.addElement("list",values[11]); // packet.users.list |
146 |
< |
xml.closeNest(); |
147 |
< |
|
148 |
< |
|
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 |
< |
// xml. |
178 |
< |
|
179 |
< |
|
180 |
< |
// } // for |
181 |
< |
|
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 + attributes" << 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 + attributes" << 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 + attributes" << 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 |
> |
if ( debug == 1 ){ |
342 |
> |
std::cout << "Adding Element" << endl; |
343 |
> |
} |
344 |
> |
} |
345 |
> |
count++; |
346 |
> |
} else { |
347 |
> |
// gotta change it |
348 |
> |
|
349 |
> |
while( newLevelString.find(currentLevel) == string::npos ){ |
350 |
> |
|
351 |
> |
if ( currentLevelint <= 0 ){ |
352 |
> |
break; |
353 |
> |
} |
354 |
> |
|
355 |
> |
// back up atleast one level... |
356 |
> |
xml.closeNest(); |
357 |
> |
if ( debug == 1 ){ |
358 |
> |
std::cout << "Closing Nest" << endl; |
359 |
> |
} |
360 |
> |
// remove the last item |
361 |
> |
currentLevelint--; |
362 |
> |
|
363 |
> |
currentLevel = ""; |
364 |
> |
for ( int i=0; i<currentLevelint; i++ ){ |
365 |
> |
currentLevel += currentLevels[i]; |
366 |
> |
if ( i != currentLevelint-1 ){ |
367 |
> |
currentLevel += "."; |
368 |
> |
} // if |
369 |
> |
} // for |
370 |
> |
|
371 |
> |
} // while |
372 |
> |
|
373 |
> |
|
374 |
> |
while ( newLevelString != currentLevel ){ |
375 |
> |
|
376 |
> |
// now got to a level which we like. |
377 |
> |
currentLevels[currentLevelint] = levels[currentLevelint]; |
378 |
> |
|
379 |
> |
// add in one more level and restart the loop |
380 |
> |
xml.addNest(levels[currentLevelint]); |
381 |
> |
if ( debug == 1 ){ |
382 |
> |
std::cout << "Adding nest " << levels[currentLevelint] << endl; |
383 |
> |
} |
384 |
> |
|
385 |
> |
// set this to be the top level of the new level |
386 |
> |
currentLevelint++; |
387 |
> |
|
388 |
> |
currentLevel = ""; |
389 |
> |
for ( int i=0; i<currentLevelint; i++ ){ |
390 |
> |
currentLevel += currentLevels[i]; |
391 |
> |
if ( i != currentLevelint-1 ){ |
392 |
> |
currentLevel += "."; |
393 |
> |
} // if |
394 |
> |
} // for |
395 |
> |
|
396 |
> |
} // while |
397 |
> |
|
398 |
> |
|
399 |
> |
} // if |
400 |
> |
|
401 |
> |
if ( debug == 1 ){ |
402 |
> |
std::cout << "Retrying Main While" << endl; |
403 |
> |
} // if |
404 |
> |
|
405 |
> |
if ( retryCount > 10 ){ |
406 |
> |
count++; |
407 |
> |
if ( debug == 1 ){ |
408 |
> |
std::cout << "Tried too many times" << endl; |
409 |
> |
} // if |
410 |
> |
retryCount = 0; |
411 |
> |
} // if |
412 |
> |
|
413 |
> |
} // while |
414 |
> |
|
415 |
> |
if ( debug == 1 ){ |
416 |
> |
std::cout << "\n"; |
417 |
> |
} |
418 |
> |
|
419 |
> |
std::cout << xml.returnXML() << endl; |
420 |
|
// return the string |
421 |
|
return xml.returnXML(); |
422 |
|
|