| 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 |
|
|
ipipestream p("statgrab.pl"); |
| 43 |
|
|
|
| 44 |
|
|
// get the output into input from statgrab |
| 45 |
|
|
while ( p.getline (input, 2048) ){ |
| 46 |
|
|
// find the first non-whitespace char in input |
| 47 |
|
|
string sinput = input; |
| 48 |
|
|
// locate the first white space |
| 49 |
|
|
int found = sinput.find(" ",0); |
| 50 |
|
|
if ( found == string::npos ){ |
| 51 |
|
|
// error |
| 52 |
|
|
if ( debug == 1 ){ |
| 53 |
|
|
std::cout << "SysMon::collect::Parse error from statgrab.pl\n"; |
| 54 |
|
|
} |
| 55 |
|
|
} else { |
| 56 |
|
|
int gotTitle = 0; |
| 57 |
|
|
// now use the array of titles and values |
| 58 |
|
|
for ( int i = 0; i < 60; i++ ){ |
| 59 |
|
|
// check against the array value |
| 60 |
|
|
if ( titles[i] == sinput.substr(0,found) ){ |
| 61 |
|
|
// is the one we are looking for. |
| 62 |
ab11 |
1.2 |
if ( debug == 2 ){ |
| 63 |
ab11 |
1.1 |
std::cout << "SysMon::collect::Found Value\n"; |
| 64 |
ab11 |
1.2 |
|
| 65 |
ab11 |
1.1 |
} |
| 66 |
ab11 |
1.2 |
gotTitle = 1; |
| 67 |
ab11 |
1.1 |
// now modify the data in the arrays if they are numbers |
| 68 |
|
|
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 |
ab11 |
1.2 |
} // if |
| 79 |
ab11 |
1.1 |
} else { |
| 80 |
|
|
// must have worked! |
| 81 |
|
|
double dbl2 = 0; //strtod(values[i], &end); |
| 82 |
|
|
dbl2 += dbl; |
| 83 |
|
|
char *buffer; |
| 84 |
ab11 |
1.2 |
int precision = 5; |
| 85 |
ab11 |
1.1 |
int decimal, sign; |
| 86 |
|
|
buffer = ecvt (dbl2, precision, &decimal, &sign); |
| 87 |
|
|
} // if |
| 88 |
|
|
} // if |
| 89 |
|
|
} // for |
| 90 |
|
|
// did we find this title? if not add it |
| 91 |
|
|
if ( gotTitle == 0 ){ |
| 92 |
|
|
// didnt |
| 93 |
|
|
if ( debug == 1 ){ |
| 94 |
|
|
std::cout << "SysMon::collect::Adding New Value\n"; |
| 95 |
|
|
std::cout << "'" << sinput.substr(0,found) << "'\n"; |
| 96 |
|
|
std::cout << "'" << sinput.substr(found+1, sinput.length()) << "'\n"; |
| 97 |
|
|
} |
| 98 |
|
|
titles[titlepointer] = sinput.substr(0,found); |
| 99 |
|
|
values[titlepointer] = sinput.substr(found+1, sinput.length()); |
| 100 |
|
|
titlepointer++; |
| 101 |
|
|
} // if |
| 102 |
|
|
|
| 103 |
|
|
} // if (not) found |
| 104 |
|
|
|
| 105 |
|
|
} // while |
| 106 |
|
|
|
| 107 |
ab11 |
1.2 |
// delete p; |
| 108 |
|
|
|
| 109 |
ab11 |
1.1 |
if ( debug == 1 ){ |
| 110 |
|
|
std::cout << "SysMon::collect::Parse from StatGrab finished\n"; |
| 111 |
|
|
} |
| 112 |
|
|
|
| 113 |
|
|
// alert? |
| 114 |
|
|
// return 1; |
| 115 |
|
|
|
| 116 |
|
|
// return sucessful |
| 117 |
|
|
return 0; |
| 118 |
|
|
|
| 119 |
|
|
} // collect |
| 120 |
|
|
|
| 121 |
|
|
string SysMon::getData(){ |
| 122 |
|
|
|
| 123 |
|
|
if ( debug == 1 ){ |
| 124 |
|
|
std::cout << "SysMon::getData()\n"; |
| 125 |
|
|
} |
| 126 |
|
|
|
| 127 |
|
|
// create an xml object |
| 128 |
|
|
XMLFormatter xml = XMLFormatter(); |
| 129 |
ab11 |
1.2 |
|
| 130 |
|
|
int i=0; |
| 131 |
|
|
int count = 0; |
| 132 |
|
|
int finished = 0; |
| 133 |
|
|
string root = "packet."; |
| 134 |
|
|
string level = root; |
| 135 |
|
|
string attributes = ""; |
| 136 |
|
|
string attr = ""; |
| 137 |
|
|
// 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 |
| 242 |
|
|
|
| 243 |
|
|
|
| 244 |
|
|
|
| 245 |
|
|
|
| 246 |
|
|
} else { // if doesn't contains level |
| 247 |
|
|
|
| 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 |
| 268 |
|
|
|
| 269 |
ab11 |
1.1 |
|
| 270 |
|
|
|
| 271 |
|
|
|
| 272 |
|
|
|
| 273 |
|
|
|
| 274 |
ab11 |
1.2 |
|
| 275 |
ab11 |
1.1 |
// return the string |
| 276 |
|
|
return xml.returnXML(); |
| 277 |
|
|
|
| 278 |
|
|
} // getData |