Module: check_mk
Branch: master
Commit: f253e7bd86f45e787b72164289ad2ea5055e4d67
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=f253e7bd86f45e…
Author: Sven Panne <sp(a)mathias-kettner.de>
Date: Mon Dec 11 12:02:10 2017 +0100
Made counter for active Livestatus connections atomic.
We have seen Livestatus usage >100% out in the wild, which is total
nonsense. This commit is trying to fix this, but there could be other
problems: We really, really have to introduce memory barriers and locks in
Livestatus, the current code is simply wrong in this respect.
Change-Id: Ie063ef2b4ff0dc78d1a05159d320d452d9e7e9bc
---
livestatus/src/AtomicInt32PointerColumn.h | 48 +++++++++++++++++++++++++++++++
livestatus/src/TableStatus.cc | 6 ++--
livestatus/src/module.cc | 3 +-
3 files changed, 54 insertions(+), 3 deletions(-)
diff --git a/livestatus/src/AtomicInt32PointerColumn.h
b/livestatus/src/AtomicInt32PointerColumn.h
new file mode 100644
index 0000000..42fd34c
--- /dev/null
+++ b/livestatus/src/AtomicInt32PointerColumn.h
@@ -0,0 +1,48 @@
+// +------------------------------------------------------------------+
+// | ____ _ _ __ __ _ __ |
+// | / ___| |__ ___ ___| | __ | \/ | |/ / |
+// | | | | '_ \ / _ \/ __| |/ / | |\/| | ' / |
+// | | |___| | | | __/ (__| < | | | | . \ |
+// | \____|_| |_|\___|\___|_|\_\___|_| |_|_|\_\ |
+// | |
+// | Copyright Mathias Kettner 2014 mk(a)mathias-kettner.de |
+// +------------------------------------------------------------------+
+//
+// This file is part of Check_MK.
+// The official homepage is at
http://mathias-kettner.de/check_mk.
+//
+// check_mk is free software; you can redistribute it and/or modify it
+// under the terms of the GNU General Public License as published by
+// the Free Software Foundation in version 2. check_mk is distributed
+// in the hope that it will be useful, but WITHOUT ANY WARRANTY; with-
+// out even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+// PARTICULAR PURPOSE. See the GNU General Public License for more de-
+// tails. You should have received a copy of the GNU General Public
+// License along with GNU Make; see the file COPYING. If not, write
+// to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
+// Boston, MA 02110-1301 USA.
+
+#ifndef AtomicInt32PointerColumn_h
+#define AtomicInt32PointerColumn_h
+
+#include "config.h" // IWYU pragma: keep
+#include <atomic>
+#include "IntColumn.h"
+
+class AtomicInt32PointerColumn : public IntColumn {
+public:
+ AtomicInt32PointerColumn(const std::string& name,
+ const std::string& description,
+ const std::atomic_int32_t* number)
+ : IntColumn(name, description, -1, -1, -1, 0), _number(number) {}
+
+ int32_t getValue(Row /* row */,
+ const contact* /* auth_user */) const override {
+ return _number->load();
+ }
+
+private:
+ const std::atomic_int32_t* const _number;
+};
+
+#endif // AtomicInt32PointerColumn_h
diff --git a/livestatus/src/TableStatus.cc b/livestatus/src/TableStatus.cc
index acf5604..b09aebc 100644
--- a/livestatus/src/TableStatus.cc
+++ b/livestatus/src/TableStatus.cc
@@ -23,8 +23,10 @@
// Boston, MA 02110-1301 USA.
#include "TableStatus.h"
+#include <atomic>
#include <ctime>
#include <memory>
+#include "AtomicInt32PointerColumn.h"
#include "Column.h"
#include "DoublePointerColumn.h"
#include "IntPointerColumn.h"
@@ -61,7 +63,7 @@ extern int g_num_hosts;
extern int g_num_services;
extern int g_livestatus_threads;
extern int g_num_queued_connections;
-extern int g_livestatus_active_connections;
+extern std::atomic_int32_t g_livestatus_active_connections;
#ifndef NAGIOS4
extern circular_buffer external_command_buffer;
@@ -217,7 +219,7 @@ TableStatus::TableStatus(MonitoringCore *mc) : Table(mc) {
addColumn(std::make_unique<StringPointerColumn>(
"livestatus_version", "The version of the MK Livestatus
module",
VERSION));
- addColumn(std::make_unique<IntPointerColumn>(
+ addColumn(std::make_unique<AtomicInt32PointerColumn>(
"livestatus_active_connections",
"The current number of active connections to MK Livestatus",
&g_livestatus_active_connections));
diff --git a/livestatus/src/module.cc b/livestatus/src/module.cc
index 6875cfa..90b6a8a 100644
--- a/livestatus/src/module.cc
+++ b/livestatus/src/module.cc
@@ -34,6 +34,7 @@
#include <sys/stat.h>
#include <sys/un.h>
#include <unistd.h>
+#include <atomic>
#include <chrono>
#include <cstddef>
#include <cstdint>
@@ -98,7 +99,7 @@ size_t g_livestatus_threads = 10;
// current number of queued connections (for statistics)
int g_num_queued_connections = 0;
// current number of active connections (for statistics)
-int g_livestatus_active_connections = 0;
+std::atomic_int32_t g_livestatus_active_connections{0};
size_t g_thread_stack_size = 1024 * 1024; /* stack size of threads */
static int fl_disable_statehist_filtering;