diff options
| author | Apollon Oikonomopoulos <apoikos@debian.org> | 2016-01-14 00:10:06 +0200 |
|---|---|---|
| committer | Apollon Oikonomopoulos <apollon@skroutz.gr> | 2016-01-14 00:10:06 +0200 |
| commit | 374e1947abcd3e127a2a613aff73ecffdb9199ea (patch) | |
| tree | d83973c3c9802450acd5b5e86fe0d4e8e60a3a1b /src/mongo/tools/stat.cpp | |
| parent | 65585c90b12d6523bea75a2aebaae2a2fdf9e641 (diff) | |
Imported Upstream version 2.6.11upstream/2.6.11
Diffstat (limited to 'src/mongo/tools/stat.cpp')
| -rw-r--r-- | src/mongo/tools/stat.cpp | 211 |
1 files changed, 84 insertions, 127 deletions
diff --git a/src/mongo/tools/stat.cpp b/src/mongo/tools/stat.cpp index 07194444671..002d8a4a5f0 100644 --- a/src/mongo/tools/stat.cpp +++ b/src/mongo/tools/stat.cpp @@ -12,148 +12,92 @@ * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. +* +* As a special exception, the copyright holders give permission to link the +* code of portions of this program with the OpenSSL library under certain +* conditions as described in each individual source file and distribute +* linked combinations including the program with the OpenSSL library. You +* must comply with the GNU Affero General Public License in all respects +* for all of the code used other than as permitted herein. If you modify +* file(s) with this exception, you may extend this exception to your +* version of the file(s), but you are not obligated to do so. If you do not +* wish to do so, delete this exception statement from your version. If you +* delete this exception statement from all source files in the program, +* then also delete it in the license file. */ -#include "pch.h" - -#include "mongo/tools/tool.h" +#include "mongo/pch.h" -#include <boost/program_options.hpp> #include <boost/thread/thread.hpp> #include <fstream> #include <iostream> -#include "mongo/base/initializer.h" +#include "mongo/base/init.h" #include "mongo/client/dbclientcursor.h" +#include "mongo/client/sasl_client_authenticate.h" #include "mongo/db/jsobjmanipulator.h" #include "mongo/db/json.h" #include "mongo/s/type_shard.h" #include "mongo/tools/stat_util.h" +#include "mongo/tools/mongostat_options.h" +#include "mongo/tools/tool.h" #include "mongo/util/net/httpclient.h" +#include "mongo/util/options_parser/option_section.h" #include "mongo/util/text.h" -namespace po = boost::program_options; - namespace mongo { class Stat : public Tool { public: - Stat() : Tool( "stat" , REMOTE_SERVER , "admin" ) { - _http = false; - _many = false; - - add_hidden_options() - ( "sleep" , po::value<int>() , "time to sleep between calls" ) - ; - add_options() - ("noheaders", "don't output column names") - ("rowcount,n", po::value<int>()->default_value(0), "number of stats lines to print (0 for indefinite)") - ("http", "use http instead of raw db connection") - ("discover" , "discover nodes and display stats for all" ) - ("all" , "all optional fields" ) - ; - - addPositionArg( "sleep" , 1 ); - + Stat() : Tool() { _autoreconnect = true; } - virtual void printExtraHelp( ostream & out ) { - out << "View live MongoDB performance statistics.\n" << endl; - out << "usage: " << _name << " [options] [sleep time]" << endl; - out << "sleep time: time to wait (in seconds) between calls" << endl; - } - - virtual void printExtraHelpAfter( ostream & out ) { - out << "\n"; - out << " Fields\n"; - out << " inserts \t- # of inserts per second (* means replicated op)\n"; - out << " query \t- # of queries per second\n"; - out << " update \t- # of updates per second\n"; - out << " delete \t- # of deletes per second\n"; - out << " getmore \t- # of get mores (cursor batch) per second\n"; - out << " command \t- # of commands per second, on a slave its local|replicated\n"; - out << " flushes \t- # of fsync flushes per second\n"; - out << " mapped \t- amount of data mmaped (total data size) megabytes\n"; - out << " vsize \t- virtual size of process in megabytes\n"; - out << " res \t- resident size of process in megabytes\n"; - out << " faults \t- # of pages faults per sec\n"; - out << " locked \t- name of and percent time for most locked database\n"; - out << " idx miss \t- percent of btree page misses (sampled)\n"; - out << " qr|qw \t- queue lengths for clients waiting (read|write)\n"; - out << " ar|aw \t- active clients (read|write)\n"; - out << " netIn \t- network traffic in - bits\n"; - out << " netOut \t- network traffic out - bits\n"; - out << " conn \t- number of open connections\n"; - out << " set \t- replica set name\n"; - out << " repl \t- replication type \n"; - out << " \t PRI - primary (master)\n"; - out << " \t SEC - secondary\n"; - out << " \t REC - recovering\n"; - out << " \t UNK - unknown\n"; - out << " \t SLV - slave\n"; - out << " \t RTR - mongos process (\"router\")\n"; + virtual void printHelp( ostream & out ) { + printMongoStatHelp(&out); } BSONObj stats() { - if ( _http ) { + if (mongoStatGlobalParams.http) { HttpClient c; HttpClient::Result r; string url; { stringstream ss; - ss << "http://" << _host; - if ( _host.find( ":" ) == string::npos ) + ss << "http://" << toolGlobalParams.connectionString; + if (toolGlobalParams.connectionString.find( ":" ) == string::npos) ss << ":28017"; ss << "/_status"; url = ss.str(); } if ( c.get( url , &r ) != 200 ) { - cout << "error (http): " << r.getEntireResponse() << endl; + toolError() << "error (http): " << r.getEntireResponse() << std::endl; return BSONObj(); } BSONObj x = fromjson( r.getBody() ); BSONElement e = x["serverStatus"]; if ( e.type() != Object ) { - cout << "BROKEN: " << x << endl; + toolError() << "BROKEN: " << x << std::endl; return BSONObj(); } return e.embeddedObjectUserCheck(); } BSONObj out; - if ( ! conn().simpleCommand( _db , &out , "serverStatus" ) ) { - cout << "error: " << out << endl; + if (!conn().simpleCommand(toolGlobalParams.db, &out, "serverStatus")) { + toolError() << "error: " << out << std::endl; return BSONObj(); } return out.getOwned(); } - - virtual void preSetup() { - if ( hasParam( "http" ) ) { - _http = true; - _noconnection = true; - } - - if ( hasParam( "host" ) && - getParam( "host" ).find( ',' ) != string::npos ) { - _noconnection = true; - _many = true; - } - - if ( hasParam( "discover" ) ) { - _many = true; - } - } - int run() { - _statUtil.setSeconds( getParam( "sleep" , 1 ) ); - _statUtil.setAll( hasParam( "all" ) ); - if ( _many ) + _statUtil.setAll(mongoStatGlobalParams.allFields); + _statUtil.setSeconds(mongoStatGlobalParams.sleep); + if (mongoStatGlobalParams.many) return runMany(); return runNormal(); } @@ -200,8 +144,6 @@ namespace mongo { } int runNormal() { - bool showHeaders = ! hasParam( "noheaders" ); - int rowCount = getParam( "rowcount" , 0 ); int rowNum = 0; BSONObj prev = stats(); @@ -209,15 +151,17 @@ namespace mongo { return -1; int maxLockedDbWidth = 0; + bool warned = false; - while ( rowCount == 0 || rowNum < rowCount ) { + while (mongoStatGlobalParams.rowCount == 0 || + rowNum < mongoStatGlobalParams.rowCount) { sleepsecs((int)ceil(_statUtil.getSeconds())); BSONObj now; try { now = stats(); } catch ( std::exception& e ) { - cout << "can't get data: " << e.what() << endl; + toolError() << "can't get data: " << e.what() << std::endl; continue; } @@ -226,11 +170,16 @@ namespace mongo { try { + if ( !warned && now["storageEngine"].type() ) { + toolError() << "warning: detected a 3.0 mongod, some columns not applicable" << endl; + warned = true; + } + BSONObj out = _statUtil.doRow( prev , now ); // adjust width up as longer 'locked db' values appear setMaxLockedDbWidth( &out, &maxLockedDbWidth ); - if ( showHeaders && rowNum % 10 == 0 ) { + if (mongoStatGlobalParams.showHeaders && rowNum % 10 == 0) { printHeaders( out ); } @@ -238,9 +187,9 @@ namespace mongo { } catch ( AssertionException& e ) { - cout << "\nerror: " << e.what() << "\n" - << now - << endl; + toolError() << "\nerror: " << e.what() << "\n" + << now + << std::endl; } prev = now; @@ -287,8 +236,9 @@ namespace mongo { static void serverThread( shared_ptr<ServerState> state , int sleepTime) { try { + bool warned = false; DBClientConnection conn( true ); - conn._logLevel = 1; + conn._logLevel = logger::LogSeverity::Debug(1); string errmsg; if ( ! conn.connect( state->host , errmsg ) ) state->error = errmsg; @@ -308,19 +258,28 @@ namespace mongo { state->now = out.getOwned(); } else { + str::stream errorStream; + errorStream << "serverStatus failed"; + BSONElement errorField = out["errmsg"]; + if (errorField.type() == String) + errorStream << ": " << errorField.str(); scoped_lock lk( state->lock ); - state->error = "serverStatus failed"; + state->error = errorStream; state->lastUpdate = time(0); } + if ( !warned && out["storageEngine"].type() ) { + toolError() << "warning: detected a 3.0 mongod, some columns not applicable" << endl; + warned = true; + } if ( out["shardCursorType"].type() == Object || - out["process"].String() == "mongos" ) { + out["process"].str() == "mongos" ) { state->mongos = true; if ( cycleNumber % 10 == 1 ) { auto_ptr<DBClientCursor> c = conn.query( ShardType::ConfigNS , BSONObj() ); vector<BSONObj> shards; while ( c->more() ) { - shards.push_back( c->next().getOwned() ); + shards.push_back( c->nextSafe().getOwned() ); } scoped_lock lk( state->lock ); state->shards = shards; @@ -338,10 +297,11 @@ namespace mongo { } catch ( std::exception& e ) { - cout << "serverThread (" << state->host << ") fatal error : " << e.what() << endl; + toolError() << "serverThread (" << state->host << ") fatal error : " << e.what() + << std::endl; } catch ( ... ) { - cout << "serverThread (" << state->host << ") fatal error" << endl; + toolError() << "serverThread (" << state->host << ") fatal error" << std::endl; } } @@ -358,10 +318,11 @@ namespace mongo { state->thr.reset( new boost::thread( boost::bind( serverThread, state, (int)ceil(_statUtil.getSeconds()) ) ) ); - state->authParams = BSON( "user" << _username << - "pwd" << _password << - "userSource" << getAuthenticationDatabase() << - "mechanism" << _authenticationMechanism ); + state->authParams = BSON(saslCommandUserFieldName << toolGlobalParams.username + << saslCommandPasswordFieldName << toolGlobalParams.password + << saslCommandUserDBFieldName << getAuthenticationDatabase() + << saslCommandMechanismFieldName + << toolGlobalParams.authenticationMechanism); return true; } @@ -401,7 +362,7 @@ namespace mongo { string errmsg; ConnectionString cs = ConnectionString::parse( x["host"].String() , errmsg ); if ( errmsg.size() ) { - cerr << errmsg << endl; + toolError() << errmsg << std::endl; continue; } @@ -420,12 +381,13 @@ namespace mongo { StateMap threads; { - string orig = getParam( "host" ); + string orig = "localhost"; bool showPorts = false; - if ( orig == "" ) - orig = "localhost"; + if (toolGlobalParams.hostSet) { + orig = toolGlobalParams.host; + } - if ( orig.find( ":" ) != string::npos || hasParam( "port" ) ) + if (orig.find(":") != string::npos || toolGlobalParams.portSet) showPorts = true; StringSplitter ss( orig.c_str() , "," ); @@ -433,10 +395,14 @@ namespace mongo { string host = ss.next(); if ( showPorts && host.find( ":" ) == string::npos) { // port supplied, but not for this host. use default. - if ( hasParam( "port" ) ) - host += ":" + _params["port"].as<string>(); - else - host += ":27017"; + StringBuilder sb; + if (toolGlobalParams.portSet) { + sb << host << ":" << toolGlobalParams.port; + } + else { + sb << host << ":27017"; + } + host = sb.str(); } _add( threads , host ); } @@ -445,10 +411,9 @@ namespace mongo { sleepsecs(1); int row = 0; - bool discover = hasParam( "discover" ); int maxLockedDbWidth = 0; - while ( 1 ) { + while (mongoStatGlobalParams.rowCount == 0 || row < mongoStatGlobalParams.rowCount) { sleepsecs( (int)ceil(_statUtil.getSeconds()) ); // collect data @@ -467,7 +432,7 @@ namespace mongo { rows.push_back( Row( i->first , out ) ); } - if ( discover && ! i->second->now.isEmpty() ) { + if (mongoStatGlobalParams.discover && ! i->second->now.isEmpty()) { if ( _discover( threads , i->first , i->second ) ) break; } @@ -528,7 +493,7 @@ namespace mongo { cout << endl; // header - if ( row++ % 5 == 0 && ! biggest.isEmpty() ) { + if (row++ % 5 == 0 && mongoStatGlobalParams.showHeaders && !biggest.isEmpty()) { cout << setw( longestHost ) << "" << "\t"; printHeaders( biggest ); } @@ -550,8 +515,6 @@ namespace mongo { } StatUtil _statUtil; - bool _http; - bool _many; struct Row { Row( string h , string e ) { @@ -572,11 +535,5 @@ namespace mongo { BSONObj data; }; }; - -} - -int main( int argc , char ** argv, char ** envp ) { - mongo::runGlobalInitializersOrDie(argc, argv, envp); - mongo::Stat stat; - return stat.main( argc , argv ); + REGISTER_MONGO_TOOL(Stat); } |
