icinga2/lib/icinga/service.cpp

379 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;
2013-02-26 10:13:54 +01:00
REGISTER_TYPE(Service, NULL);
Service::Service(const Dictionary::Ptr& serializedObject)
: DynamicObject(serializedObject)
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);
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);
RegisterAttribute("last_result", Attribute_Replicated, &m_LastResult);
RegisterAttribute("last_state_change", Attribute_Replicated, &m_LastStateChange);
RegisterAttribute("last_hard_state_change", Attribute_Replicated, &m_LastHardStateChange);
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
RegisterAttribute("last_notification", Attribute_Replicated, &m_LastNotification);
RegisterAttribute("notification_interval", Attribute_Config, &m_NotificationInterval);
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
}
void Service::OnRegistrationCompleted(void)
{
DynamicObject::OnRegistrationCompleted();
}
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
if (!configObject)
BOOST_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
}
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();
return Host::GetServiceByShortName(host, serviceName);
} else {
return Service::GetByName(serviceName);
}
}
Host::Ptr Service::GetHost(void) const
{
return Host::GetByName(m_HostName);
}
Dictionary::Ptr Service::GetMacros(void) const
{
2013-02-26 10:13:54 +01:00
return m_Macros;
}
2013-02-07 20:29:35 +01:00
Dictionary::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-02-07 20:29:35 +01:00
Dictionary::Ptr Service::GetServiceDependencies(void) const
{
2013-02-26 10:13:54 +01:00
return m_ServiceDependencies;
2012-07-09 10:09:53 +02:00
}
2012-06-30 13:39:55 +02:00
Dictionary::Ptr Service::GetGroups(void) const
{
2013-02-26 10:13:54 +01:00
return m_ServiceGroups;
2012-06-30 13:39:55 +02:00
}
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-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-02-19 23:02:08 +01:00
bool Service::IsReachable(const Service::Ptr& self)
2012-07-03 14:18:46 +02:00
{
BOOST_FOREACH(const Service::Ptr& service, Service::GetParentServices(self)) {
2013-02-19 23:02:08 +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
BOOST_FOREACH(const Host::Ptr& host, Service::GetParentHosts(self)) {
Service::Ptr hc = Host::GetHostCheckService(host);
ObjectLock olock(hc);
2013-02-19 23:02:08 +01:00
2013-02-07 20:29:35 +01:00
/* ignore hosts that are up */
2013-02-19 23:02:08 +01:00
if (hc && 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
}
AcknowledgementType Service::GetAcknowledgement(void)
{
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;
}
void Service::SetAcknowledgement(AcknowledgementType acknowledgement)
{
2013-02-26 10:13:54 +01:00
m_Acknowledgement = acknowledgement;
Touch("acknowledgement");
}
bool Service::IsAcknowledged(void)
{
return GetAcknowledgement() != AcknowledgementNone;
}
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);
}
void Service::SetAcknowledgementExpiry(double timestamp)
{
2013-02-26 10:13:54 +01:00
m_AcknowledgementExpiry = timestamp;
Touch("acknowledgement_expiry");
}
void Service::AcknowledgeProblem(AcknowledgementType type, double expiry)
{
SetAcknowledgement(type);
SetAcknowledgementExpiry(expiry);
RequestNotifications(NotificationAcknowledgement);
}
void Service::ClearAcknowledgement(void)
{
SetAcknowledgement(AcknowledgementNone);
SetAcknowledgementExpiry(0);
}
void Service::OnAttributeChanged(const String& name, const Value& oldValue)
{
2013-02-26 10:13:54 +01:00
if (name == "current_checker")
OnCheckerChanged(GetSelf(), oldValue);
else if (name == "next_check")
OnNextCheckChanged(GetSelf(), oldValue);
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-02-26 10:13:54 +01:00
UpdateSlaveNotifications(GetSelf());
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-02-26 10:13:54 +01:00
UpdateSlaveNotifications(GetSelf());
else if (name == "check_interval") {
2013-02-19 23:02:08 +01:00
ObjectLock(this);
ConfigItem::Ptr item = ConfigItem::GetObject("Service", GetName());
/* update the next check timestamp if we're the owner of this service */
if (item && !IsAbstract())
UpdateNextCheck();
}
}
set<Host::Ptr> Service::GetParentHosts(const Service::Ptr& self)
2013-02-07 20:29:35 +01:00
{
set<Host::Ptr> parents;
Dictionary::Ptr dependencies;
{
ObjectLock olock(self);
/* The service's host is implicitly a parent. */
parents.insert(self->GetHost());
dependencies = self->GetHostDependencies();
}
2013-02-07 20:29:35 +01:00
if (dependencies) {
String key;
BOOST_FOREACH(tie(key, tuples::ignore), dependencies) {
2013-02-27 15:23:25 +01:00
Host::Ptr host = Host::GetByName(key);
if (!host)
continue;
parents.insert(host);
2013-02-07 20:29:35 +01:00
}
}
return parents;
}
set<Service::Ptr> Service::GetParentServices(const Service::Ptr& self)
2013-02-07 20:29:35 +01:00
{
set<Service::Ptr> parents;
Dictionary::Ptr dependencies;
Host::Ptr host;
String service_name;
{
ObjectLock olock(self);
dependencies = self->GetServiceDependencies();
host = self->GetHost();
service_name = self->GetName();
}
2013-02-07 20:29:35 +01:00
if (dependencies) {
String key;
Value value;
BOOST_FOREACH(tie(key, value), dependencies) {
Service::Ptr service = Host::GetServiceByShortName(host, value);
ObjectLock olock(service);
2013-02-07 20:29:35 +01:00
if (service->GetName() == service_name)
2013-02-07 20:29:35 +01:00
continue;
parents.insert(service);
}
}
return parents;
}
2013-02-24 01:10:34 +01:00
Dictionary::Ptr Service::CalculateDynamicMacros(const Service::Ptr& self)
{
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
{
ObjectLock olock(self);
macros->Set("SERVICEDESC", self->GetShortName());
macros->Set("SERVICEDISPLAYNAME", self->GetDisplayName());
macros->Set("SERVICESTATE", StateToString(self->GetState()));
macros->Set("SERVICESTATEID", self->GetState());
macros->Set("SERVICESTATETYPE", StateTypeToString(self->GetStateType()));
macros->Set("SERVICEATTEMPT", self->GetCurrentCheckAttempt());
macros->Set("MAXSERVICEATTEMPT", self->GetMaxCheckAttempts());
cr = self->GetLastCheckResult();
}
2013-02-11 16:29:23 +01:00
if (cr) {
2013-02-24 01:10:34 +01:00
macros->Set("SERVICELATENCY", Service::CalculateLatency(cr));
macros->Set("SERVICEEXECUTIONTIME", Service::CalculateExecutionTime(cr));
ObjectLock olock(cr);
2013-02-11 16:29:23 +01:00
macros->Set("SERVICEOUTPUT", cr->Get("output"));
macros->Set("SERVICEPERFDATA", cr->Get("performance_data_raw"));
}
2013-02-24 01:10:34 +01:00
macros->Seal();
return macros;
}