diff --git a/components/compat/compatlogger.cpp b/components/compat/compatlogger.cpp index b20d8616d..34dba75bf 100644 --- a/components/compat/compatlogger.cpp +++ b/components/compat/compatlogger.cpp @@ -127,9 +127,14 @@ void CompatLogger::CheckResultHandler(const Checkable::Ptr& checkable, const Che << output << "" << ""; } else { + String state = Host::StateToString(Host::CalculateState(static_cast(state_after))); + + if (!reachable_after) + state = "UNREACHABLE"; + msgbuf << "HOST ALERT: " << host->GetName() << ";" - << Host::StateToString(Host::CalculateState(static_cast(state_after), host_reachable_after)) << ";" + << state << ";" << Host::StateTypeToString(static_cast(stateType_after)) << ";" << attempt_after << ";" << output << "" @@ -245,7 +250,7 @@ void CompatLogger::NotificationSentHandler(const Notification::Ptr& notification if (service) notification_type_str = Service::StateToString(service->GetState()); else - notification_type_str = Host::StateToString(host->GetState()); + notification_type_str = host->IsReachable() ? Host::StateToString(host->GetState()) : "UNREACHABLE"; } String author_comment = ""; @@ -277,7 +282,7 @@ void CompatLogger::NotificationSentHandler(const Notification::Ptr& notification << user->GetName() << ";" << host->GetName() << ";" << notification_type_str << " " - << "(" << Host::StateToString(host->GetState()) << ");" + << "(" << (host->IsReachable() ? Host::StateToString(host->GetState()) : "UNREACHABLE") << ");" << command_name << ";" << output << ";" << author_comment @@ -382,7 +387,7 @@ void CompatLogger::EventCommandHandler(const Checkable::Ptr& checkable) } else { msgbuf << "HOST EVENT HANDLER: " << host->GetName() << ";" - << Host::StateToString(host->GetState()) << ";" + << (host->IsReachable() ? Host::StateToString(host->GetState()) : "UNREACHABLE") << ";" << Host::StateTypeToString(host->GetStateType()) << ";" << current_attempt << ";" << event_command_name; @@ -456,7 +461,7 @@ void CompatLogger::ReopenFile(bool rotate) std::ostringstream msgbuf; msgbuf << "CURRENT HOST STATE: " << host->GetName() << ";" - << Host::StateToString(host->GetState()) << ";" + << (host->IsReachable() ? Host::StateToString(host->GetState()) : "UNREACHABLE") << ";" << Host::StateTypeToString(host->GetStateType()) << ";" << host->GetCheckAttempt() << ";" << output << ""; diff --git a/components/compat/statusdatawriter.cpp b/components/compat/statusdatawriter.cpp index f25fec7b8..2a739e155 100644 --- a/components/compat/statusdatawriter.cpp +++ b/components/compat/statusdatawriter.cpp @@ -354,7 +354,7 @@ void StatusDataWriter::DumpCheckableStatusAttrs(std::ostream& fp, const Checkabl if (service) fp << "\t" << "current_state=" << service->GetState() << "\n"; else - fp << "\t" << "current_state=" << host->GetState() << "\n"; + fp << "\t" << "current_state=" << (host->IsReachable() ? host->GetState() : 2) << "\n"; fp << "\t" "state_type=" << checkable->GetStateType() << "\n" "\t" "plugin_output=" << CompatUtility::GetCheckResultOutput(cr) << "\n" diff --git a/components/livestatus/hostgroupstable.cpp b/components/livestatus/hostgroupstable.cpp index aa856ce5a..a525cc651 100644 --- a/components/livestatus/hostgroupstable.cpp +++ b/components/livestatus/hostgroupstable.cpp @@ -197,7 +197,7 @@ Value HostGroupsTable::NumHostsUnreachAccessor(const Value& row) int num_hosts = 0; BOOST_FOREACH(const Host::Ptr& host, static_cast(row)->GetMembers()) { - if (host->GetState() == HostUnreachable) + if (!host->IsReachable()) num_hosts++; } diff --git a/components/livestatus/hoststable.cpp b/components/livestatus/hoststable.cpp index dff16e915..09f36c993 100644 --- a/components/livestatus/hoststable.cpp +++ b/components/livestatus/hoststable.cpp @@ -660,7 +660,7 @@ Value HostsTable::StateAccessor(const Value& row) if (!host) return Empty; - return host->GetState(); + return host->IsReachable() ? host->GetState() : 2; } Value HostsTable::StateTypeAccessor(const Value& row) diff --git a/components/perfdata/graphitewriter.cpp b/components/perfdata/graphitewriter.cpp index f8ec8ddb2..f81d4c76c 100644 --- a/components/perfdata/graphitewriter.cpp +++ b/components/perfdata/graphitewriter.cpp @@ -127,6 +127,7 @@ void GraphiteWriter::CheckResultHandler(const Checkable::Ptr& checkable, const C SendMetric(prefix, "current_attempt", checkable->GetCheckAttempt()); SendMetric(prefix, "max_check_attempts", checkable->GetMaxCheckAttempts()); SendMetric(prefix, "state_type", checkable->GetStateType()); + SendMetric(prefix, "reachable", checkable->IsReachable()); SendMetric(prefix, "latency", Service::CalculateLatency(cr)); SendMetric(prefix, "execution_time", Service::CalculateExecutionTime(cr)); SendPerfdata(prefix, cr); diff --git a/lib/db_ido/dbevents.cpp b/lib/db_ido/dbevents.cpp index 8b7bd7837..8b5fd908c 100644 --- a/lib/db_ido/dbevents.cpp +++ b/lib/db_ido/dbevents.cpp @@ -596,7 +596,6 @@ void DbEvents::AddCheckResultLogHistory(const Checkable::Ptr& checkable, const C long stateType_after = vars_after->Get("state_type"); long attempt_after = vars_after->Get("attempt"); bool reachable_after = vars_after->Get("reachable"); - bool host_reachable_after = vars_after->Get("host_reachable"); Dictionary::Ptr vars_before = cr->GetVarsBefore(); @@ -651,9 +650,14 @@ 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() << ";" - << Host::StateToString(Host::CalculateState(static_cast(state_after), host_reachable_after)) << ";" + << state << ";" << Service::StateTypeToString(static_cast(stateType_after)) << ";" << attempt_after << ";" << output << "" @@ -666,14 +670,13 @@ void DbEvents::AddCheckResultLogHistory(const Checkable::Ptr& checkable, const C case HostDown: type = LogEntryTypeHostDown; break; - case HostUnreachable: - type = LogEntryTypeHostUnreachable; - break; default: Log(LogCritical, "db_ido", "Unknown host state: " + Convert::ToString(state_after)); return; } + if (!reachable_after) + type = LogEntryTypeHostUnreachable; } AddLogHistory(checkable, msgbuf.str(), type); diff --git a/lib/db_ido/hostdbobject.cpp b/lib/db_ido/hostdbobject.cpp index e335ab76a..82e75ec7a 100644 --- a/lib/db_ido/hostdbobject.cpp +++ b/lib/db_ido/hostdbobject.cpp @@ -132,7 +132,7 @@ Dictionary::Ptr HostDbObject::GetStatusFields(void) const fields->Set("check_source", cr->GetCheckSource()); } - fields->Set("current_state", host->GetState()); + fields->Set("current_state", host->IsReachable() ? host->GetState() : 2); fields->Set("has_been_checked", CompatUtility::GetCheckableHasBeenChecked(host)); fields->Set("should_be_scheduled", CompatUtility::GetCheckableShouldBeScheduled(host)); fields->Set("current_check_attempt", host->GetCheckAttempt()); diff --git a/lib/icinga/checkresult.ti b/lib/icinga/checkresult.ti index 6c5a586b7..e94d10d39 100644 --- a/lib/icinga/checkresult.ti +++ b/lib/icinga/checkresult.ti @@ -10,8 +10,7 @@ code {{{ enum HostState { HostUp = 0, - HostDown = 1, - HostUnreachable = 2 + HostDown = 1 }; /** diff --git a/lib/icinga/cib.cpp b/lib/icinga/cib.cpp index 0c4f6c271..149b1ccb2 100644 --- a/lib/icinga/cib.cpp +++ b/lib/icinga/cib.cpp @@ -140,11 +140,12 @@ HostStatistics CIB::CalculateHostStats(void) BOOST_FOREACH(const Host::Ptr& host, DynamicType::GetObjects()) { ObjectLock olock(host); - if (host->GetState() == HostUp) - hs.hosts_up++; - if (host->GetState() == HostDown) - hs.hosts_down++; - if (host->GetState() == HostUnreachable) + if (host->IsReachable()) { + if (host->GetState() == HostUp) + hs.hosts_up++; + if (host->GetState() == HostDown) + hs.hosts_down++; + } else hs.hosts_unreachable++; if (!host->GetLastCheckResult()) diff --git a/lib/icinga/host.cpp b/lib/icinga/host.cpp index 166593576..3723055bd 100644 --- a/lib/icinga/host.cpp +++ b/lib/icinga/host.cpp @@ -134,11 +134,8 @@ Service::Ptr Host::GetServiceByShortName(const Value& name) } } -HostState Host::CalculateState(ServiceState state, bool reachable) +HostState Host::CalculateState(ServiceState state) { - if (!reachable) - return HostUnreachable; - switch (state) { case StateOK: case StateWarning: @@ -152,49 +149,21 @@ HostState Host::GetState(void) const { ASSERT(!OwnsLock()); - if (!IsReachable()) - return HostUnreachable; - - switch (GetStateRaw()) { - case StateOK: - case StateWarning: - return HostUp; - default: - return HostDown; - } - + return CalculateState(GetStateRaw()); } HostState Host::GetLastState(void) const { ASSERT(!OwnsLock()); - if (!IsReachable()) - return HostUnreachable; - - switch (GetLastStateRaw()) { - case StateOK: - case StateWarning: - return HostUp; - default: - return HostDown; - } + return CalculateState(GetLastStateRaw()); } HostState Host::GetLastHardState(void) const { ASSERT(!OwnsLock()); - if (!IsReachable()) - return HostUnreachable; - - switch (GetLastHardStateRaw()) { - case StateOK: - case StateWarning: - return HostUp; - default: - return HostDown; - } + return CalculateState(GetLastHardStateRaw()); } double Host::GetLastStateUp(void) const @@ -218,12 +187,8 @@ HostState Host::StateFromString(const String& state) { if (state == "UP") return HostUp; - else if (state == "DOWN") - return HostDown; - else if (state == "UNREACHABLE") - return HostUnreachable; else - return HostUnreachable; + return HostDown; } String Host::StateToString(HostState state) @@ -233,8 +198,6 @@ String Host::StateToString(HostState state) return "UP"; case HostDown: return "DOWN"; - case HostUnreachable: - return "UNREACHABLE"; default: return "INVALID"; } @@ -319,20 +282,7 @@ bool Host::ResolveMacro(const String& macro, const CheckResult::Ptr&, String *re CheckResult::Ptr cr = GetLastCheckResult(); if (key == "state") { - switch (GetState()) { - case HostUnreachable: - *result = "UNREACHABLE"; - break; - case HostUp: - *result = "UP"; - break; - case HostDown: - *result = "DOWN"; - break; - default: - ASSERT(0); - } - + *result = StateToString(GetState()); return true; } else if (key == "stateid") { *result = Convert::ToString(GetState()); diff --git a/lib/icinga/host.h b/lib/icinga/host.h index 868b183c7..c920dfd1d 100644 --- a/lib/icinga/host.h +++ b/lib/icinga/host.h @@ -53,7 +53,7 @@ public: int GetTotalServices(void) const; - static HostState CalculateState(ServiceState state, bool reachable); + static HostState CalculateState(ServiceState state); HostState GetState(void) const; HostState GetLastState(void) const; diff --git a/lib/icinga/notification.cpp b/lib/icinga/notification.cpp index d516f730b..c143ee4d7 100644 --- a/lib/icinga/notification.cpp +++ b/lib/icinga/notification.cpp @@ -411,8 +411,7 @@ int icinga::HostStateToFilter(HostState state) switch (state) { case HostUp: return StateFilterUp; - case HostDown: /* fall through */ - case HostUnreachable: + case HostDown: return StateFilterDown; default: VERIFY(!"Invalid state type.");