From 0a43e81306a38bcb28d4db3a1b6172b0ef235efd Mon Sep 17 00:00:00 2001 From: Michael Friedrich Date: Fri, 4 Sep 2015 13:24:41 +0200 Subject: [PATCH] Fix wrong calculation for host compat state UNREACHABLE fixes #10058 --- lib/compat/compatlogger.cpp | 19 ++++++++----------- lib/compat/statusdatawriter.cpp | 2 +- lib/db_ido/dbevents.cpp | 13 ++++--------- lib/db_ido/hostdbobject.cpp | 2 +- lib/icinga/checkable-dependency.cpp | 4 ++-- lib/icinga/compatutility.cpp | 16 ++++++++++++++++ lib/icinga/compatutility.hpp | 2 ++ lib/icinga/dependency.cpp | 26 +++++++++++++------------- lib/perfdata/gelfwriter.cpp | 2 +- 9 files changed, 48 insertions(+), 38 deletions(-) diff --git a/lib/compat/compatlogger.cpp b/lib/compat/compatlogger.cpp index 4d88edbaa..92c0e2109 100644 --- a/lib/compat/compatlogger.cpp +++ b/lib/compat/compatlogger.cpp @@ -117,21 +117,18 @@ void CompatLogger::CheckResultHandler(const Checkable::Ptr& checkable, const Che msgbuf << "SERVICE ALERT: " << host->GetName() << ";" << service->GetShortName() << ";" - << Service::StateToString(static_cast(state_after)) << ";" - << Service::StateTypeToString(static_cast(stateType_after)) << ";" + << Service::StateToString(service->GetState()) << ";" + << Service::StateTypeToString(service->GetStateType()) << ";" << attempt_after << ";" << output << "" << ""; } else { String state = Host::StateToString(Host::CalculateState(static_cast(state_after))); - if (!reachable_after) - state = "UNREACHABLE"; - msgbuf << "HOST ALERT: " << host->GetName() << ";" - << state << ";" - << Host::StateTypeToString(static_cast(stateType_after)) << ";" + << CompatUtility::GetHostStateString(host) << ";" + << Host::StateTypeToString(host->GetStateType()) << ";" << attempt_after << ";" << output << "" << ""; @@ -246,7 +243,7 @@ void CompatLogger::NotificationSentHandler(const Notification::Ptr& notification if (service) notification_type_str = Service::StateToString(service->GetState()); else - notification_type_str = host->IsReachable() ? Host::StateToString(host->GetState()) : "UNREACHABLE"; + notification_type_str = CompatUtility::GetHostStateString(host); } String author_comment = ""; @@ -278,7 +275,7 @@ void CompatLogger::NotificationSentHandler(const Notification::Ptr& notification << user->GetName() << ";" << host->GetName() << ";" << notification_type_str << " " - << "(" << (host->IsReachable() ? Host::StateToString(host->GetState()) : "UNREACHABLE") << ");" + << "(" << CompatUtility::GetHostStateString(host) << ");" << command_name << ";" << output << ";" << author_comment @@ -385,7 +382,7 @@ void CompatLogger::EventCommandHandler(const Checkable::Ptr& checkable) } else { msgbuf << "HOST EVENT HANDLER: " << host->GetName() << ";" - << (host->IsReachable() ? Host::StateToString(host->GetState()) : "UNREACHABLE") << ";" + << CompatUtility::GetHostStateString(host) << ";" << Host::StateTypeToString(host->GetStateType()) << ";" << current_attempt << ";" << event_command_name; @@ -462,7 +459,7 @@ void CompatLogger::ReopenFile(bool rotate) std::ostringstream msgbuf; msgbuf << "CURRENT HOST STATE: " << host->GetName() << ";" - << (host->IsReachable() ? Host::StateToString(host->GetState()) : "UNREACHABLE") << ";" + << CompatUtility::GetHostStateString(host) << ";" << Host::StateTypeToString(host->GetStateType()) << ";" << host->GetCheckAttempt() << ";" << output << ""; diff --git a/lib/compat/statusdatawriter.cpp b/lib/compat/statusdatawriter.cpp index 2e3338041..4f1e77b39 100644 --- a/lib/compat/statusdatawriter.cpp +++ b/lib/compat/statusdatawriter.cpp @@ -355,7 +355,7 @@ void StatusDataWriter::DumpCheckableStatusAttrs(std::ostream& fp, const Checkabl fp << "\t" << "current_state=" << service->GetState() << "\n" << "\t" << "last_hard_state=" << service->GetLastHardState() << "\n"; } else { - fp << "\t" << "current_state=" << (host->IsReachable() ? host->GetState() : 2) << "\n" + fp << "\t" << "current_state=" << CompatUtility::GetHostCurrentState(host) << "\n" << "\t" << "last_hard_state=" << host->GetLastHardState() << "\n"; } diff --git a/lib/db_ido/dbevents.cpp b/lib/db_ido/dbevents.cpp index 3453ac621..4af88285d 100644 --- a/lib/db_ido/dbevents.cpp +++ b/lib/db_ido/dbevents.cpp @@ -992,8 +992,8 @@ void DbEvents::AddCheckResultLogHistory(const Checkable::Ptr& checkable, const C msgbuf << "SERVICE ALERT: " << host->GetName() << ";" << service->GetShortName() << ";" - << Service::StateToString(static_cast(state_after)) << ";" - << Service::StateTypeToString(static_cast(stateType_after)) << ";" + << Service::StateToString(service->GetState()) << ";" + << Service::StateTypeToString(service->GetStateType()) << ";" << attempt_after << ";" << output << "" << ""; @@ -1017,15 +1017,10 @@ void DbEvents::AddCheckResultLogHistory(const Checkable::Ptr& checkable, const C return; } } else { - String state = Host::StateToString(Host::CalculateState(static_cast(state_after))); - - if (!reachable_after) - state = "UNREACHABLE"; - msgbuf << "HOST ALERT: " << host->GetName() << ";" - << state << ";" - << Service::StateTypeToString(static_cast(stateType_after)) << ";" + << CompatUtility::GetHostStateString(host) << ";" + << Host::StateTypeToString(host->GetStateType()) << ";" << attempt_after << ";" << output << "" << ""; diff --git a/lib/db_ido/hostdbobject.cpp b/lib/db_ido/hostdbobject.cpp index 3210c3095..919061c27 100644 --- a/lib/db_ido/hostdbobject.cpp +++ b/lib/db_ido/hostdbobject.cpp @@ -122,7 +122,7 @@ Dictionary::Ptr HostDbObject::GetStatusFields(void) const fields->Set("check_source", cr->GetCheckSource()); } - fields->Set("current_state", host->IsReachable() ? host->GetState() : 2); + fields->Set("current_state", CompatUtility::GetHostCurrentState(host)); fields->Set("has_been_checked", CompatUtility::GetCheckableHasBeenChecked(host)); fields->Set("should_be_scheduled", host->GetEnableActiveChecks()); fields->Set("current_check_attempt", host->GetCheckAttempt()); diff --git a/lib/icinga/checkable-dependency.cpp b/lib/icinga/checkable-dependency.cpp index 89f2a96de..f3769faec 100644 --- a/lib/icinga/checkable-dependency.cpp +++ b/lib/icinga/checkable-dependency.cpp @@ -69,8 +69,8 @@ bool Checkable::IsReachable(DependencyType dt, Dependency::Ptr *failedDependency return false; } - BOOST_FOREACH(const Checkable::Ptr& service, GetParents()) { - if (!service->IsReachable(dt, failedDependency, rstack + 1)) + BOOST_FOREACH(const Checkable::Ptr& checkable, GetParents()) { + if (!checkable->IsReachable(dt, failedDependency, rstack + 1)) return false; } diff --git a/lib/icinga/compatutility.cpp b/lib/icinga/compatutility.cpp index b88aafe9b..d30a28940 100644 --- a/lib/icinga/compatutility.cpp +++ b/lib/icinga/compatutility.cpp @@ -81,6 +81,22 @@ String CompatUtility::GetCommandName(const Command::Ptr command) } /* host */ +int CompatUtility::GetHostCurrentState(const Host::Ptr& host) +{ + if (host->GetState() != HostUp && !host->IsReachable()) + return 2; /* hardcoded compat state */ + + return host->GetState(); +} + +String CompatUtility::GetHostStateString(const Host::Ptr& host) +{ + if (host->GetState() != HostUp && !host->IsReachable()) + return "UNREACHABLE"; /* hardcoded compat state */ + + return Host::StateToString(host->GetState()); +} + String CompatUtility::GetHostAlias(const Host::Ptr& host) { if (!host->GetDisplayName().IsEmpty()) diff --git a/lib/icinga/compatutility.hpp b/lib/icinga/compatutility.hpp index 5ee326c12..d97b4673f 100644 --- a/lib/icinga/compatutility.hpp +++ b/lib/icinga/compatutility.hpp @@ -44,6 +44,8 @@ public: static String GetCommandName(const Command::Ptr command); /* host */ + static int GetHostCurrentState(const Host::Ptr& host); + static String GetHostStateString(const Host::Ptr& host); static String GetHostAlias(const Host::Ptr& host); static int GetHostNotifyOnDown(const Host::Ptr& host); static int GetHostNotifyOnUnreachable(const Host::Ptr& host); diff --git a/lib/icinga/dependency.cpp b/lib/icinga/dependency.cpp index 214b16dfb..bd203ac94 100644 --- a/lib/icinga/dependency.cpp +++ b/lib/icinga/dependency.cpp @@ -113,21 +113,21 @@ bool Dependency::IsAvailable(DependencyType dt) const { Checkable::Ptr parent = GetParent(); - Host::Ptr host; - Service::Ptr service; - tie(host, service) = GetHostService(parent); + Host::Ptr parentHost; + Service::Ptr parentService; + tie(parentHost, parentService) = GetHostService(parent); /* ignore if it's the same checkable object */ if (parent == GetChild()) { Log(LogNotice, "Dependency") - << "Dependency '" << GetName() << "' passed: Parent and child " << (service ? "service" : "host") << " are identical."; + << "Dependency '" << GetName() << "' passed: Parent and child " << (parentService ? "service" : "host") << " are identical."; return true; } /* ignore pending */ if (!parent->GetLastCheckResult()) { Log(LogNotice, "Dependency") - << "Dependency '" << GetName() << "' passed: " << (service ? "Service" : "Host") << " '" << parent->GetName() << "' hasn't been checked yet."; + << "Dependency '" << GetName() << "' passed: Parent " << (parentService ? "service" : "host") << " '" << parent->GetName() << "' hasn't been checked yet."; return true; } @@ -135,25 +135,25 @@ bool Dependency::IsAvailable(DependencyType dt) const /* ignore soft states */ if (parent->GetStateType() == StateTypeSoft) { Log(LogNotice, "Dependency") - << "Dependency '" << GetName() << "' passed: " << (service ? "Service" : "Host") << " '" << parent->GetName() << "' is in a soft state."; + << "Dependency '" << GetName() << "' passed: Parent " << (parentService ? "service" : "host") << " '" << parent->GetName() << "' is in a soft state."; return true; } } else { Log(LogNotice, "Dependency") - << "Dependency '" << GetName() << "' failed: " << (service ? "Service" : "Host") << " '" << parent->GetName() << "' is in a soft state."; + << "Dependency '" << GetName() << "' failed: Parent " << (parentService ? "service" : "host") << " '" << parent->GetName() << "' is in a soft state."; } int state; - if (service) - state = ServiceStateToFilter(service->GetState()); + if (parentService) + state = ServiceStateToFilter(parentService->GetState()); else - state = HostStateToFilter(host->GetState()); + state = HostStateToFilter(parentHost->GetState()); /* check state */ if (state & GetStateFilter()) { Log(LogNotice, "Dependency") - << "Dependency '" << GetName() << "' passed: " << (service ? "Service" : "Host") << " '" << parent->GetName() << "' matches state filter."; + << "Dependency '" << GetName() << "' passed: Parent " << (parentService ? "service" : "host") << " '" << parent->GetName() << "' matches state filter."; return true; } @@ -177,8 +177,8 @@ bool Dependency::IsAvailable(DependencyType dt) const Log(LogNotice, "Dependency") << "Dependency '" << GetName() << "' failed. Parent " - << (service ? "service" : "host") << " '" << parent->GetName() << "' is " - << (service ? Service::StateToString(service->GetState()) : Host::StateToString(host->GetState())); + << (parentService ? "service" : "host") << " '" << parent->GetName() << "' is " + << (parentService ? Service::StateToString(parentService->GetState()) : Host::StateToString(parentHost->GetState())); return false; } diff --git a/lib/perfdata/gelfwriter.cpp b/lib/perfdata/gelfwriter.cpp index f05af5553..f6c677299 100644 --- a/lib/perfdata/gelfwriter.cpp +++ b/lib/perfdata/gelfwriter.cpp @@ -149,7 +149,7 @@ void GelfWriter::NotificationToUserHandler(const Notification::Ptr& notification fields->Set("short_message", output); } else { fields->Set("_type", "HOST NOTIFICATION"); - fields->Set("short_message", "(" + (host->IsReachable() ? Host::StateToString(host->GetState()) : String("UNREACHABLE")) + ")"); + fields->Set("short_message", "(" + CompatUtility::GetHostStateString(host) + ")"); } fields->Set("_state", service ? Service::StateToString(service->GetState()) : Host::StateToString(host->GetState()));