summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMark Benvenuto <mark.benvenuto@mongodb.com>2016-07-21 13:16:55 -0400
committerMark Benvenuto <mark.benvenuto@mongodb.com>2016-07-21 13:16:55 -0400
commitc1042b38c9f6469ddcce2dfdf5dacbff0334cd14 (patch)
tree1fb45bd9b046a700c986087051abda4eda2903c8 /src
parent28d04fda95321c84c402338c0849155140dc6cff (diff)
downloadmongo-c1042b38c9f6469ddcce2dfdf5dacbff0334cd14.tar.gz
SERVER-24616 Add new getDiagnosticData command
Diffstat (limited to 'src')
-rw-r--r--src/mongo/db/ftdc/SConscript1
-rw-r--r--src/mongo/db/ftdc/controller.cpp13
-rw-r--r--src/mongo/db/ftdc/controller.h18
-rw-r--r--src/mongo/db/ftdc/ftdc_commands.cpp115
-rw-r--r--src/mongo/db/ftdc/ftdc_mongod.cpp8
-rw-r--r--src/mongo/s/commands/SConscript1
-rw-r--r--src/mongo/s/commands/cluster_ftdc_commands.cpp95
7 files changed, 250 insertions, 1 deletions
diff --git a/src/mongo/db/ftdc/SConscript b/src/mongo/db/ftdc/SConscript
index 20294484bb4..ba630ba68c1 100644
--- a/src/mongo/db/ftdc/SConscript
+++ b/src/mongo/db/ftdc/SConscript
@@ -38,6 +38,7 @@ if env.TargetOSIs('linux'):
env.Library(
target='ftdc_mongod',
source=[
+ 'ftdc_commands.cpp',
'ftdc_mongod.cpp',
'ftdc_system_stats.cpp',
],
diff --git a/src/mongo/db/ftdc/controller.cpp b/src/mongo/db/ftdc/controller.cpp
index 3639be9ebed..bf3e80437be 100644
--- a/src/mongo/db/ftdc/controller.cpp
+++ b/src/mongo/db/ftdc/controller.cpp
@@ -99,6 +99,13 @@ void FTDCController::addOnRotateCollector(std::unique_ptr<FTDCCollectorInterface
}
}
+BSONObj FTDCController::getMostRecentPeriodicDocument() {
+ {
+ stdx::lock_guard<stdx::mutex> lock(_mutex);
+ return _mostRecentPeriodicDocument.getOwned();
+ }
+}
+
void FTDCController::start() {
log() << "Initializing full-time diagnostic data capture with directory '"
<< _path.generic_string() << "'";
@@ -211,6 +218,12 @@ void FTDCController::doLoop() {
client, std::get<0>(collectSample), std::get<1>(collectSample));
uassertStatusOK(s);
+
+ // Store a reference to the most recent document from the periodic collectors
+ {
+ stdx::lock_guard<stdx::mutex> lock(_mutex);
+ _mostRecentPeriodicDocument = std::get<0>(collectSample);
+ }
}
}
} catch (...) {
diff --git a/src/mongo/db/ftdc/controller.h b/src/mongo/db/ftdc/controller.h
index 3db82b6b171..44b5e838506 100644
--- a/src/mongo/db/ftdc/controller.h
+++ b/src/mongo/db/ftdc/controller.h
@@ -43,6 +43,8 @@
namespace mongo {
+class ServiceContext;
+
/**
* Responsible for periodic collection of samples, writing them to disk,
* and rotation.
@@ -118,6 +120,16 @@ public:
*/
void stop();
+ /**
+ * Get the FTDCController from ServiceContext.
+ */
+ static FTDCController* get(ServiceContext* serviceContext);
+
+ /**
+ * Get a reference to most recent document from the periodic collectors.
+ */
+ BSONObj getMostRecentPeriodicDocument();
+
private:
/**
* Do periodic statistics collection, and all other work on the background thread.
@@ -162,7 +174,7 @@ private:
// Directory to store files
const boost::filesystem::path _path;
- // Mutex to protect the condvar, and configuration changes.
+ // Mutex to protect the condvar, configuration changes, and most recent periodic document.
stdx::mutex _mutex;
stdx::condition_variable _condvar;
@@ -176,6 +188,10 @@ private:
// Set of periodic collectors
FTDCCollectorCollection _periodicCollectors;
+ // Last seen sample document from periodic collectors
+ // Owned
+ BSONObj _mostRecentPeriodicDocument;
+
// Set of file rotation collectors
FTDCCollectorCollection _rotateCollectors;
diff --git a/src/mongo/db/ftdc/ftdc_commands.cpp b/src/mongo/db/ftdc/ftdc_commands.cpp
new file mode 100644
index 00000000000..8fea20ee1da
--- /dev/null
+++ b/src/mongo/db/ftdc/ftdc_commands.cpp
@@ -0,0 +1,115 @@
+/**
+ * Copyright (C) 2016 MongoDB 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/>.
+ *
+ * As a special exception, the copyright holders give permission to link the
+ * code of portions of this program with the OpenSSL library under certain
+ * conditions as described in each individual source file and distribute
+ * linked combinations including the program with the OpenSSL library. You
+ * must comply with the GNU Affero General Public License in all respects
+ * for all of the code used other than as permitted herein. If you modify
+ * file(s) with this exception, you may extend this exception to your
+ * version of the file(s), but you are not obligated to do so. If you do not
+ * wish to do so, delete this exception statement from your version. If you
+ * delete this exception statement from all source files in the program,
+ * then also delete it in the license file.
+ */
+
+#include "mongo/platform/basic.h"
+
+#include "mongo/base/init.h"
+#include "mongo/db/auth/action_type.h"
+#include "mongo/db/auth/authorization_session.h"
+#include "mongo/db/client.h"
+#include "mongo/db/commands.h"
+#include "mongo/db/ftdc/controller.h"
+#include "mongo/db/jsobj.h"
+#include "mongo/db/operation_context.h"
+
+namespace mongo {
+namespace {
+
+/**
+ * Get the most recent document FTDC collected from its periodic collectors.
+ *
+ * Document will be empty if FTDC has never run.
+ */
+class GetDiagnosticDataCommand final : public Command {
+public:
+ GetDiagnosticDataCommand() : Command("getDiagnosticData") {}
+
+ bool adminOnly() const override {
+ return true;
+ }
+
+ void help(std::stringstream& help) const override {
+ help << "get latest diagnostic data collection snapshot";
+ }
+
+ bool slaveOk() const override {
+ return true;
+ }
+
+ bool supportsWriteConcern(const BSONObj& cmd) const override {
+ return false;
+ }
+
+ Status checkAuthForCommand(ClientBasic* client,
+ const std::string& dbname,
+ const BSONObj& cmdObj) override {
+
+ if (!AuthorizationSession::get(client)->isAuthorizedForActionsOnResource(
+ ResourcePattern::forClusterResource(), ActionType::serverStatus)) {
+ return Status(ErrorCodes::Unauthorized, "Unauthorized");
+ }
+
+ if (!AuthorizationSession::get(client)->isAuthorizedForActionsOnResource(
+ ResourcePattern::forClusterResource(), ActionType::replSetGetStatus)) {
+ return Status(ErrorCodes::Unauthorized, "Unauthorized");
+ }
+
+ if (!AuthorizationSession::get(client)->isAuthorizedForActionsOnResource(
+ ResourcePattern::forExactNamespace(NamespaceString("local", "oplog.rs")),
+ ActionType::collStats)) {
+ return Status(ErrorCodes::Unauthorized, "Unauthorized");
+ }
+
+ return Status::OK();
+ }
+
+ bool run(OperationContext* txn,
+ const std::string& db,
+ BSONObj& cmdObj,
+ int options,
+ std::string& errmsg,
+ BSONObjBuilder& result) override {
+
+ result.append(
+ "data", FTDCController::get(txn->getServiceContext())->getMostRecentPeriodicDocument());
+
+ return true;
+ }
+};
+
+Command* ftdcCommand;
+
+MONGO_INITIALIZER(CreateDiagnosticDataCommand)(InitializerContext* context) {
+ ftdcCommand = new GetDiagnosticDataCommand();
+
+ return Status::OK();
+}
+
+} // namespace
+
+} // namespace mongo
diff --git a/src/mongo/db/ftdc/ftdc_mongod.cpp b/src/mongo/db/ftdc/ftdc_mongod.cpp
index 6cbd781de3c..0f0ea5a49df 100644
--- a/src/mongo/db/ftdc/ftdc_mongod.cpp
+++ b/src/mongo/db/ftdc/ftdc_mongod.cpp
@@ -28,6 +28,8 @@
#include "mongo/platform/basic.h"
+#include "mongo/db/ftdc/ftdc_mongod.h"
+
#include <boost/filesystem.hpp>
#include <fstream>
#include <memory>
@@ -304,6 +306,8 @@ void startFTDC() {
// Install periodic collectors
// These are collected on the period interval in FTDCConfig.
+ // NOTE: For each command here, there must be an equivalent privilege check in
+ // GetDiagnosticDataCommand
// CmdServerStatus
controller->addPeriodicCollector(stdx::make_unique<FTDCSimpleInternalCommandCollector>(
@@ -359,4 +363,8 @@ void stopFTDC() {
}
}
+FTDCController* FTDCController::get(ServiceContext* serviceContext) {
+ return getFTDCController(serviceContext).get();
+}
+
} // namespace mongo
diff --git a/src/mongo/s/commands/SConscript b/src/mongo/s/commands/SConscript
index 52e20b68ad1..27e8592d881 100644
--- a/src/mongo/s/commands/SConscript
+++ b/src/mongo/s/commands/SConscript
@@ -34,6 +34,7 @@ env.Library(
'cluster_find_and_modify_cmd.cpp',
'cluster_flush_router_config_cmd.cpp',
'cluster_fsync_cmd.cpp',
+ 'cluster_ftdc_commands.cpp',
'cluster_get_last_error_cmd.cpp',
'cluster_get_prev_error_cmd.cpp',
'cluster_get_shard_version_cmd.cpp',
diff --git a/src/mongo/s/commands/cluster_ftdc_commands.cpp b/src/mongo/s/commands/cluster_ftdc_commands.cpp
new file mode 100644
index 00000000000..bfe339198c4
--- /dev/null
+++ b/src/mongo/s/commands/cluster_ftdc_commands.cpp
@@ -0,0 +1,95 @@
+/**
+ * Copyright (C) 2016 MongoDB 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/>.
+ *
+ * As a special exception, the copyright holders give permission to link the
+ * code of portions of this program with the OpenSSL library under certain
+ * conditions as described in each individual source file and distribute
+ * linked combinations including the program with the OpenSSL library. You
+ * must comply with the GNU Affero General Public License in all respects
+ * for all of the code used other than as permitted herein. If you modify
+ * file(s) with this exception, you may extend this exception to your
+ * version of the file(s), but you are not obligated to do so. If you do not
+ * wish to do so, delete this exception statement from your version. If you
+ * delete this exception statement from all source files in the program,
+ * then also delete it in the license file.
+ */
+
+#include "mongo/platform/basic.h"
+
+#include "mongo/base/init.h"
+#include "mongo/db/auth/action_type.h"
+#include "mongo/db/auth/authorization_session.h"
+#include "mongo/db/client.h"
+#include "mongo/db/commands.h"
+#include "mongo/db/ftdc/controller.h"
+#include "mongo/db/jsobj.h"
+#include "mongo/db/operation_context.h"
+
+namespace mongo {
+namespace {
+
+/**
+ * getDiagnosticData is a MongoD only command. We implement in MongoS to give users a better error
+ * message.
+ */
+class GetDiagnosticDataCommand final : public Command {
+public:
+ GetDiagnosticDataCommand() : Command("getDiagnosticData") {}
+
+ bool adminOnly() const override {
+ return true;
+ }
+
+ void help(std::stringstream& help) const override {
+ help << "get latest diagnostic data collection snapshot";
+ }
+
+ bool slaveOk() const override {
+ return true;
+ }
+
+ bool supportsWriteConcern(const BSONObj& cmd) const override {
+ return false;
+ }
+
+ Status checkAuthForCommand(ClientBasic* client,
+ const std::string& dbname,
+ const BSONObj& cmdObj) override {
+ return Status::OK();
+ }
+
+ bool run(OperationContext* txn,
+ const std::string& db,
+ BSONObj& cmdObj,
+ int options,
+ std::string& errmsg,
+ BSONObjBuilder& result) override {
+
+ errmsg = "getDiagnosticData not allowed through mongos";
+
+ return false;
+ }
+};
+
+Command* ftdcCommand;
+
+MONGO_INITIALIZER(CreateDiagnosticDataCommand)(InitializerContext* context) {
+ ftdcCommand = new GetDiagnosticDataCommand();
+
+ return Status::OK();
+}
+
+} // namespace
+} // namespace mongo