icinga2/lib/icinga/service.cpp

523 lines
15 KiB
C++
Raw Normal View History

/******************************************************************************
* Icinga 2 *
2013-09-25 07:43:57 +02:00
* Copyright (C) 2012-2013 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 "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>
2013-09-01 06:01:27 +02:00
#include <boost/bind/apply.hpp>
using namespace icinga;
2013-03-01 12:07:52 +01:00
REGISTER_TYPE(Service);
boost::signals2::signal<void (const Service::Ptr&, const String&, const String&, AcknowledgementType, double, const String&)> Service::OnAcknowledgementSet;
boost::signals2::signal<void (const Service::Ptr&, const String&)> Service::OnAcknowledgementCleared;
2013-09-01 06:01:27 +02:00
Service::Service(void)
: m_CheckRunning(false)
{ }
void Service::Start(void)
2013-02-26 10:13:54 +01:00
{
DynamicObject::Start();
2013-06-21 10:20:29 +02:00
VERIFY(GetHost());
2013-02-26 10:13:54 +01:00
SetSchedulingOffset(rand());
UpdateNextCheck();
AddDowntimesToCache();
AddCommentsToCache();
StartDowntimesExpiredTimer();
}
void Service::OnConfigLoaded(void)
{
Array::Ptr groups = GetGroups();
2013-02-20 19:52:25 +01:00
if (groups) {
2013-09-09 13:52:37 +02:00
ObjectLock olock(groups);
BOOST_FOREACH(const String& name, groups) {
ServiceGroup::Ptr sg = ServiceGroup::GetByName(name);
if (sg)
sg->AddMember(GetSelf());
}
}
2013-03-02 09:07:47 +01:00
Host::Ptr host = GetHost();
if (host)
host->AddService(GetSelf());
UpdateSlaveNotifications();
}
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::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-09-25 09:24:34 +02:00
return m_Groups;
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()->GetCheckService();
2013-07-09 16:59:31 +02:00
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->GetCheckService();
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;
ClearAcknowledgement();
}
}
return avalue;
}
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::AcknowledgeProblem(const String& author, const String& comment, AcknowledgementType type, double expiry, const String& authority)
{
2013-03-08 16:36:26 +01:00
{
ObjectLock olock(this);
2013-03-02 09:07:47 +01:00
m_Acknowledgement = type;
m_AcknowledgementExpiry = expiry;
2013-03-08 16:36:26 +01:00
}
OnNotificationsRequested(GetSelf(), NotificationAcknowledgement, GetLastCheckResult(), author, comment);
2013-09-01 06:01:27 +02:00
boost::function<void (void)> f = boost::bind(boost::ref(Service::OnAcknowledgementSet), GetSelf(), author, comment, type, expiry, authority);
Utility::QueueAsyncCallback(f);
}
void Service::ClearAcknowledgement(const String& authority)
{
ASSERT(OwnsLock());
m_Acknowledgement = AcknowledgementNone;
m_AcknowledgementExpiry = 0;
2013-03-04 15:52:42 +01:00
2013-09-01 06:01:27 +02:00
Utility::QueueAsyncCallback(boost::bind(boost::ref(OnAcknowledgementCleared), GetSelf(), authority));
}
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
}
void Service::InternalSerialize(const Dictionary::Ptr& bag, int attributeTypes) const
{
DynamicObject::InternalSerialize(bag, attributeTypes);
if (attributeTypes & Attribute_Config) {
bag->Set("display_name", m_DisplayName);
bag->Set("macros", m_Macros);
bag->Set("host_dependencies", m_HostDependencies);
bag->Set("service_dependencies", m_ServiceDependencies);
2013-09-25 09:24:34 +02:00
bag->Set("groups", m_Groups);
bag->Set("check_command", m_CheckCommand);
bag->Set("max_check_attempts", m_MaxCheckAttempts);
bag->Set("check_period", m_CheckPeriod);
bag->Set("check_interval", m_CheckInterval);
bag->Set("retry_interval", m_RetryInterval);
bag->Set("event_command", m_EventCommand);
bag->Set("volatile", m_Volatile);
bag->Set("short_name", m_ShortName);
2013-09-25 09:19:25 +02:00
bag->Set("host", m_HostName);
bag->Set("flapping_threshold", m_FlappingThreshold);
bag->Set("notifications", m_NotificationDescriptions);
}
2013-08-29 10:40:43 +02:00
if (attributeTypes & Attribute_State) {
bag->Set("next_check", m_NextCheck);
bag->Set("current_checker", m_CurrentChecker);
bag->Set("check_attempt", m_CheckAttempt);
bag->Set("state", m_State);
bag->Set("state_type", m_StateType);
bag->Set("last_state", m_LastState);
bag->Set("last_hard_state", m_LastHardState);
bag->Set("last_state_type", m_LastStateType);
bag->Set("last_reachable", m_LastReachable);
bag->Set("last_result", m_LastResult);
bag->Set("last_state_change", m_LastStateChange);
bag->Set("last_hard_state_change", m_LastHardStateChange);
bag->Set("last_state_ok", m_LastStateOK);
bag->Set("last_state_warning", m_LastStateWarning);
bag->Set("last_state_critical", m_LastStateCritical);
bag->Set("last_state_unknown", m_LastStateUnknown);
bag->Set("last_state_unreachable", m_LastStateUnreachable);
bag->Set("last_in_downtime", m_LastInDowntime);
bag->Set("enable_active_checks", m_EnableActiveChecks);
bag->Set("enable_passive_checks", m_EnablePassiveChecks);
bag->Set("force_next_check", m_ForceNextCheck);
bag->Set("acknowledgement", m_Acknowledgement);
bag->Set("acknowledgement_expiry", m_AcknowledgementExpiry);
bag->Set("comments", m_Comments);
bag->Set("downtimes", m_Downtimes);
bag->Set("enable_notifications", m_EnableNotifications);
bag->Set("force_next_notification", m_ForceNextNotification);
bag->Set("flapping_positive", m_FlappingPositive);
bag->Set("flapping_negative", m_FlappingNegative);
bag->Set("flapping_lastchange", m_FlappingLastChange);
bag->Set("enable_flapping", m_EnableFlapping);
}
}
void Service::InternalDeserialize(const Dictionary::Ptr& bag, int attributeTypes)
{
DynamicObject::InternalDeserialize(bag, attributeTypes);
if (attributeTypes & Attribute_Config) {
m_DisplayName = bag->Get("display_name");
m_Macros = bag->Get("macros");
m_HostDependencies = bag->Get("host_dependencies");
m_ServiceDependencies = bag->Get("service_dependencies");
2013-09-25 09:24:34 +02:00
m_Groups = bag->Get("groups");
m_CheckCommand = bag->Get("check_command");
m_MaxCheckAttempts = bag->Get("max_check_attempts");
m_CheckPeriod = bag->Get("check_period");
m_CheckInterval = bag->Get("check_interval");
m_RetryInterval = bag->Get("retry_interval");
m_EventCommand = bag->Get("event_command");
m_Volatile = bag->Get("volatile");
m_ShortName = bag->Get("short_name");
2013-09-25 09:19:25 +02:00
m_HostName = bag->Get("host");
m_FlappingThreshold = bag->Get("flapping_threshold");
m_NotificationDescriptions = bag->Get("notifications");
}
2013-08-29 10:40:43 +02:00
if (attributeTypes & Attribute_State) {
m_NextCheck = bag->Get("next_check");
m_CurrentChecker = bag->Get("current_checker");
m_CheckAttempt = bag->Get("check_attempt");
m_State = bag->Get("state");
m_StateType = bag->Get("state_type");
m_LastState = bag->Get("last_state");
m_LastHardState = bag->Get("last_hard_state");
m_LastStateType = bag->Get("last_state_type");
m_LastReachable = bag->Get("last_reachable");
m_LastResult = bag->Get("last_result");
m_LastStateChange = bag->Get("last_state_change");
m_LastHardStateChange = bag->Get("last_hard_state_change");
m_LastStateOK = bag->Get("last_state_ok");
m_LastStateWarning = bag->Get("last_state_warning");
m_LastStateCritical = bag->Get("last_state_critical");
m_LastStateUnknown = bag->Get("last_state_unknown");
m_LastStateUnreachable = bag->Get("last_state_unreachable");
m_LastInDowntime = bag->Get("last_in_downtime");
m_EnableActiveChecks = bag->Get("enable_active_checks");
m_EnablePassiveChecks = bag->Get("enable_passive_checks");
m_ForceNextCheck = bag->Get("force_next_check");
m_Acknowledgement = bag->Get("acknowledgement");
m_AcknowledgementExpiry = bag->Get("acknowledgement_expiry");
m_Comments = bag->Get("comments");
m_Downtimes = bag->Get("downtimes");
m_EnableNotifications = bag->Get("enable_notifications");
m_ForceNextNotification = bag->Get("force_next_notification");
m_FlappingPositive = bag->Get("flapping_positive");
m_FlappingNegative = bag->Get("flapping_negative");
m_FlappingLastChange = bag->Get("flapping_lastchange");
m_EnableFlapping = bag->Get("enable_flapping");
}
}