icinga2/lib/icinga/service.cpp

508 lines
14 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/checkcommand.h"
2013-03-17 20:19:29 +01:00
#include "icinga/icingaapplication.h"
#include "icinga/macroprocessor.h"
#include "icinga/downtimemessage.h"
#include "config/configitembuilder.h"
2013-03-16 21:18:53 +01:00
#include "base/dynamictype.h"
#include "base/objectlock.h"
#include "base/convert.h"
2013-03-25 18:36:15 +01:00
#include "base/utility.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);
Endpoint::Ptr Service::m_Endpoint;
boost::once_flag Service::m_OnceFlag = BOOST_ONCE_INIT;
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("event_command", Attribute_Config, &m_EventCommand);
RegisterAttribute("volatile", Attribute_Config, &m_Volatile);
2013-02-26 10:13:54 +01:00
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_hard_state", Attribute_Replicated, &m_LastHardState);
2013-03-07 12:04:20 +01:00
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);
RegisterAttribute("last_state_ok", Attribute_Replicated, &m_LastStateOK);
RegisterAttribute("last_state_warning", Attribute_Replicated, &m_LastStateWarning);
RegisterAttribute("last_state_critical", Attribute_Replicated, &m_LastStateCritical);
RegisterAttribute("last_state_unknown", Attribute_Replicated, &m_LastStateUnknown);
RegisterAttribute("last_state_unreachable", Attribute_Replicated, &m_LastStateUnreachable);
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);
RegisterAttribute("force_next_notification", Attribute_Replicated, &m_ForceNextNotification);
2013-02-26 10:13:54 +01:00
2013-06-21 12:51:29 +02:00
RegisterAttribute("flapping_positive", Attribute_Replicated, &m_FlappingPositive);
RegisterAttribute("flapping_negative", Attribute_Replicated, &m_FlappingNegative);
2013-06-21 10:20:29 +02:00
RegisterAttribute("flapping_lastchange", Attribute_Replicated, &m_FlappingLastChange);
RegisterAttribute("flapping_threshold", Attribute_Config, &m_FlappingThreshold);
RegisterAttribute("enable_flapping", Attribute_Replicated, &m_EnableFlapping);
2013-06-21 10:20:29 +02:00
2013-02-26 10:13:54 +01:00
SetSchedulingOffset(rand());
boost::call_once(m_OnceFlag, &Service::Initialize);
2013-02-26 10:13:54 +01:00
}
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::Initialize(void)
{
m_Endpoint = Endpoint::MakeEndpoint("service", false);
m_Endpoint->RegisterTopicHandler("icinga::NotificationSent",
boost::bind(&Service::NotificationSentRequestHandler, _3));
m_Endpoint->RegisterTopicHandler("icinga::Downtime",
boost::bind(&Service::DowntimeRequestHandler, _3));
m_Endpoint->RegisterTopicHandler("icinga::Flapping",
boost::bind(&Service::FlappingRequestHandler, _3));
}
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();
}
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;
}
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
}
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);
}
}
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-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-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-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-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-07-09 16:59:31 +02:00
bool Service::IsHostCheck(void) const
{
ASSERT(!OwnsLock());
Service::Ptr hc = GetHost()->GetHostCheckService();
if (!hc)
return false;
return (hc->GetName() == GetName());
}
2013-03-02 09:07:47 +01:00
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
}
bool Service::IsVolatile(void) const
{
if (m_Volatile.IsEmpty())
return false;
return m_Volatile;
}
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;
}
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(const String& author, const String& comment, 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);
}
(void) AddComment(CommentAcknowledgement, author, comment, 0);
RequestNotifications(NotificationAcknowledgement, GetLastCheckResult(), author, comment);
}
void Service::ClearAcknowledgement(void)
{
2013-03-04 15:52:42 +01:00
ObjectLock olock(this);
SetAcknowledgement(AcknowledgementNone);
SetAcknowledgementExpiry(0);
}
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-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-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-06-21 08:30:26 +02:00
ObjectLock olock(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;
}
bool Service::ResolveMacro(const String& macro, const Dictionary::Ptr& cr, String *result) const
{
if (macro == "SERVICEDESC") {
*result = GetShortName();
return true;
} else if (macro == "SERVICEDISPLAYNAME") {
*result = GetDisplayName();
return true;
} else if (macro == "SERVICECHECKCOMMAND") {
CheckCommand::Ptr commandObj = GetCheckCommand();
if (commandObj)
*result = commandObj->GetName();
else
*result = "";
return true;
2013-02-24 01:10:34 +01:00
}
2013-02-11 16:29:23 +01:00
if (macro == "SERVICESTATE") {
*result = StateToString(GetState());
return true;
} else if (macro == "SERVICESTATEID") {
*result = Convert::ToString(GetState());
return true;
} else if (macro == "SERVICESTATETYPE") {
*result = StateTypeToString(GetStateType());
return true;
} else if (macro == "SERVICEATTEMPT") {
*result = Convert::ToString(GetCurrentCheckAttempt());
return true;
} else if (macro == "MAXSERVICEATTEMPT") {
*result = Convert::ToString(GetMaxCheckAttempts());
return true;
} else if (macro == "LASTSERVICESTATE") {
*result = StateToString(GetLastState());
return true;
} else if (macro == "LASTSERVICESTATEID") {
*result = Convert::ToString(GetLastState());
return true;
} else if (macro == "LASTSERVICESTATETYPE") {
*result = StateTypeToString(GetLastStateType());
return true;
} else if (macro == "LASTSERVICESTATECHANGE") {
*result = Convert::ToString((long)GetLastStateChange());
return true;
}
2013-03-15 13:29:41 +01:00
2013-02-11 16:29:23 +01:00
if (cr) {
if (macro == "SERVICELATENCY") {
*result = Convert::ToString(Service::CalculateLatency(cr));
return true;
} else if (macro == "SERVICEEXECUTIONTIME") {
*result = Convert::ToString(Service::CalculateExecutionTime(cr));
return true;
} else if (macro == "SERVICEOUTPUT") {
*result = cr->Get("output");
return true;
} else if (macro == "SERVICEPERFDATA") {
*result = cr->Get("performance_data_raw");
return true;
} else if (macro == "LASTSERVICECHECK") {
*result = Convert::ToString((long)cr->Get("execution_end"));
return true;
}
2013-02-11 16:29:23 +01:00
}
Dictionary::Ptr macros = GetMacros();
2013-03-07 12:04:20 +01:00
if (macros && macros->Contains(macro)) {
*result = macros->Get(macro);
return true;
2013-03-07 12:04:20 +01:00
}
return false;
2013-03-07 12:04:20 +01:00
}