From 7c54863213a6632096bd3e4d691263cef518002e Mon Sep 17 00:00:00 2001 From: Jens Rehsack Date: Wed, 24 Oct 2012 16:51:07 +0200 Subject: [PATCH] refactor for independent debug dumping ... --- src/.gitignore | 2 + src/Makefile | 11 +++-- src/watch/common.cpp | 20 +++++++++ src/watch/dump_mongodb.cpp | 102 ++++++++++++++++++++++++++++++++++++++++++++ src/watch/watch_mongodb.cpp | 72 +++---------------------------- 5 files changed, 139 insertions(+), 68 deletions(-) create mode 100644 src/watch/common.cpp create mode 100644 src/watch/dump_mongodb.cpp diff --git a/src/.gitignore b/src/.gitignore index 7fcf5c2..0df75d9 100644 --- a/src/.gitignore +++ b/src/.gitignore @@ -1,4 +1,6 @@ mongodb-stats +mongodb-dump +mongo_pw.cpp *.o *.swp *~ diff --git a/src/Makefile b/src/Makefile index 95a856c..87fb91b 100644 --- a/src/Makefile +++ b/src/Makefile @@ -3,14 +3,19 @@ .cpp.o: $(CXX) -c -g -o $@ $(CXXFLAGS) -pthread -Imongo -I. -I/usr/pkg/include -I/usr/include $< -all: mongodb-stats +all: mongodb-stats mongodb-dump mongo_client_lib.o: mongo/client/mongo_client_lib.cpp -watch_mongodb.o: watch/watch_mongodb.cpp mongo_pw.cpp +common.o: watch/common.cpp mongo_pw.cpp +dump_mongodb.o: watch/dump_mongodb.cpp +watch_mongodb.o: watch/watch_mongodb.cpp mongo_pw.cpp: mongo_client_lib.o $(PERL5) ../script/obfuscatepw.pl --nm-file mongo_client_lib.o --password $(MONGO_PW) --filter mongo\\d >mongo_pw.cpp -mongodb-stats: mongo_client_lib.o watch_mongodb.o +mongodb-dump: mongo_client_lib.o dump_mongodb.o common.o + $(CXX) -o $@ -L/usr/pkg/lib -Wl,-R/usr/pkg/lib -pthread -lboost_thread -lboost_filesystem -lboost_program_options -lboost_locale -lboost_timer $> + +mongodb-stats: mongo_client_lib.o watch_mongodb.o common.o $(CXX) -o $@ -L/usr/pkg/lib -Wl,-R/usr/pkg/lib -pthread -lboost_thread -lboost_filesystem -lboost_program_options -lboost_locale -lboost_timer $> diff --git a/src/watch/common.cpp b/src/watch/common.cpp new file mode 100644 index 0000000..719a649 --- /dev/null +++ b/src/watch/common.cpp @@ -0,0 +1,20 @@ +#include + +#include "mongo_pw.cpp" + +using namespace mongo; + +void +connect(DBClientConnection &c, + std::string const &dsn, + std::string const &dbname, + std::string const &user) +{ + std::string pw = summarize( get_summarizers() ); + + std::string errmsg; + c.connect(dsn); + c.auth(dbname, user, pw, errmsg); +} + + diff --git a/src/watch/dump_mongodb.cpp b/src/watch/dump_mongodb.cpp new file mode 100644 index 0000000..5153b72 --- /dev/null +++ b/src/watch/dump_mongodb.cpp @@ -0,0 +1,102 @@ +#include +#include + +#include +#include +#include + +using namespace mongo; +using namespace std; +using namespace boost; +using namespace boost::program_options; + +#if 1 +#define DBNAME "admin" +#else +#define DBNAME "local" +#endif + +extern +void connect(DBClientConnection &c, std::string const &dsn, std::string const &dbname, std::string const &user); + +void +info(DBClientConnection &c) +{ + BSONObj info, cmd; + + cmd = BSONObjBuilder().append( "serverStatus", 1 ).obj(); + c.runCommand(DBNAME, cmd, info); + cout << info << endl; + + cmd = BSONObjBuilder().append("replSetGetStatus", 1).obj(); + c.runCommand(DBNAME, cmd, info); + cout << info << endl; + + cmd = BSONObjBuilder().append("listDatabases", 1).obj(); + c.runCommand(DBNAME, cmd, info); + cout << info << endl; + + // foreach db in dbases { runCommand(db, "dbstats") } + cmd = BSONObjBuilder().append("dbstats", 1).obj(); + cout << info["databases"].size() << endl; + vector dbases = info["databases"].Array(); + cout << dbases.size() << " -- " << dbases.capacity() << endl; + for( vector::iterator iter = dbases.begin(); + iter != dbases.end(); + ++iter ) + { + if( iter->ok() ) + { + BSONObj dbinfo; + BSONElement &db = *iter; + string dbname; + + if( !db["name"].ok() ) + continue; + + db["name"].Val(dbname); + cout << dbname << endl; + c.runCommand(dbname, cmd, dbinfo); + cout << dbinfo << endl; + } + } +} + +int +main(int argc, char *argv[]) +{ + try + { + options_description desc("Allowed options"); + desc.add_options() + ("help", "produce help message") + ("dsn", value(), "set mongodb dsn") + ; + variables_map vm; + store( parse_command_line( argc, argv, desc ), vm ); + notify(vm); + + if( vm.count("help") ) + { + cout << desc << endl; + return 1; + } + + if( vm.count("dsn") == 0 ) + { + cerr << desc << endl; + return 255; + } + + DBClientConnection c; + + connect(c, vm["dsn"].as(), DBNAME, "admin"); + info(c); + } + catch( DBException &e ) + { + cout << "caught " << e.what() << endl; + } + + return 0; +} diff --git a/src/watch/watch_mongodb.cpp b/src/watch/watch_mongodb.cpp index d545a34..1dde587 100644 --- a/src/watch/watch_mongodb.cpp +++ b/src/watch/watch_mongodb.cpp @@ -21,68 +21,14 @@ using namespace std; using namespace boost; using namespace boost::program_options; -#include "mongo_pw.cpp" - #if 1 #define DBNAME "admin" #else #define DBNAME "local" #endif -void -connect(DBClientConnection &c, - std::string const &dsn, - std::string const &dbname, - std::string const &user, - std::string const &password) -{ - string errmsg; - c.connect(dsn); - c.auth(dbname, user, password, errmsg); -} - -void -info(DBClientConnection &c) -{ - BSONObj info, cmd; - - cmd = BSONObjBuilder().append( "serverStatus", 1 ).obj(); - c.runCommand(DBNAME, cmd, info); - cout << info << endl; - - cmd = BSONObjBuilder().append("replSetGetStatus", 1).obj(); - c.runCommand(DBNAME, cmd, info); - cout << info << endl; - - cmd = BSONObjBuilder().append("listDatabases", 1).obj(); - c.runCommand(DBNAME, cmd, info); - cout << info << endl; - - // foreach db in dbases { runCommand(db, "dbstats") } - cmd = BSONObjBuilder().append("dbstats", 1).obj(); - cout << info["databases"].size() << endl; - vector dbases = info["databases"].Array(); - cout << dbases.size() << " -- " << dbases.capacity() << endl; - for( vector::iterator iter = dbases.begin(); - iter != dbases.end(); - ++iter ) - { - if( iter->ok() ) - { - BSONObj dbinfo; - BSONElement &db = *iter; - string dbname; - - if( !db["name"].ok() ) - continue; - - db["name"].Val(dbname); - cout << dbname << endl; - c.runCommand(dbname, cmd, dbinfo); - cout << dbinfo << endl; - } - } -} +extern +void connect(DBClientConnection &c, std::string const &dsn, std::string const &dbname, std::string const &user); namespace boost { @@ -727,12 +673,11 @@ dump(set const &out_vals) << "]" << endl; } -int main(int argc, char *argv[]) +int +main(int argc, char *argv[]) { try { - string pw = summarize( get_summarizers() ); - options_description desc("Allowed options"); desc.add_options() ("help", "produce help message") @@ -756,16 +701,14 @@ int main(int argc, char *argv[]) set out_vals; do { + DBClientConnection c; + boost::timer::cpu_timer db_dur; db_dur.start(); - DBClientConnection c; - connect(c, vm["dsn"].as(), DBNAME, "admin", pw); - info(c); + connect(c, vm["dsn"].as(), DBNAME, "admin"); collect(c, out_vals); db_dur.stop(); - info(c); - out_val val = { ".99.1", SMI_COUNTER64 }; val.value = lexical_cast( db_dur.elapsed().user ); out_vals.insert( val ); @@ -779,7 +722,6 @@ int main(int argc, char *argv[]) out_vals.insert( val ); } while(0); - dump(out_vals); } catch( DBException &e ) -- 2.11.4.GIT