Module: check_mk
Branch: master
Commit: 1861058283dc0b28a9624d7ab4e9a7ed3a3eaca0
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=1861058283dc0b…
Author: Sven Panne <sp(a)mathias-kettner.de>
Date: Thu Nov 2 11:52:00 2017 +0100
Make "badness" notion of host/service states explicit.
Change-Id: I89c5a7451dfed281785201525eee40aff68a54ae
---
livestatus/src/HostListStateColumn.cc | 25 +++++--------------------
livestatus/src/LogEntry.h | 26 ++++++++++++++++++++++++++
livestatus/src/ServiceListStateColumn.cc | 23 +++--------------------
livestatus/src/ServiceListStateColumn.h | 2 --
4 files changed, 34 insertions(+), 42 deletions(-)
diff --git a/livestatus/src/HostListStateColumn.cc
b/livestatus/src/HostListStateColumn.cc
index b1acd56..a91576d 100644
--- a/livestatus/src/HostListStateColumn.cc
+++ b/livestatus/src/HostListStateColumn.cc
@@ -23,6 +23,7 @@
// Boston, MA 02110-1301 USA.
#include "HostListStateColumn.h"
+#include "LogEntry.h"
#include "Row.h"
#include "auth.h"
@@ -40,24 +41,6 @@ int32_t HostListStateColumn::getValue(Row row, const contact
*auth_user) const {
return result;
}
-namespace {
-bool hst_state_is_worse(int32_t state1, int32_t state2) {
- if (state1 == 0) {
- return false; // UP is worse than nothing
- }
- if (state2 == 0) {
- return true; // everything else is worse then UP
- }
- if (state2 == 1) {
- return false; // nothing is worse than DOWN
- }
- if (state1 == 1) {
- return true; // state1 is DOWN, state2 not
- }
- return false; // both are UNREACHABLE
-}
-} // namespace
-
void HostListStateColumn::update(host *hst, const contact *auth_user,
int32_t &result) const {
switch (_logictype) {
@@ -76,7 +59,8 @@ void HostListStateColumn::update(host *hst, const contact *auth_user,
int state = ServiceListStateColumn::getValueFromServices(
_mc, static_cast<ServiceListStateColumn::Type>(_logictype),
hst->services, auth_user);
- if (ServiceListStateColumn::svcStateIsWorse(state, result)) {
+ if (worse(static_cast<ServiceState>(state),
+ static_cast<ServiceState>(result))) {
result = state;
}
break;
@@ -104,7 +88,8 @@ void HostListStateColumn::update(host *hst, const contact *auth_user,
break;
case Type::worst_hst_state:
- if (hst_state_is_worse(hst->current_state, result)) {
+ if (worse(static_cast<HostState>(hst->current_state),
+ static_cast<HostState>(result))) {
result = hst->current_state;
}
break;
diff --git a/livestatus/src/LogEntry.h b/livestatus/src/LogEntry.h
index 0d56ea7..1d46973 100644
--- a/livestatus/src/LogEntry.h
+++ b/livestatus/src/LogEntry.h
@@ -35,8 +35,34 @@
enum class ServiceState { ok = 0, warning = 1, critical = 2, unknown = 3 };
+inline double badness(ServiceState state) {
+ // unknown is effectively between warning and critical
+ return state == ServiceState::unknown
+ ? (static_cast<double>(ServiceState::warning) +
+ static_cast<double>(ServiceState::critical)) /
+ 2.0
+ : static_cast<double>(state);
+}
+
+inline bool worse(ServiceState state1, ServiceState state2) {
+ return badness(state1) > badness(state2);
+}
+
enum class HostState { up = 0, down = 1, unreachable = 2 };
+inline double badness(HostState state) {
+ // unreachable is effectively between up and down
+ return state == HostState::unreachable
+ ? (static_cast<double>(HostState::up) +
+ static_cast<double>(HostState::down)) /
+ 2.0
+ : static_cast<double>(state);
+}
+
+inline bool worse(HostState state1, HostState state2) {
+ return badness(state1) > badness(state2);
+}
+
enum class LogEntryType {
none,
alert_host,
diff --git a/livestatus/src/ServiceListStateColumn.cc
b/livestatus/src/ServiceListStateColumn.cc
index 0b04681..6014341 100644
--- a/livestatus/src/ServiceListStateColumn.cc
+++ b/livestatus/src/ServiceListStateColumn.cc
@@ -23,6 +23,7 @@
// Boston, MA 02110-1301 USA.
#include "ServiceListStateColumn.h"
+#include "LogEntry.h"
#include "Row.h"
#include "auth.h"
@@ -51,25 +52,6 @@ int32_t ServiceListStateColumn::getValueFromServices(MonitoringCore
*mc,
return result;
}
-// return true if state1 is worse than state2
-
-// static
-bool ServiceListStateColumn::svcStateIsWorse(int32_t state1, int32_t state2) {
- if (state1 == 0) {
- return false; // OK is worse than nothing
- }
- if (state2 == 0) {
- return true; // everything else is worse then OK
- }
- if (state2 == 2) {
- return false; // nothing is worse than CRIT
- }
- if (state1 == 2) {
- return true; // state1 is CRIT, state2 not
- }
- return (state1 > state2); // both or WARN or UNKNOWN
-}
-
// static
void ServiceListStateColumn::update(Type logictype, service *svc,
int32_t &result) {
@@ -84,7 +66,8 @@ void ServiceListStateColumn::update(Type logictype, service *svc,
}
switch (lt) {
case Type::worst_state:
- if (svcStateIsWorse(service_state, result)) {
+ if (worse(static_cast<ServiceState>(service_state),
+ static_cast<ServiceState>(result))) {
result = service_state;
}
break;
diff --git a/livestatus/src/ServiceListStateColumn.h
b/livestatus/src/ServiceListStateColumn.h
index 41cb199..23d5557 100644
--- a/livestatus/src/ServiceListStateColumn.h
+++ b/livestatus/src/ServiceListStateColumn.h
@@ -78,8 +78,6 @@ public:
service_list mem,
const contact *auth_user);
- static bool svcStateIsWorse(int32_t state1, int32_t state2);
-
private:
MonitoringCore *_mc;
const Type _logictype;