Files
mongo/db/modules/mms.cpp

171 lines
5.6 KiB
C++
Raw Normal View History

2010-06-01 08:56:06 -04:00
// @file mms.cpp
/*
* Copyright (C) 2010 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/>.
*/
2009-07-17 15:49:24 -04:00
2010-04-27 15:27:52 -04:00
#include "pch.h"
2009-11-18 12:53:56 -05:00
#include "../db.h"
#include "../instance.h"
#include "../module.h"
#include "../../util/httpclient.h"
#include "../../util/background.h"
2010-02-12 13:33:22 -05:00
#include "../commands.h"
2009-11-18 12:53:56 -05:00
namespace po = boost::program_options;
2009-07-17 15:49:24 -04:00
namespace mongo {
2009-11-18 12:53:56 -05:00
/** Mongo Monitoring Service
if enabled, this runs in the background ands pings mss
2010-02-12 10:00:50 -05:00
*/
2009-11-18 12:53:56 -05:00
class MMS : public BackgroundJob , Module {
public:
MMS()
: Module( "mms" ) , _baseurl( "" ) ,
2009-11-18 12:53:56 -05:00
_secsToSleep(1) , _token( "" ) , _name( "" ) {
add_options()
2010-02-12 13:47:27 -05:00
( "mms-url" , po::value<string>()->default_value("http://mms.10gen.com/ping") , "url for mongo monitoring server" )
2009-11-18 12:53:56 -05:00
( "mms-token" , po::value<string>() , "account token for mongo monitoring server" )
( "mms-name" , po::value<string>() , "server name for mongo monitoring server" )
( "mms-interval" , po::value<int>()->default_value(30) , "ping interval (in seconds) for mongo monitoring server" )
2009-11-18 12:53:56 -05:00
;
}
2009-07-17 15:49:24 -04:00
2009-11-18 12:53:56 -05:00
~MMS(){}
2010-02-12 10:00:50 -05:00
2009-11-18 12:53:56 -05:00
void config( program_options::variables_map& params ){
_baseurl = params["mms-url"].as<string>();
2009-11-18 12:53:56 -05:00
if ( params.count( "mms-token" ) ){
_token = params["mms-token"].as<string>();
}
if ( params.count( "mms-name" ) ){
_name = params["mms-name"].as<string>();
}
_secsToSleep = params["mms-interval"].as<int>();
}
2009-07-17 15:49:24 -04:00
2009-11-18 12:53:56 -05:00
void run(){
2010-02-12 10:00:50 -05:00
if ( _token.size() == 0 && _name.size() == 0 ){
log(1) << "mms not configured" << endl;
return;
}
2009-07-17 15:49:24 -04:00
2010-02-12 10:00:50 -05:00
if ( _token.size() == 0 ){
log() << "no token for mms - not running" << endl;
return;
}
2009-07-17 15:49:24 -04:00
2010-02-12 10:00:50 -05:00
if ( _name.size() == 0 ){
log() << "no name for mms - not running" << endl;
return;
}
2009-07-17 15:49:24 -04:00
2010-02-12 11:33:15 -05:00
log() << "mms monitor staring... token:" << _token << " name:" << _name << " interval: " << _secsToSleep << endl;
Client::initThread( "mms" );
Client& c = cc();
2009-07-17 15:49:24 -04:00
2010-02-12 11:33:15 -05:00
// TODO: using direct client is bad, but easy for now
2009-07-17 15:49:24 -04:00
2010-02-12 11:33:15 -05:00
while ( ! inShutdown() ){
sleepsecs( _secsToSleep );
2010-02-12 10:00:50 -05:00
try {
2010-02-12 13:33:22 -05:00
stringstream url;
url << _baseurl << "?"
<< "token=" << _token << "&"
<< "name=" << _name << "&"
<< "ts=" << time(0)
;
BSONObjBuilder bb;
// duplicated so the post has everything
bb.append( "token" , _token );
2010-02-12 14:23:05 -05:00
bb.append( "name" , _name );
2010-02-12 13:33:22 -05:00
bb.appendDate( "ts" , jsTime() );
// any commands
_add( bb , "buildinfo" );
_add( bb , "serverStatus" );
BSONObj postData = bb.obj();
log(1) << "mms url: " << url.str() << "\n\t post: " << postData << endl;;
2010-02-12 10:00:50 -05:00
HttpClient c;
2010-02-12 11:33:15 -05:00
HttpClient::Result r;
int rc = c.post( url.str() , postData.jsonString() , &r );
2010-02-12 10:00:50 -05:00
log(1) << "\t response code: " << rc << endl;
if ( rc != 200 ){
log() << "mms error response code:" << rc << endl;
2010-02-12 11:33:15 -05:00
log(1) << "mms error body:" << r.getEntireResponse() << endl;
2010-02-12 10:00:50 -05:00
}
}
catch ( std::exception& e ){
2010-02-12 13:33:22 -05:00
log() << "mms exception: " << e.what() << endl;
2009-07-17 15:49:24 -04:00
}
}
2010-02-12 11:33:15 -05:00
c.shutdown();
2009-11-18 12:53:56 -05:00
}
2010-02-12 11:33:15 -05:00
void _add( BSONObjBuilder& postData , const char* cmd ){
2010-02-12 13:33:22 -05:00
Command * c = Command::findCommand( cmd );
if ( ! c ){
log() << "MMS can't find command: " << cmd << endl;
postData.append( cmd , "can't find command" );
2010-02-12 11:33:15 -05:00
return;
}
2010-02-12 13:33:22 -05:00
2010-02-26 14:38:51 -05:00
if ( c->locktype() ){
2010-02-12 14:33:43 -05:00
log() << "MMS can only use noLocking commands not: " << cmd << endl;
2010-02-12 13:33:22 -05:00
postData.append( cmd , "not noLocking" );
return;
}
BSONObj co = BSON( cmd << 1 );
2010-02-12 11:33:15 -05:00
2010-02-12 13:33:22 -05:00
string errmsg;
BSONObjBuilder sub;
if ( ! c->run( "admin.$cmd" , co , errmsg , sub , false ) )
postData.append( cmd , errmsg );
else
postData.append( cmd , sub.obj() );
2010-02-12 11:33:15 -05:00
}
2009-07-17 15:49:24 -04:00
2009-11-18 12:53:56 -05:00
void init(){ go(); }
void shutdown(){
// TODO
}
private:
string _baseurl;
int _secsToSleep;
string _token;
string _name;
2010-02-12 11:33:15 -05:00
2010-03-02 00:00:01 -05:00
} /*mms*/ ;
2009-07-17 15:49:24 -04:00
}
2009-11-18 12:53:56 -05:00