2012-07-02 12:34:54 +02:00
|
|
|
#include "i2-cib.h"
|
2012-06-13 13:42:55 +02:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2012-07-03 14:18:46 +02:00
|
|
|
bool Service::m_DependencyCacheValid = false;
|
|
|
|
|
2012-06-28 14:24:41 +02:00
|
|
|
string Service::GetAlias(void) const
|
2012-06-13 13:42:55 +02:00
|
|
|
{
|
|
|
|
string value;
|
|
|
|
|
2012-06-28 14:24:41 +02:00
|
|
|
if (GetConfigObject()->GetProperty("alias", &value))
|
2012-06-13 13:42:55 +02:00
|
|
|
return value;
|
|
|
|
|
|
|
|
return GetName();
|
|
|
|
}
|
|
|
|
|
2012-06-30 13:39:55 +02:00
|
|
|
bool Service::Exists(const string& name)
|
|
|
|
{
|
|
|
|
return (ConfigObject::GetObject("service", name));
|
|
|
|
}
|
|
|
|
|
2012-06-29 14:14:51 +02:00
|
|
|
Service Service::GetByName(const string& name)
|
2012-06-27 18:43:34 +02:00
|
|
|
{
|
|
|
|
ConfigObject::Ptr configObject = ConfigObject::GetObject("service", name);
|
|
|
|
|
|
|
|
if (!configObject)
|
|
|
|
throw invalid_argument("Service '" + name + "' does not exist.");
|
|
|
|
|
|
|
|
return configObject;
|
|
|
|
}
|
|
|
|
|
2012-06-13 13:42:55 +02:00
|
|
|
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-27 18:43:34 +02:00
|
|
|
long value;
|
|
|
|
if (!GetConfigObject()->GetProperty("retry_interval", &value))
|
|
|
|
value = GetCheckInterval() / 5;
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
Dictionary::Ptr Service::GetDependencies(void) const
|
|
|
|
{
|
|
|
|
Dictionary::Ptr value;
|
|
|
|
GetConfigObject()->GetProperty("dependencies", &value);
|
2012-06-13 13:42:55 +02:00
|
|
|
return value;
|
|
|
|
}
|
2012-06-14 11:18:20 +02:00
|
|
|
|
2012-06-30 13:39:55 +02:00
|
|
|
Dictionary::Ptr Service::GetGroups(void) const
|
|
|
|
{
|
|
|
|
Dictionary::Ptr value;
|
|
|
|
GetConfigObject()->GetProperty("servicegroups", &value);
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2012-07-02 16:19:43 +02:00
|
|
|
Dictionary::Ptr Service::GetCheckers(void) const
|
|
|
|
{
|
|
|
|
Dictionary::Ptr value;
|
|
|
|
GetConfigObject()->GetProperty("checkers", &value);
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2012-07-03 14:18:46 +02:00
|
|
|
vector<Service> Service::GetParents(void) const
|
|
|
|
{
|
|
|
|
vector<Service> parents;
|
|
|
|
|
|
|
|
Dictionary::Ptr dependencies = GetDependencies();
|
|
|
|
if (dependencies) {
|
|
|
|
Dictionary::Iterator it;
|
|
|
|
for (it = dependencies->Begin(); it != dependencies->End(); it++)
|
|
|
|
parents.push_back(Service::GetByName(it->second));
|
|
|
|
}
|
|
|
|
return parents;
|
|
|
|
}
|
|
|
|
|
|
|
|
vector<Service> Service::GetChildren(void) const
|
|
|
|
{
|
|
|
|
vector<Service> children;
|
|
|
|
|
|
|
|
UpdateDependencyCache();
|
|
|
|
|
|
|
|
Dictionary::Ptr childrenCache;
|
|
|
|
GetConfigObject()->GetTag("dependency_children", &childrenCache);
|
|
|
|
|
|
|
|
if (childrenCache) {
|
|
|
|
Dictionary::Iterator it;
|
|
|
|
for (it = childrenCache->Begin(); it != childrenCache->End(); it++)
|
|
|
|
children.push_back(Service::GetByName(it->second));
|
|
|
|
}
|
|
|
|
|
|
|
|
return children;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Service::UpdateDependencyCache(void)
|
|
|
|
{
|
|
|
|
static long cacheTx = 0;
|
|
|
|
|
|
|
|
if (m_DependencyCacheValid)
|
|
|
|
return;
|
|
|
|
|
|
|
|
cacheTx++;
|
|
|
|
|
|
|
|
ConfigObject::TMap::Range range = ConfigObject::GetObjects("service");
|
|
|
|
ConfigObject::TMap::Iterator it;
|
|
|
|
for (it = range.first; it != range.second; it++) {
|
|
|
|
Service child = it->second;
|
|
|
|
|
|
|
|
vector<Service> parents = child.GetParents();
|
|
|
|
|
|
|
|
vector<Service>::iterator st;
|
|
|
|
for (st = parents.begin(); st != parents.end(); st++) {
|
|
|
|
Service parent = *st;
|
|
|
|
|
|
|
|
long tx = 0;
|
|
|
|
parent.GetConfigObject()->GetTag("dependency_cache_tx", &tx);
|
|
|
|
|
|
|
|
Dictionary::Ptr children;
|
|
|
|
|
|
|
|
/* rather than resetting the dependency dictionary in a separate loop we use the cache_tx
|
|
|
|
* tag to check if the dictionary is from this cache update run. */
|
|
|
|
if (tx != cacheTx) {
|
|
|
|
children = boost::make_shared<Dictionary>();
|
|
|
|
parent.GetConfigObject()->SetTag("dependency_children", children);
|
|
|
|
parent.GetConfigObject()->SetTag("dependency_cache_tx", cacheTx);
|
|
|
|
} else {
|
|
|
|
parent.GetConfigObject()->GetTag("dependency_children", &children);
|
|
|
|
assert(children);
|
|
|
|
}
|
|
|
|
|
|
|
|
children->AddUnnamedProperty(child.GetName());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m_DependencyCacheValid = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Service::InvalidateDependencyCache(void)
|
|
|
|
{
|
|
|
|
m_DependencyCacheValid = false;
|
|
|
|
}
|
|
|
|
|
2012-07-03 15:01:09 +02:00
|
|
|
ServiceStatusMessage Service::CalculateCombinedStatus(Service *current, ServiceStatusMessage *input, const vector<Service>& parents)
|
2012-07-03 14:18:46 +02:00
|
|
|
{
|
|
|
|
vector<Service> failedServices;
|
|
|
|
|
|
|
|
time_t nextCheck = -1;
|
|
|
|
|
|
|
|
vector<Service>::const_iterator it;
|
|
|
|
for (it = parents.begin(); it != parents.end(); it++) {
|
|
|
|
Service parent = *it;
|
|
|
|
|
2012-07-03 15:01:09 +02:00
|
|
|
if (current && current->GetName() == parent.GetName())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!parent.HasLastCheckResult())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
string svcname;
|
|
|
|
ServiceState state = StateUnknown;
|
|
|
|
ServiceStateType type = StateTypeHard;
|
|
|
|
if (input && input->GetService(&svcname) && svcname == parent.GetName()) {
|
|
|
|
input->GetState(&state);
|
|
|
|
input->GetStateType(&type);
|
|
|
|
} else {
|
|
|
|
state = parent.GetState();
|
|
|
|
type = parent.GetStateType();
|
|
|
|
}
|
2012-07-03 14:18:46 +02:00
|
|
|
|
2012-07-03 15:01:09 +02:00
|
|
|
if (state != StateOK && state != StateWarning && type == StateTypeHard)
|
|
|
|
failedServices.push_back(parent);
|
2012-07-03 14:18:46 +02:00
|
|
|
|
|
|
|
if (nextCheck == -1 || parent.GetNextCheck() < nextCheck)
|
|
|
|
nextCheck = parent.GetNextCheck();
|
|
|
|
}
|
|
|
|
|
|
|
|
string message;
|
|
|
|
ServiceState state;
|
|
|
|
|
|
|
|
if (failedServices.empty()) {
|
|
|
|
if (input)
|
|
|
|
return *input;
|
|
|
|
|
|
|
|
state = StateOK;
|
|
|
|
message = "Dependant services are available.";
|
|
|
|
} else {
|
|
|
|
state = StateUnreachable;
|
|
|
|
message = "One or more dependant services have failed.";
|
|
|
|
}
|
|
|
|
|
|
|
|
ServiceStatusMessage result;
|
|
|
|
result.SetState(state);
|
|
|
|
result.SetStateType(StateTypeHard);
|
|
|
|
result.SetCurrentCheckAttempt(1);
|
|
|
|
result.SetNextCheck(nextCheck);
|
|
|
|
|
|
|
|
time_t now;
|
|
|
|
time(&now);
|
|
|
|
|
|
|
|
CheckResult cr;
|
|
|
|
cr.SetScheduleStart(now);
|
|
|
|
cr.SetScheduleEnd(now);
|
|
|
|
cr.SetExecutionStart(now);
|
|
|
|
cr.SetExecutionEnd(now);
|
|
|
|
cr.SetOutput(message);
|
|
|
|
cr.SetState(state);
|
|
|
|
|
|
|
|
result.SetCheckResult(cr);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
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-27 18:43:34 +02:00
|
|
|
long value;
|
|
|
|
if (!GetConfigObject()->GetTag("next_check", &value)) {
|
2012-06-20 15:33:38 +02:00
|
|
|
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
|
|
|
|
2012-06-27 18:43:34 +02:00
|
|
|
void Service::UpdateNextCheck(void)
|
|
|
|
{
|
|
|
|
time_t now;
|
|
|
|
time(&now);
|
|
|
|
|
|
|
|
if (GetStateType() == StateTypeSoft)
|
|
|
|
SetNextCheck(now + GetRetryInterval());
|
|
|
|
else
|
|
|
|
SetNextCheck(now + GetCheckInterval());
|
|
|
|
}
|
|
|
|
|
2012-06-29 14:14:51 +02:00
|
|
|
void Service::SetChecker(const string& checker)
|
2012-06-16 20:44:24 +02:00
|
|
|
{
|
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
|
|
|
|
{
|
2012-06-25 15:54:50 +02:00
|
|
|
long value = 1;
|
2012-06-25 15:42:46 +02:00
|
|
|
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-07-03 14:18:46 +02:00
|
|
|
void Service::SetLastCheckResult(const CheckResult& result)
|
2012-06-27 18:43:34 +02:00
|
|
|
{
|
2012-07-03 14:18:46 +02:00
|
|
|
GetConfigObject()->SetTag("last_result", result.GetDictionary());
|
2012-06-27 18:43:34 +02:00
|
|
|
}
|
|
|
|
|
2012-07-03 14:18:46 +02:00
|
|
|
bool Service::HasLastCheckResult(void) const
|
2012-06-27 18:43:34 +02:00
|
|
|
{
|
|
|
|
Dictionary::Ptr value;
|
2012-07-03 14:18:46 +02:00
|
|
|
return GetConfigObject()->GetTag("last_result", &value) && value;
|
|
|
|
}
|
|
|
|
|
|
|
|
CheckResult Service::GetLastCheckResult(void) const
|
|
|
|
{
|
|
|
|
Dictionary::Ptr value;
|
|
|
|
if (!GetConfigObject()->GetTag("last_result", &value))
|
|
|
|
throw invalid_argument("Service has no last check result.");
|
|
|
|
return CheckResult(value);
|
2012-06-27 18:43:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Service::SetLastStateChange(time_t ts)
|
|
|
|
{
|
2012-07-01 13:21:49 +02:00
|
|
|
GetConfigObject()->SetTag("last_state_change", static_cast<long>(ts));
|
2012-06-27 18:43:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
time_t Service::GetLastStateChange(void) const
|
|
|
|
{
|
2012-06-27 23:38:50 +02:00
|
|
|
long value;
|
|
|
|
if (!GetConfigObject()->GetTag("last_state_change", &value))
|
|
|
|
value = IcingaApplication::GetInstance()->GetStartTime();
|
2012-06-27 18:43:34 +02:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Service::SetLastHardStateChange(time_t ts)
|
|
|
|
{
|
2012-07-01 13:21:49 +02:00
|
|
|
GetConfigObject()->SetTag("last_hard_state_change", static_cast<long>(ts));
|
2012-06-27 18:43:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
time_t Service::GetLastHardStateChange(void) const
|
|
|
|
{
|
2012-06-27 23:38:50 +02:00
|
|
|
long value;
|
|
|
|
if (!GetConfigObject()->GetTag("last_hard_state_change", &value))
|
|
|
|
value = IcingaApplication::GetInstance()->GetStartTime();
|
2012-06-27 18:43:34 +02:00
|
|
|
return 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();
|
|
|
|
|
2012-06-25 15:54:50 +02:00
|
|
|
if (cr.GetState() == StateOK) {
|
|
|
|
if (GetState() == StateOK)
|
|
|
|
SetStateType(StateTypeHard);
|
2012-06-25 15:42:46 +02:00
|
|
|
|
2012-06-25 15:54:50 +02:00
|
|
|
attempt = 1;
|
|
|
|
} else {
|
2012-06-25 15:42:46 +02:00
|
|
|
if (attempt >= GetMaxCheckAttempts()) {
|
|
|
|
SetStateType(StateTypeHard);
|
2012-06-25 15:54:50 +02:00
|
|
|
attempt = 1;
|
|
|
|
} else if (GetStateType() == StateTypeSoft || GetState() == StateOK) {
|
2012-06-25 15:42:46 +02:00
|
|
|
SetStateType(StateTypeSoft);
|
2012-06-25 15:54:50 +02:00
|
|
|
attempt++;
|
2012-06-25 15:42:46 +02:00
|
|
|
}
|
|
|
|
}
|
2012-06-25 14:13:24 +02:00
|
|
|
|
2012-06-25 15:54:50 +02:00
|
|
|
SetCurrentCheckAttempt(attempt);
|
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
|
|
|
|
2012-06-29 14:14:51 +02:00
|
|
|
ServiceState Service::StateFromString(const string& state)
|
2012-06-27 18:43:34 +02:00
|
|
|
{
|
|
|
|
/* TODO: make this thread-safe */
|
|
|
|
static map<string, ServiceState> stateLookup;
|
|
|
|
|
|
|
|
if (stateLookup.empty()) {
|
|
|
|
stateLookup["ok"] = StateOK;
|
|
|
|
stateLookup["warning"] = StateWarning;
|
|
|
|
stateLookup["critical"] = StateCritical;
|
|
|
|
stateLookup["unreachable"] = StateUnreachable;
|
|
|
|
stateLookup["uncheckable"] = StateUncheckable;
|
|
|
|
stateLookup["unknown"] = StateUnknown;
|
|
|
|
}
|
|
|
|
|
|
|
|
map<string, ServiceState>::iterator it;
|
|
|
|
it = stateLookup.find(state);
|
|
|
|
|
|
|
|
if (it == stateLookup.end())
|
|
|
|
return StateUnknown;
|
|
|
|
else
|
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
string Service::StateToString(ServiceState state)
|
|
|
|
{
|
|
|
|
switch (state) {
|
|
|
|
case StateOK:
|
|
|
|
return "ok";
|
|
|
|
case StateWarning:
|
|
|
|
return "warning";
|
|
|
|
case StateCritical:
|
|
|
|
return "critical";
|
|
|
|
case StateUnreachable:
|
|
|
|
return "unreachable";
|
|
|
|
case StateUncheckable:
|
|
|
|
return "uncheckable";
|
|
|
|
case StateUnknown:
|
|
|
|
default:
|
|
|
|
return "unknown";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-29 14:14:51 +02:00
|
|
|
ServiceStateType Service::StateTypeFromString(const string& type)
|
2012-06-27 18:43:34 +02:00
|
|
|
{
|
|
|
|
if (type == "soft")
|
|
|
|
return StateTypeSoft;
|
|
|
|
else
|
|
|
|
return StateTypeHard;
|
|
|
|
}
|
|
|
|
|
|
|
|
string Service::StateTypeToString(ServiceStateType type)
|
|
|
|
{
|
|
|
|
if (type == StateTypeSoft)
|
|
|
|
return "soft";
|
|
|
|
else
|
|
|
|
return "hard";
|
|
|
|
}
|
|
|
|
|
2012-06-29 14:14:51 +02:00
|
|
|
bool Service::IsAllowedChecker(const string& checker) const
|
2012-06-27 18:43:34 +02:00
|
|
|
{
|
2012-07-02 16:19:43 +02:00
|
|
|
Dictionary::Ptr checkers = GetCheckers();
|
|
|
|
|
|
|
|
if (!checkers)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
Dictionary::Iterator it;
|
|
|
|
for (it = checkers->Begin(); it != checkers->End(); it++) {
|
|
|
|
string pattern = it->second;
|
|
|
|
|
|
|
|
if (Utility::Match(pattern, checker))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2012-06-27 18:43:34 +02:00
|
|
|
}
|