--- projects/cms/source/host/c++/SysMon.cpp 2001/03/23 15:00:50 1.4 +++ projects/cms/source/host/c++/SysMon.cpp 2002/05/21 16:47:11 1.10 @@ -1,3 +1,23 @@ +/* + * i-scream central monitoring system + * http://www.i-scream.org.uk + * Copyright (C) 2000-2002 i-scream + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + #include "SysMon.h" SysMon::SysMon( Config config, int printDebug){ @@ -11,12 +31,9 @@ SysMon::SysMon( Config config, int printDebug){ // setup some standard variables sequence = 0; // no packets sent yet - // get our values from config - - // setup our arrays titlepointer = 0; - for ( int i=0; i < maxTitles; i++ ){ + for ( int i=0; i < max_titles; i++ ){ titles[i] = ""; values[i] = ""; } @@ -28,35 +45,42 @@ SysMon::SysMon( Config config, int printDebug){ int SysMon::collect(){ if ( debug == 1 ){ std::cout << "SysMon::collect()\n"; - } + } // if // say we have checked again checks++; // collect the system data and place it in an array of strings. // presume that the data is all formtted correctly. - // char input[2048]; + if ( debug == 1 ){ std::cout << "SysMon::collect::ipipestream\n"; } - // ipipestream p("statgrab.pl"); - p = new SubPipe("statgrab.pl"); + + p = new SubPipe("./statgrab.pl"); // get the output into input from statgrab string sinput; while ( p->getLine(&sinput) == 0 ){ + if ( titlepointer == max_titles ){ + std::cout << "WARNING: Max Titles reached, skipping remaining" << endl; + std::cout << "Try recompiling with a value greater than the current (" << max_titles << ")" << endl; + titlepointer--; + break; + } // if + // locate the first white space int found = sinput.find(" ",0); if ( found == string::npos ){ // error if ( debug == 1 ){ std::cout << "SysMon::collect::Parse error from statgrab.pl\n"; - } + } // if } else { int gotTitle = 0; // now use the array of titles and values - for ( int i = 0; i < maxTitles; i++ ){ + for ( int i = 0; i < max_titles; i++ ){ // check against the array value if ( titles[i] == sinput.substr(0,found) ){ // is the one we are looking for. @@ -73,10 +97,10 @@ int SysMon::collect(){ if ( gotTitle == 0 ){ // didnt if ( debug == 1 ){ - std::cout << "SysMon::collect::Adding New Value\n"; - std::cout << "'" << sinput.substr(0,found) << "' : "; - std::cout << "'" << sinput.substr(found+1, sinput.length()) << "'\n"; - } + std::cout << "SysMon::collect::Adding New Value\n"; + std::cout << "'" << sinput.substr(0,found) << "' : "; + std::cout << "'" << sinput.substr(found+1, sinput.length()) << "'\n"; + } // if titles[titlepointer] = sinput.substr(0,found); values[titlepointer] = sinput.substr(found+1, sinput.length()); titlepointer++; @@ -86,11 +110,12 @@ int SysMon::collect(){ } // while + // delete the pointer to free up memory delete p; if ( debug == 1 ){ std::cout << "SysMon::collect::Parse from StatGrab finished\n"; - } + } // if // return sucessful return 0; @@ -100,10 +125,10 @@ int SysMon::collect(){ void SysMon::clearData(){ titlepointer = 0; - for ( int i=0; i < maxTitles; i++ ){ + for ( int i=0; i < max_titles; i++ ){ titles[i] = ""; values[i] = ""; - } + } // for return; @@ -114,14 +139,14 @@ string SysMon::getData(){ if ( debug == 1 ){ std::cout << "SysMon::getData()\n"; - } + } // if // create an xml object XMLFormatter xml = XMLFormatter(); if ( debug == 1 ){ std::cout << "SysMon::getData::Sorting " << titlepointer << " elements\n"; - } + } // if // firstly sort the data. // simple bubble sort @@ -145,14 +170,12 @@ string SysMon::getData(){ } // for } // while - /* + /* used to check the array has sorted correctly 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. @@ -172,15 +195,13 @@ string SysMon::getData(){ if ( debug == 1 ){ std::cout << "SysMon::getData::Starting while\n"; - } + } // if while ( count <= titlepointer ){ - - - + if ( debug == 1 ){ std::cout << "Processing: " << titles[count] << endl; - } + } // if retryCount++; @@ -224,7 +245,8 @@ string SysMon::getData(){ // first one mind! if ( levels[0] == "packet" ){ level--; - } + } // if + } // while dotPosition // now grab the value title @@ -246,8 +268,8 @@ string SysMon::getData(){ xml.addElement(attributeLevel,attributes,""); if ( debug == 1 ){ std::cout << "Adding Element + attributes" << endl; - } - count++; + } // if + } // if isAttrib = 0; attributeLevel = ""; @@ -257,7 +279,7 @@ string SysMon::getData(){ newLevelString += levels[i]; if ( i != level-1 ){ newLevelString += "."; - } + } // if } // for } // if levels[level] @@ -270,7 +292,7 @@ string SysMon::getData(){ newLevelString += levels[i]; if ( i != level-1 ){ newLevelString += "."; - } + } // if } // for isAttrib = 1; @@ -283,8 +305,8 @@ string SysMon::getData(){ if ( attributeLevel != levels[level] ){ xml.addElement(attributeLevel,attributes,""); if ( debug == 1 ){ - std::cout << "Adding Element + attributes" << endl; - } + std::cout << "Adding Element + attributes1" << endl; + } // if attributeLevel = ""; attributes = ""; count++; @@ -292,8 +314,8 @@ string SysMon::getData(){ if ( count == titlepointer ){ xml.addElement(attributeLevel,attributes,""); if ( debug == 1 ){ - std::cout << "Adding Element + attributes" << endl; - } + std::cout << "Adding Element + attributes2" << endl; + } // if attributeLevel = ""; attributes = ""; count++; @@ -308,8 +330,8 @@ string SysMon::getData(){ if ( attributeLevel != "" ){ xml.addElement(attributeLevel,attributes,""); if ( debug == 1 ){ - std::cout << "Adding Element + attributes" << endl; - } + std::cout << "Adding Element + attributes3" << endl; + } // if attributeLevel = ""; attributes = ""; count++; @@ -321,7 +343,7 @@ string SysMon::getData(){ newLevelString += levels[i]; if ( i != level-1 ){ newLevelString += "."; - } + } // if } // for } // if level == 1 @@ -330,19 +352,18 @@ string SysMon::getData(){ // it is the same level, just add this as another element if ( isAttrib ){ attributes += levels[level+2]; - attributes += "="; + attributes += "=\""; attributes += values[count]; - attributes += " "; - // now check if this is the last element - - + attributes += "\" "; + } else { xml.addElement(levels[level], values[count]); + count++; if ( debug == 1 ){ std::cout << "Adding Element" << endl; - } - } - count++; + } // if + } // if + } else { // gotta change it @@ -350,13 +371,13 @@ string SysMon::getData(){ if ( currentLevelint <= 0 ){ break; - } + } // if // back up atleast one level... xml.closeNest(); if ( debug == 1 ){ std::cout << "Closing Nest" << endl; - } + } // if // remove the last item currentLevelint--; @@ -380,7 +401,7 @@ string SysMon::getData(){ xml.addNest(levels[currentLevelint]); if ( debug == 1 ){ std::cout << "Adding nest " << levels[currentLevelint] << endl; - } + } // if // set this to be the top level of the new level currentLevelint++; @@ -414,9 +435,8 @@ string SysMon::getData(){ if ( debug == 1 ){ std::cout << "\n"; - } + } // if - std::cout << xml.returnXML() << endl; // return the string return xml.returnXML();