diff options
Diffstat (limited to 'src/mongo/db/ftdc/ftdc_system_stats.cpp')
-rw-r--r-- | src/mongo/db/ftdc/ftdc_system_stats.cpp | 93 |
1 files changed, 12 insertions, 81 deletions
diff --git a/src/mongo/db/ftdc/ftdc_system_stats.cpp b/src/mongo/db/ftdc/ftdc_system_stats.cpp index f0ffb319284..b087735d244 100644 --- a/src/mongo/db/ftdc/ftdc_system_stats.cpp +++ b/src/mongo/db/ftdc/ftdc_system_stats.cpp @@ -28,103 +28,34 @@ #include "mongo/platform/basic.h" +#include "mongo/db/ftdc/ftdc_system_stats.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 all the memory keys by specifying an empty set. -static const std::vector<StringData> kMemKeys{}; +namespace { /** - * Collect metrics from the Linux /proc file system. + * Name of FTDC collector to create. */ -class LinuxSystemMetricsCollector final : public FTDCCollectorInterface { -public: - LinuxSystemMetricsCollector() : _disks(procparser::findPhysicalDisks("/sys/block")) { - for (const auto& disk : _disks) { - _disksStringData.emplace_back(disk); - } - } - - void collect(OperationContext* txn, BSONObjBuilder& builder) override { - { - BSONObjBuilder subObjBuilder(builder.subobjStart("cpu")); - processStatusErrors( - procparser::parseProcStatFile("/proc/stat", kCpuKeys, &subObjBuilder), - &subObjBuilder); - subObjBuilder.doneFast(); - } - - { - BSONObjBuilder subObjBuilder(builder.subobjStart("memory")); - processStatusErrors( - procparser::parseProcMemInfoFile("/proc/meminfo", kMemKeys, &subObjBuilder), - &subObjBuilder); - subObjBuilder.doneFast(); - } +constexpr auto kSystemMetricsCollector = "systemMetrics"; - // Skip the disks section if we could not find any disks. - // This can happen when we do not have permission to /sys/block for instance. - if (!_disksStringData.empty()) { - BSONObjBuilder subObjBuilder(builder.subobjStart("disks")); - processStatusErrors(procparser::parseProcDiskStatsFile( - "/proc/diskstats", _disksStringData, &subObjBuilder), - &subObjBuilder); - subObjBuilder.doneFast(); - } - } +} // namespace - std::string name() const override { - return kSystemMetricsCollector; - } +std::string SystemMetricsCollector::name() const { + 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 SystemMetricsCollector::processStatusErrors(Status s, BSONObjBuilder* builder) { + if (!s.isOK()) { + builder->append("error", s.toString()); } - -private: - // List of physical disks to collect stats from as string from findPhysicalDisks. - std::vector<std::string> _disks; - - // List of physical disks to collect stats from as StringData to pass to parseProcDiskStatsFile. - std::vector<StringData> _disksStringData; -}; - -void installSystemMetricsCollector(FTDCController* controller) { - controller->addPeriodicCollector(stdx::make_unique<LinuxSystemMetricsCollector>()); } -#else - -void installSystemMetricsCollector(FTDCController* controller) {} - -#endif - } // namespace mongo |