| 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 |
if ( debug == 1 ){ |
| 63 |
std::cout << "SysMon::collect::Found Value\n"; |
| 64 |
gotTitle = 1; |
| 65 |
} |
| 66 |
// now modify the data in the arrays if they are numbers |
| 67 |
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 |
| 88 |
} // for |
| 89 |
// did we find this title? if not add it |
| 90 |
if ( gotTitle == 0 ){ |
| 91 |
// didnt |
| 92 |
if ( debug == 1 ){ |
| 93 |
std::cout << "SysMon::collect::Adding New Value\n"; |
| 94 |
std::cout << "'" << sinput.substr(0,found) << "'\n"; |
| 95 |
std::cout << "'" << sinput.substr(found+1, sinput.length()) << "'\n"; |
| 96 |
} |
| 97 |
titles[titlepointer] = sinput.substr(0,found); |
| 98 |
values[titlepointer] = sinput.substr(found+1, sinput.length()); |
| 99 |
titlepointer++; |
| 100 |
} // if |
| 101 |
|
| 102 |
} // if (not) found |
| 103 |
|
| 104 |
} // while |
| 105 |
|
| 106 |
if ( debug == 1 ){ |
| 107 |
std::cout << "SysMon::collect::Parse from StatGrab finished\n"; |
| 108 |
} |
| 109 |
|
| 110 |
// alert? |
| 111 |
// return 1; |
| 112 |
|
| 113 |
// return sucessful |
| 114 |
return 0; |
| 115 |
|
| 116 |
} // collect |
| 117 |
|
| 118 |
string SysMon::getData(){ |
| 119 |
|
| 120 |
if ( debug == 1 ){ |
| 121 |
std::cout << "SysMon::getData()\n"; |
| 122 |
} |
| 123 |
|
| 124 |
// create an xml object |
| 125 |
XMLFormatter xml = XMLFormatter(); |
| 126 |
|
| 127 |
// spammage |
| 128 |
// xml.addElement(titles[0],values[0]); // version |
| 129 |
xml.addNest("os"); |
| 130 |
xml.addElement("name",values[1]); // packet.os.name |
| 131 |
xml.addElement("release",values[2]); // packet.os.release |
| 132 |
xml.addElement("platform",values[3]); // packet.os.platform |
| 133 |
xml.addElement("sysname",values[4]); // packet.os.sysname |
| 134 |
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 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
// xml. |
| 153 |
|
| 154 |
|
| 155 |
// } // for |
| 156 |
|
| 157 |
// return the string |
| 158 |
return xml.returnXML(); |
| 159 |
|
| 160 |
} // getData |