icinga2/lib/icinga/service.cpp

487 lines
12 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 "icinga/pluginutility.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/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
{
VERIFY(GetHost());
StartDowntimesExpiredTimer();
double now = Utility::GetTime();
if (GetNextCheck() < now + 300)
SetNextCheck(now + Utility::Random() % 300);
DynamicObject::Start();
}
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
2013-11-10 22:04:18 +01:00
m_Host = Host::GetByName(GetHostRaw());
if (m_Host)
m_Host->AddService(GetSelf());
UpdateSlaveNotifications();
UpdateSlaveScheduledDowntimes();
SetSchedulingOffset(Utility::Random());
}
void Service::OnStateLoaded(void)
{
AddDowntimesToCache();
AddCommentsToCache();
std::vector<String> ids;
Dictionary::Ptr downtimes = GetDowntimes();
{
ObjectLock dlock(downtimes);
BOOST_FOREACH(const Dictionary::Pair& kv, downtimes) {
Downtime::Ptr downtime = kv.second;
if (downtime->GetScheduledBy().IsEmpty())
continue;
ids.push_back(kv.first);
}
}
BOOST_FOREACH(const String& id, ids) {
RemoveDowntime(id, true);
}
}
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
{
2013-11-10 22:04:18 +01:00
return m_Host;
}
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
}
AcknowledgementType Service::GetAcknowledgement(void)
{
ASSERT(OwnsLock());
2013-03-06 11:03:50 +01:00
2013-10-26 09:41:45 +02:00
AcknowledgementType avalue = static_cast<AcknowledgementType>(GetAcknowledgementRaw());
if (avalue != AcknowledgementNone) {
double expiry = GetAcknowledgementExpiry();
if (expiry != 0 && expiry < Utility::GetTime()) {
avalue = AcknowledgementNone;
ClearAcknowledgement();
}
}
return avalue;
}
bool Service::IsAcknowledged(void)
{
return GetAcknowledgement() != AcknowledgementNone;
}
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
2013-10-26 09:41:45 +02:00
SetAcknowledgementRaw(type);
SetAcknowledgementExpiry(expiry);
2013-03-08 16:36:26 +01:00
}
OnNotificationsRequested(GetSelf(), NotificationAcknowledgement, GetLastCheckResult(), author, comment);
2013-11-10 16:53:57 +01:00
OnAcknowledgementSet(GetSelf(), author, comment, type, expiry, authority);
}
void Service::ClearAcknowledgement(const String& authority)
{
ASSERT(OwnsLock());
2013-10-26 09:41:45 +02:00
SetAcknowledgementRaw(AcknowledgementNone);
SetAcknowledgementExpiry(0);
2013-03-04 15:52:42 +01:00
2013-11-10 16:53:57 +01:00
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. */
2013-11-15 14:01:30 +01:00
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-11-10 22:04:18 +01:00
BOOST_FOREACH(const String& dependency, dependencies) {
2013-11-15 14:01:30 +01:00
parents.insert(Host::GetByName(dependency));
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-11-15 14:01:30 +01:00
if (!service || service->GetName() == GetName())
2013-02-07 20:29:35 +01:00
continue;
parents.insert(service);
}
}
return parents;
}
bool Service::GetEnablePerfdata(void) const
{
if (!GetOverrideEnablePerfdata().IsEmpty())
return GetOverrideEnablePerfdata();
else
return GetEnablePerfdataRaw();
}
void Service::SetEnablePerfdata(bool enabled, const String& authority)
{
SetOverrideEnablePerfdata(enabled);
}
int Service::GetModifiedAttributes(void) const
{
int attrs = 0;
if (!GetOverrideEnableNotifications().IsEmpty())
attrs |= ModAttrNotificationsEnabled;
2013-10-26 09:41:45 +02:00
if (!GetOverrideEnableActiveChecks().IsEmpty())
attrs |= ModAttrActiveChecksEnabled;
2013-10-26 09:41:45 +02:00
if (!GetOverrideEnablePassiveChecks().IsEmpty())
attrs |= ModAttrPassiveChecksEnabled;
if (!GetOverrideEnableFlapping().IsEmpty())
attrs |= ModAttrFlapDetectionEnabled;
2013-10-26 09:41:45 +02:00
if (!GetOverrideEnableEventHandler().IsEmpty())
attrs |= ModAttrEventHandlerEnabled;
if (!GetOverrideEnablePerfdata().IsEmpty())
attrs |= ModAttrPerformanceDataEnabled;
2013-10-26 09:41:45 +02:00
if (!GetOverrideCheckInterval().IsEmpty())
attrs |= ModAttrNormalCheckInterval;
2013-10-26 09:41:45 +02:00
if (!GetOverrideRetryInterval().IsEmpty())
attrs |= ModAttrRetryCheckInterval;
if (!GetOverrideEventCommand().IsEmpty())
attrs |= ModAttrEventHandlerCommand;
if (!GetOverrideCheckCommand().IsEmpty())
attrs |= ModAttrCheckCommand;
if (!GetOverrideMaxCheckAttempts().IsEmpty())
attrs |= ModAttrMaxCheckAttempts;
if (!GetOverrideCheckPeriod().IsEmpty())
attrs |= ModAttrCheckTimeperiod;
// TODO: finish
return attrs;
}
void Service::SetModifiedAttributes(int flags)
{
if ((flags & ModAttrNotificationsEnabled) == 0)
SetOverrideEnableNotifications(Empty);
if ((flags & ModAttrActiveChecksEnabled) == 0)
2013-10-26 09:41:45 +02:00
SetOverrideEnableActiveChecks(Empty);
if ((flags & ModAttrPassiveChecksEnabled) == 0)
2013-10-26 09:41:45 +02:00
SetOverrideEnablePassiveChecks(Empty);
if ((flags & ModAttrFlapDetectionEnabled) == 0)
SetOverrideEnableFlapping(Empty);
if ((flags & ModAttrEventHandlerEnabled) == 0)
2013-10-26 09:41:45 +02:00
SetOverrideEnableEventHandler(Empty);
if ((flags & ModAttrPerformanceDataEnabled) == 0)
SetOverrideEnablePerfdata(Empty);
if ((flags & ModAttrNormalCheckInterval) == 0)
2013-10-26 09:41:45 +02:00
SetOverrideCheckInterval(Empty);
if ((flags & ModAttrRetryCheckInterval) == 0)
2013-10-26 09:41:45 +02:00
SetOverrideRetryInterval(Empty);
if ((flags & ModAttrEventHandlerCommand) == 0)
SetOverrideEventCommand(Empty);
if ((flags & ModAttrCheckCommand) == 0)
SetOverrideCheckCommand(Empty);
if ((flags & ModAttrMaxCheckAttempts) == 0)
SetOverrideMaxCheckAttempts(Empty);
if ((flags & ModAttrCheckTimeperiod) == 0)
SetOverrideCheckPeriod(Empty);
}
bool Service::ResolveMacro(const String& macro, const CheckResult::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") {
2013-10-26 09:41:45 +02:00
*result = Convert::ToString(GetCheckAttempt());
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;
} else if (macro == "SERVICEDURATIONSEC") {
*result = Convert::ToString((long)(Utility::GetTime() - GetLastStateChange()));
return true;
} else if (macro == "TOTALHOSTSERVICES" || macro == "TOTALHOSTSERVICESOK" || macro == "TOTALHOSTSERVICESWARNING"
|| macro == "TOTALHOSTSERVICESUNKNOWN" || macro == "TOTALHOSTSERVICESCRITICAL") {
int filter = -1;
int count = 0;
if (macro == "TOTALHOSTSERVICESOK")
filter = StateOK;
else if (macro == "TOTALHOSTSERVICESWARNING")
filter = StateWarning;
else if (macro == "TOTALHOSTSERVICESUNKNOWN")
filter = StateUnknown;
else if (macro == "TOTALHOSTSERVICESCRITICAL")
filter = StateCritical;
BOOST_FOREACH(const Service::Ptr& service, GetHost()->GetServices()) {
if (filter != -1 && service->GetState() != filter)
continue;
count++;
}
*result = Convert::ToString(count);
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->GetOutput();
return true;
} else if (macro == "SERVICEPERFDATA") {
*result = PluginUtility::FormatPerfdata(cr->GetPerformanceData());
return true;
} else if (macro == "LASTSERVICECHECK") {
*result = Convert::ToString((long)cr->GetExecutionEnd());
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
}