diff --git a/components/checker/checkercomponent.cpp b/components/checker/checkercomponent.cpp index c7205d6a8..862b7e992 100644 --- a/components/checker/checkercomponent.cpp +++ b/components/checker/checkercomponent.cpp @@ -131,19 +131,23 @@ void CheckerComponent::ResultTimerHandler(void) if (result.GetState() != StateOK) failed++; + /* update service state */ + service.ApplyCheckResult(result); + RequestMessage rm; rm.SetMethod("checker::CheckResult"); MessagePart params; params.SetProperty("service", service.GetName()); + params.SetProperty("state", static_cast(service.GetState())); + params.SetProperty("state_type", static_cast(service.GetStateType())); + params.SetProperty("current_attempt", static_cast(service.GetCurrentCheckAttempt())); params.SetProperty("result", result.GetDictionary()); rm.SetParams(params); GetEndpointManager()->SendMulticastMessage(m_CheckerEndpoint, rm); - service.ApplyCheckResult(result); - service.SetNextCheck(now + service.GetCheckInterval()); m_PendingServices.erase(service.GetConfigObject()); m_Services.push(service); diff --git a/icinga/checkresult.cpp b/icinga/checkresult.cpp index caeaa9e67..4cef02fa9 100644 --- a/icinga/checkresult.cpp +++ b/icinga/checkresult.cpp @@ -39,16 +39,16 @@ time_t CheckResult::GetEndTime(void) const return static_cast(value); } -void CheckResult::SetState(CheckState state) +void CheckResult::SetState(ServiceState state) { m_Data->SetProperty("state", static_cast(state)); } -CheckState CheckResult::GetState(void) const +ServiceState CheckResult::GetState(void) const { long value = StateUnknown; m_Data->GetProperty("state", &value); - return static_cast(value); + return static_cast(value); } void CheckResult::SetOutput(string output) diff --git a/icinga/checkresult.h b/icinga/checkresult.h index a1a6a2bd0..fbd4ebf3d 100644 --- a/icinga/checkresult.h +++ b/icinga/checkresult.h @@ -4,16 +4,6 @@ namespace icinga { -enum CheckState -{ - StateOK, - StateWarning, - StateCritical, - StateUnreachable, - StateUncheckable, - StateUnknown -}; - struct CheckResult { public: @@ -28,8 +18,8 @@ public: void SetEndTime(time_t ts); time_t GetEndTime(void) const; - void SetState(CheckState state); - CheckState GetState(void) const; + void SetState(ServiceState state); + ServiceState GetState(void) const; void SetOutput(string output); string GetOutput(void) const; diff --git a/icinga/nagioschecktask.cpp b/icinga/nagioschecktask.cpp index b33813b3e..ec39c6af2 100644 --- a/icinga/nagioschecktask.cpp +++ b/icinga/nagioschecktask.cpp @@ -165,7 +165,7 @@ bool NagiosCheckTask::RunTask(void) exitcode = status; #endif /* _MSC_VER */ - CheckState state; + ServiceState state; switch (exitcode) { case 0: diff --git a/icinga/service.cpp b/icinga/service.cpp index 068af4749..cf9fc5d06 100644 --- a/icinga/service.cpp +++ b/icinga/service.cpp @@ -44,7 +44,7 @@ string Service::GetCheckCommand(void) const long Service::GetMaxCheckAttempts(void) const { - long value = 1; + long value = 3; GetConfigObject()->GetProperty("max_check_attempts", &value); return value; } @@ -62,20 +62,20 @@ long Service::GetCheckInterval(void) const long Service::GetRetryInterval(void) const { - long value = 15; + long value = 60; GetConfigObject()->GetProperty("retry_interval", &value); return value; } void Service::SetNextCheck(time_t nextCheck) { - GetConfigObject()->SetProperty("next_check", (long)nextCheck); + GetConfigObject()->SetTag("next_check", (long)nextCheck); } time_t Service::GetNextCheck(void) { long value = -1; - GetConfigObject()->GetProperty("next_check", &value); + GetConfigObject()->GetTag("next_check", &value); if (value == -1) { value = time(NULL) + rand() % GetCheckInterval(); SetNextCheck(value); @@ -85,17 +85,74 @@ time_t Service::GetNextCheck(void) void Service::SetChecker(string checker) { - GetConfigObject()->SetProperty("checker", checker); + GetConfigObject()->SetTag("checker", checker); } string Service::GetChecker(void) const { string value; - GetConfigObject()->GetProperty("checker", &value); + GetConfigObject()->GetTag("checker", &value); return value; } +void Service::SetCurrentCheckAttempt(long attempt) +{ + GetConfigObject()->SetTag("check_attempt", attempt); +} + +long Service::GetCurrentCheckAttempt(void) const +{ + long value = 0; + GetConfigObject()->GetTag("check_attempt", &value); + return value; +} + +void Service::SetState(ServiceState state) +{ + GetConfigObject()->SetTag("state", static_cast(state)); +} + +ServiceState Service::GetState(void) const +{ + long value = StateUnknown; + GetConfigObject()->GetTag("state", &value); + return static_cast(value); +} + +void Service::SetStateType(ServiceStateType type) +{ + GetConfigObject()->SetTag("state_type", static_cast(type)); +} + +ServiceStateType Service::GetStateType(void) const +{ + long value = StateTypeHard; + GetConfigObject()->GetTag("state_type", &value); + return static_cast(value); +} + void Service::ApplyCheckResult(const CheckResult& cr) { + long attempt = GetCurrentCheckAttempt(); + if (GetState() == StateOK && cr.GetState() == StateOK) { + SetStateType(StateTypeHard); + SetCurrentCheckAttempt(0); + } else if (GetState() == StateOK && cr.GetState() != StateOK) { + attempt++; + + if (attempt >= GetMaxCheckAttempts()) { + SetStateType(StateTypeHard); + attempt = 0; + } else { + SetStateType(StateTypeSoft); + } + + SetCurrentCheckAttempt(attempt); + } else if (GetState() != StateOK && cr.GetState() == StateOK) { + SetState(StateOK); + } + + SetState(cr.GetState()); } + diff --git a/icinga/service.h b/icinga/service.h index e8ac647a7..5cf13e862 100644 --- a/icinga/service.h +++ b/icinga/service.h @@ -4,6 +4,22 @@ namespace icinga { +enum ServiceState +{ + StateOK, + StateWarning, + StateCritical, + StateUnreachable, + StateUncheckable, + StateUnknown +}; + +enum ServiceStateType +{ + StateTypeHard, + StateTypeSoft +}; + struct CheckResult; class I2_ICINGA_API Service : public ConfigObjectAdapter @@ -24,9 +40,19 @@ public: void SetNextCheck(time_t nextCheck); time_t GetNextCheck(void); + void SetChecker(string checker); string GetChecker(void) const; + void SetCurrentCheckAttempt(long attempt); + long GetCurrentCheckAttempt(void) const; + + void SetState(ServiceState state); + ServiceState GetState(void) const; + + void SetStateType(ServiceStateType type); + ServiceStateType GetStateType(void) const; + void ApplyCheckResult(const CheckResult& cr); }; diff --git a/jsonrpc/jsonrpcclient.cpp b/jsonrpc/jsonrpcclient.cpp index e457b9617..6f6ab2d3b 100644 --- a/jsonrpc/jsonrpcclient.cpp +++ b/jsonrpc/jsonrpcclient.cpp @@ -62,6 +62,8 @@ void JsonRpcClient::DataAvailableHandler(void) return; } + std::cerr << jsonString << std::endl; + message = MessagePart(jsonString); OnNewMessage(GetSelf(), message); } catch (const std::exception& ex) {