1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
|
// @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/>.
*/
#include "pch.h"
#include "../db.h"
#include "../instance.h"
#include "../module.h"
#include "../../util/net/httpclient.h"
#include "../../util/background.h"
#include "../commands.h"
namespace po = boost::program_options;
namespace mongo {
/** Mongo Monitoring Service
if enabled, this runs in the background ands pings mss
*/
class MMS : public BackgroundJob , Module {
public:
MMS()
: Module( "mms" ) , _baseurl( "" ) ,
_secsToSleep(1) , _token( "" ) , _name( "" ) {
add_options()
( "mms-url" , po::value<string>()->default_value("http://mms.10gen.com/ping") , "url for mongo monitoring server" )
( "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" )
;
}
~MMS() {}
void config( program_options::variables_map& params ) {
_baseurl = params["mms-url"].as<string>();
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>();
}
void run() {
if ( _token.size() == 0 && _name.size() == 0 ) {
log(1) << "mms not configured" << endl;
return;
}
if ( _token.size() == 0 ) {
log() << "no token for mms - not running" << endl;
return;
}
if ( _name.size() == 0 ) {
log() << "no name for mms - not running" << endl;
return;
}
log() << "mms monitor staring... token:" << _token << " name:" << _name << " interval: " << _secsToSleep << endl;
Client::initThread( "mms" );
Client& c = cc();
// TODO: using direct client is bad, but easy for now
while ( ! inShutdown() ) {
sleepsecs( _secsToSleep );
try {
stringstream url;
url << _baseurl << "?"
<< "token=" << _token << "&"
<< "name=" << _name << "&"
<< "ts=" << time(0)
;
BSONObjBuilder bb;
// duplicated so the post has everything
bb.append( "token" , _token );
bb.append( "name" , _name );
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;;
HttpClient c;
HttpClient::Result r;
int rc = c.post( url.str() , postData.jsonString() , &r );
log(1) << "\t response code: " << rc << endl;
if ( rc != 200 ) {
log() << "mms error response code:" << rc << endl;
log(1) << "mms error body:" << r.getEntireResponse() << endl;
}
}
catch ( std::exception& e ) {
log() << "mms exception: " << e.what() << endl;
}
}
c.shutdown();
}
void _add( BSONObjBuilder& postData , const char* cmd ) {
Command * c = Command::findCommand( cmd );
if ( ! c ) {
log() << "MMS can't find command: " << cmd << endl;
postData.append( cmd , "can't find command" );
return;
}
if ( c->locktype() ) {
log() << "MMS can only use noLocking commands not: " << cmd << endl;
postData.append( cmd , "not noLocking" );
return;
}
BSONObj co = BSON( cmd << 1 );
string errmsg;
BSONObjBuilder sub;
if ( ! c->run( "admin.$cmd" , co , 0 , errmsg , sub , false ) )
postData.append( cmd , errmsg );
else
postData.append( cmd , sub.obj() );
}
void init() { go(); }
void shutdown() {
// TODO
}
private:
string _baseurl;
int _secsToSleep;
string _token;
string _name;
} /*mms*/ ;
}
|