support doing mongostat over http

This commit is contained in:
Eliot Horowitz 2010-04-16 01:15:09 -04:00
parent d9e4b16738
commit 29b39b18de
3 changed files with 61 additions and 6 deletions

View File

@ -19,6 +19,7 @@
#include "stdafx.h"
#include "client/dbclient.h"
#include "db/json.h"
#include "../util/httpclient.h"
#include "tool.h"
@ -38,6 +39,7 @@ namespace mongo {
_sleep = 1;
_rowNum = 0;
_showHeaders = true;
_http = false;
add_hidden_options()
( "sleep" , po::value<int>() , "time to sleep between calls" )
@ -45,6 +47,7 @@ namespace mongo {
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")
;
addPositionArg( "sleep" , 1 );
@ -56,6 +59,33 @@ namespace mongo {
}
BSONObj stats(){
if ( _http ){
HttpClient c;
HttpClient::Result r;
string url;
{
stringstream ss;
ss << "http://" << _host;
if ( _host.find( ":" ) == string::npos )
ss << ":28017";
ss << "/_status";
url = ss.str();
}
if ( c.get( url , &r ) != 200 ){
cout << "error (http): " << r.getEntireResponse() << endl;
return BSONObj();
}
BSONObj x = fromjson( r.getBody() );
BSONElement e = x["serverStatus"];
if ( e.type() != Object ){
cout << "BROKEN: " << x << endl;
return BSONObj();
}
return e.embeddedObjectUserCheck();
}
BSONObj out;
if ( ! conn().simpleCommand( _db , &out , "serverStatus" ) ){
cout << "error: " << out << endl;
@ -154,6 +184,13 @@ namespace mongo {
return ss.str();
}
virtual void preSetup(){
if ( hasParam( "http" ) ){
_http = true;
_noconnection = true;
}
}
int run(){
_sleep = getParam( "sleep" , _sleep );
if ( hasParam( "noheaders" ) ) {
@ -171,7 +208,14 @@ namespace mongo {
if ( now.isEmpty() )
return -2;
cout << doRow( prev , now ) << endl;
try {
cout << doRow( prev , now ) << endl;
}
catch ( AssertionException& e ){
cout << "\nerror: " << e.what() << "\n"
<< now
<< endl;
}
prev = now;
}
@ -183,6 +227,7 @@ namespace mongo {
int _rowNum;
int _rowCount;
bool _showHeaders;
bool _http;
};
}

View File

@ -37,7 +37,7 @@ namespace mongo {
Tool::Tool( string name , bool localDBAllowed , string defaultDB ,
string defaultCollection , bool usesstdout ) :
_name( name ) , _db( defaultDB ) , _coll( defaultCollection ) ,
_usesstdout(usesstdout), _conn(0), _paired(false) {
_usesstdout(usesstdout), _noconnection(false), _conn(0), _paired(false) {
_options = new po::options_description( "options" );
_options->add_options()
@ -126,15 +126,20 @@ namespace mongo {
logLevel = s.length();
}
}
preSetup();
bool useDirectClient = hasParam( "dbpath" );
if ( ! useDirectClient ) {
_host = "127.0.0.1";
if ( _params.count( "host" ) )
_host = _params["host"].as<string>();
if ( _host.find( "," ) == string::npos ){
if ( _noconnection ){
// do nothing
}
else if ( _host.find( "," ) == string::npos ){
DBClientConnection * c = new DBClientConnection();
_conn = c;

View File

@ -72,6 +72,8 @@ namespace mongo {
}
return _db + "." + _coll;
}
virtual void preSetup(){}
virtual int run() = 0;
@ -93,6 +95,7 @@ namespace mongo {
string _password;
bool _usesstdout;
bool _noconnection;
void addFieldOptions();
void needFields();
@ -101,8 +104,10 @@ namespace mongo {
BSONObj _fieldsObj;
private:
string _host;
protected:
mongo::DBClientBase * _conn;
bool _paired;