Implemented soft/hard states.

This commit is contained in:
Gunnar Beutner 2012-06-25 15:42:46 +02:00
parent 593e329248
commit 22f301073e
7 changed files with 103 additions and 24 deletions

View File

@ -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<long>(service.GetState()));
params.SetProperty("state_type", static_cast<long>(service.GetStateType()));
params.SetProperty("current_attempt", static_cast<long>(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);

View File

@ -39,16 +39,16 @@ time_t CheckResult::GetEndTime(void) const
return static_cast<time_t>(value);
}
void CheckResult::SetState(CheckState state)
void CheckResult::SetState(ServiceState state)
{
m_Data->SetProperty("state", static_cast<long>(state));
}
CheckState CheckResult::GetState(void) const
ServiceState CheckResult::GetState(void) const
{
long value = StateUnknown;
m_Data->GetProperty("state", &value);
return static_cast<CheckState>(value);
return static_cast<ServiceState>(value);
}
void CheckResult::SetOutput(string output)

View File

@ -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;

View File

@ -165,7 +165,7 @@ bool NagiosCheckTask::RunTask(void)
exitcode = status;
#endif /* _MSC_VER */
CheckState state;
ServiceState state;
switch (exitcode) {
case 0:

View File

@ -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<long>(state));
}
ServiceState Service::GetState(void) const
{
long value = StateUnknown;
GetConfigObject()->GetTag("state", &value);
return static_cast<ServiceState>(value);
}
void Service::SetStateType(ServiceStateType type)
{
GetConfigObject()->SetTag("state_type", static_cast<long>(type));
}
ServiceStateType Service::GetStateType(void) const
{
long value = StateTypeHard;
GetConfigObject()->GetTag("state_type", &value);
return static_cast<ServiceStateType>(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());
}

View File

@ -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);
};

View File

@ -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) {