1 /******************************************************************************
3 * Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org) *
5 * This program is free software; you can redistribute it and/or *
6 * modify it under the terms of the GNU General Public License *
7 * as published by the Free Software Foundation; either version 2 *
8 * of the License, or (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the Free Software Foundation *
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ******************************************************************************/
20 #include "icinga/service.h"
21 #include "icinga/servicegroup.h"
22 #include "icinga/checkcommand.h"
23 #include "icinga/icingaapplication.h"
24 #include "icinga/macroprocessor.h"
25 #include "icinga/pluginutility.h"
26 #include "icinga/dependency.h"
27 #include "config/configitembuilder.h"
28 #include "base/dynamictype.h"
29 #include "base/objectlock.h"
30 #include "base/convert.h"
31 #include "base/utility.h"
32 #include "base/initialize.h"
33 #include <boost/foreach.hpp>
34 #include <boost/bind/apply.hpp>
36 using namespace icinga;
38 REGISTER_TYPE(Service);
40 INITIALIZE_ONCE(&Service::StartDowntimesExpiredTimer);
42 String ServiceNameComposer::MakeName(const String& shortName, const Dictionary::Ptr props) const {
46 return props->Get("host_name") + "!" + shortName;
49 void Service::OnConfigLoaded(void)
51 Array::Ptr groups = GetGroups();
54 ObjectLock olock(groups);
56 BOOST_FOREACH(const String& name, groups) {
57 ServiceGroup::Ptr sg = ServiceGroup::GetByName(name);
60 sg->ResolveGroupMembership(GetSelf(), true);
64 m_Host = Host::GetByName(GetHostName());
67 m_Host->AddService(GetSelf());
69 SetSchedulingOffset(Utility::Random());
71 Checkable::OnConfigLoaded();
74 Service::Ptr Service::GetByNamePair(const String& hostName, const String& serviceName)
76 if (!hostName.IsEmpty()) {
77 Host::Ptr host = Host::GetByName(hostName);
80 return Service::Ptr();
82 return host->GetServiceByShortName(serviceName);
84 return Service::GetByName(serviceName);
88 Host::Ptr Service::GetHost(void) const
93 ServiceState Service::StateFromString(const String& state)
97 else if (state == "WARNING")
98 return ServiceWarning;
99 else if (state == "CRITICAL")
100 return ServiceCritical;
102 return ServiceUnknown;
105 String Service::StateToString(ServiceState state)
112 case ServiceCritical:
120 StateType Service::StateTypeFromString(const String& type)
123 return StateTypeSoft;
125 return StateTypeHard;
128 String Service::StateTypeToString(StateType type)
130 if (type == StateTypeSoft)
136 bool Service::ResolveMacro(const String& macro, const CheckResult::Ptr& cr, String *result) const
138 if (macro == "state") {
139 *result = StateToString(GetState());
141 } else if (macro == "state_id") {
142 *result = Convert::ToString(GetState());
144 } else if (macro == "state_type") {
145 *result = StateTypeToString(GetStateType());
147 } else if (macro == "last_state") {
148 *result = StateToString(GetLastState());
150 } else if (macro == "last_state_id") {
151 *result = Convert::ToString(GetLastState());
153 } else if (macro == "last_state_type") {
154 *result = StateTypeToString(GetLastStateType());
156 } else if (macro == "last_state_change") {
157 *result = Convert::ToString((long)GetLastStateChange());
159 } else if (macro == "duration_sec") {
160 *result = Convert::ToString((long)(Utility::GetTime() - GetLastStateChange()));
165 if (macro == "latency") {
166 *result = Convert::ToString(Service::CalculateLatency(cr));
168 } else if (macro == "execution_time") {
169 *result = Convert::ToString(Service::CalculateExecutionTime(cr));
171 } else if (macro == "output") {
172 *result = cr->GetOutput();
174 } else if (macro == "perfdata") {
175 *result = PluginUtility::FormatPerfdata(cr->GetPerformanceData());
177 } else if (macro == "last_check") {
178 *result = Convert::ToString((long)cr->GetExecutionEnd());
186 boost::tuple<Host::Ptr, Service::Ptr> icinga::GetHostService(const Checkable::Ptr& checkable)
188 Service::Ptr service = dynamic_pointer_cast<Service>(checkable);
191 return boost::make_tuple(service->GetHost(), service);
193 return boost::make_tuple(static_pointer_cast<Host>(checkable), Service::Ptr());