~evarlast/ubuntu/utopic/mongodb/upstart-workaround-debian-bug-718702

« back to all changes in this revision

Viewing changes to src/mongo/db/modules/mms.cpp

  • Committer: Package Import Robot
  • Author(s): James Page, James Page, Robie Basak
  • Date: 2013-05-29 17:44:42 UTC
  • mfrom: (44.1.7 sid)
  • Revision ID: package-import@ubuntu.com-20130529174442-z0a4qmoww4y0t458
Tags: 1:2.4.3-1ubuntu1
[ James Page ]
* Merge from Debian unstable, remaining changes:
  - Enable SSL support:
    + d/control: Add libssl-dev to BD's.
    + d/rules: Enabled --ssl option.
    + d/mongodb.conf: Add example SSL configuration options.
  - d/mongodb-server.mongodb.upstart: Add upstart configuration.
  - d/rules: Don't strip binaries during scons build for Ubuntu.
  - d/control: Add armhf to target archs.
  - d/p/SConscript.client.patch: fixup install of client libraries.
  - d/p/0010-install-libs-to-usr-lib-not-usr-lib64-Closes-588557.patch:
    Install libraries to lib not lib64.
* Dropped changes:
  - d/p/arm-support.patch: Included in Debian.
  - d/p/double-alignment.patch: Included in Debian.
  - d/rules,control: Debian also builds with avaliable system libraries
    now.
* Fix FTBFS due to gcc and boost upgrades in saucy:
  - d/p/0008-ignore-unused-local-typedefs.patch: Add -Wno-unused-typedefs
    to unbreak building with g++-4.8.
  - d/p/0009-boost-1.53.patch: Fixup signed/unsigned casting issue.

[ Robie Basak ]
* d/p/0011-Use-a-signed-char-to-store-BSONType-enumerations.patch: Fixup
  build failure on ARM due to missing signed'ness of char cast.

Show diffs side-by-side

added added

removed removed

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