icinga2/lib/icinga/service.cpp

505 lines
13 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. *
******************************************************************************/
2013-03-16 21:18:53 +01:00
#include "icinga/service.h"
2013-03-17 20:19:29 +01:00
#include "icinga/servicegroup.h"
#include "icinga/icingaapplication.h"
#include "icinga/macroprocessor.h"
2013-03-16 21:18:53 +01:00
#include "base/dynamictype.h"
#include "base/objectlock.h"
2013-03-17 20:19:29 +01:00
#include "config/configitembuilder.h"
2013-03-16 21:18:53 +01:00
#include <boost/smart_ptr/make_shared.hpp>
#include <boost/foreach.hpp>
using namespace icinga;
2013-03-01 12:07:52 +01:00
REGISTER_TYPE(Service);
Service::Service(const Dictionary::Ptr& serializedObject)
2013-03-06 11:03:50 +01:00
: DynamicObject(serializedObject), m_CheckRunning(false)
2013-02-26 10:13:54 +01:00
{
RegisterAttribute("display_name", Attribute_Config, &m_DisplayName);
RegisterAttribute("macros", Attribute_Config, &m_Macros);
RegisterAttribute("hostdependencies", Attribute_Config, &m_HostDependencies);
RegisterAttribute("servicedependencies", Attribute_Config, &m_ServiceDependencies);
RegisterAttribute("servicegroups", Attribute_Config, &m_ServiceGroups);
RegisterAttribute("check_command", Attribute_Config, &m_CheckCommand);
RegisterAttribute("max_check_attempts", Attribute_Config, &m_MaxCheckAttempts);
2013-03-13 16:04:53 +01:00
RegisterAttribute("check_period", Attribute_Config, &m_CheckPeriod);
2013-02-26 10:13:54 +01:00
RegisterAttribute("check_interval", Attribute_Config, &m_CheckInterval);
RegisterAttribute("retry_interval", Attribute_Config, &m_RetryInterval);
RegisterAttribute("checkers", Attribute_Config, &m_Checkers);
RegisterAttribute("next_check", Attribute_Replicated, &m_NextCheck);
RegisterAttribute("current_checker", Attribute_Replicated, &m_CurrentChecker);
RegisterAttribute("check_attempt", Attribute_Replicated, &m_CheckAttempt);
RegisterAttribute("state", Attribute_Replicated, &m_State);
RegisterAttribute("state_type", Attribute_Replicated, &m_StateType);
2013-03-07 12:04:20 +01:00
RegisterAttribute("last_state", Attribute_Replicated, &m_LastState);
RegisterAttribute("last_state_type", Attribute_Replicated, &m_LastStateType);
2013-03-19 13:04:30 +01:00
RegisterAttribute("last_reachable", Attribute_Replicated, &m_LastReachable);
2013-02-26 10:13:54 +01:00
RegisterAttribute("last_result", Attribute_Replicated, &m_LastResult);
RegisterAttribute("last_state_change", Attribute_Replicated, &m_LastStateChange);
RegisterAttribute("last_hard_state_change", Attribute_Replicated, &m_LastHardStateChange);
2013-03-18 12:55:41 +01:00
RegisterAttribute("last_in_downtime", Attribute_Replicated, &m_LastInDowntime);
2013-02-26 10:13:54 +01:00
RegisterAttribute("enable_active_checks", Attribute_Replicated, &m_EnableActiveChecks);
RegisterAttribute("enable_passive_checks", Attribute_Replicated, &m_EnablePassiveChecks);
RegisterAttribute("force_next_check", Attribute_Replicated, &m_ForceNextCheck);
RegisterAttribute("short_name", Attribute_Config, &m_ShortName);
RegisterAttribute("host_name", Attribute_Config, &m_HostName);
RegisterAttribute("acknowledgement", Attribute_Replicated, &m_Acknowledgement);
RegisterAttribute("acknowledgement_expiry", Attribute_Replicated, &m_AcknowledgementExpiry);
RegisterAttribute("comments", Attribute_Replicated, &m_Comments);
RegisterAttribute("downtimes", Attribute_Replicated, &m_Downtimes);
RegisterAttribute("enable_notifications", Attribute_Replicated, &m_EnableNotifications);
2013-02-26 10:13:54 +01:00
SetSchedulingOffset(rand());
}
2013-02-20 19:52:25 +01:00
Service::~Service(void)
2013-02-19 23:02:08 +01:00
{
2013-02-27 15:23:25 +01:00
ServiceGroup::InvalidateMembersCache();
Host::InvalidateServicesCache();
Service::InvalidateDowntimesCache();
Service::InvalidateCommentsCache();
2013-02-19 23:02:08 +01:00
}
2013-03-02 09:07:47 +01:00
/**
* @threadsafety Always.
*/
void Service::OnRegistrationCompleted(void)
{
ASSERT(!OwnsLock());
2013-03-02 09:07:47 +01:00
DynamicObject::OnRegistrationCompleted();
2013-03-02 09:07:47 +01:00
InvalidateNotificationsCache();
}
2013-03-02 09:07:47 +01:00
/**
* @threadsafety Always.
*/
String Service::GetDisplayName(void) const
{
2013-02-26 10:13:54 +01:00
if (m_DisplayName.IsEmpty())
2013-02-24 01:10:34 +01:00
return GetShortName();
2013-02-26 10:13:54 +01:00
else
return m_DisplayName;
}
2013-02-17 19:14:34 +01:00
/**
* @threadsafety Always.
*/
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
return dynamic_pointer_cast<Service>(configObject);
2012-06-27 18:43:34 +02:00
}
2013-02-17 19:14:34 +01:00
/**
* @threadsafety Always.
*/
Service::Ptr Service::GetByNamePair(const String& hostName, const String& serviceName)
{
if (!hostName.IsEmpty()) {
Host::Ptr host = Host::GetByName(hostName);
2013-02-27 15:23:25 +01:00
if (!host)
return Service::Ptr();
2013-03-02 09:07:47 +01:00
return host->GetServiceByShortName(serviceName);
} else {
return Service::GetByName(serviceName);
}
}
2013-03-02 09:07:47 +01:00
/**
* @threadsafety Always.
*/
Host::Ptr Service::GetHost(void) const
{
return Host::GetByName(m_HostName);
}
2013-03-02 09:07:47 +01:00
/**
* @threadsafety Always.
*/
Dictionary::Ptr Service::GetMacros(void) const
{
2013-02-26 10:13:54 +01:00
return m_Macros;
}
2013-03-02 09:07:47 +01:00
/**
* @threadsafety Always.
*/
2013-03-14 12:17:46 +01:00
Array::Ptr Service::GetHostDependencies(void) const
2012-06-27 18:43:34 +02:00
{
2013-02-26 10:13:54 +01:00
return m_HostDependencies;
}
2013-03-02 09:07:47 +01:00
/**
* @threadsafety Always.
*/
2013-03-14 12:17:46 +01:00
Array::Ptr Service::GetServiceDependencies(void) const
2013-02-07 20:29:35 +01:00
{
2013-02-26 10:13:54 +01:00
return m_ServiceDependencies;
2012-07-09 10:09:53 +02:00
}
2013-03-02 09:07:47 +01:00
/**
* @threadsafety Always.
*/
2013-03-14 12:17:46 +01:00
Array::Ptr Service::GetGroups(void) const
2012-06-30 13:39:55 +02:00
{
2013-02-26 10:13:54 +01:00
return m_ServiceGroups;
2012-06-30 13:39:55 +02:00
}
2013-03-02 09:07:47 +01:00
/**
* @threadsafety Always.
*/
2013-02-26 10:13:54 +01:00
String Service::GetHostName(void) const
{
2013-02-26 10:13:54 +01:00
return m_HostName;
}
2013-03-02 09:07:47 +01:00
/**
* @threadsafety Always.
*/
2013-02-26 10:13:54 +01:00
String Service::GetShortName(void) const
{
if (m_ShortName.IsEmpty())
return GetName();
2013-02-26 10:13:54 +01:00
else
return m_ShortName;
}
2013-03-02 09:07:47 +01:00
/**
* @threadsafety Always.
*/
bool Service::IsReachable(void) const
2012-07-03 14:18:46 +02:00
{
ASSERT(!OwnsLock());
2013-02-19 23:02:08 +01:00
2013-03-02 09:07:47 +01:00
BOOST_FOREACH(const Service::Ptr& service, GetParentServices()) {
/* ignore ourselves */
2013-03-02 09:07:47 +01:00
if (service->GetName() == GetName())
continue;
2013-03-02 09:07:47 +01:00
ObjectLock olock(service);
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
2013-03-02 09:07:47 +01:00
BOOST_FOREACH(const Host::Ptr& host, GetParentHosts()) {
Service::Ptr hc = host->GetHostCheckService();
2013-02-28 10:26:33 +01:00
/* ignore hosts that don't have a hostcheck */
if (!hc)
continue;
2013-03-02 09:07:47 +01:00
/* ignore ourselves */
if (hc->GetName() == GetName())
continue;
ObjectLock olock(hc);
2013-02-19 23:02:08 +01:00
2013-03-02 09:07:47 +01:00
/* ignore soft states */
if (hc->GetStateType() == StateTypeSoft)
continue;
2013-02-07 20:29:35 +01:00
/* ignore hosts that are up */
2013-03-02 09:07:47 +01:00
if (hc->GetState() == StateOK)
2013-02-07 20:29:35 +01:00
continue;
return false;
}
2012-07-03 15:20:44 +02:00
return true;
2012-07-03 14:18:46 +02:00
}
2013-03-02 09:07:47 +01:00
/**
* @threadsafety Always.
*/
AcknowledgementType Service::GetAcknowledgement(void)
{
ASSERT(OwnsLock());
2013-03-06 11:03:50 +01:00
2013-02-26 10:13:54 +01:00
if (m_Acknowledgement.IsEmpty())
return AcknowledgementNone;
2013-02-26 10:13:54 +01:00
int ivalue = static_cast<int>(m_Acknowledgement);
AcknowledgementType avalue = static_cast<AcknowledgementType>(ivalue);
if (avalue != AcknowledgementNone) {
double expiry = GetAcknowledgementExpiry();
if (expiry != 0 && expiry < Utility::GetTime()) {
avalue = AcknowledgementNone;
2013-01-23 13:46:35 +01:00
SetAcknowledgement(avalue);
SetAcknowledgementExpiry(0);
}
}
return avalue;
}
2013-03-02 09:07:47 +01:00
/**
* @threadsafety Always.
*/
void Service::SetAcknowledgement(AcknowledgementType acknowledgement)
{
2013-02-26 10:13:54 +01:00
m_Acknowledgement = acknowledgement;
Touch("acknowledgement");
}
2013-03-02 09:07:47 +01:00
/**
* @threadsafety Always.
*/
bool Service::IsAcknowledged(void)
{
return GetAcknowledgement() != AcknowledgementNone;
}
2013-03-02 09:07:47 +01:00
/**
* @threadsafety Always.
*/
double Service::GetAcknowledgementExpiry(void) const
{
2013-02-26 10:13:54 +01:00
if (m_AcknowledgementExpiry.IsEmpty())
return 0;
2013-02-26 10:13:54 +01:00
return static_cast<double>(m_AcknowledgementExpiry);
}
2013-03-02 09:07:47 +01:00
/**
* @threadsafety Always.
*/
void Service::SetAcknowledgementExpiry(double timestamp)
{
2013-02-26 10:13:54 +01:00
m_AcknowledgementExpiry = timestamp;
Touch("acknowledgement_expiry");
}
2013-03-02 09:07:47 +01:00
/**
* @threadsafety Always.
*/
void Service::AcknowledgeProblem(AcknowledgementType type, double expiry)
{
2013-03-08 16:36:26 +01:00
{
ObjectLock olock(this);
2013-03-02 09:07:47 +01:00
2013-03-08 16:36:26 +01:00
SetAcknowledgement(type);
SetAcknowledgementExpiry(expiry);
}
2013-03-07 12:04:20 +01:00
RequestNotifications(NotificationAcknowledgement, GetLastCheckResult());
}
2013-03-02 09:07:47 +01:00
/**
* @threadsafety Always.
*/
void Service::ClearAcknowledgement(void)
{
2013-03-04 15:52:42 +01:00
ObjectLock olock(this);
SetAcknowledgement(AcknowledgementNone);
SetAcknowledgementExpiry(0);
}
2013-03-02 09:07:47 +01:00
/**
* @threadsafety Always.
*/
2013-03-04 15:52:42 +01:00
void Service::OnAttributeChanged(const String& name)
{
ASSERT(!OwnsLock());
2013-03-02 09:07:47 +01:00
2013-03-04 15:52:42 +01:00
Service::Ptr self = GetSelf();
2013-03-01 12:07:52 +01:00
2013-02-26 10:13:54 +01:00
if (name == "current_checker")
2013-03-04 15:52:42 +01:00
OnCheckerChanged(self);
else if (name == "next_check")
2013-03-04 15:52:42 +01:00
OnNextCheckChanged(self);
else if (name == "servicegroups")
2013-02-27 15:23:25 +01:00
ServiceGroup::InvalidateMembersCache();
2013-02-11 16:25:32 +01:00
else if (name == "host_name" || name == "short_name") {
2013-02-27 15:23:25 +01:00
Host::InvalidateServicesCache();
2013-02-19 23:02:08 +01:00
2013-03-04 15:52:42 +01:00
UpdateSlaveNotifications();
2013-02-11 16:25:32 +01:00
} else if (name == "downtimes")
2013-02-27 15:23:25 +01:00
Service::InvalidateDowntimesCache();
else if (name == "comments")
2013-02-27 15:23:25 +01:00
Service::InvalidateCommentsCache();
2013-02-09 11:42:22 +01:00
else if (name == "notifications")
2013-03-04 15:52:42 +01:00
UpdateSlaveNotifications();
else if (name == "check_interval") {
2013-03-04 15:52:42 +01:00
ConfigItem::Ptr item = ConfigItem::GetObject("Service", GetName());
/* update the next check timestamp if we're the owner of this service */
if (item)
UpdateNextCheck();
}
}
2013-03-02 09:07:47 +01:00
/**
* @threadsafety Always.
*/
2013-03-16 21:18:53 +01:00
std::set<Host::Ptr> Service::GetParentHosts(void) const
2013-02-07 20:29:35 +01:00
{
2013-03-16 21:18:53 +01:00
std::set<Host::Ptr> parents;
2013-02-07 20:29:35 +01:00
2013-03-02 09:07:47 +01:00
Host::Ptr host = GetHost();
2013-03-02 09:07:47 +01:00
/* The service's host is implicitly a parent. */
if (host)
parents.insert(host);
2013-02-07 20:29:35 +01:00
2013-03-14 12:17:46 +01:00
Array::Ptr dependencies = GetHostDependencies();
2013-02-07 20:29:35 +01:00
if (dependencies) {
2013-03-02 09:07:47 +01:00
ObjectLock olock(dependencies);
2013-03-14 12:17:46 +01:00
BOOST_FOREACH(const Value& dependency, dependencies) {
Host::Ptr host = Host::GetByName(dependency);
2013-02-27 15:23:25 +01:00
if (!host)
continue;
parents.insert(host);
2013-02-07 20:29:35 +01:00
}
}
return parents;
}
2013-03-02 09:07:47 +01:00
/**
* @threadsafety Always.
*/
2013-03-16 21:18:53 +01:00
std::set<Service::Ptr> Service::GetParentServices(void) const
2013-02-07 20:29:35 +01:00
{
2013-03-16 21:18:53 +01:00
std::set<Service::Ptr> parents;
2013-02-07 20:29:35 +01:00
2013-03-02 09:07:47 +01:00
Host::Ptr host = GetHost();
2013-03-14 12:17:46 +01:00
Array::Ptr dependencies = GetServiceDependencies();
2013-03-02 09:07:47 +01:00
if (host && dependencies) {
2013-03-14 12:17:46 +01:00
BOOST_FOREACH(const Value& dependency, dependencies) {
Service::Ptr service = host->GetServiceByShortName(dependency);
2013-02-07 20:29:35 +01:00
2013-03-02 09:07:47 +01:00
if (!service)
continue;
if (service->GetName() == GetName())
2013-02-07 20:29:35 +01:00
continue;
parents.insert(service);
}
}
return parents;
}
2013-03-02 09:07:47 +01:00
/**
* @threadsafety Always.
*/
2013-03-15 13:29:41 +01:00
Dictionary::Ptr Service::CalculateDynamicMacros(const Dictionary::Ptr& crOverride) const
{
Dictionary::Ptr macros = boost::make_shared<Dictionary>();
2013-02-24 01:10:34 +01:00
Dictionary::Ptr cr;
2013-02-24 01:10:34 +01:00
{
2013-03-02 09:07:47 +01:00
ObjectLock olock(this);
macros->Set("SERVICEDESC", GetShortName());
macros->Set("SERVICEDISPLAYNAME", GetDisplayName());
macros->Set("SERVICESTATE", StateToString(GetState()));
macros->Set("SERVICESTATEID", GetState());
macros->Set("SERVICESTATETYPE", StateTypeToString(GetStateType()));
macros->Set("SERVICEATTEMPT", GetCurrentCheckAttempt());
macros->Set("MAXSERVICEATTEMPT", GetMaxCheckAttempts());
2013-02-28 11:45:47 +01:00
macros->Set("SERVICECHECKCOMMAND", "check_i2");
2013-03-07 12:04:20 +01:00
macros->Set("LASTSERVICESTATE", StateToString(GetLastState()));
macros->Set("LASTSERVICESTATEID", GetLastState());
macros->Set("LASTSERVICESTATETYPE", StateTypeToString(GetLastStateType()));
macros->Set("LASTSERVICESTATECHANGE", (long)GetLastStateChange());
2013-02-24 01:10:34 +01:00
2013-03-02 09:07:47 +01:00
cr = GetLastCheckResult();
2013-02-24 01:10:34 +01:00
}
2013-02-11 16:29:23 +01:00
2013-03-15 13:29:41 +01:00
if (crOverride)
cr = crOverride;
2013-02-11 16:29:23 +01:00
if (cr) {
2013-03-15 13:29:41 +01:00
ASSERT(crOverride || cr->IsSealed());
2013-03-04 15:52:42 +01:00
2013-02-24 01:10:34 +01:00
macros->Set("SERVICELATENCY", Service::CalculateLatency(cr));
macros->Set("SERVICEEXECUTIONTIME", Service::CalculateExecutionTime(cr));
2013-02-11 16:29:23 +01:00
macros->Set("SERVICEOUTPUT", cr->Get("output"));
macros->Set("SERVICEPERFDATA", cr->Get("performance_data_raw"));
2013-03-07 12:04:20 +01:00
2013-03-19 16:20:13 +01:00
macros->Set("LASTSERVICECHECK", (long)cr->Get("execution_end"));
2013-02-11 16:29:23 +01:00
}
2013-02-24 01:10:34 +01:00
macros->Seal();
return macros;
}
2013-03-07 12:04:20 +01:00
2013-03-15 13:29:41 +01:00
Dictionary::Ptr Service::CalculateAllMacros(const Dictionary::Ptr& crOverride) const
2013-03-07 12:04:20 +01:00
{
2013-03-16 21:18:53 +01:00
std::vector<Dictionary::Ptr> macroDicts;
2013-03-07 12:04:20 +01:00
macroDicts.push_back(GetMacros());
Host::Ptr host = GetHost();
2013-03-15 13:29:41 +01:00
macroDicts.push_back(CalculateDynamicMacros(crOverride));
2013-03-07 12:04:20 +01:00
if (host) {
macroDicts.push_back(host->GetMacros());
macroDicts.push_back(host->CalculateDynamicMacros());
}
IcingaApplication::Ptr app = IcingaApplication::GetInstance();
macroDicts.push_back(app->GetMacros());
macroDicts.push_back(IcingaApplication::CalculateDynamicMacros());
return MacroProcessor::MergeMacroDicts(macroDicts);
}