2012-07-09 20:32:02 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* 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"
|
2012-06-13 13:42:55 +02:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2012-12-04 08:42:24 +01:00
|
|
|
static AttributeDescription serviceAttributes[] = {
|
|
|
|
{ "scheduling_offset", Attribute_Transient },
|
2013-01-23 15:25:00 +01:00
|
|
|
{ "first_check", Attribute_Transient },
|
2012-12-04 08:42:24 +01:00
|
|
|
{ "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 },
|
2013-01-22 16:01:08 +01:00
|
|
|
{ "last_hard_state_change", Attribute_Replicated },
|
2013-01-24 23:10:07 +01:00
|
|
|
{ "enable_active_checks", Attribute_Replicated },
|
|
|
|
{ "enable_passive_checks", Attribute_Replicated },
|
2013-01-23 13:21:07 +01:00
|
|
|
{ "force_next_check", Attribute_Replicated },
|
|
|
|
{ "acknowledgement", Attribute_Replicated },
|
2013-01-29 14:19:54 +01:00
|
|
|
{ "acknowledgement_expiry", Attribute_Replicated },
|
2013-01-30 09:59:22 +01:00
|
|
|
{ "downtimes", Attribute_Replicated },
|
2013-02-09 17:27:32 +01:00
|
|
|
{ "comments", Attribute_Replicated },
|
|
|
|
{ "last_notification", Attribute_Replicated },
|
|
|
|
{ "next_notification", Attribute_Replicated }
|
2012-12-04 08:42:24 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
REGISTER_TYPE(Service, serviceAttributes);
|
2012-07-26 12:28:29 +02:00
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
Service::Service(const Dictionary::Ptr& serializedObject)
|
|
|
|
: DynamicObject(serializedObject)
|
2013-02-11 14:01:52 +01:00
|
|
|
{ }
|
2012-08-02 09:38:08 +02:00
|
|
|
|
2013-01-24 15:01:06 +01:00
|
|
|
Service::~Service(void)
|
|
|
|
{
|
|
|
|
ServiceGroup::InvalidateMembersCache();
|
2013-01-24 15:10:17 +01:00
|
|
|
Host::InvalidateServicesCache();
|
2013-02-09 01:16:43 +01:00
|
|
|
Service::InvalidateDowntimeCache();
|
|
|
|
Service::InvalidateCommentCache();
|
2013-01-24 15:01:06 +01:00
|
|
|
}
|
|
|
|
|
2013-02-09 17:50:47 +01:00
|
|
|
String Service::GetDisplayName(void) const
|
2012-08-02 09:38:08 +02:00
|
|
|
{
|
2012-08-03 13:19:55 +02:00
|
|
|
String value = Get("alias");
|
2012-08-02 09:38:08 +02:00
|
|
|
|
2012-08-03 13:19:55 +02:00
|
|
|
if (!value.IsEmpty())
|
2012-06-13 13:42:55 +02:00
|
|
|
return value;
|
|
|
|
|
|
|
|
return GetName();
|
|
|
|
}
|
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2012-08-02 09:38:08 +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)
|
2013-02-06 12:51:12 +01:00
|
|
|
BOOST_THROW_EXCEPTION(invalid_argument("Service '" + name + "' does not exist."));
|
2012-06-27 18:43:34 +02:00
|
|
|
|
2012-07-27 16:05:02 +02:00
|
|
|
return dynamic_pointer_cast<Service>(configObject);
|
2012-06-27 18:43:34 +02:00
|
|
|
}
|
|
|
|
|
2013-02-08 15:38:22 +01:00
|
|
|
Service::Ptr Service::GetByNamePair(const String& hostName, const String& serviceName)
|
|
|
|
{
|
|
|
|
if (!hostName.IsEmpty()) {
|
|
|
|
Host::Ptr host = Host::GetByName(hostName);
|
|
|
|
return host->GetServiceByShortName(serviceName);
|
|
|
|
} else {
|
|
|
|
return Service::GetByName(serviceName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-27 16:05:02 +02:00
|
|
|
Host::Ptr Service::GetHost(void) const
|
2012-06-13 13:42:55 +02:00
|
|
|
{
|
2012-08-03 13:19:55 +02:00
|
|
|
String hostname = Get("host_name");
|
|
|
|
|
|
|
|
if (hostname.IsEmpty())
|
2013-02-06 12:51:12 +01:00
|
|
|
BOOST_THROW_EXCEPTION(runtime_error("Service object is missing the 'host_name' property."));
|
2012-06-13 13:42:55 +02:00
|
|
|
|
|
|
|
return Host::GetByName(hostname);
|
|
|
|
}
|
|
|
|
|
|
|
|
Dictionary::Ptr Service::GetMacros(void) const
|
|
|
|
{
|
2012-08-03 13:19:55 +02:00
|
|
|
return Get("macros");
|
2012-06-13 13:42:55 +02:00
|
|
|
}
|
|
|
|
|
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");
|
2012-06-13 13:42:55 +02:00
|
|
|
}
|
2012-06-14 11:18:20 +02:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-02-08 15:38:22 +01:00
|
|
|
String Service::GetShortName(void) const
|
|
|
|
{
|
|
|
|
Value value = Get("short_name");
|
|
|
|
|
|
|
|
if (value.IsEmpty())
|
|
|
|
return GetName();
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2012-07-03 15:26:58 +02:00
|
|
|
bool Service::IsReachable(void) const
|
2012-07-03 14:18:46 +02:00
|
|
|
{
|
2013-02-07 20:29:35 +01:00
|
|
|
BOOST_FOREACH(const Service::Ptr& service, GetParentServices()) {
|
2012-07-09 12:44:31 +02:00
|
|
|
/* ignore pending services */
|
2012-08-02 09:38:08 +02:00
|
|
|
if (!service->GetLastCheckResult())
|
2012-07-09 12:44:31 +02:00
|
|
|
continue;
|
|
|
|
|
2012-07-09 17:03:24 +02:00
|
|
|
/* ignore soft states */
|
2012-07-27 16:05:02 +02:00
|
|
|
if (service->GetStateType() == StateTypeSoft)
|
2012-07-09 17:03:24 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
/* ignore services states OK and Warning */
|
2012-07-27 16:05:02 +02:00
|
|
|
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-07 20:29:35 +01:00
|
|
|
BOOST_FOREACH(const Host::Ptr& host, GetParentHosts()) {
|
|
|
|
/* ignore hosts that are up */
|
|
|
|
if (host->IsUp())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-07-03 15:20:44 +02:00
|
|
|
return true;
|
2012-07-03 14:18:46 +02:00
|
|
|
}
|
|
|
|
|
2013-01-23 13:21:07 +01: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);
|
2013-01-23 13:21:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return avalue;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Service::SetAcknowledgement(AcknowledgementType acknowledgement)
|
|
|
|
{
|
|
|
|
Set("acknowledgement", static_cast<long>(acknowledgement));
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2012-08-03 23:03:58 +02:00
|
|
|
void Service::OnAttributeChanged(const String& name, const Value& oldValue)
|
|
|
|
{
|
|
|
|
if (name == "checker")
|
|
|
|
OnCheckerChanged(GetSelf(), oldValue);
|
2013-01-22 12:44:23 +01:00
|
|
|
else if (name == "next_check")
|
|
|
|
OnNextCheckChanged(GetSelf(), oldValue);
|
2013-01-24 13:21:35 +01:00
|
|
|
else if (name == "servicegroups")
|
|
|
|
ServiceGroup::InvalidateMembersCache();
|
2013-02-08 15:38:22 +01:00
|
|
|
else if (name == "host_name" || name == "short_name")
|
2013-01-24 15:10:17 +01:00
|
|
|
Host::InvalidateServicesCache();
|
2013-01-29 14:19:54 +01:00
|
|
|
else if (name == "downtimes")
|
2013-02-09 01:16:43 +01:00
|
|
|
Service::InvalidateDowntimeCache();
|
|
|
|
else if (name == "comments")
|
|
|
|
Service::InvalidateCommentCache();
|
2013-02-09 11:42:22 +01:00
|
|
|
else if (name == "notifications")
|
2013-02-09 15:20:10 +01:00
|
|
|
UpdateSlaveNotifications();
|
2013-02-11 16:11:11 +01:00
|
|
|
else if (name == "check_interval") {
|
|
|
|
ConfigItem::Ptr item = ConfigItem::GetObject("Service", GetName());
|
|
|
|
|
|
|
|
/* update the next check timestamp if we're the owner of this service */
|
|
|
|
if (item && !IsAbstract())
|
|
|
|
UpdateNextCheck();
|
|
|
|
}
|
2012-08-03 23:03:58 +02:00
|
|
|
}
|
2013-01-22 11:07:09 +01:00
|
|
|
|
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;
|
2013-02-08 21:30:14 +01:00
|
|
|
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-09 18:39:43 +01:00
|
|
|
|
2013-02-09 18:50:22 +01:00
|
|
|
Dictionary::Ptr Service::CalculateDynamicMacros(void) const
|
2013-02-09 18:39:43 +01:00
|
|
|
{
|
|
|
|
Dictionary::Ptr macros = boost::make_shared<Dictionary>();
|
|
|
|
|
|
|
|
macros->Set("SERVICEDESC", GetShortName());
|
|
|
|
macros->Set("SERVICEDISPLAYNAME", GetDisplayName());
|
|
|
|
macros->Set("SERVICESTATE", StateToString(GetState()));
|
|
|
|
macros->Set("SERVICESTATEID", GetState());
|
|
|
|
macros->Set("SERVICESTATETYPE", StateTypeToString(GetStateType()));
|
2013-02-09 19:12:30 +01:00
|
|
|
macros->Set("SERVICEATTEMPT", GetCurrentCheckAttempt());
|
|
|
|
macros->Set("MAXSERVICEATTEMPT", GetMaxCheckAttempts());
|
2013-02-09 18:39:43 +01:00
|
|
|
|
|
|
|
return macros;
|
|
|
|
}
|