summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMark Benvenuto <mark.benvenuto@mongodb.com>2017-04-17 17:59:04 -0400
committerMark Benvenuto <mark.benvenuto@mongodb.com>2017-04-17 17:59:04 -0400
commit61fc1d72075430bd58c65b1644b8eaa1ca290377 (patch)
treef86ba8c445ea5b29fb421ccffe16ac1f744bb7db
parent2b6b83c8a2d8e622458f4be2191ac53d20f20899 (diff)
downloadmongo-61fc1d72075430bd58c65b1644b8eaa1ca290377.tar.gz
SERVER-24616 Add new getDiagnosticData command
(cherry picked from commit c1042b38c9f6469ddcce2dfdf5dacbff0334cd14)
-rw-r--r--buildscripts/resmokeconfig/suites/sharded_collections_jscore_passthrough.yml1
-rw-r--r--buildscripts/resmokeconfig/suites/sharding_jscore_passthrough.yml1
-rw-r--r--jstests/auth/lib/commands_lib.js18
-rw-r--r--jstests/core/diagdata.js19
-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.cpp113
-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.cpp94
11 files changed, 286 insertions, 1 deletions
diff --git a/buildscripts/resmokeconfig/suites/sharded_collections_jscore_passthrough.yml b/buildscripts/resmokeconfig/suites/sharded_collections_jscore_passthrough.yml
index e88028fcb41..8096b406f4d 100644
--- a/buildscripts/resmokeconfig/suites/sharded_collections_jscore_passthrough.yml
+++ b/buildscripts/resmokeconfig/suites/sharded_collections_jscore_passthrough.yml
@@ -18,6 +18,7 @@ selector:
- jstests/core/dbadmin.js # "local" database.
- jstests/core/dbhash.js # dbhash.
- jstests/core/dbhash2.js # dbhash.
+ - jstests/core/diagdata.js # Command not supported in mongos
- jstests/core/dropdb_race.js # syncdelay.
- jstests/core/evalb.js # profiling.
- jstests/core/fsync.js # uses fsync.
diff --git a/buildscripts/resmokeconfig/suites/sharding_jscore_passthrough.yml b/buildscripts/resmokeconfig/suites/sharding_jscore_passthrough.yml
index db5e043b209..98553af4366 100644
--- a/buildscripts/resmokeconfig/suites/sharding_jscore_passthrough.yml
+++ b/buildscripts/resmokeconfig/suites/sharding_jscore_passthrough.yml
@@ -18,6 +18,7 @@ selector:
- jstests/core/dbadmin.js # "local" database.
- jstests/core/dbhash.js # dbhash.
- jstests/core/dbhash2.js # dbhash.
+ - jstests/core/diagdata.js # Command not supported in mongos
- jstests/core/dropdb_race.js # syncdelay.
- jstests/core/evalb.js # profiling.
- jstests/core/fsync.js # uses fsync.
diff --git a/jstests/auth/lib/commands_lib.js b/jstests/auth/lib/commands_lib.js
index 09052e99acc..8de3a842e99 100644
--- a/jstests/auth/lib/commands_lib.js
+++ b/jstests/auth/lib/commands_lib.js
@@ -1617,6 +1617,24 @@ var authCommandsLib = {
]
},
{
+ testname: "getDiagnosticData",
+ command: {getDiagnosticData: 1},
+ skipSharded: true,
+ testcases: [
+ {
+ runOnDb: adminDbName,
+ roles: roles_monitoring,
+ privileges: [
+ {resource: {cluster: true}, actions: ["serverStatus"]},
+ {resource: {cluster: true}, actions: ["replSetGetStatus"]},
+ {resource: {db: "local", collection: "oplog.rs"}, actions: ["collStats"]},
+ ]
+ },
+ {runOnDb: firstDbName, roles: {}},
+ {runOnDb: secondDbName, roles: {}}
+ ]
+ },
+ {
testname: "getLastError",
command: {getLastError: 1},
testcases: [
diff --git a/jstests/core/diagdata.js b/jstests/core/diagdata.js
new file mode 100644
index 00000000000..eb4ce0fb3ef
--- /dev/null
+++ b/jstests/core/diagdata.js
@@ -0,0 +1,19 @@
+// Test that verifies getDiagnosticData returns FTDC data
+
+(function() {
+ "use strict";
+
+ // Verify we require admin database
+ assert.commandFailed(db.diagdata.runCommand("getDiagnosticData"));
+
+ var result = db.adminCommand("getDiagnosticData");
+ assert.commandWorked(result);
+
+ var data = result.data;
+
+ // Check for a few common properties to ensure we got data
+ assert(data.hasOwnProperty("start"));
+ assert(data.hasOwnProperty("serverStatus"));
+ assert(data.hasOwnProperty("end"));
+
+})();
diff --git a/src/mongo/db/ftdc/SConscript b/src/mongo/db/ftdc/SConscript
index 412553cdd7b..fbf55b3ca64 100644
--- a/src/mongo/db/ftdc/SConscript
+++ b/src/mongo/db/ftdc/SConscript
@@ -42,6 +42,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 65819c7e380..c13f6fc7fcf 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..13487171aa6
--- /dev/null
+++ b/src/mongo/db/ftdc/ftdc_commands.cpp
@@ -0,0 +1,113 @@
+/**
+ * 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 isWriteCommandForConfigServer() 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 e6b214cec74..f9861186af0 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>
@@ -309,6 +311,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>(
@@ -360,4 +364,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 5400114f85d..743918fdabb 100644
--- a/src/mongo/s/commands/SConscript
+++ b/src/mongo/s/commands/SConscript
@@ -31,6 +31,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..29c80482c58
--- /dev/null
+++ b/src/mongo/s/commands/cluster_ftdc_commands.cpp
@@ -0,0 +1,94 @@
+/**
+ * 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 isWriteCommandForConfigServer() 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