icinga2/lib/icinga/service.cpp

481 lines
11 KiB
C++
Raw Normal View History

/******************************************************************************
* Icinga 2 *
* Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/) *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; either version 2 *
* of the License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the Free Software Foundation *
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
******************************************************************************/
2012-09-10 14:07:32 +02:00
#include "i2-icinga.h"
using namespace icinga;
REGISTER_CLASS(Service);
2012-09-05 10:13:24 +02:00
const int Service::DefaultMaxCheckAttempts = 3;
const int Service::DefaultCheckInterval = 5 * 60;
const int Service::MinCheckInterval = 15;
const int Service::CheckIntervalDivisor = 5;
boost::signal<void (const Service::Ptr&, const CheckResultMessage&)> Service::OnCheckResultReceived;
boost::signal<void (const Service::Ptr&, const String&)> Service::OnCheckerChanged;
Service::Service(const Dictionary::Ptr& serializedObject)
: DynamicObject(serializedObject)
{
RegisterAttribute("alias", Attribute_Config);
RegisterAttribute("host_name", Attribute_Config);
RegisterAttribute("macros", Attribute_Config);
RegisterAttribute("check_command", Attribute_Config);
RegisterAttribute("max_check_attempts", Attribute_Config);
RegisterAttribute("check_interval", Attribute_Config);
RegisterAttribute("retry_interval", Attribute_Config);
RegisterAttribute("dependencies", Attribute_Config);
RegisterAttribute("servicegroups", Attribute_Config);
RegisterAttribute("checkers", Attribute_Config);
RegisterAttribute("scheduling_offset", Attribute_Transient);
RegisterAttribute("next_check", Attribute_Replicated);
RegisterAttribute("checker", Attribute_Replicated);
RegisterAttribute("check_attempt", Attribute_Replicated);
RegisterAttribute("state", Attribute_Replicated);
RegisterAttribute("state_type", Attribute_Replicated);
RegisterAttribute("last_result", Attribute_Replicated);
RegisterAttribute("last_state_change", Attribute_Replicated);
RegisterAttribute("last_hard_state_change", Attribute_Replicated);
}
String Service::GetAlias(void) const
{
2012-08-03 13:19:55 +02:00
String value = Get("alias");
2012-08-03 13:19:55 +02:00
if (!value.IsEmpty())
return value;
return GetName();
}
bool Service::Exists(const String& name)
2012-06-30 13:39:55 +02:00
{
2012-07-30 10:17:29 +02:00
return (DynamicObject::GetObject("Service", name));
2012-06-30 13:39:55 +02:00
}
Service::Ptr Service::GetByName(const String& name)
2012-06-27 18:43:34 +02:00
{
2012-07-30 10:17:29 +02:00
DynamicObject::Ptr configObject = DynamicObject::GetObject("Service", name);
2012-06-27 18:43:34 +02:00
if (!configObject)
2012-07-17 20:41:06 +02:00
throw_exception(invalid_argument("Service '" + name + "' does not exist."));
2012-06-27 18:43:34 +02:00
return dynamic_pointer_cast<Service>(configObject);
2012-06-27 18:43:34 +02:00
}
Host::Ptr Service::GetHost(void) const
{
2012-08-03 13:19:55 +02:00
String hostname = Get("host_name");
if (hostname.IsEmpty())
2012-07-17 20:41:06 +02:00
throw_exception(runtime_error("Service object is missing the 'host_name' property."));
return Host::GetByName(hostname);
}
Dictionary::Ptr Service::GetMacros(void) const
{
2012-08-03 13:19:55 +02:00
return Get("macros");
}
String Service::GetCheckCommand(void) const
{
2012-08-03 13:19:55 +02:00
return Get("check_command");
}
long Service::GetMaxCheckAttempts(void) const
{
2012-08-03 13:19:55 +02:00
Value value = Get("max_check_attempts");
if (value.IsEmpty())
return DefaultMaxCheckAttempts;
2012-08-03 13:19:55 +02:00
return value;
}
long Service::GetCheckInterval(void) const
{
2012-08-03 13:19:55 +02:00
Value value = Get("check_interval");
if (value.IsEmpty())
return DefaultCheckInterval;
if (value < MinCheckInterval)
value = MinCheckInterval;
return value;
}
long Service::GetRetryInterval(void) const
{
2012-08-03 13:19:55 +02:00
Value value = Get("retry_interval");
if (value.IsEmpty())
return GetCheckInterval() / CheckIntervalDivisor;
2012-06-27 18:43:34 +02:00
return value;
}
Dictionary::Ptr Service::GetDependencies(void) const
{
2012-08-03 13:19:55 +02:00
return Get("dependencies");
}
2012-07-09 10:09:53 +02:00
void Service::GetDependenciesRecursive(const Dictionary::Ptr& result) const {
assert(result);
Dictionary::Ptr dependencies = GetDependencies();
if (!dependencies)
return;
Value dependency;
BOOST_FOREACH(tie(tuples::ignore, dependency), dependencies) {
2012-07-16 22:00:50 +02:00
if (result->Contains(dependency))
2012-07-09 10:09:53 +02:00
continue;
2012-07-16 22:00:50 +02:00
result->Set(dependency, dependency);
2012-07-09 10:09:53 +02:00
Service::Ptr service = Service::GetByName(dependency);
service->GetDependenciesRecursive(result);
2012-07-09 10:09:53 +02:00
}
}
2012-06-30 13:39:55 +02:00
Dictionary::Ptr Service::GetGroups(void) const
{
2012-08-03 13:19:55 +02:00
return Get("servicegroups");
2012-06-30 13:39:55 +02:00
}
2012-07-02 16:19:43 +02:00
Dictionary::Ptr Service::GetCheckers(void) const
{
2012-08-03 13:19:55 +02:00
return Get("checkers");
2012-07-02 16:19:43 +02:00
}
2012-07-03 15:26:58 +02:00
bool Service::IsReachable(void) const
2012-07-03 14:18:46 +02:00
{
2012-07-09 10:09:53 +02:00
Dictionary::Ptr dependencies = boost::make_shared<Dictionary>();
GetDependenciesRecursive(dependencies);
2012-07-03 14:18:46 +02:00
Value dependency;
BOOST_FOREACH(tie(tuples::ignore, dependency), dependencies) {
Service::Ptr service = Service::GetByName(dependency);
2012-07-09 10:09:53 +02:00
/* ignore ourselves */
if (service->GetName() == GetName())
2012-07-09 10:09:53 +02:00
continue;
2012-07-09 12:44:31 +02:00
/* ignore pending services */
if (!service->GetLastCheckResult())
2012-07-09 12:44:31 +02:00
continue;
2012-07-09 17:03:24 +02:00
/* ignore soft states */
if (service->GetStateType() == StateTypeSoft)
2012-07-09 17:03:24 +02:00
continue;
/* ignore services states OK and Warning */
if (service->GetState() == StateOK ||
service->GetState() == StateWarning)
2012-07-09 17:03:24 +02:00
continue;
return false;
2012-07-03 14:18:46 +02:00
}
2012-07-09 17:03:24 +02:00
2012-07-03 15:20:44 +02:00
return true;
2012-07-03 14:18:46 +02:00
}
void Service::SetSchedulingOffset(long offset)
{
2012-08-03 13:19:55 +02:00
Set("scheduling_offset", offset);
}
long Service::GetSchedulingOffset(void)
{
2012-08-03 13:19:55 +02:00
Value value = Get("scheduling_offset");
if (value.IsEmpty()) {
value = rand();
SetSchedulingOffset(value);
}
2012-08-03 13:19:55 +02:00
return value;
}
void Service::SetNextCheck(double nextCheck)
{
2012-08-03 13:19:55 +02:00
Set("next_check", nextCheck);
}
double Service::GetNextCheck(void)
{
2012-08-03 13:19:55 +02:00
Value value = Get("next_check");
if (value.IsEmpty()) {
UpdateNextCheck();
2012-08-03 13:19:55 +02:00
value = Get("next_check");
if (value.IsEmpty())
throw_exception(runtime_error("Failed to schedule next check."));
}
2012-08-03 13:19:55 +02:00
return value;
}
2012-06-27 18:43:34 +02:00
void Service::UpdateNextCheck(void)
{
double interval;
2012-06-27 18:43:34 +02:00
if (GetStateType() == StateTypeSoft)
interval = GetRetryInterval();
2012-06-27 18:43:34 +02:00
else
interval = GetCheckInterval();
double now = Utility::GetTime();
double adj = fmod(now + GetSchedulingOffset(), interval);
SetNextCheck(now - adj + interval);
2012-06-27 18:43:34 +02:00
}
void Service::SetChecker(const String& checker)
{
2012-08-03 13:19:55 +02:00
Set("checker", checker);
}
String Service::GetChecker(void) const
{
2012-08-03 13:19:55 +02:00
return Get("checker");
}
2012-06-25 15:42:46 +02:00
void Service::SetCurrentCheckAttempt(long attempt)
{
2012-08-03 13:19:55 +02:00
Set("check_attempt", attempt);
2012-06-25 15:42:46 +02:00
}
long Service::GetCurrentCheckAttempt(void) const
{
2012-08-03 13:19:55 +02:00
Value value = Get("check_attempt");
if (value.IsEmpty())
return 1;
2012-06-25 15:42:46 +02:00
return value;
}
void Service::SetState(ServiceState state)
{
2012-08-03 13:19:55 +02:00
Set("state", static_cast<long>(state));
2012-06-25 15:42:46 +02:00
}
ServiceState Service::GetState(void) const
{
2012-08-03 13:19:55 +02:00
Value value = Get("state");
if (value.IsEmpty())
return StateUnknown;
int ivalue = static_cast<int>(value);
return static_cast<ServiceState>(ivalue);
2012-06-25 15:42:46 +02:00
}
void Service::SetStateType(ServiceStateType type)
{
2012-08-03 13:19:55 +02:00
Set("state_type", static_cast<long>(type));
2012-06-25 15:42:46 +02:00
}
ServiceStateType Service::GetStateType(void) const
{
2012-08-03 13:19:55 +02:00
Value value = Get("state_type");
if (value.IsEmpty())
return StateTypeHard;
int ivalue = static_cast<int>(value);
return static_cast<ServiceStateType>(ivalue);
2012-06-25 15:42:46 +02:00
}
void Service::SetLastCheckResult(const Dictionary::Ptr& result)
2012-06-27 18:43:34 +02:00
{
2012-08-03 13:19:55 +02:00
Set("last_result", result);
2012-07-03 14:18:46 +02:00
}
Dictionary::Ptr Service::GetLastCheckResult(void) const
2012-07-03 14:18:46 +02:00
{
2012-08-03 13:19:55 +02:00
return Get("last_result");
2012-06-27 18:43:34 +02:00
}
void Service::SetLastStateChange(double ts)
2012-06-27 18:43:34 +02:00
{
2012-08-03 13:19:55 +02:00
Set("last_state_change", static_cast<long>(ts));
2012-06-27 18:43:34 +02:00
}
double Service::GetLastStateChange(void) const
2012-06-27 18:43:34 +02:00
{
2012-08-03 13:19:55 +02:00
Value value = Get("last_state_change");
if (value.IsEmpty())
return IcingaApplication::GetInstance()->GetStartTime();
2012-06-27 18:43:34 +02:00
return value;
}
void Service::SetLastHardStateChange(double ts)
2012-06-27 18:43:34 +02:00
{
2012-08-03 13:19:55 +02:00
Set("last_hard_state_change", ts);
2012-06-27 18:43:34 +02:00
}
double Service::GetLastHardStateChange(void) const
2012-06-27 18:43:34 +02:00
{
2012-08-03 13:19:55 +02:00
Value value = Get("last_hard_state_change");
if (value.IsEmpty())
2012-06-27 23:38:50 +02:00
value = IcingaApplication::GetInstance()->GetStartTime();
2012-08-03 13:19:55 +02:00
2012-06-27 18:43:34 +02:00
return value;
}
void Service::ApplyCheckResult(const Dictionary::Ptr& cr)
{
2012-07-24 15:38:30 +02:00
ServiceState old_state = GetState();
ServiceStateType old_stateType = GetStateType();
2012-06-25 15:42:46 +02:00
long attempt = GetCurrentCheckAttempt();
if (cr->Get("state") == StateOK) {
if (GetState() == StateOK)
SetStateType(StateTypeHard);
2012-06-25 15:42:46 +02:00
attempt = 1;
} else {
2012-06-25 15:42:46 +02:00
if (attempt >= GetMaxCheckAttempts()) {
SetStateType(StateTypeHard);
attempt = 1;
} else if (GetStateType() == StateTypeSoft || GetState() == StateOK) {
2012-06-25 15:42:46 +02:00
SetStateType(StateTypeSoft);
attempt++;
2012-06-25 15:42:46 +02:00
}
}
SetCurrentCheckAttempt(attempt);
int state = cr->Get("state");
SetState(static_cast<ServiceState>(state));
2012-07-24 15:38:30 +02:00
SetLastCheckResult(cr);
if (old_state != GetState()) {
double now = Utility::GetTime();
2012-07-24 15:38:30 +02:00
SetLastStateChange(now);
if (old_stateType != GetStateType())
SetLastHardStateChange(now);
}
}
2012-06-25 15:42:46 +02:00
ServiceState Service::StateFromString(const String& state)
2012-06-27 18:43:34 +02:00
{
if (state == "ok")
return StateOK;
else if (state == "warning")
return StateWarning;
else if (state == "critical")
return StateCritical;
else if (state == "uncheckable")
return StateUncheckable;
2012-06-27 18:43:34 +02:00
else
return StateUnknown;
2012-06-27 18:43:34 +02:00
}
String Service::StateToString(ServiceState state)
2012-06-27 18:43:34 +02:00
{
switch (state) {
case StateOK:
return "ok";
case StateWarning:
return "warning";
case StateCritical:
return "critical";
case StateUncheckable:
return "uncheckable";
case StateUnknown:
default:
return "unknown";
}
}
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)
2012-06-27 18:43:34 +02:00
{
if (type == StateTypeSoft)
return "soft";
else
return "hard";
}
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;
Value pattern;
BOOST_FOREACH(tie(tuples::ignore, pattern), checkers) {
2012-07-02 16:19:43 +02:00
if (Utility::Match(pattern, checker))
return true;
}
return false;
2012-06-27 18:43:34 +02:00
}
2012-07-09 10:09:53 +02:00
Dictionary::Ptr Service::ResolveDependencies(const Host::Ptr& host, const Dictionary::Ptr& dependencies)
2012-07-09 10:09:53 +02:00
{
2012-08-03 13:19:55 +02:00
Dictionary::Ptr services = host->Get("services");
2012-07-09 10:09:53 +02:00
Dictionary::Ptr result = boost::make_shared<Dictionary>();
Value dependency;
BOOST_FOREACH(tie(tuples::ignore, dependency), dependencies) {
String name;
2012-07-16 22:00:50 +02:00
if (services && services->Contains(dependency))
name = host->GetName() + "-" + static_cast<String>(dependency);
2012-07-09 10:09:53 +02:00
else
name = static_cast<String>(dependency);
result->Set(name, name);
2012-07-09 10:09:53 +02:00
}
2012-07-14 15:59:59 +02:00
return result;
2012-07-09 10:09:53 +02:00
}
void Service::OnAttributeChanged(const String& name, const Value& oldValue)
{
if (name == "checker")
OnCheckerChanged(GetSelf(), oldValue);
}