icinga2/lib/icinga/service.cpp

187 lines
5.2 KiB
C++
Raw Normal View History

/******************************************************************************
* Icinga 2 *
* Copyright (C) 2012-2014 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. *
******************************************************************************/
2014-05-25 16:23:35 +02:00
#include "icinga/service.hpp"
#include "icinga/servicegroup.hpp"
#include "icinga/pluginutility.hpp"
#include "base/objectlock.hpp"
#include "base/convert.hpp"
#include "base/utility.hpp"
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);
String ServiceNameComposer::MakeName(const String& shortName, const Dictionary::Ptr& props) const {
if (!props)
return "";
return props->Get("host_name") + "!" + shortName;
}
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->ResolveGroupMembership(GetSelf(), true);
}
}
2013-03-02 09:07:47 +01:00
m_Host = Host::GetByName(GetHostName());
2013-11-10 22:04:18 +01:00
if (m_Host)
m_Host->AddService(GetSelf());
SetSchedulingOffset(Utility::Random());
2014-04-03 15:36:13 +02:00
Checkable::OnConfigLoaded();
}
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;
}
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 ServiceOK;
2014-04-03 15:36:13 +02:00
else if (state == "WARNING")
return ServiceWarning;
2014-04-03 15:36:13 +02:00
else if (state == "CRITICAL")
return ServiceCritical;
2014-04-03 15:36:13 +02:00
else
return ServiceUnknown;
}
2014-04-03 15:36:13 +02:00
String Service::StateToString(ServiceState state)
{
2014-04-03 15:36:13 +02:00
switch (state) {
case ServiceOK:
2014-04-03 15:36:13 +02:00
return "OK";
case ServiceWarning:
2014-04-03 15:36:13 +02:00
return "WARNING";
case ServiceCritical:
2014-04-03 15:36:13 +02:00
return "CRITICAL";
case ServiceUnknown:
2014-04-03 15:36:13 +02:00
default:
return "UNKNOWN";
2013-03-08 16:36:26 +01:00
}
}
2014-04-03 15:36:13 +02:00
StateType Service::StateTypeFromString(const String& type)
{
2014-04-03 15:36:13 +02:00
if (type == "SOFT")
return StateTypeSoft;
else
2014-04-03 15:36:13 +02:00
return StateTypeHard;
}
2014-04-03 15:36:13 +02:00
String Service::StateTypeToString(StateType type)
{
2014-04-03 15:36:13 +02:00
if (type == StateTypeSoft)
return "SOFT";
else
return "HARD";
}
bool Service::ResolveMacro(const String& macro, const CheckResult::Ptr& cr, String *result) const
{
if (macro == "state") {
*result = StateToString(GetState());
return true;
} else if (macro == "state_id") {
*result = Convert::ToString(GetState());
return true;
} else if (macro == "state_type") {
*result = StateTypeToString(GetStateType());
return true;
} else if (macro == "last_state") {
*result = StateToString(GetLastState());
return true;
} else if (macro == "last_state_id") {
*result = Convert::ToString(GetLastState());
return true;
} else if (macro == "last_state_type") {
*result = StateTypeToString(GetLastStateType());
return true;
} else if (macro == "last_state_change") {
*result = Convert::ToString((long)GetLastStateChange());
return true;
} else if (macro == "duration_sec") {
*result = Convert::ToString((long)(Utility::GetTime() - GetLastStateChange()));
return true;
}
if (cr) {
if (macro == "latency") {
*result = Convert::ToString(Service::CalculateLatency(cr));
return true;
} else if (macro == "execution_time") {
*result = Convert::ToString(Service::CalculateExecutionTime(cr));
return true;
} else if (macro == "output") {
*result = cr->GetOutput();
return true;
} else if (macro == "perfdata") {
*result = PluginUtility::FormatPerfdata(cr->GetPerformanceData());
return true;
} else if (macro == "last_check") {
*result = Convert::ToString((long)cr->GetExecutionEnd());
return true;
}
2013-03-07 12:04:20 +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());
}