--- projects/cms/source/host/c++/SysMon.cpp 2001/03/06 00:38:32 1.3 +++ projects/cms/source/host/c++/SysMon.cpp 2001/03/23 16:39:04 1.6 @@ -16,7 +16,7 @@ SysMon::SysMon( Config config, int printDebug){ // setup our arrays titlepointer = 0; - for ( int i=0; i < 40; i++ ){ + for ( int i=0; i < maxTitles; i++ ){ titles[i] = ""; values[i] = ""; } @@ -35,7 +35,7 @@ int SysMon::collect(){ // collect the system data and place it in an array of strings. // presume that the data is all formtted correctly. - char input[2048]; + // char input[2048]; if ( debug == 1 ){ std::cout << "SysMon::collect::ipipestream\n"; } @@ -44,8 +44,7 @@ int SysMon::collect(){ // get the output into input from statgrab string sinput; - while ( p->getLine(&sinput) ){ - // find the first non-whitespace char in input + while ( p->getLine(&sinput) == 0 ){ // locate the first white space int found = sinput.find(" ",0); @@ -57,50 +56,25 @@ int SysMon::collect(){ } else { int gotTitle = 0; // now use the array of titles and values - for ( int i = 0; i < 60; i++ ){ + for ( int i = 0; i < maxTitles; i++ ){ // check against the array value if ( titles[i] == sinput.substr(0,found) ){ // is the one we are looking for. if ( debug == 2 ){ std::cout << "SysMon::collect::Found Value\n"; - - } + } // if debug + gotTitle = 1; // now modify the data in the arrays if they are numbers - char * end; - char *buffer; - // buffer = sinput.substr(found+1, sinput.length()); - double dbl = strtod( ((string) sinput.substr(found+1, sinput.length())).c_str(), &end); - if ( dbl == 0 ){ - // error? we aren't so sure. check the position - // of end. - if ( *end == sinput.length()-1 ){ - // was the end.. ok - // must have worked! - double dbl2 = 0; //strtod(values[i], &end); - dbl2 += dbl; - char * buffer; - int precision = 5; - int decimal, sign; - buffer = ecvt (dbl2, precision, &decimal, &sign); - } // if *end - } else { - // must have worked! - double dbl2 = 0; //strtod(values[i], &end); - dbl2 += dbl; - char * buffer; - int precision = 5; - int decimal, sign; - buffer = ecvt (dbl2, precision, &decimal, &sign); - } // if - } // if + } // if (titles[i]) } // for + // did we find this title? if not add it if ( gotTitle == 0 ){ // didnt if ( debug == 1 ){ std::cout << "SysMon::collect::Adding New Value\n"; - std::cout << "'" << sinput.substr(0,found) << "'\n"; + std::cout << "'" << sinput.substr(0,found) << "' : "; std::cout << "'" << sinput.substr(found+1, sinput.length()) << "'\n"; } titles[titlepointer] = sinput.substr(0,found); @@ -118,14 +92,24 @@ int SysMon::collect(){ std::cout << "SysMon::collect::Parse from StatGrab finished\n"; } - // alert? - // return 1; - // return sucessful return 0; } // collect +void SysMon::clearData(){ + + titlepointer = 0; + for ( int i=0; i < maxTitles; i++ ){ + titles[i] = ""; + values[i] = ""; + } + + return; + +} // clearData + + string SysMon::getData(){ if ( debug == 1 ){ @@ -135,149 +119,303 @@ string SysMon::getData(){ // create an xml object XMLFormatter xml = XMLFormatter(); - int i=0; + if ( debug == 1 ){ + std::cout << "SysMon::getData::Sorting " << titlepointer << " elements\n"; + } + + // firstly sort the data. + // simple bubble sort + int flag = 1; + while ( flag == 1 ){ + flag = 0; + for ( int i=1; i<=titlepointer; i++ ){ + if (titles[i-1].compare(titles[i]) > 0 ){ + // swap them + string temp; + temp = titles[i-1]; + titles[i-1] = titles[i]; + titles[i] = temp; + // now do the values + temp = values[i-1]; + values[i-1] = values[i]; + values[i] = temp; + // say we have changed something + flag = 1; + } // if + } // for + } // while + + /* + for ( int i=0; i < titlepointer; i++ ){ + std::cout << titles[i] << endl; + + } + */ + + + // work through each one of the titles in turn and + // work out their levels. + int count = 0; - int finished = 0; - string root = "packet."; - string level = root; + string currentLevel = "packet"; + string currentLevels[10]; + int currentLevelint = 0; + int retryCount = 0; string attributes = ""; - string attr = ""; - while ( i < titlepointer ){ - finished = 0; - count = 0; - attr = ""; - while ( finished != 1 ){ - count++; - if ( count > 10 ){ - // don't do more than 10 times.. its probably not gonna work! - finished = 1; - } - - // do for each item - int where = titles[i].find(level,0); - - // where now contains the position of 'level' - if ( where != string::npos ){ - // we found it, now check the size to see if this is the final level - // grab the end bit a look for .'s - string end = titles[i].substr(level.length(), titles[i].length()-level.length() ); - - // now end should contain "something.something" etc - // look to find a . in there that means more to traverse - // but check for 'attributes' tags... - - where = end.find("attributes.",0); - if ( where != string::npos ){ - // is an attribute - // need to check that there are no more levels above this one - - - - /* - - // where points to the start of attributes - int start = 0; - if ( level.find(".",start) < where-1 ){ - start = level.find(".", start); - } // if - - // start points to the thing to apply the attributes to. - // need to take the level upto before this point - string needLevel = end.substr(0,start-2); - std::cout << "needlevel " << needLevel << endl; - - // now we need to find the level we are at - while (( needLevel.length() > 0 ) && (needLevel.find(".",0) != string::npos ) ){ - int got = needLevel.find(".",0); - if ( got != string::npos ){ - // we need to grab up until the . - xml.addNest(needLevel.substr(0,got)); - level += needLevel.substr(0,got); - level += "."; - std::cout << "Adding Nest " << needLevel.substr(0,got) << endl; - needLevel = needLevel.substr(got+1, needLevel.length()-(got+1)); - } // if - std::cout << "needlevel " << needLevel << endl; - } // while - - // add the final nest - xml.addNest(needLevel); - level += needLevel; - level += "."; - std::cout << "Adding Nest " << needLevel << endl; - - */ - - // now write out the variable - xml.addElement(end, values[i]); - finished = 1; - - } else { - if ( where == 0 ){ - // this is something we care about. - // it has an attribute tab, and thus we want to add it - // to the tag one lower than this (hopefully level!) - - // attributes = attributes + mount - attributes += end.substr(where+11, end.length()-(where+11)); - // attributes = attributes + mount=" - attributes += "=\""; - // attributes = attributes + mount="/dev - attributes += values[i]; - // attributes = attributes + mount="/dev" - attributes += "\" "; - } else { - // need to check if we are the last element before - // an attribute tag, so we want to get up to this tag first - while ( end.find(".",0) != string::npos ){ - - where = end.find(".",0); - string newNest = end.substr(0,where); - xml.addNest(newNest); - level += newNest; - level += "."; - // redefine end - end = end.substr(where+1, end.length()-(where+1)); - } // while - - - xml.addElement(end, values[i]); - finished = 1; - } // if where == 0 - } // if attribute tags + int isAttrib = 0; + string attributeLevel = ""; - + // init that array quickly ;) + for ( int i=0; i< 10; i++ ){ + currentLevels[i] = ""; + } // for - - } else { // if doesn't contains level - - // we need to move one back and try again, until we are all the way - // back to "packet." - // go about it by closing nests - - while ( level.length() > root.length() ){ - // need to close one nest and locate the next . from the end (hassle) - int start = 0; - if ( level.find(".",start) != string::npos ){ - start = level.find(".", start); - } // if - // should now have the last . - xml.closeNest(); - level = level.substr(0, start+1); - } - - } // if contains level - - } // while - i++; - } // while + if ( debug == 1 ){ + std::cout << "SysMon::getData::Starting while\n"; + } - - - - - - + while ( count <= titlepointer ){ + + + + if ( debug == 1 ){ + std::cout << "Processing: " << titles[count] << endl; + } + + retryCount++; + + currentLevel = ""; + for ( int i=0; i 0 ){ + if ( levels[level-1] != "attributes" ){ + // say it isn't an attribute, but write out + // the attribute first if the last one was + if ( attributeLevel != "" ){ + xml.addElement(attributeLevel,attributes,""); + if ( debug == 1 ){ + std::cout << "Adding Element + attributes" << endl; + } + count++; + } // if + isAttrib = 0; + attributeLevel = ""; + attributes = ""; + + for ( int i=0; i < level; i++ ){ + newLevelString += levels[i]; + if ( i != level-1 ){ + newLevelString += "."; + } + } // for + } // if levels[level] + + else + + { + // we need to add to the attributes + level -= 2; + for ( int i=0; i < level; i++ ){ + newLevelString += levels[i]; + if ( i != level-1 ){ + newLevelString += "."; + } + } // for + + isAttrib = 1; + if ( attributeLevel == "" ){ + // set this attribute to be the level + attributeLevel = levels[level]; + } else { + if ( levels[level] != attributeLevel ){ + // arrg! its a different level! + if ( attributeLevel != levels[level] ){ + xml.addElement(attributeLevel,attributes,""); + if ( debug == 1 ){ + std::cout << "Adding Element + attributes" << endl; + } + attributeLevel = ""; + attributes = ""; + count++; + } // if + if ( count == titlepointer ){ + xml.addElement(attributeLevel,attributes,""); + if ( debug == 1 ){ + std::cout << "Adding Element + attributes" << endl; + } + attributeLevel = ""; + attributes = ""; + count++; + } // if + } // if + } // if else + + } // else attributes + } // if level > 1 + else { + if ( count == titlepointer ){ + if ( attributeLevel != "" ){ + xml.addElement(attributeLevel,attributes,""); + if ( debug == 1 ){ + std::cout << "Adding Element + attributes" << endl; + } + attributeLevel = ""; + attributes = ""; + count++; + } // if + } // if + + + for ( int i=0; i < level; i++ ){ + newLevelString += levels[i]; + if ( i != level-1 ){ + newLevelString += "."; + } + } // for + } // if level == 1 + + // check if it is the same + if ( newLevelString == currentLevel ){ + // it is the same level, just add this as another element + if ( isAttrib ){ + attributes += levels[level+2]; + attributes += "=\""; + attributes += values[count]; + attributes += "\" "; + // now check if this is the last element + + + } else { + xml.addElement(levels[level], values[count]); + if ( debug == 1 ){ + std::cout << "Adding Element" << endl; + } + } + count++; + } else { + // gotta change it + + while( newLevelString.find(currentLevel) == string::npos ){ + + if ( currentLevelint <= 0 ){ + break; + } + + // back up atleast one level... + xml.closeNest(); + if ( debug == 1 ){ + std::cout << "Closing Nest" << endl; + } + // remove the last item + currentLevelint--; + + currentLevel = ""; + for ( int i=0; i 10 ){ + count++; + if ( debug == 1 ){ + std::cout << "Tried too many times" << endl; + } // if + retryCount = 0; + } // if + + } // while + + if ( debug == 1 ){ + std::cout << "\n"; + } + // return the string return xml.returnXML();