2012-07-09 20:32:02 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2014-03-19 01:02:29 +01:00
|
|
|
* Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org) *
|
2012-07-09 20:32:02 +02:00
|
|
|
* *
|
|
|
|
* 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"
|
2013-06-13 11:33:00 +02:00
|
|
|
#include "icinga/checkcommand.h"
|
2013-03-17 20:19:29 +01:00
|
|
|
#include "icinga/icingaapplication.h"
|
|
|
|
#include "icinga/macroprocessor.h"
|
2013-11-07 14:38:37 +01:00
|
|
|
#include "icinga/pluginutility.h"
|
2014-02-27 11:05:55 +01:00
|
|
|
#include "icinga/dependency.h"
|
2013-03-22 14:40:55 +01:00
|
|
|
#include "config/configitembuilder.h"
|
2013-03-16 21:18:53 +01:00
|
|
|
#include "base/dynamictype.h"
|
|
|
|
#include "base/objectlock.h"
|
2013-03-22 14:40:55 +01:00
|
|
|
#include "base/convert.h"
|
2013-03-25 18:36:15 +01:00
|
|
|
#include "base/utility.h"
|
2013-12-12 06:30:11 +01:00
|
|
|
#include "base/initialize.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>
|
2012-06-13 13:42:55 +02:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2013-03-01 12:07:52 +01:00
|
|
|
REGISTER_TYPE(Service);
|
2012-07-26 12:28:29 +02:00
|
|
|
|
2013-12-12 06:30:11 +01:00
|
|
|
INITIALIZE_ONCE(&Service::StartDowntimesExpiredTimer);
|
|
|
|
|
2013-08-29 16:53:57 +02:00
|
|
|
void Service::OnConfigLoaded(void)
|
|
|
|
{
|
2013-08-20 11:06:04 +02:00
|
|
|
Array::Ptr groups = GetGroups();
|
2013-02-20 19:52:25 +01:00
|
|
|
|
2013-08-20 11:06:04 +02:00
|
|
|
if (groups) {
|
2013-09-09 13:52:37 +02:00
|
|
|
ObjectLock olock(groups);
|
|
|
|
|
2013-08-20 11:06:04 +02:00
|
|
|
BOOST_FOREACH(const String& name, groups) {
|
|
|
|
ServiceGroup::Ptr sg = ServiceGroup::GetByName(name);
|
2012-08-02 09:38:08 +02:00
|
|
|
|
2013-08-20 11:06:04 +02:00
|
|
|
if (sg)
|
|
|
|
sg->AddMember(GetSelf());
|
|
|
|
}
|
|
|
|
}
|
2013-03-02 09:07:47 +01:00
|
|
|
|
2014-04-05 09:15:40 +02:00
|
|
|
m_Host = Host::GetByName(GetHostName());
|
2013-11-10 22:04:18 +01:00
|
|
|
|
|
|
|
if (m_Host)
|
|
|
|
m_Host->AddService(GetSelf());
|
2013-08-29 16:53:57 +02:00
|
|
|
|
2013-11-21 16:09:21 +01:00
|
|
|
SetSchedulingOffset(Utility::Random());
|
2013-11-13 14:56:31 +01:00
|
|
|
|
2014-04-03 15:36:13 +02:00
|
|
|
Checkable::OnConfigLoaded();
|
2013-11-13 14:56:31 +01: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);
|
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);
|
2013-02-08 15:38:22 +01:00
|
|
|
} 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
|
|
|
{
|
2013-11-10 22:04:18 +01:00
|
|
|
return m_Host;
|
2013-02-08 15:38:22 +01:00
|
|
|
}
|
|
|
|
|
2014-04-03 15:36:13 +02:00
|
|
|
ServiceState Service::StateFromString(const String& state)
|
2013-07-09 16:59:31 +02:00
|
|
|
{
|
2014-04-03 15:36:13 +02:00
|
|
|
if (state == "OK")
|
|
|
|
return StateOK;
|
|
|
|
else if (state == "WARNING")
|
|
|
|
return StateWarning;
|
|
|
|
else if (state == "CRITICAL")
|
|
|
|
return StateCritical;
|
|
|
|
else
|
|
|
|
return StateUnknown;
|
2013-02-11 16:38:16 +01:00
|
|
|
}
|
|
|
|
|
2014-04-03 15:36:13 +02:00
|
|
|
String Service::StateToString(ServiceState state)
|
2013-02-24 12:47:24 +01:00
|
|
|
{
|
2014-04-03 15:36:13 +02:00
|
|
|
switch (state) {
|
|
|
|
case StateOK:
|
|
|
|
return "OK";
|
|
|
|
case StateWarning:
|
|
|
|
return "WARNING";
|
|
|
|
case StateCritical:
|
|
|
|
return "CRITICAL";
|
|
|
|
case StateUnknown:
|
|
|
|
default:
|
|
|
|
return "UNKNOWN";
|
2013-03-08 16:36:26 +01:00
|
|
|
}
|
2013-02-24 12:47:24 +01:00
|
|
|
}
|
|
|
|
|
2014-04-03 15:36:13 +02:00
|
|
|
StateType Service::StateTypeFromString(const String& type)
|
2013-02-24 12:47:24 +01:00
|
|
|
{
|
2014-04-03 15:36:13 +02:00
|
|
|
if (type == "SOFT")
|
|
|
|
return StateTypeSoft;
|
2013-11-26 11:51:06 +01:00
|
|
|
else
|
2014-04-03 15:36:13 +02:00
|
|
|
return StateTypeHard;
|
2013-10-16 11:46:54 +02:00
|
|
|
}
|
|
|
|
|
2014-04-03 15:36:13 +02:00
|
|
|
String Service::StateTypeToString(StateType type)
|
2013-10-16 11:46:54 +02:00
|
|
|
{
|
2014-04-03 15:36:13 +02:00
|
|
|
if (type == StateTypeSoft)
|
|
|
|
return "SOFT";
|
|
|
|
else
|
|
|
|
return "HARD";
|
2013-10-16 11:46:54 +02:00
|
|
|
}
|
|
|
|
|
2013-11-09 14:22:38 +01:00
|
|
|
bool Service::ResolveMacro(const String& macro, const CheckResult::Ptr& cr, String *result) const
|
2013-02-09 18:39:43 +01:00
|
|
|
{
|
2014-04-05 17:13:17 +02:00
|
|
|
String key;
|
|
|
|
Dictionary::Ptr vars;
|
|
|
|
|
2014-04-04 20:09:23 +02:00
|
|
|
/* require prefix for object macros */
|
|
|
|
if (macro.SubStr(0, 8) == "service.") {
|
2014-04-05 17:13:17 +02:00
|
|
|
key = macro.SubStr(8);
|
|
|
|
|
|
|
|
if (key.SubStr(0, 5) == "vars.") {
|
|
|
|
vars = GetVars();
|
|
|
|
String vars_key = key.SubStr(5);
|
2013-02-11 16:29:23 +01:00
|
|
|
|
2014-04-05 17:13:17 +02:00
|
|
|
if (vars && vars->Contains(vars_key)) {
|
|
|
|
*result = vars->Get(vars_key);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} else if (key == "description") {
|
2014-04-04 20:09:23 +02:00
|
|
|
*result = GetShortName();
|
|
|
|
return true;
|
|
|
|
} else if (key == "displayname") {
|
|
|
|
*result = GetDisplayName();
|
|
|
|
return true;
|
|
|
|
} else if (key == "checkcommand") {
|
|
|
|
CheckCommand::Ptr commandObj = GetCheckCommand();
|
2013-10-18 11:40:46 +02:00
|
|
|
|
2014-04-04 20:09:23 +02:00
|
|
|
if (commandObj)
|
|
|
|
*result = commandObj->GetName();
|
|
|
|
else
|
|
|
|
*result = "";
|
2013-03-15 13:29:41 +01:00
|
|
|
|
2013-03-22 14:40:55 +01:00
|
|
|
return true;
|
2014-04-04 20:09:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (key == "state") {
|
|
|
|
*result = StateToString(GetState());
|
|
|
|
return true;
|
|
|
|
} else if (key == "stateid") {
|
|
|
|
*result = Convert::ToString(GetState());
|
|
|
|
return true;
|
|
|
|
} else if (key == "statetype") {
|
|
|
|
*result = StateTypeToString(GetStateType());
|
|
|
|
return true;
|
|
|
|
} else if (key == "attempt") {
|
|
|
|
*result = Convert::ToString(GetCheckAttempt());
|
2013-03-22 14:40:55 +01:00
|
|
|
return true;
|
2014-04-04 20:09:23 +02:00
|
|
|
} else if (key == "maxattempt") {
|
|
|
|
*result = Convert::ToString(GetMaxCheckAttempts());
|
2013-03-22 14:40:55 +01:00
|
|
|
return true;
|
2014-04-04 20:09:23 +02:00
|
|
|
} else if (key == "laststate") {
|
|
|
|
*result = StateToString(GetLastState());
|
2013-03-22 14:40:55 +01:00
|
|
|
return true;
|
2014-04-04 20:09:23 +02:00
|
|
|
} else if (key == "laststateid") {
|
|
|
|
*result = Convert::ToString(GetLastState());
|
|
|
|
return true;
|
|
|
|
} else if (key == "laststatetype") {
|
|
|
|
*result = StateTypeToString(GetLastStateType());
|
|
|
|
return true;
|
|
|
|
} else if (key == "laststatechange") {
|
|
|
|
*result = Convert::ToString((long)GetLastStateChange());
|
|
|
|
return true;
|
|
|
|
} else if (key == "durationsec") {
|
|
|
|
*result = Convert::ToString((long)(Utility::GetTime() - GetLastStateChange()));
|
2013-03-22 14:40:55 +01:00
|
|
|
return true;
|
|
|
|
}
|
2014-04-03 10:30:11 +02:00
|
|
|
|
2014-04-04 20:09:23 +02:00
|
|
|
if (cr) {
|
|
|
|
if (key == "latency") {
|
|
|
|
*result = Convert::ToString(Service::CalculateLatency(cr));
|
|
|
|
return true;
|
|
|
|
} else if (key == "executiontime") {
|
|
|
|
*result = Convert::ToString(Service::CalculateExecutionTime(cr));
|
|
|
|
return true;
|
|
|
|
} else if (key == "output") {
|
|
|
|
*result = cr->GetOutput();
|
|
|
|
return true;
|
|
|
|
} else if (key == "perfdata") {
|
|
|
|
*result = PluginUtility::FormatPerfdata(cr->GetPerformanceData());
|
|
|
|
return true;
|
|
|
|
} else if (key == "lastcheck") {
|
|
|
|
*result = Convert::ToString((long)cr->GetExecutionEnd());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2014-04-05 17:13:17 +02:00
|
|
|
} else {
|
|
|
|
vars = GetVars();
|
2013-03-07 12:04:20 +01:00
|
|
|
|
2014-04-05 17:13:17 +02:00
|
|
|
if (vars && vars->Contains(macro)) {
|
|
|
|
*result = vars->Get(macro);
|
|
|
|
return true;
|
|
|
|
}
|
2013-03-07 12:04:20 +01:00
|
|
|
}
|
|
|
|
|
2013-03-22 14:40:55 +01:00
|
|
|
return false;
|
2013-03-07 12:04:20 +01:00
|
|
|
}
|
2014-04-03 15:36:13 +02:00
|
|
|
|
|
|
|
boost::tuple<Host::Ptr, Service::Ptr> icinga::GetHostService(const Checkable::Ptr& checkable)
|
|
|
|
{
|
|
|
|
Service::Ptr service = dynamic_pointer_cast<Service>(checkable);
|
|
|
|
|
|
|
|
if (service)
|
|
|
|
return boost::make_tuple(service->GetHost(), service);
|
|
|
|
else
|
|
|
|
return boost::make_tuple(static_pointer_cast<Host>(checkable), Service::Ptr());
|
|
|
|
}
|
|
|
|
|