Files
mongo/tools/stat.cpp

545 lines
18 KiB
C++
Raw Normal View History

2010-02-23 15:37:30 -05:00
// stat.cpp
/**
* Copyright (C) 2008 10gen Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* 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 Affero General Public License for more details.
*
* 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/>.
*/
2010-04-27 15:27:52 -04:00
#include "pch.h"
2010-02-23 15:37:30 -05:00
#include "client/dbclient.h"
#include "db/json.h"
#include "../util/net/httpclient.h"
#include "../util/text.h"
2010-02-23 15:37:30 -05:00
#include "tool.h"
2011-11-27 02:36:50 -05:00
#include "stat_util.h"
2010-02-23 15:37:30 -05:00
#include <fstream>
#include <iostream>
#include <boost/program_options.hpp>
namespace po = boost::program_options;
namespace mongo {
2011-01-04 00:40:41 -05:00
2010-02-23 15:37:30 -05:00
class Stat : public Tool {
public:
2011-01-04 00:40:41 -05:00
Stat() : Tool( "stat" , REMOTE_SERVER , "admin" ) {
2010-04-16 01:15:09 -04:00
_http = false;
_many = false;
2011-01-04 00:40:41 -05:00
2010-02-28 12:35:18 -05:00
add_hidden_options()
2011-01-04 00:40:41 -05:00
( "sleep" , po::value<int>() , "time to sleep between calls" )
;
add_options()
2011-01-04 00:40:41 -05:00
("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" )
;
2010-02-28 12:35:18 -05:00
addPositionArg( "sleep" , 1 );
2010-06-09 13:20:35 -04:00
_autoreconnect = true;
2010-02-23 15:37:30 -05:00
}
2011-01-04 00:40:41 -05:00
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;
}
2011-01-04 00:40:41 -05:00
virtual void printExtraHelpAfter( ostream & out ) {
2010-07-12 13:17:34 -04:00
out << "\n";
out << " Fields\n";
2011-02-26 11:45:10 -05:00
out << " inserts \t- # of inserts per second (* means replicated op)\n";
2011-02-26 11:02:39 -05:00
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";
2011-02-26 11:45:10 -05:00
out << " command \t- # of commands per second, on a slave its local|replicated\n";
2011-02-26 11:02:39 -05:00
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 (linux only)\n";
out << " locked \t- percent of time in global write lock\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";
2011-02-26 11:45:10 -05:00
out << " set \t- replica set name\n";
out << " repl \t- replication type \n";
out << " \t PRI - primary (master)\n";
2011-02-26 11:45:10 -05:00
out << " \t SEC - secondary\n";
out << " \t REC - recovering\n";
out << " \t UNK - unknown\n";
out << " \t SLV - slave\n";
2011-09-28 16:13:13 -04:00
out << " \t RTR - mongos process (\"router\")\n";
2010-07-12 13:17:34 -04:00
}
2011-01-04 00:40:41 -05:00
BSONObj stats() {
if ( _http ) {
2010-04-16 01:15:09 -04:00
HttpClient c;
HttpClient::Result r;
2011-01-04 00:40:41 -05:00
2010-04-16 01:15:09 -04:00
string url;
{
stringstream ss;
ss << "http://" << _host;
if ( _host.find( ":" ) == string::npos )
ss << ":28017";
ss << "/_status";
url = ss.str();
}
2011-01-04 00:40:41 -05:00
if ( c.get( url , &r ) != 200 ) {
2010-04-16 01:15:09 -04:00
cout << "error (http): " << r.getEntireResponse() << endl;
return BSONObj();
}
2011-01-04 00:40:41 -05:00
2010-04-16 01:15:09 -04:00
BSONObj x = fromjson( r.getBody() );
BSONElement e = x["serverStatus"];
2011-01-04 00:40:41 -05:00
if ( e.type() != Object ) {
2010-04-16 01:15:09 -04:00
cout << "BROKEN: " << x << endl;
return BSONObj();
}
return e.embeddedObjectUserCheck();
}
2010-02-23 15:37:30 -05:00
BSONObj out;
2011-01-04 00:40:41 -05:00
if ( ! conn().simpleCommand( _db , &out , "serverStatus" ) ) {
2010-02-23 15:37:30 -05:00
cout << "error: " << out << endl;
return BSONObj();
}
return out.getOwned();
}
2011-01-04 00:40:41 -05:00
virtual void preSetup() {
if ( hasParam( "http" ) ) {
2010-04-16 01:15:09 -04:00
_http = true;
_noconnection = true;
}
2011-01-04 00:40:41 -05:00
if ( hasParam( "host" ) &&
getParam( "host" ).find( ',' ) != string::npos ) {
_noconnection = true;
_many = true;
}
2010-10-15 13:56:40 -04:00
2011-01-04 00:40:41 -05:00
if ( hasParam( "discover" ) ) {
2010-10-15 13:56:40 -04:00
_many = true;
}
2010-04-16 01:15:09 -04:00
}
2011-01-04 00:40:41 -05:00
int run() {
2011-11-29 00:53:53 -05:00
_statUtil.setSeconds( getParam( "sleep" , 1 ) );
2011-11-27 02:36:50 -05:00
_statUtil.setAll( hasParam( "all" ) );
if ( _many )
return runMany();
return runNormal();
}
2011-01-04 00:40:41 -05:00
static void printHeaders( const BSONObj& o ) {
BSONObjIterator i(o);
2011-01-04 00:40:41 -05:00
while ( i.more() ) {
BSONElement e = i.next();
BSONObj x = e.Obj();
cout << setw( x["width"].numberInt() ) << e.fieldName() << ' ';
}
2011-01-04 00:40:41 -05:00
cout << endl;
}
2011-01-04 00:40:41 -05:00
static void printData( const BSONObj& o , const BSONObj& headers ) {
BSONObjIterator i(headers);
2011-01-04 00:40:41 -05:00
while ( i.more() ) {
BSONElement e = i.next();
BSONObj h = e.Obj();
int w = h["width"].numberInt();
2011-01-04 00:40:41 -05:00
BSONElement data;
{
BSONElement temp = o[e.fieldName()];
if ( temp.isABSONObj() )
data = temp.Obj()["data"];
}
2011-01-04 00:40:41 -05:00
if ( data.type() == String )
cout << setw(w) << data.String();
else if ( data.type() == NumberDouble )
cout << setw(w) << setprecision(3) << data.number();
else if ( data.type() == NumberInt )
cout << setw(w) << data.numberInt();
else if ( data.eoo() )
cout << setw(w) << "";
2011-01-04 00:40:41 -05:00
else
cout << setw(w) << "???";
2011-01-04 00:40:41 -05:00
cout << ' ';
}
2011-01-04 00:40:41 -05:00
cout << endl;
}
2011-01-04 00:40:41 -05:00
int runNormal() {
bool showHeaders = ! hasParam( "noheaders" );
int rowCount = getParam( "rowcount" , 0 );
int rowNum = 0;
auth();
2010-02-23 15:37:30 -05:00
BSONObj prev = stats();
if ( prev.isEmpty() )
return -1;
2011-01-04 00:40:41 -05:00
while ( rowCount == 0 || rowNum < rowCount ) {
2011-11-29 00:53:53 -05:00
sleepsecs((int)ceil(_statUtil.getSeconds()));
2010-06-09 13:20:35 -04:00
BSONObj now;
try {
now = stats();
}
2011-01-04 00:40:41 -05:00
catch ( std::exception& e ) {
2010-06-09 13:20:35 -04:00
cout << "can't get data: " << e.what() << endl;
continue;
}
2010-02-23 15:37:30 -05:00
if ( now.isEmpty() )
return -2;
2011-01-04 00:40:41 -05:00
2010-04-16 01:15:09 -04:00
try {
2011-11-27 02:36:50 -05:00
BSONObj out = _statUtil.doRow( prev , now );
2011-01-04 00:40:41 -05:00
if ( showHeaders && rowNum % 10 == 0 ) {
printHeaders( out );
}
2011-01-04 00:40:41 -05:00
printData( out , out );
2010-04-16 01:15:09 -04:00
}
2011-01-04 00:40:41 -05:00
catch ( AssertionException& e ) {
2010-04-16 01:15:09 -04:00
cout << "\nerror: " << e.what() << "\n"
<< now
<< endl;
}
2011-01-04 00:40:41 -05:00
2010-02-23 15:37:30 -05:00
prev = now;
rowNum++;
2010-02-23 15:37:30 -05:00
}
return 0;
}
2011-01-04 00:40:41 -05:00
struct ServerState {
2011-01-04 00:40:41 -05:00
ServerState() : lock( "Stat::ServerState" ) {}
string host;
scoped_ptr<boost::thread> thr;
2011-01-04 00:40:41 -05:00
mongo::mutex lock;
2011-01-04 00:40:41 -05:00
BSONObj prev;
BSONObj now;
time_t lastUpdate;
vector<BSONObj> shards;
string error;
bool mongos;
string username;
string password;
};
2011-01-04 00:40:41 -05:00
static void serverThread( shared_ptr<ServerState> state ) {
try {
DBClientConnection conn( true );
conn._logLevel = 1;
string errmsg;
if ( ! conn.connect( state->host , errmsg ) )
state->error = errmsg;
long long cycleNumber = 0;
conn.auth("admin", state->username, state->password, errmsg);
2011-01-04 00:40:41 -05:00
while ( ++cycleNumber ) {
try {
BSONObj out;
2011-01-04 00:40:41 -05:00
if ( conn.simpleCommand( "admin" , &out , "serverStatus" ) ) {
scoped_lock lk( state->lock );
state->error = "";
state->lastUpdate = time(0);
state->prev = state->now;
state->now = out.getOwned();
}
else {
scoped_lock lk( state->lock );
state->error = "serverStatus failed";
state->lastUpdate = time(0);
}
2011-01-04 00:40:41 -05:00
if ( out["shardCursorType"].type() == Object ) {
state->mongos = true;
2011-01-04 00:40:41 -05:00
if ( cycleNumber % 10 == 1 ) {
auto_ptr<DBClientCursor> c = conn.query( "config.shards" , BSONObj() );
vector<BSONObj> shards;
2011-01-04 00:40:41 -05:00
while ( c->more() ) {
shards.push_back( c->next().getOwned() );
}
scoped_lock lk( state->lock );
state->shards = shards;
}
}
}
2011-01-04 00:40:41 -05:00
catch ( std::exception& e ) {
scoped_lock lk( state->lock );
state->error = e.what();
}
2011-01-04 00:40:41 -05:00
sleepsecs( 1 );
}
2011-01-04 00:40:41 -05:00
}
2011-01-04 00:40:41 -05:00
catch ( std::exception& e ) {
cout << "serverThread (" << state->host << ") fatal error : " << e.what() << endl;
}
2011-01-04 00:40:41 -05:00
catch ( ... ) {
cout << "serverThread (" << state->host << ") fatal error" << endl;
}
}
2010-10-15 13:56:40 -04:00
typedef map<string,shared_ptr<ServerState> > StateMap;
2011-01-04 00:40:41 -05:00
bool _add( StateMap& threads , string host ) {
2010-10-15 13:56:40 -04:00
shared_ptr<ServerState>& state = threads[host];
if ( state )
return false;
2011-01-04 00:40:41 -05:00
2010-10-15 13:56:40 -04:00
state.reset( new ServerState() );
state->host = host;
state->thr.reset( new boost::thread( boost::bind( serverThread , state ) ) );
state->username = _username;
state->password = _password;
2010-10-15 13:56:40 -04:00
return true;
}
2011-01-04 00:40:41 -05:00
/**
* @param hosts [ "a.foo.com" , "b.foo.com" ]
*/
2011-01-04 00:40:41 -05:00
bool _addAll( StateMap& threads , const BSONObj& hosts ) {
2010-10-15 13:56:40 -04:00
BSONObjIterator i( hosts );
bool added = false;
2011-01-04 00:40:41 -05:00
while ( i.more() ) {
2010-10-15 13:56:40 -04:00
bool me = _add( threads , i.next().String() );
added = added || me;
}
return added;
}
2011-01-04 00:40:41 -05:00
bool _discover( StateMap& threads , const string& host , const shared_ptr<ServerState>& ss ) {
BSONObj info = ss->now;
bool found = false;
2011-01-04 00:40:41 -05:00
if ( info["repl"].isABSONObj() ) {
BSONObj x = info["repl"].Obj();
if ( x["hosts"].isABSONObj() )
if ( _addAll( threads , x["hosts"].Obj() ) )
found = true;
if ( x["passives"].isABSONObj() )
if ( _addAll( threads , x["passives"].Obj() ) )
found = true;
}
2011-01-04 00:40:41 -05:00
if ( ss->mongos ) {
for ( unsigned i=0; i<ss->shards.size(); i++ ) {
BSONObj x = ss->shards[i];
string errmsg;
ConnectionString cs = ConnectionString::parse( x["host"].String() , errmsg );
2011-01-04 00:40:41 -05:00
if ( errmsg.size() ) {
cerr << errmsg << endl;
continue;
}
2011-01-04 00:40:41 -05:00
vector<HostAndPort> v = cs.getServers();
2011-01-04 00:40:41 -05:00
for ( unsigned i=0; i<v.size(); i++ ) {
if ( _add( threads , v[i].toString() ) )
found = true;
}
}
}
2011-01-04 00:40:41 -05:00
return found;
}
2011-01-04 00:40:41 -05:00
int runMany() {
2010-10-15 13:56:40 -04:00
StateMap threads;
{
string orig = getParam( "host" );
2010-10-15 13:56:40 -04:00
if ( orig == "" )
orig = "localhost";
if ( orig.find( ":" ) == string::npos ) {
if ( hasParam( "port" ) )
orig += ":" + _params["port"].as<string>();
else
orig += ":27017";
}
StringSplitter ss( orig.c_str() , "," );
2011-01-04 00:40:41 -05:00
while ( ss.more() ) {
string host = ss.next();
2010-10-15 13:56:40 -04:00
_add( threads , host );
}
}
2011-01-04 00:40:41 -05:00
sleepsecs(1);
2011-01-04 00:40:41 -05:00
int row = 0;
2010-10-15 13:56:40 -04:00
bool discover = hasParam( "discover" );
2011-01-04 00:40:41 -05:00
while ( 1 ) {
2011-11-29 00:53:53 -05:00
sleepsecs( (int)ceil(_statUtil.getSeconds()) );
2011-01-04 00:40:41 -05:00
// collect data
vector<Row> rows;
2011-01-04 00:40:41 -05:00
for ( map<string,shared_ptr<ServerState> >::iterator i=threads.begin(); i!=threads.end(); ++i ) {
scoped_lock lk( i->second->lock );
2011-01-04 00:40:41 -05:00
if ( i->second->error.size() ) {
rows.push_back( Row( i->first , i->second->error ) );
}
2011-01-04 00:40:41 -05:00
else if ( i->second->prev.isEmpty() || i->second->now.isEmpty() ) {
rows.push_back( Row( i->first ) );
}
else {
2011-11-27 02:36:50 -05:00
BSONObj out = _statUtil.doRow( i->second->prev , i->second->now );
rows.push_back( Row( i->first , out ) );
}
2011-01-04 00:40:41 -05:00
if ( discover && ! i->second->now.isEmpty() ) {
if ( _discover( threads , i->first , i->second ) )
break;
2010-10-15 13:56:40 -04:00
}
}
2011-01-04 00:40:41 -05:00
// compute some stats
unsigned longestHost = 0;
BSONObj biggest;
2011-01-04 00:40:41 -05:00
for ( unsigned i=0; i<rows.size(); i++ ) {
if ( rows[i].host.size() > longestHost )
longestHost = rows[i].host.size();
if ( rows[i].data.nFields() > biggest.nFields() )
biggest = rows[i].data;
}
2010-11-03 18:37:31 -04:00
2011-01-04 00:40:41 -05:00
{
// check for any headers not in biggest
// TODO: we put any new headers at end,
2010-11-03 18:37:31 -04:00
// ideally we would interleave
set<string> seen;
2011-01-04 00:40:41 -05:00
2010-11-03 18:37:31 -04:00
BSONObjBuilder b;
2011-01-04 00:40:41 -05:00
{
// iterate biggest
2010-11-03 18:37:31 -04:00
BSONObjIterator i( biggest );
2011-01-04 00:40:41 -05:00
while ( i.more() ) {
2010-11-03 18:37:31 -04:00
BSONElement e = i.next();
seen.insert( e.fieldName() );
b.append( e );
}
}
2011-01-04 00:40:41 -05:00
2010-11-03 18:37:31 -04:00
// now do the rest
2011-01-04 00:40:41 -05:00
for ( unsigned j=0; j<rows.size(); j++ ) {
2010-11-03 18:37:31 -04:00
BSONObjIterator i( rows[j].data );
2011-01-04 00:40:41 -05:00
while ( i.more() ) {
2010-11-03 18:37:31 -04:00
BSONElement e = i.next();
if ( seen.count( e.fieldName() ) )
continue;
seen.insert( e.fieldName() );
b.append( e );
}
}
biggest = b.obj();
2011-01-04 00:40:41 -05:00
2010-11-03 18:37:31 -04:00
}
2011-01-04 00:40:41 -05:00
// display data
2011-01-04 00:40:41 -05:00
cout << endl;
// header
2011-01-04 00:40:41 -05:00
if ( row++ % 5 == 0 && ! biggest.isEmpty() ) {
cout << setw( longestHost ) << "" << "\t";
printHeaders( biggest );
}
2011-01-04 00:40:41 -05:00
// rows
2011-01-04 00:40:41 -05:00
for ( unsigned i=0; i<rows.size(); i++ ) {
cout << setw( longestHost ) << rows[i].host << "\t";
if ( rows[i].err.size() )
cout << rows[i].err << endl;
else if ( rows[i].data.isEmpty() )
cout << "no data" << endl;
2011-01-04 00:40:41 -05:00
else
printData( rows[i].data , biggest );
}
2011-01-04 00:40:41 -05:00
}
return 0;
}
2010-02-23 15:37:30 -05:00
2011-11-27 02:36:50 -05:00
StatUtil _statUtil;
2010-04-16 01:15:09 -04:00
bool _http;
bool _many;
struct Row {
2011-01-04 00:40:41 -05:00
Row( string h , string e ) {
host = h;
err = e;
}
2011-01-04 00:40:41 -05:00
Row( string h ) {
host = h;
}
2011-01-04 00:40:41 -05:00
Row( string h , BSONObj d ) {
host = h;
data = d;
}
string host;
string err;
BSONObj data;
};
2010-02-23 15:37:30 -05:00
};
}
int main( int argc , char ** argv ) {
mongo::Stat stat;
return stat.main( argc , argv );
}