icinga2/lib/icinga/service.cpp

286 lines
6.5 KiB
C++
Raw Normal View History

/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
2014-05-25 16:23:35 +02:00
#include "icinga/service.hpp"
2018-01-18 13:50:38 +01:00
#include "icinga/service-ti.cpp"
2014-05-25 16:23:35 +02:00
#include "icinga/servicegroup.hpp"
#include "icinga/scheduleddowntime.hpp"
2014-05-25 16:23:35 +02:00
#include "icinga/pluginutility.hpp"
#include "base/objectlock.hpp"
#include "base/convert.hpp"
#include "base/utility.hpp"
using namespace icinga;
2013-03-01 12:07:52 +01:00
REGISTER_TYPE(Service);
boost::signals2::signal<void (const Service::Ptr&, const CheckResult::Ptr&, const MessageOrigin::Ptr&)> Service::OnHostProblemChanged;
2014-11-06 19:35:47 +01:00
String ServiceNameComposer::MakeName(const String& shortName, const Object::Ptr& context) const
{
Service::Ptr service = dynamic_pointer_cast<Service>(context);
if (!service)
return "";
2014-11-06 19:35:47 +01:00
return service->GetHostName() + "!" + shortName;
}
Dictionary::Ptr ServiceNameComposer::ParseName(const String& name) const
{
2018-01-04 18:24:45 +01:00
std::vector<String> tokens = name.Split("!");
if (tokens.size() < 2)
BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid Service name."));
return new Dictionary({
{ "host_name", tokens[0] },
{ "name", tokens[1] }
});
}
void Service::OnAllConfigLoaded()
{
ObjectImpl<Service>::OnAllConfigLoaded();
String zoneName = GetZoneName();
if (!zoneName.IsEmpty()) {
Zone::Ptr zone = Zone::GetByName(zoneName);
if (zone && zone->IsGlobal())
BOOST_THROW_EXCEPTION(std::invalid_argument("Service '" + GetName() + "' cannot be put into global zone '" + zone->GetName() + "'."));
}
m_Host = Host::GetByName(GetHostName());
if (m_Host)
m_Host->AddService(this);
ServiceGroup::EvaluateObjectRules(this);
Array::Ptr groups = GetGroups();
2013-02-20 19:52:25 +01:00
if (groups) {
groups = groups->ShallowClone();
2013-09-09 13:52:37 +02:00
ObjectLock olock(groups);
for (const String& name : groups) {
ServiceGroup::Ptr sg = ServiceGroup::GetByName(name);
if (sg)
sg->ResolveGroupMembership(this, true);
}
}
}
void Service::CreateChildObjects(const Type::Ptr& childType)
{
if (childType == ScheduledDowntime::TypeInstance)
ScheduledDowntime::EvaluateApplyRules(this);
if (childType == Notification::TypeInstance)
Notification::EvaluateApplyRules(this);
2013-03-02 09:07:47 +01:00
if (childType == Dependency::TypeInstance)
Dependency::EvaluateApplyRules(this);
}
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)
2017-11-30 08:36:35 +01:00
return nullptr;
2013-02-27 15:23:25 +01:00
2013-03-02 09:07:47 +01:00
return host->GetServiceByShortName(serviceName);
} else {
return Service::GetByName(serviceName);
}
}
Host::Ptr Service::GetHost() const
{
2013-11-10 22:04:18 +01:00
return m_Host;
}
2018-12-07 16:33:10 +01:00
/* keep in sync with Host::GetSeverity()
* One could think it may be smart to use an enum and some bitmask math here.
* But the only thing the consuming icingaweb2 cares about is being able to
* sort by severity. It is therefore easier to keep them seperated here. */
int Service::GetSeverity() const
{
2018-12-07 16:33:10 +01:00
int severity;
ObjectLock olock(this);
ServiceState state = GetStateRaw();
2018-12-07 16:33:10 +01:00
if (!HasBeenChecked()) {
severity = 16;
} else if (state == ServiceOK) {
severity = 0;
} else {
switch (state) {
case ServiceWarning:
severity = 32;
break;
case ServiceUnknown:
severity = 64;
break;
case ServiceCritical:
severity = 128;
break;
default:
severity = 256;
}
Host::Ptr host = GetHost();
ObjectLock hlock (host);
if (host->GetState() != HostUp || !host->IsReachable()) {
severity += 1024;
} else {
if (IsAcknowledged())
severity += 512;
else if (IsInDowntime())
severity += 256;
else
severity += 2048;
}
hlock.Unlock();
}
olock.Unlock();
return severity;
}
2019-04-11 11:25:45 +02:00
bool Service::GetHandled() const
{
return Checkable::GetHandled() || (m_Host && m_Host->GetProblem());
2019-04-11 11:25:45 +02:00
}
2019-04-09 11:26:34 +02:00
bool Service::IsStateOK(ServiceState state) const
{
return state == ServiceOK;
}
void Service::SaveLastState(ServiceState state, double timestamp)
{
if (state == ServiceOK)
SetLastStateOK(timestamp);
else if (state == ServiceWarning)
SetLastStateWarning(timestamp);
else if (state == ServiceCritical)
SetLastStateCritical(timestamp);
else if (state == ServiceUnknown)
SetLastStateUnknown(timestamp);
}
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, Value *result) const
{
if (macro == "state") {
*result = StateToString(GetState());
return true;
} else if (macro == "state_id") {
*result = 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 = GetLastState();
return true;
} else if (macro == "last_state_type") {
*result = StateTypeToString(GetLastStateType());
return true;
} else if (macro == "last_state_change") {
*result = static_cast<long>(GetLastStateChange());
return true;
} else if (macro == "downtime_depth") {
*result = GetDowntimeDepth();
return true;
} else if (macro == "duration_sec") {
*result = Utility::GetTime() - GetLastStateChange();
return true;
}
if (cr) {
if (macro == "latency") {
*result = cr->CalculateLatency();
return true;
} else if (macro == "execution_time") {
*result = cr->CalculateExecutionTime();
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 == "check_source") {
*result = cr->GetCheckSource();
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
2017-11-22 12:05:36 +01:00
std::pair<Host::Ptr, Service::Ptr> icinga::GetHostService(const Checkable::Ptr& checkable)
2014-04-03 15:36:13 +02:00
{
Service::Ptr service = dynamic_pointer_cast<Service>(checkable);
if (service)
2017-11-22 12:05:36 +01:00
return std::make_pair(service->GetHost(), service);
2014-04-03 15:36:13 +02:00
else
2017-11-30 08:36:35 +01:00
return std::make_pair(static_pointer_cast<Host>(checkable), nullptr);
2014-04-03 15:36:13 +02:00
}