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 == 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 |
< |
int * end; |
69 |
< |
char *buffer; |
70 |
< |
// buffer = sinput.substr(found+1, sinput.length()); |
71 |
< |
double dbl = 0; // strtod((char *) sinput.substr(found+1, sinput.length()), &end); |
72 |
< |
if ( dbl == 0 ){ |
73 |
< |
// error? we aren't so sure. check the position |
74 |
< |
// of end. |
75 |
< |
if ( *end == sinput.length()-1 ){ |
76 |
< |
// was the end.. ok |
77 |
< |
|
78 |
< |
} // if |
79 |
< |
} else { |
80 |
< |
// must have worked! |
81 |
< |
double dbl2 = 0; //strtod(values[i], &end); |
82 |
< |
dbl2 += dbl; |
83 |
< |
char *buffer; |
84 |
< |
int precision = 5; |
85 |
< |
int decimal, sign; |
86 |
< |
buffer = ecvt (dbl2, precision, &decimal, &sign); |
87 |
< |
} // if |
88 |
< |
} // 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; |
89 |
> |
delete p; |
90 |
|
|
91 |
|
if ( debug == 1 ){ |
92 |
|
std::cout << "SysMon::collect::Parse from StatGrab finished\n"; |
93 |
|
} |
94 |
|
|
113 |
– |
// alert? |
114 |
– |
// return 1; |
115 |
– |
|
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 ){ |
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 ){ |
138 |
< |
// hard coding |
139 |
< |
while ( i < 10 ){ |
140 |
< |
finished = 0; |
141 |
< |
count = 0; |
142 |
< |
attr = ""; |
143 |
< |
while ( finished != 1 ){ |
144 |
< |
count++; |
145 |
< |
if ( count > 10 ){ |
146 |
< |
// don't do more than 10 times.. its probably not gonna work! |
147 |
< |
finished = 1; |
148 |
< |
} |
149 |
< |
|
150 |
< |
// do for each item |
151 |
< |
int where = titles[i].find(level,0); |
152 |
< |
|
153 |
< |
// where now contains the position of 'level' |
154 |
< |
if ( where != string::npos ){ |
155 |
< |
// we found it, now check the size to see if this is the final level |
156 |
< |
// grab the end bit a look for .'s |
157 |
< |
string end = titles[i].substr(level.length(), titles[i].length()-level.length() ); |
158 |
< |
|
159 |
< |
// now end should contain "something.something" etc |
160 |
< |
// look to find a . in there that means more to traverse |
161 |
< |
// but check for 'attributes' tags... |
162 |
< |
|
163 |
< |
where = end.find("attributes.",0); |
164 |
< |
if ( where != string::npos ){ |
165 |
< |
// is an attribute |
166 |
< |
// need to check that there are no more levels above this one |
167 |
< |
|
168 |
< |
|
169 |
< |
|
170 |
< |
/* |
171 |
< |
|
172 |
< |
// where points to the start of attributes |
173 |
< |
int start = 0; |
174 |
< |
if ( level.find(".",start) < where-1 ){ |
175 |
< |
start = level.find(".", start); |
176 |
< |
} // if |
177 |
< |
|
178 |
< |
// start points to the thing to apply the attributes to. |
179 |
< |
// need to take the level upto before this point |
180 |
< |
string needLevel = end.substr(0,start-2); |
181 |
< |
std::cout << "needlevel " << needLevel << endl; |
182 |
< |
|
183 |
< |
// now we need to find the level we are at |
184 |
< |
while (( needLevel.length() > 0 ) && (needLevel.find(".",0) != string::npos ) ){ |
185 |
< |
int got = needLevel.find(".",0); |
186 |
< |
if ( got != string::npos ){ |
187 |
< |
// we need to grab up until the . |
188 |
< |
xml.addNest(needLevel.substr(0,got)); |
189 |
< |
level += needLevel.substr(0,got); |
190 |
< |
level += "."; |
191 |
< |
std::cout << "Adding Nest " << needLevel.substr(0,got) << endl; |
192 |
< |
needLevel = needLevel.substr(got+1, needLevel.length()-(got+1)); |
193 |
< |
} // if |
194 |
< |
std::cout << "needlevel " << needLevel << endl; |
195 |
< |
} // while |
196 |
< |
|
197 |
< |
// add the final nest |
198 |
< |
xml.addNest(needLevel); |
199 |
< |
level += needLevel; |
200 |
< |
level += "."; |
201 |
< |
std::cout << "Adding Nest " << needLevel << endl; |
202 |
< |
|
203 |
< |
*/ |
204 |
< |
|
205 |
< |
// now write out the variable |
206 |
< |
xml.addElement(end, values[i]); |
207 |
< |
finished = 1; |
208 |
< |
|
209 |
< |
} else { |
210 |
< |
if ( where == 0 ){ |
211 |
< |
// this is something we care about. |
212 |
< |
// it has an attribute tab, and thus we want to add it |
213 |
< |
// to the tag one lower than this (hopefully level!) |
214 |
< |
|
215 |
< |
// attributes = attributes + mount |
216 |
< |
attributes += end.substr(where+11, end.length()-(where+11)); |
217 |
< |
// attributes = attributes + mount=" |
218 |
< |
attributes += "=\""; |
219 |
< |
// attributes = attributes + mount="/dev |
220 |
< |
attributes += values[i]; |
221 |
< |
// attributes = attributes + mount="/dev" |
222 |
< |
attributes += "\" "; |
223 |
< |
} else { |
224 |
< |
// need to check if we are the last element before |
225 |
< |
// an attribute tag, so we want to get up to this tag first |
226 |
< |
while ( end.find(".",0) != string::npos ){ |
227 |
< |
|
228 |
< |
where = end.find(".",0); |
229 |
< |
string newNest = end.substr(0,where); |
230 |
< |
xml.addNest(newNest); |
231 |
< |
level += newNest; |
232 |
< |
level += "."; |
233 |
< |
// redefine end |
234 |
< |
end = end.substr(where+1, end.length()-(where+1)); |
235 |
< |
} // while |
236 |
< |
|
237 |
< |
|
238 |
< |
xml.addElement(end, values[i]); |
239 |
< |
finished = 1; |
240 |
< |
} // if where == 0 |
241 |
< |
} // 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 |
< |
|
248 |
< |
// we need to move one back and try again, until we are all the way |
249 |
< |
// back to "packet." |
250 |
< |
// go about it by closing nests |
251 |
< |
|
252 |
< |
while ( level.length() > root.length() ){ |
253 |
< |
// need to close one nest and locate the next . from the end (hassle) |
254 |
< |
int start = 0; |
255 |
< |
if ( level.find(".",start) != string::npos ){ |
256 |
< |
start = level.find(".", start); |
257 |
< |
} // if |
258 |
< |
// should now have the last . |
259 |
< |
xml.closeNest(); |
260 |
< |
level = level.substr(0, start+1); |
261 |
< |
} |
262 |
< |
|
263 |
< |
} // if contains level |
264 |
< |
|
265 |
< |
} // while |
266 |
< |
i++; |
267 |
< |
} // 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 + 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 |
|
// return the string |
420 |
|
return xml.returnXML(); |
421 |
|
|