3
* Copyright (C) 2010 10gen Inc.
5
* This program is free software: you can redistribute it and/or modify
6
* it under the terms of the GNU Affero General Public License, version 3,
7
* as published by the Free Software Foundation.
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU Affero General Public License for more details.
14
* You should have received a copy of the GNU Affero General Public License
15
* along with this program. If not, see <http://www.gnu.org/licenses/>.
21
#include "../instance.h"
22
#include "../module.h"
23
#include "../../util/net/httpclient.h"
24
#include "../../util/background.h"
25
#include "../commands.h"
27
namespace po = boost::program_options;
31
/** Mongo Monitoring Service
32
if enabled, this runs in the background ands pings mss
34
class MMS : public BackgroundJob , Module {
38
: Module( "mms" ) , _baseurl( "" ) ,
39
_secsToSleep(1) , _token( "" ) , _name( "" ) {
42
( "mms-url" , po::value<string>()->default_value("http://mms.10gen.com/ping") , "url for mongo monitoring server" )
43
( "mms-token" , po::value<string>() , "account token for mongo monitoring server" )
44
( "mms-name" , po::value<string>() , "server name for mongo monitoring server" )
45
( "mms-interval" , po::value<int>()->default_value(30) , "ping interval (in seconds) for mongo monitoring server" )
51
void config( boost::program_options::variables_map& params ) {
52
_baseurl = params["mms-url"].as<string>();
53
if ( params.count( "mms-token" ) ) {
54
_token = params["mms-token"].as<string>();
56
if ( params.count( "mms-name" ) ) {
57
_name = params["mms-name"].as<string>();
59
_secsToSleep = params["mms-interval"].as<int>();
63
if ( _token.size() == 0 && _name.size() == 0 ) {
64
LOG(1) << "mms not configured" << endl;
68
if ( _token.size() == 0 ) {
69
log() << "no token for mms - not running" << endl;
73
if ( _name.size() == 0 ) {
74
log() << "no name for mms - not running" << endl;
78
log() << "mms monitor staring... token:" << _token << " name:" << _name << " interval: " << _secsToSleep << endl;
79
Client::initThread( "mms" );
83
// TODO: using direct client is bad, but easy for now
85
while ( ! inShutdown() ) {
86
sleepsecs( _secsToSleep );
90
url << _baseurl << "?"
91
<< "token=" << _token << "&"
92
<< "name=" << _name << "&"
97
// duplicated so the post has everything
98
bb.append( "token" , _token );
99
bb.append( "name" , _name );
100
bb.appendDate( "ts" , jsTime() );
103
_add( bb , "buildinfo" );
104
_add( bb , "serverStatus" );
106
BSONObj postData = bb.obj();
108
LOG(1) << "mms url: " << url.str() << "\n\t post: " << postData << endl;;
111
HttpClient::Result r;
112
int rc = c.post( url.str() , postData.jsonString() , &r );
113
LOG(1) << "\t response code: " << rc << endl;
115
log() << "mms error response code:" << rc << endl;
116
LOG(1) << "mms error body:" << r.getEntireResponse() << endl;
119
catch ( std::exception& e ) {
120
log() << "mms exception: " << e.what() << endl;
127
void _add( BSONObjBuilder& postData , const char* cmd ) {
128
Command * c = Command::findCommand( cmd );
130
log() << "MMS can't find command: " << cmd << endl;
131
postData.append( cmd , "can't find command" );
135
if ( c->locktype() ) {
136
log() << "MMS can only use noLocking commands not: " << cmd << endl;
137
postData.append( cmd , "not noLocking" );
141
BSONObj co = BSON( cmd << 1 );
145
if ( ! c->run( "admin.$cmd" , co , 0 , errmsg , sub , false ) )
146
postData.append( cmd , errmsg );
148
postData.append( cmd , sub.obj() );
152
void init() { go(); }