]> granicus.if.org Git - icinga2/blob - lib/icinga/service.cpp
Update Vagrant documentation link.
[icinga2] / lib / icinga / service.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org)    *
4  *                                                                            *
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.                     *
9  *                                                                            *
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.                               *
14  *                                                                            *
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  ******************************************************************************/
19
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>
35
36 using namespace icinga;
37
38 REGISTER_TYPE(Service);
39
40 INITIALIZE_ONCE(&Service::StartDowntimesExpiredTimer);
41
42 String ServiceNameComposer::MakeName(const String& shortName, const Dictionary::Ptr props) const {
43         if (!props)
44                 return "";
45
46         return props->Get("host_name") + "!" + shortName;
47 }
48
49 void Service::OnConfigLoaded(void)
50 {
51         Array::Ptr groups = GetGroups();
52
53         if (groups) {
54                 ObjectLock olock(groups);
55
56                 BOOST_FOREACH(const String& name, groups) {
57                         ServiceGroup::Ptr sg = ServiceGroup::GetByName(name);
58
59                         if (sg)
60                                 sg->ResolveGroupMembership(GetSelf(), true);
61                 }
62         }
63
64         m_Host = Host::GetByName(GetHostName());
65
66         if (m_Host)
67                 m_Host->AddService(GetSelf());
68
69         SetSchedulingOffset(Utility::Random());
70
71         Checkable::OnConfigLoaded();
72 }
73
74 Service::Ptr Service::GetByNamePair(const String& hostName, const String& serviceName)
75 {
76         if (!hostName.IsEmpty()) {
77                 Host::Ptr host = Host::GetByName(hostName);
78
79                 if (!host)
80                         return Service::Ptr();
81
82                 return host->GetServiceByShortName(serviceName);
83         } else {
84                 return Service::GetByName(serviceName);
85         }
86 }
87
88 Host::Ptr Service::GetHost(void) const
89 {
90         return m_Host;
91 }
92
93 ServiceState Service::StateFromString(const String& state)
94 {
95         if (state == "OK")
96                 return ServiceOK;
97         else if (state == "WARNING")
98                 return ServiceWarning;
99         else if (state == "CRITICAL")
100                 return ServiceCritical;
101         else
102                 return ServiceUnknown;
103 }
104
105 String Service::StateToString(ServiceState state)
106 {
107         switch (state) {
108                 case ServiceOK:
109                         return "OK";
110                 case ServiceWarning:
111                         return "WARNING";
112                 case ServiceCritical:
113                         return "CRITICAL";
114                 case ServiceUnknown:
115                 default:
116                         return "UNKNOWN";
117         }
118 }
119
120 StateType Service::StateTypeFromString(const String& type)
121 {
122         if (type == "SOFT")
123                 return StateTypeSoft;
124         else
125                 return StateTypeHard;
126 }
127
128 String Service::StateTypeToString(StateType type)
129 {
130         if (type == StateTypeSoft)
131                 return "SOFT";
132         else
133                 return "HARD";
134 }
135
136 bool Service::ResolveMacro(const String& macro, const CheckResult::Ptr& cr, String *result) const
137 {
138         if (macro == "state") {
139                 *result = StateToString(GetState());
140                 return true;
141         } else if (macro == "state_id") {
142                 *result = Convert::ToString(GetState());
143                 return true;
144         } else if (macro == "state_type") {
145                 *result = StateTypeToString(GetStateType());
146                 return true;
147         } else if (macro == "last_state") {
148                 *result = StateToString(GetLastState());
149                 return true;
150         } else if (macro == "last_state_id") {
151                 *result = Convert::ToString(GetLastState());
152                 return true;
153         } else if (macro == "last_state_type") {
154                 *result = StateTypeToString(GetLastStateType());
155                 return true;
156         } else if (macro == "last_state_change") {
157                 *result = Convert::ToString((long)GetLastStateChange());
158                 return true;
159         } else if (macro == "duration_sec") {
160                 *result = Convert::ToString((long)(Utility::GetTime() - GetLastStateChange()));
161                 return true;
162         }
163
164         if (cr) {
165                 if (macro == "latency") {
166                         *result = Convert::ToString(Service::CalculateLatency(cr));
167                         return true;
168                 } else if (macro == "execution_time") {
169                         *result = Convert::ToString(Service::CalculateExecutionTime(cr));
170                         return true;
171                 } else if (macro == "output") {
172                         *result = cr->GetOutput();
173                         return true;
174                 } else if (macro == "perfdata") {
175                         *result = PluginUtility::FormatPerfdata(cr->GetPerformanceData());
176                         return true;
177                 } else if (macro == "last_check") {
178                         *result = Convert::ToString((long)cr->GetExecutionEnd());
179                         return true;
180                 }
181         }
182
183         return false;
184 }
185
186 boost::tuple<Host::Ptr, Service::Ptr> icinga::GetHostService(const Checkable::Ptr& checkable)
187 {
188         Service::Ptr service = dynamic_pointer_cast<Service>(checkable);
189
190         if (service)
191                 return boost::make_tuple(service->GetHost(), service);
192         else
193                 return boost::make_tuple(static_pointer_cast<Host>(checkable), Service::Ptr());
194 }
195