icinga2/lib/icinga/service.cpp

380 lines
9.5 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;
static AttributeDescription serviceAttributes[] = {
{ "scheduling_offset", Attribute_Transient },
{ "first_check", Attribute_Transient },
{ "next_check", Attribute_Replicated },
{ "checker", Attribute_Replicated },
{ "check_attempt", Attribute_Replicated },
{ "state", Attribute_Replicated },
{ "state_type", Attribute_Replicated },
{ "last_result", Attribute_Replicated },
{ "last_state_change", Attribute_Replicated },
{ "last_hard_state_change", Attribute_Replicated },
{ "enable_active_checks", Attribute_Replicated },
{ "enable_passive_checks", Attribute_Replicated },
{ "force_next_check", Attribute_Replicated },
{ "acknowledgement", Attribute_Replicated },
2013-01-29 14:19:54 +01:00
{ "acknowledgement_expiry", Attribute_Replicated },
{ "downtimes", Attribute_Replicated },
{ "comments", Attribute_Replicated },
{ "last_notification", Attribute_Replicated },
{ "next_notification", Attribute_Replicated }
};
REGISTER_TYPE(Service, serviceAttributes);
Service::Service(const Dictionary::Ptr& serializedObject)
: DynamicObject(serializedObject)
2013-02-20 19:52:25 +01:00
{ }
void Service::OnRegistrationCompleted(void)
2013-02-19 23:02:08 +01:00
{
2013-02-20 19:52:25 +01:00
DynamicObject::OnRegistrationCompleted();
2013-02-19 23:02:08 +01:00
ServiceGroup::InvalidateMembersCache();
Host::InvalidateServicesCache();
Service::InvalidateDowntimesCache();
Service::InvalidateCommentsCache();
2013-02-20 19:52:25 +01:00
{
ObjectLock olock(this);
UpdateSlaveNotifications();
}
2013-02-19 23:02:08 +01:00
}
Service::~Service(void)
{
ServiceGroup::InvalidateMembersCache();
Host::InvalidateServicesCache();
Service::InvalidateDowntimesCache();
Service::InvalidateCommentsCache();
}
String Service::GetDisplayName(void) const
{
String value = Get("display_name");
2013-02-24 01:10:34 +01:00
if (value.IsEmpty())
return GetShortName();
2013-02-24 01:10:34 +01:00
return value;
}
2013-02-17 19:14:34 +01:00
/**
* @threadsafety Always.
*/
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
}
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-18 14:40:24 +01:00
ObjectLock olock(host);
return host->GetServiceByShortName(serviceName);
} else {
return Service::GetByName(serviceName);
}
}
Host::Ptr Service::GetHost(void) const
{
2012-08-03 13:19:55 +02:00
String hostname = Get("host_name");
if (hostname.IsEmpty())
BOOST_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");
}
2013-02-07 20:29:35 +01:00
Dictionary::Ptr Service::GetHostDependencies(void) const
2012-06-27 18:43:34 +02:00
{
2013-02-07 20:29:35 +01:00
return Get("hostdependencies");
}
2013-02-07 20:29:35 +01:00
Dictionary::Ptr Service::GetServiceDependencies(void) const
{
return Get("servicedependencies");
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
}
String Service::GetShortName(void) const
{
Value value = Get("short_name");
if (value.IsEmpty())
return GetName();
return value;
}
2013-02-19 23:02:08 +01:00
bool Service::IsReachable(const Service::Ptr& self)
2012-07-03 14:18:46 +02:00
{
2013-02-19 23:02:08 +01:00
set<Service::Ptr> parentServices;
{
ObjectLock olock(self);
parentServices = self->GetParentServices();
}
BOOST_FOREACH(const Service::Ptr& service, parentServices) {
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-02-19 23:02:08 +01:00
set<Host::Ptr> parentHosts;
{
ObjectLock olock(self);
parentHosts = self->GetParentHosts();
}
BOOST_FOREACH(const Host::Ptr& host, parentHosts) {
Service::Ptr hc;
{
ObjectLock olock(host);
hc = host->GetHostCheckService();
}
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)
{
Value value = Get("acknowledgement");
if (value.IsEmpty())
return AcknowledgementNone;
int ivalue = static_cast<int>(value);
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)
{
Set("acknowledgement", static_cast<long>(acknowledgement));
}
bool Service::IsAcknowledged(void)
{
return GetAcknowledgement() != AcknowledgementNone;
}
double Service::GetAcknowledgementExpiry(void) const
{
Value value = Get("acknowledgement_expiry");
if (value.IsEmpty())
return 0;
return static_cast<double>(value);
}
void Service::SetAcknowledgementExpiry(double timestamp)
{
Set("acknowledgement_expiry", timestamp);
}
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)
{
if (name == "checker")
OnCheckerChanged(GetSelf(), oldValue);
else if (name == "next_check")
OnNextCheckChanged(GetSelf(), oldValue);
else if (name == "servicegroups")
ServiceGroup::InvalidateMembersCache();
2013-02-11 16:25:32 +01:00
else if (name == "host_name" || name == "short_name") {
Host::InvalidateServicesCache();
2013-02-19 23:02:08 +01:00
{
ObjectLock olock(this);
UpdateSlaveNotifications();
}
2013-02-11 16:25:32 +01:00
} else if (name == "downtimes")
Service::InvalidateDowntimesCache();
else if (name == "comments")
Service::InvalidateCommentsCache();
2013-02-09 11:42:22 +01:00
else if (name == "notifications")
UpdateSlaveNotifications();
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();
}
}
2013-02-07 20:29:35 +01:00
set<Host::Ptr> Service::GetParentHosts(void) const
{
set<Host::Ptr> parents;
/* The service's host is implicitly a parent. */
parents.insert(GetHost());
Dictionary::Ptr dependencies = GetHostDependencies();
if (dependencies) {
String key;
BOOST_FOREACH(tie(key, tuples::ignore), dependencies) {
parents.insert(Host::GetByName(key));
}
}
return parents;
}
set<Service::Ptr> Service::GetParentServices(void) const
{
set<Service::Ptr> parents;
Dictionary::Ptr dependencies = GetServiceDependencies();
if (dependencies) {
String key;
Value value;
BOOST_FOREACH(tie(key, value), dependencies) {
Service::Ptr service = GetHost()->GetServiceByShortName(value);
2013-02-07 20:29:35 +01:00
if (service->GetName() == GetName())
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;
}