1 |
ab11 |
1.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 < 40; 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 |
ab11 |
1.3 |
// ipipestream p("statgrab.pl"); |
43 |
|
|
p = new SubPipe("statgrab.pl"); |
44 |
ab11 |
1.1 |
|
45 |
|
|
// get the output into input from statgrab |
46 |
ab11 |
1.3 |
string sinput; |
47 |
|
|
while ( p->getLine(&sinput) ){ |
48 |
ab11 |
1.1 |
// find the first non-whitespace char in input |
49 |
ab11 |
1.3 |
|
50 |
ab11 |
1.1 |
// locate the first white space |
51 |
|
|
int found = sinput.find(" ",0); |
52 |
|
|
if ( found == string::npos ){ |
53 |
|
|
// error |
54 |
|
|
if ( debug == 1 ){ |
55 |
|
|
std::cout << "SysMon::collect::Parse error from statgrab.pl\n"; |
56 |
|
|
} |
57 |
|
|
} else { |
58 |
|
|
int gotTitle = 0; |
59 |
|
|
// now use the array of titles and values |
60 |
|
|
for ( int i = 0; i < 60; i++ ){ |
61 |
|
|
// check against the array value |
62 |
|
|
if ( titles[i] == sinput.substr(0,found) ){ |
63 |
|
|
// is the one we are looking for. |
64 |
ab11 |
1.2 |
if ( debug == 2 ){ |
65 |
ab11 |
1.1 |
std::cout << "SysMon::collect::Found Value\n"; |
66 |
ab11 |
1.2 |
|
67 |
ab11 |
1.1 |
} |
68 |
ab11 |
1.2 |
gotTitle = 1; |
69 |
ab11 |
1.1 |
// now modify the data in the arrays if they are numbers |
70 |
ab11 |
1.3 |
char * end; |
71 |
ab11 |
1.1 |
char *buffer; |
72 |
|
|
// buffer = sinput.substr(found+1, sinput.length()); |
73 |
ab11 |
1.3 |
double dbl = strtod( ((string) sinput.substr(found+1, sinput.length())).c_str(), &end); |
74 |
ab11 |
1.1 |
if ( dbl == 0 ){ |
75 |
|
|
// error? we aren't so sure. check the position |
76 |
|
|
// of end. |
77 |
|
|
if ( *end == sinput.length()-1 ){ |
78 |
|
|
// was the end.. ok |
79 |
ab11 |
1.3 |
// 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 *end |
87 |
ab11 |
1.1 |
} else { |
88 |
|
|
// must have worked! |
89 |
|
|
double dbl2 = 0; //strtod(values[i], &end); |
90 |
|
|
dbl2 += dbl; |
91 |
ab11 |
1.3 |
char * buffer; |
92 |
ab11 |
1.2 |
int precision = 5; |
93 |
ab11 |
1.1 |
int decimal, sign; |
94 |
|
|
buffer = ecvt (dbl2, precision, &decimal, &sign); |
95 |
|
|
} // if |
96 |
|
|
} // if |
97 |
|
|
} // for |
98 |
|
|
// did we find this title? if not add it |
99 |
|
|
if ( gotTitle == 0 ){ |
100 |
|
|
// didnt |
101 |
|
|
if ( debug == 1 ){ |
102 |
|
|
std::cout << "SysMon::collect::Adding New Value\n"; |
103 |
|
|
std::cout << "'" << sinput.substr(0,found) << "'\n"; |
104 |
|
|
std::cout << "'" << sinput.substr(found+1, sinput.length()) << "'\n"; |
105 |
|
|
} |
106 |
|
|
titles[titlepointer] = sinput.substr(0,found); |
107 |
|
|
values[titlepointer] = sinput.substr(found+1, sinput.length()); |
108 |
|
|
titlepointer++; |
109 |
|
|
} // if |
110 |
|
|
|
111 |
|
|
} // if (not) found |
112 |
|
|
|
113 |
|
|
} // while |
114 |
|
|
|
115 |
ab11 |
1.3 |
delete p; |
116 |
ab11 |
1.2 |
|
117 |
ab11 |
1.1 |
if ( debug == 1 ){ |
118 |
|
|
std::cout << "SysMon::collect::Parse from StatGrab finished\n"; |
119 |
|
|
} |
120 |
|
|
|
121 |
|
|
// alert? |
122 |
|
|
// return 1; |
123 |
|
|
|
124 |
|
|
// return sucessful |
125 |
|
|
return 0; |
126 |
|
|
|
127 |
|
|
} // collect |
128 |
|
|
|
129 |
|
|
string SysMon::getData(){ |
130 |
|
|
|
131 |
|
|
if ( debug == 1 ){ |
132 |
|
|
std::cout << "SysMon::getData()\n"; |
133 |
|
|
} |
134 |
|
|
|
135 |
|
|
// create an xml object |
136 |
|
|
XMLFormatter xml = XMLFormatter(); |
137 |
ab11 |
1.2 |
|
138 |
|
|
int i=0; |
139 |
|
|
int count = 0; |
140 |
|
|
int finished = 0; |
141 |
|
|
string root = "packet."; |
142 |
|
|
string level = root; |
143 |
|
|
string attributes = ""; |
144 |
|
|
string attr = ""; |
145 |
ab11 |
1.3 |
while ( i < titlepointer ){ |
146 |
|
|
finished = 0; |
147 |
ab11 |
1.2 |
count = 0; |
148 |
|
|
attr = ""; |
149 |
|
|
while ( finished != 1 ){ |
150 |
|
|
count++; |
151 |
|
|
if ( count > 10 ){ |
152 |
|
|
// don't do more than 10 times.. its probably not gonna work! |
153 |
|
|
finished = 1; |
154 |
|
|
} |
155 |
|
|
|
156 |
|
|
// do for each item |
157 |
|
|
int where = titles[i].find(level,0); |
158 |
|
|
|
159 |
|
|
// where now contains the position of 'level' |
160 |
|
|
if ( where != string::npos ){ |
161 |
|
|
// we found it, now check the size to see if this is the final level |
162 |
|
|
// grab the end bit a look for .'s |
163 |
|
|
string end = titles[i].substr(level.length(), titles[i].length()-level.length() ); |
164 |
|
|
|
165 |
|
|
// now end should contain "something.something" etc |
166 |
|
|
// look to find a . in there that means more to traverse |
167 |
|
|
// but check for 'attributes' tags... |
168 |
|
|
|
169 |
|
|
where = end.find("attributes.",0); |
170 |
|
|
if ( where != string::npos ){ |
171 |
|
|
// is an attribute |
172 |
|
|
// need to check that there are no more levels above this one |
173 |
|
|
|
174 |
|
|
|
175 |
|
|
|
176 |
|
|
/* |
177 |
|
|
|
178 |
|
|
// where points to the start of attributes |
179 |
|
|
int start = 0; |
180 |
|
|
if ( level.find(".",start) < where-1 ){ |
181 |
|
|
start = level.find(".", start); |
182 |
|
|
} // if |
183 |
|
|
|
184 |
|
|
// start points to the thing to apply the attributes to. |
185 |
|
|
// need to take the level upto before this point |
186 |
|
|
string needLevel = end.substr(0,start-2); |
187 |
|
|
std::cout << "needlevel " << needLevel << endl; |
188 |
|
|
|
189 |
|
|
// now we need to find the level we are at |
190 |
|
|
while (( needLevel.length() > 0 ) && (needLevel.find(".",0) != string::npos ) ){ |
191 |
|
|
int got = needLevel.find(".",0); |
192 |
|
|
if ( got != string::npos ){ |
193 |
|
|
// we need to grab up until the . |
194 |
|
|
xml.addNest(needLevel.substr(0,got)); |
195 |
|
|
level += needLevel.substr(0,got); |
196 |
|
|
level += "."; |
197 |
|
|
std::cout << "Adding Nest " << needLevel.substr(0,got) << endl; |
198 |
|
|
needLevel = needLevel.substr(got+1, needLevel.length()-(got+1)); |
199 |
|
|
} // if |
200 |
|
|
std::cout << "needlevel " << needLevel << endl; |
201 |
|
|
} // while |
202 |
|
|
|
203 |
|
|
// add the final nest |
204 |
|
|
xml.addNest(needLevel); |
205 |
|
|
level += needLevel; |
206 |
|
|
level += "."; |
207 |
|
|
std::cout << "Adding Nest " << needLevel << endl; |
208 |
|
|
|
209 |
|
|
*/ |
210 |
|
|
|
211 |
|
|
// now write out the variable |
212 |
|
|
xml.addElement(end, values[i]); |
213 |
|
|
finished = 1; |
214 |
|
|
|
215 |
|
|
} else { |
216 |
|
|
if ( where == 0 ){ |
217 |
|
|
// this is something we care about. |
218 |
|
|
// it has an attribute tab, and thus we want to add it |
219 |
|
|
// to the tag one lower than this (hopefully level!) |
220 |
|
|
|
221 |
|
|
// attributes = attributes + mount |
222 |
|
|
attributes += end.substr(where+11, end.length()-(where+11)); |
223 |
|
|
// attributes = attributes + mount=" |
224 |
|
|
attributes += "=\""; |
225 |
|
|
// attributes = attributes + mount="/dev |
226 |
|
|
attributes += values[i]; |
227 |
|
|
// attributes = attributes + mount="/dev" |
228 |
|
|
attributes += "\" "; |
229 |
|
|
} else { |
230 |
|
|
// need to check if we are the last element before |
231 |
|
|
// an attribute tag, so we want to get up to this tag first |
232 |
|
|
while ( end.find(".",0) != string::npos ){ |
233 |
|
|
|
234 |
|
|
where = end.find(".",0); |
235 |
|
|
string newNest = end.substr(0,where); |
236 |
|
|
xml.addNest(newNest); |
237 |
|
|
level += newNest; |
238 |
|
|
level += "."; |
239 |
|
|
// redefine end |
240 |
|
|
end = end.substr(where+1, end.length()-(where+1)); |
241 |
|
|
} // while |
242 |
|
|
|
243 |
|
|
|
244 |
|
|
xml.addElement(end, values[i]); |
245 |
|
|
finished = 1; |
246 |
|
|
} // if where == 0 |
247 |
|
|
} // if attribute tags |
248 |
|
|
|
249 |
|
|
|
250 |
|
|
|
251 |
|
|
|
252 |
|
|
} else { // if doesn't contains level |
253 |
|
|
|
254 |
|
|
// we need to move one back and try again, until we are all the way |
255 |
|
|
// back to "packet." |
256 |
|
|
// go about it by closing nests |
257 |
|
|
|
258 |
|
|
while ( level.length() > root.length() ){ |
259 |
|
|
// need to close one nest and locate the next . from the end (hassle) |
260 |
|
|
int start = 0; |
261 |
|
|
if ( level.find(".",start) != string::npos ){ |
262 |
|
|
start = level.find(".", start); |
263 |
|
|
} // if |
264 |
|
|
// should now have the last . |
265 |
|
|
xml.closeNest(); |
266 |
|
|
level = level.substr(0, start+1); |
267 |
|
|
} |
268 |
|
|
|
269 |
|
|
} // if contains level |
270 |
|
|
|
271 |
|
|
} // while |
272 |
|
|
i++; |
273 |
|
|
} // while |
274 |
|
|
|
275 |
ab11 |
1.1 |
|
276 |
|
|
|
277 |
|
|
|
278 |
|
|
|
279 |
|
|
|
280 |
ab11 |
1.2 |
|
281 |
ab11 |
1.1 |
// return the string |
282 |
|
|
return xml.returnXML(); |
283 |
|
|
|
284 |
|
|
} // getData |