2012-06-13 13:42:55 +02:00
|
|
|
#include "i2-icinga.h"
|
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
|
|
|
string Service::GetDisplayName(void) const
|
|
|
|
{
|
|
|
|
string value;
|
|
|
|
|
|
|
|
if (GetConfigObject()->GetProperty("displayname", &value))
|
|
|
|
return value;
|
|
|
|
|
|
|
|
return GetName();
|
|
|
|
}
|
|
|
|
|
|
|
|
Host Service::GetHost(void) const
|
|
|
|
{
|
|
|
|
string hostname;
|
|
|
|
if (!GetConfigObject()->GetProperty("host_name", &hostname))
|
|
|
|
throw runtime_error("Service object is missing the 'host_name' property.");
|
|
|
|
|
|
|
|
return Host::GetByName(hostname);
|
|
|
|
}
|
|
|
|
|
|
|
|
Dictionary::Ptr Service::GetMacros(void) const
|
|
|
|
{
|
|
|
|
Dictionary::Ptr macros;
|
|
|
|
GetConfigObject()->GetProperty("macros", ¯os);
|
|
|
|
return macros;
|
|
|
|
}
|
|
|
|
|
|
|
|
string Service::GetCheckType(void) const
|
|
|
|
{
|
|
|
|
string value = "nagios";
|
|
|
|
GetConfigObject()->GetProperty("check_type", &value);
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
string Service::GetCheckCommand(void) const
|
|
|
|
{
|
|
|
|
string value;
|
|
|
|
GetConfigObject()->GetProperty("check_command", &value);
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
long Service::GetMaxCheckAttempts(void) const
|
|
|
|
{
|
2012-06-25 15:42:46 +02:00
|
|
|
long value = 3;
|
2012-06-13 13:42:55 +02:00
|
|
|
GetConfigObject()->GetProperty("max_check_attempts", &value);
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
long Service::GetCheckInterval(void) const
|
|
|
|
{
|
2012-06-14 11:18:20 +02:00
|
|
|
long value = 300;
|
2012-06-13 13:42:55 +02:00
|
|
|
GetConfigObject()->GetProperty("check_interval", &value);
|
2012-06-18 02:03:24 +02:00
|
|
|
|
|
|
|
if (value < 15)
|
|
|
|
value = 15;
|
|
|
|
|
2012-06-13 13:42:55 +02:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
long Service::GetRetryInterval(void) const
|
|
|
|
{
|
2012-06-25 15:42:46 +02:00
|
|
|
long value = 60;
|
2012-06-13 13:42:55 +02:00
|
|
|
GetConfigObject()->GetProperty("retry_interval", &value);
|
|
|
|
return value;
|
|
|
|
}
|
2012-06-14 11:18:20 +02:00
|
|
|
|
|
|
|
void Service::SetNextCheck(time_t nextCheck)
|
|
|
|
{
|
2012-06-25 15:42:46 +02:00
|
|
|
GetConfigObject()->SetTag("next_check", (long)nextCheck);
|
2012-06-14 11:18:20 +02:00
|
|
|
}
|
|
|
|
|
2012-06-18 02:03:24 +02:00
|
|
|
time_t Service::GetNextCheck(void)
|
2012-06-14 11:18:20 +02:00
|
|
|
{
|
2012-06-20 15:33:38 +02:00
|
|
|
long value = -1;
|
2012-06-25 15:42:46 +02:00
|
|
|
GetConfigObject()->GetTag("next_check", &value);
|
2012-06-20 15:33:38 +02:00
|
|
|
if (value == -1) {
|
|
|
|
value = time(NULL) + rand() % GetCheckInterval();
|
|
|
|
SetNextCheck(value);
|
|
|
|
}
|
|
|
|
return value;
|
2012-06-14 11:18:20 +02:00
|
|
|
}
|
2012-06-16 20:44:24 +02:00
|
|
|
|
|
|
|
void Service::SetChecker(string checker)
|
|
|
|
{
|
2012-06-25 15:42:46 +02:00
|
|
|
GetConfigObject()->SetTag("checker", checker);
|
2012-06-16 20:44:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
string Service::GetChecker(void) const
|
|
|
|
{
|
|
|
|
string value;
|
2012-06-25 15:42:46 +02:00
|
|
|
GetConfigObject()->GetTag("checker", &value);
|
2012-06-16 20:44:24 +02:00
|
|
|
return value;
|
|
|
|
}
|
2012-06-17 22:46:40 +02:00
|
|
|
|
2012-06-25 15:42:46 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2012-06-25 14:13:24 +02:00
|
|
|
void Service::ApplyCheckResult(const CheckResult& cr)
|
|
|
|
{
|
2012-06-25 15:42:46 +02:00
|
|
|
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);
|
|
|
|
}
|
2012-06-25 14:13:24 +02:00
|
|
|
|
2012-06-25 15:42:46 +02:00
|
|
|
SetState(cr.GetState());
|
2012-06-25 14:13:24 +02:00
|
|
|
}
|
2012-06-25 15:42:46 +02:00
|
|
|
|