diff options
author | Mark Benvenuto <mark.benvenuto@mongodb.com> | 2017-04-17 17:59:04 -0400 |
---|---|---|
committer | Mark Benvenuto <mark.benvenuto@mongodb.com> | 2017-04-17 17:59:04 -0400 |
commit | 0bb11fa3305619b000cca96a8c13a6ebffe85162 (patch) | |
tree | 42e50dd3cfc6513129db62ffefa216ecdb66c7d3 | |
parent | 68919e8894a84d3ed4c7cfa214237c4da93ef5a8 (diff) | |
download | mongo-0bb11fa3305619b000cca96a8c13a6ebffe85162.tar.gz |
SERVER-24607 Add Collector for system statistics
(cherry picked from commit a05b8e17ea4e92ab6e1d7062cb5ffb9f4e8721a3)
-rw-r--r-- | src/mongo/db/ftdc/SConscript | 15 | ||||
-rw-r--r-- | src/mongo/db/ftdc/ftdc_mongod.cpp | 6 | ||||
-rw-r--r-- | src/mongo/db/ftdc/ftdc_system_stats.cpp | 96 | ||||
-rw-r--r-- | src/mongo/db/ftdc/ftdc_system_stats.h | 38 |
4 files changed, 147 insertions, 8 deletions
diff --git a/src/mongo/db/ftdc/SConscript b/src/mongo/db/ftdc/SConscript index 67601b2d3ab..412553cdd7b 100644 --- a/src/mongo/db/ftdc/SConscript +++ b/src/mongo/db/ftdc/SConscript @@ -32,21 +32,24 @@ ftdcEnv.Library( ], ) +platform_libs = [] + +if env.TargetOSIs('linux'): + platform_libs = [ + '$BUILD_DIR/mongo/util/procparser' + ] + env.Library( target='ftdc_mongod', source=[ 'ftdc_mongod.cpp', + 'ftdc_system_stats.cpp', ], LIBDEPS=[ '$BUILD_DIR/mongo/base', '$BUILD_DIR/mongo/db/server_parameters', 'ftdc' - ], - LIBDEPS_TAGS=[ - # Needs 'mongo::storageGlobalParams' in storage_options.cpp which is in serveronly - # And this library is already part of serveronly - 'incomplete', - ], + ] + platform_libs, ) env.CppUnitTest( diff --git a/src/mongo/db/ftdc/ftdc_mongod.cpp b/src/mongo/db/ftdc/ftdc_mongod.cpp index e328dbd3dff..e6b214cec74 100644 --- a/src/mongo/db/ftdc/ftdc_mongod.cpp +++ b/src/mongo/db/ftdc/ftdc_mongod.cpp @@ -35,11 +35,11 @@ #include "mongo/base/init.h" #include "mongo/base/status.h" #include "mongo/bson/bsonobjbuilder.h" - #include "mongo/db/commands.h" #include "mongo/db/ftdc/collector.h" #include "mongo/db/ftdc/config.h" #include "mongo/db/ftdc/controller.h" +#include "mongo/db/ftdc/ftdc_system_stats.h" #include "mongo/db/jsobj.h" #include "mongo/db/repl/replication_coordinator.h" #include "mongo/db/repl/replication_coordinator_global.h" @@ -288,7 +288,6 @@ private: } // namespace - // Register the FTDC system // Note: This must be run before the server parameters are parsed during startup // so that the FTDCController is initialized. @@ -327,6 +326,9 @@ void startFTDC() { "collStats", "local.oplog.rs.stats", "local.oplog.rs", BSONObj())); } + // Install System Metric Collector as a periodic collector + installSystemMetricsCollector(controller.get()); + // Install file rotation collectors // These are collected on each file rotation. diff --git a/src/mongo/db/ftdc/ftdc_system_stats.cpp b/src/mongo/db/ftdc/ftdc_system_stats.cpp new file mode 100644 index 00000000000..35764e7768f --- /dev/null +++ b/src/mongo/db/ftdc/ftdc_system_stats.cpp @@ -0,0 +1,96 @@ +/** + * 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 <string> +#include <vector> + +#include "mongo/base/status.h" +#include "mongo/base/string_data.h" +#include "mongo/bson/bsonobjbuilder.h" +#include "mongo/db/ftdc/collector.h" +#include "mongo/db/ftdc/controller.h" +#include "mongo/stdx/memory.h" + +#ifdef __linux__ +#include "mongo/util/procparser.h" +#endif + +namespace mongo { + +constexpr auto kSystemMetricsCollector = "systemMetrics"; + +#ifdef __linux__ +static const std::vector<StringData> kCpuKeys{ + "btime", "cpu", "ctxt", "processes", "procs_blocked", "procs_running"}; + +/** + * Collect metrics from the Linux /proc file system. + */ +class LinuxSystemMetricsCollector final : public FTDCCollectorInterface { +public: + void collect(OperationContext* txn, BSONObjBuilder& builder) override { + { + BSONObjBuilder subObjBuilder(builder.subobjStart("cpu")); + processStatusErrors( + procparser::parseProcStatFile("/proc/stat", kCpuKeys, &subObjBuilder), + &subObjBuilder); + subObjBuilder.doneFast(); + } + } + + std::string name() const override { + return kSystemMetricsCollector; + } + +private: + /** + * Convert any errors we see into BSON for the user to see in the final FTDC document. It is + * acceptable for the proc parser to fail, but we do not want to shutdown the FTDC loop because + * of it. We assume that the BSONBuilder is not corrupt on non-OK Status but nothing else with + * regards to the final document output. + */ + static void processStatusErrors(Status s, BSONObjBuilder* builder) { + if (!s.isOK()) { + builder->append("error", s.toString()); + } + } +}; + +void installSystemMetricsCollector(FTDCController* controller) { + controller->addPeriodicCollector(stdx::make_unique<LinuxSystemMetricsCollector>()); +} + +#else + +void installSystemMetricsCollector(FTDCController* controller) {} + +#endif + +} // namespace mongo diff --git a/src/mongo/db/ftdc/ftdc_system_stats.h b/src/mongo/db/ftdc/ftdc_system_stats.h new file mode 100644 index 00000000000..4d88b596da2 --- /dev/null +++ b/src/mongo/db/ftdc/ftdc_system_stats.h @@ -0,0 +1,38 @@ +/** + * 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. + */ + +namespace mongo { + +class FTDCController; + +/** + * Install a system metrics collector if it exists as a periodic collector. + */ +void installSystemMetricsCollector(FTDCController* controller); + +} // namespace mongo |