2012-07-09 20:32:02 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
|
|
|
* Copyright (C) 2012 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. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
2012-09-10 14:07:32 +02:00
|
|
|
#include "i2-icinga.h"
|
2012-06-13 13:42:55 +02:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2012-12-04 08:42:24 +01:00
|
|
|
static AttributeDescription serviceAttributes[] = {
|
|
|
|
{ "scheduling_offset", Attribute_Transient },
|
2013-01-23 15:25:00 +01:00
|
|
|
{ "first_check", Attribute_Transient },
|
2012-12-04 08:42:24 +01:00
|
|
|
{ "next_check", Attribute_Replicated },
|
|
|
|
{ "checker", Attribute_Replicated },
|
|
|
|
{ "check_attempt", Attribute_Replicated },
|
|
|
|
{ "state", Attribute_Replicated },
|
|
|
|
{ "state_type", Attribute_Replicated },
|
|
|
|
{ "last_result", Attribute_Replicated },
|
|
|
|
{ "last_state_change", Attribute_Replicated },
|
2013-01-22 16:01:08 +01:00
|
|
|
{ "last_hard_state_change", Attribute_Replicated },
|
2013-01-24 23:10:07 +01:00
|
|
|
{ "enable_active_checks", Attribute_Replicated },
|
|
|
|
{ "enable_passive_checks", Attribute_Replicated },
|
2013-01-23 13:21:07 +01:00
|
|
|
{ "force_next_check", Attribute_Replicated },
|
|
|
|
{ "acknowledgement", Attribute_Replicated },
|
2013-01-29 14:19:54 +01:00
|
|
|
{ "acknowledgement_expiry", Attribute_Replicated },
|
2013-01-30 09:59:22 +01:00
|
|
|
{ "downtimes", Attribute_Replicated },
|
|
|
|
{ "comments", Attribute_Replicated }
|
2012-12-04 08:42:24 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
REGISTER_TYPE(Service, serviceAttributes);
|
2012-07-26 12:28:29 +02:00
|
|
|
|
2012-09-05 10:13:24 +02:00
|
|
|
const int Service::DefaultMaxCheckAttempts = 3;
|
2012-09-03 12:48:20 +02:00
|
|
|
const int Service::DefaultCheckInterval = 5 * 60;
|
2013-01-28 09:48:20 +01:00
|
|
|
const int Service::MinCheckInterval = 5;
|
2012-09-03 12:48:20 +02:00
|
|
|
const int Service::CheckIntervalDivisor = 5;
|
|
|
|
|
2012-07-27 16:05:02 +02:00
|
|
|
boost::signal<void (const Service::Ptr&, const CheckResultMessage&)> Service::OnCheckResultReceived;
|
2012-08-03 23:03:58 +02:00
|
|
|
boost::signal<void (const Service::Ptr&, const String&)> Service::OnCheckerChanged;
|
2013-01-22 12:44:23 +01:00
|
|
|
boost::signal<void (const Service::Ptr&, const Value&)> Service::OnNextCheckChanged;
|
2012-07-09 17:07:20 +02:00
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
Service::Service(const Dictionary::Ptr& serializedObject)
|
|
|
|
: DynamicObject(serializedObject)
|
2013-01-24 13:21:35 +01:00
|
|
|
{
|
|
|
|
ServiceGroup::InvalidateMembersCache();
|
2013-01-24 15:10:17 +01:00
|
|
|
Host::InvalidateServicesCache();
|
2013-01-29 14:19:54 +01:00
|
|
|
DowntimeProcessor::InvalidateDowntimeCache();
|
2013-01-24 13:21:35 +01:00
|
|
|
}
|
2012-08-02 09:38:08 +02:00
|
|
|
|
2013-01-24 15:01:06 +01:00
|
|
|
Service::~Service(void)
|
|
|
|
{
|
|
|
|
ServiceGroup::InvalidateMembersCache();
|
2013-01-24 15:10:17 +01:00
|
|
|
Host::InvalidateServicesCache();
|
2013-01-29 14:19:54 +01:00
|
|
|
DowntimeProcessor::InvalidateDowntimeCache();
|
2013-01-24 15:01:06 +01:00
|
|
|
}
|
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
String Service::GetAlias(void) const
|
|
|
|
{
|
2012-08-03 13:19:55 +02:00
|
|
|
String value = Get("alias");
|
2012-08-02 09:38:08 +02:00
|
|
|
|
2012-08-03 13:19:55 +02:00
|
|
|
if (!value.IsEmpty())
|
2012-06-13 13:42:55 +02:00
|
|
|
return value;
|
|
|
|
|
|
|
|
return GetName();
|
|
|
|
}
|
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
bool Service::Exists(const String& name)
|
2012-06-30 13:39:55 +02:00
|
|
|
{
|
2012-07-30 10:17:29 +02:00
|
|
|
return (DynamicObject::GetObject("Service", name));
|
2012-06-30 13:39:55 +02:00
|
|
|
}
|
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
Service::Ptr Service::GetByName(const String& name)
|
2012-06-27 18:43:34 +02:00
|
|
|
{
|
2012-07-30 10:17:29 +02:00
|
|
|
DynamicObject::Ptr configObject = DynamicObject::GetObject("Service", name);
|
2012-06-27 18:43:34 +02:00
|
|
|
|
|
|
|
if (!configObject)
|
2012-07-17 20:41:06 +02:00
|
|
|
throw_exception(invalid_argument("Service '" + name + "' does not exist."));
|
2012-06-27 18:43:34 +02:00
|
|
|
|
2012-07-27 16:05:02 +02:00
|
|
|
return dynamic_pointer_cast<Service>(configObject);
|
2012-06-27 18:43:34 +02:00
|
|
|
}
|
|
|
|
|
2012-07-27 16:05:02 +02:00
|
|
|
Host::Ptr Service::GetHost(void) const
|
2012-06-13 13:42:55 +02:00
|
|
|
{
|
2012-08-03 13:19:55 +02:00
|
|
|
String hostname = Get("host_name");
|
|
|
|
|
|
|
|
if (hostname.IsEmpty())
|
2012-07-17 20:41:06 +02:00
|
|
|
throw_exception(runtime_error("Service object is missing the 'host_name' property."));
|
2012-06-13 13:42:55 +02:00
|
|
|
|
|
|
|
return Host::GetByName(hostname);
|
|
|
|
}
|
|
|
|
|
|
|
|
Dictionary::Ptr Service::GetMacros(void) const
|
|
|
|
{
|
2012-08-03 13:19:55 +02:00
|
|
|
return Get("macros");
|
2012-06-13 13:42:55 +02:00
|
|
|
}
|
|
|
|
|
2013-01-29 14:19:54 +01:00
|
|
|
Dictionary::Ptr Service::GetDowntimes(void) const
|
|
|
|
{
|
2013-01-30 13:02:20 +01:00
|
|
|
DowntimeProcessor::ValidateDowntimeCache();
|
|
|
|
|
2013-01-29 14:19:54 +01:00
|
|
|
return Get("downtimes");
|
|
|
|
}
|
|
|
|
|
2013-01-29 16:29:09 +01:00
|
|
|
Dictionary::Ptr Service::GetComments(void) const
|
|
|
|
{
|
2013-01-30 13:02:20 +01:00
|
|
|
CommentProcessor::ValidateCommentCache();
|
|
|
|
|
2013-01-29 16:29:09 +01:00
|
|
|
return Get("comments");
|
|
|
|
}
|
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
String Service::GetCheckCommand(void) const
|
2012-06-13 13:42:55 +02:00
|
|
|
{
|
2012-08-03 13:19:55 +02:00
|
|
|
return Get("check_command");
|
2012-06-13 13:42:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
long Service::GetMaxCheckAttempts(void) const
|
|
|
|
{
|
2012-08-03 13:19:55 +02:00
|
|
|
Value value = Get("max_check_attempts");
|
|
|
|
|
|
|
|
if (value.IsEmpty())
|
2012-09-03 12:48:20 +02:00
|
|
|
return DefaultMaxCheckAttempts;
|
2012-08-03 13:19:55 +02:00
|
|
|
|
2012-06-13 13:42:55 +02:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
long Service::GetCheckInterval(void) const
|
|
|
|
{
|
2012-08-03 13:19:55 +02:00
|
|
|
Value value = Get("check_interval");
|
|
|
|
|
|
|
|
if (value.IsEmpty())
|
2012-09-03 12:48:20 +02:00
|
|
|
return DefaultCheckInterval;
|
2012-06-18 02:03:24 +02:00
|
|
|
|
2012-09-03 12:48:20 +02:00
|
|
|
if (value < MinCheckInterval)
|
|
|
|
value = MinCheckInterval;
|
2012-06-18 02:03:24 +02:00
|
|
|
|
2012-06-13 13:42:55 +02:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
long Service::GetRetryInterval(void) const
|
|
|
|
{
|
2012-08-03 13:19:55 +02:00
|
|
|
Value value = Get("retry_interval");
|
|
|
|
|
|
|
|
if (value.IsEmpty())
|
2012-09-03 12:48:20 +02:00
|
|
|
return GetCheckInterval() / CheckIntervalDivisor;
|
2012-06-27 18:43:34 +02:00
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
Dictionary::Ptr Service::GetDependencies(void) const
|
|
|
|
{
|
2012-08-03 13:19:55 +02:00
|
|
|
return Get("dependencies");
|
2012-06-13 13:42:55 +02:00
|
|
|
}
|
2012-06-14 11:18:20 +02:00
|
|
|
|
2012-07-09 10:09:53 +02:00
|
|
|
void Service::GetDependenciesRecursive(const Dictionary::Ptr& result) const {
|
|
|
|
assert(result);
|
|
|
|
|
|
|
|
Dictionary::Ptr dependencies = GetDependencies();
|
|
|
|
|
|
|
|
if (!dependencies)
|
|
|
|
return;
|
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
Value dependency;
|
2012-07-17 19:19:03 +02:00
|
|
|
BOOST_FOREACH(tie(tuples::ignore, dependency), dependencies) {
|
2012-07-16 22:00:50 +02:00
|
|
|
if (result->Contains(dependency))
|
2012-07-09 10:09:53 +02:00
|
|
|
continue;
|
|
|
|
|
2012-07-16 22:00:50 +02:00
|
|
|
result->Set(dependency, dependency);
|
2012-07-09 10:09:53 +02:00
|
|
|
|
2012-07-27 16:05:02 +02:00
|
|
|
Service::Ptr service = Service::GetByName(dependency);
|
|
|
|
service->GetDependenciesRecursive(result);
|
2012-07-09 10:09:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-30 13:39:55 +02:00
|
|
|
Dictionary::Ptr Service::GetGroups(void) const
|
|
|
|
{
|
2012-08-03 13:19:55 +02:00
|
|
|
return Get("servicegroups");
|
2012-06-30 13:39:55 +02:00
|
|
|
}
|
|
|
|
|
2012-07-02 16:19:43 +02:00
|
|
|
Dictionary::Ptr Service::GetCheckers(void) const
|
|
|
|
{
|
2012-08-03 13:19:55 +02:00
|
|
|
return Get("checkers");
|
2012-07-02 16:19:43 +02:00
|
|
|
}
|
|
|
|
|
2012-07-03 15:26:58 +02:00
|
|
|
bool Service::IsReachable(void) const
|
2012-07-03 14:18:46 +02:00
|
|
|
{
|
2012-07-09 10:09:53 +02:00
|
|
|
Dictionary::Ptr dependencies = boost::make_shared<Dictionary>();
|
|
|
|
GetDependenciesRecursive(dependencies);
|
2012-07-03 14:18:46 +02:00
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
Value dependency;
|
2012-07-17 19:19:03 +02:00
|
|
|
BOOST_FOREACH(tie(tuples::ignore, dependency), dependencies) {
|
2012-07-27 16:05:02 +02:00
|
|
|
Service::Ptr service = Service::GetByName(dependency);
|
2012-07-09 10:09:53 +02:00
|
|
|
|
|
|
|
/* ignore ourselves */
|
2012-07-27 16:05:02 +02:00
|
|
|
if (service->GetName() == GetName())
|
2012-07-09 10:09:53 +02:00
|
|
|
continue;
|
|
|
|
|
2012-07-09 12:44:31 +02:00
|
|
|
/* ignore pending services */
|
2012-08-02 09:38:08 +02:00
|
|
|
if (!service->GetLastCheckResult())
|
2012-07-09 12:44:31 +02:00
|
|
|
continue;
|
|
|
|
|
2012-07-09 17:03:24 +02:00
|
|
|
/* ignore soft states */
|
2012-07-27 16:05:02 +02:00
|
|
|
if (service->GetStateType() == StateTypeSoft)
|
2012-07-09 17:03:24 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
/* ignore services states OK and Warning */
|
2012-07-27 16:05:02 +02:00
|
|
|
if (service->GetState() == StateOK ||
|
|
|
|
service->GetState() == StateWarning)
|
2012-07-09 17:03:24 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
return false;
|
2012-07-03 14:18:46 +02:00
|
|
|
}
|
2012-07-09 17:03:24 +02:00
|
|
|
|
2012-07-03 15:20:44 +02:00
|
|
|
return true;
|
2012-07-03 14:18:46 +02:00
|
|
|
}
|
|
|
|
|
2013-01-29 14:19:54 +01:00
|
|
|
bool Service::IsInDowntime(void) const
|
|
|
|
{
|
|
|
|
Dictionary::Ptr downtimes = GetDowntimes();
|
|
|
|
|
|
|
|
if (!downtimes)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
Dictionary::Ptr downtime;
|
|
|
|
BOOST_FOREACH(tie(tuples::ignore, downtime), downtimes) {
|
|
|
|
if (DowntimeProcessor::IsDowntimeActive(downtime))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-07-25 12:59:17 +02:00
|
|
|
void Service::SetSchedulingOffset(long offset)
|
2012-06-14 11:18:20 +02:00
|
|
|
{
|
2012-08-03 13:19:55 +02:00
|
|
|
Set("scheduling_offset", offset);
|
2012-06-14 11:18:20 +02:00
|
|
|
}
|
|
|
|
|
2012-07-25 12:59:17 +02:00
|
|
|
long Service::GetSchedulingOffset(void)
|
2012-06-14 11:18:20 +02:00
|
|
|
{
|
2012-08-03 13:19:55 +02:00
|
|
|
Value value = Get("scheduling_offset");
|
|
|
|
|
|
|
|
if (value.IsEmpty()) {
|
2012-07-25 12:59:17 +02:00
|
|
|
value = rand();
|
|
|
|
SetSchedulingOffset(value);
|
|
|
|
}
|
2012-08-03 13:19:55 +02:00
|
|
|
|
2012-07-25 12:59:17 +02:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2013-01-23 15:25:00 +01:00
|
|
|
void Service::SetFirstCheck(bool first)
|
|
|
|
{
|
|
|
|
Set("first_check", first ? 1 : 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Service::GetFirstCheck(void) const
|
|
|
|
{
|
|
|
|
Value value = Get("first_check");
|
|
|
|
|
|
|
|
if (value.IsEmpty())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return static_cast<long>(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-25 12:59:17 +02:00
|
|
|
void Service::SetNextCheck(double nextCheck)
|
|
|
|
{
|
2012-08-03 13:19:55 +02:00
|
|
|
Set("next_check", nextCheck);
|
2012-07-25 12:59:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
double Service::GetNextCheck(void)
|
|
|
|
{
|
2012-08-03 13:19:55 +02:00
|
|
|
Value value = Get("next_check");
|
|
|
|
|
|
|
|
if (value.IsEmpty()) {
|
2012-07-25 12:59:17 +02:00
|
|
|
UpdateNextCheck();
|
2012-08-02 09:38:08 +02:00
|
|
|
|
2012-08-03 13:19:55 +02:00
|
|
|
value = Get("next_check");
|
|
|
|
|
|
|
|
if (value.IsEmpty())
|
2012-08-02 09:38:08 +02:00
|
|
|
throw_exception(runtime_error("Failed to schedule next check."));
|
2012-06-20 15:33:38 +02:00
|
|
|
}
|
2012-08-03 13:19:55 +02:00
|
|
|
|
2012-06-20 15:33:38 +02:00
|
|
|
return value;
|
2012-06-14 11:18:20 +02:00
|
|
|
}
|
2012-06-16 20:44:24 +02:00
|
|
|
|
2012-06-27 18:43:34 +02:00
|
|
|
void Service::UpdateNextCheck(void)
|
|
|
|
{
|
2012-07-25 12:59:17 +02:00
|
|
|
double interval;
|
2012-06-27 18:43:34 +02:00
|
|
|
|
|
|
|
if (GetStateType() == StateTypeSoft)
|
2012-07-17 19:10:14 +02:00
|
|
|
interval = GetRetryInterval();
|
2012-06-27 18:43:34 +02:00
|
|
|
else
|
2012-07-17 19:10:14 +02:00
|
|
|
interval = GetCheckInterval();
|
|
|
|
|
2012-07-25 12:59:17 +02:00
|
|
|
double now = Utility::GetTime();
|
|
|
|
double adj = fmod(now + GetSchedulingOffset(), interval);
|
|
|
|
SetNextCheck(now - adj + interval);
|
2012-06-27 18:43:34 +02:00
|
|
|
}
|
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
void Service::SetChecker(const String& checker)
|
2012-06-16 20:44:24 +02:00
|
|
|
{
|
2012-08-03 13:19:55 +02:00
|
|
|
Set("checker", checker);
|
2012-06-16 20:44:24 +02:00
|
|
|
}
|
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
String Service::GetChecker(void) const
|
2012-06-16 20:44:24 +02:00
|
|
|
{
|
2012-08-03 13:19:55 +02:00
|
|
|
return Get("checker");
|
2012-06-16 20:44:24 +02:00
|
|
|
}
|
2012-06-17 22:46:40 +02:00
|
|
|
|
2012-06-25 15:42:46 +02:00
|
|
|
void Service::SetCurrentCheckAttempt(long attempt)
|
|
|
|
{
|
2012-08-03 13:19:55 +02:00
|
|
|
Set("check_attempt", attempt);
|
2012-06-25 15:42:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
long Service::GetCurrentCheckAttempt(void) const
|
|
|
|
{
|
2012-08-03 13:19:55 +02:00
|
|
|
Value value = Get("check_attempt");
|
|
|
|
|
|
|
|
if (value.IsEmpty())
|
|
|
|
return 1;
|
|
|
|
|
2012-06-25 15:42:46 +02:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Service::SetState(ServiceState state)
|
|
|
|
{
|
2012-08-03 13:19:55 +02:00
|
|
|
Set("state", static_cast<long>(state));
|
2012-06-25 15:42:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ServiceState Service::GetState(void) const
|
|
|
|
{
|
2012-08-03 13:19:55 +02:00
|
|
|
Value value = Get("state");
|
|
|
|
|
|
|
|
if (value.IsEmpty())
|
|
|
|
return StateUnknown;
|
|
|
|
|
|
|
|
int ivalue = static_cast<int>(value);
|
|
|
|
return static_cast<ServiceState>(ivalue);
|
2012-06-25 15:42:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Service::SetStateType(ServiceStateType type)
|
|
|
|
{
|
2012-08-03 13:19:55 +02:00
|
|
|
Set("state_type", static_cast<long>(type));
|
2012-06-25 15:42:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ServiceStateType Service::GetStateType(void) const
|
|
|
|
{
|
2012-08-03 13:19:55 +02:00
|
|
|
Value value = Get("state_type");
|
|
|
|
|
|
|
|
if (value.IsEmpty())
|
2013-01-28 13:38:18 +01:00
|
|
|
return StateTypeSoft;
|
2012-08-03 13:19:55 +02:00
|
|
|
|
|
|
|
int ivalue = static_cast<int>(value);
|
|
|
|
return static_cast<ServiceStateType>(ivalue);
|
2012-06-25 15:42:46 +02:00
|
|
|
}
|
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
void Service::SetLastCheckResult(const Dictionary::Ptr& result)
|
2012-06-27 18:43:34 +02:00
|
|
|
{
|
2012-08-03 13:19:55 +02:00
|
|
|
Set("last_result", result);
|
2012-07-03 14:18:46 +02:00
|
|
|
}
|
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
Dictionary::Ptr Service::GetLastCheckResult(void) const
|
2012-07-03 14:18:46 +02:00
|
|
|
{
|
2012-08-03 13:19:55 +02:00
|
|
|
return Get("last_result");
|
2012-06-27 18:43:34 +02:00
|
|
|
}
|
|
|
|
|
2012-07-25 12:59:17 +02:00
|
|
|
void Service::SetLastStateChange(double ts)
|
2012-06-27 18:43:34 +02:00
|
|
|
{
|
2012-08-03 13:19:55 +02:00
|
|
|
Set("last_state_change", static_cast<long>(ts));
|
2012-06-27 18:43:34 +02:00
|
|
|
}
|
|
|
|
|
2012-07-25 12:59:17 +02:00
|
|
|
double Service::GetLastStateChange(void) const
|
2012-06-27 18:43:34 +02:00
|
|
|
{
|
2012-08-03 13:19:55 +02:00
|
|
|
Value value = Get("last_state_change");
|
|
|
|
|
|
|
|
if (value.IsEmpty())
|
|
|
|
return IcingaApplication::GetInstance()->GetStartTime();
|
|
|
|
|
2012-06-27 18:43:34 +02:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2012-07-25 12:59:17 +02:00
|
|
|
void Service::SetLastHardStateChange(double ts)
|
2012-06-27 18:43:34 +02:00
|
|
|
{
|
2012-08-03 13:19:55 +02:00
|
|
|
Set("last_hard_state_change", ts);
|
2012-06-27 18:43:34 +02:00
|
|
|
}
|
|
|
|
|
2012-07-25 12:59:17 +02:00
|
|
|
double Service::GetLastHardStateChange(void) const
|
2012-06-27 18:43:34 +02:00
|
|
|
{
|
2012-08-03 13:19:55 +02:00
|
|
|
Value value = Get("last_hard_state_change");
|
|
|
|
|
|
|
|
if (value.IsEmpty())
|
2012-06-27 23:38:50 +02:00
|
|
|
value = IcingaApplication::GetInstance()->GetStartTime();
|
2012-08-03 13:19:55 +02:00
|
|
|
|
2012-06-27 18:43:34 +02:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2013-01-24 23:10:07 +01:00
|
|
|
bool Service::GetEnableActiveChecks(void) const
|
2013-01-22 16:01:08 +01:00
|
|
|
{
|
2013-01-24 23:10:07 +01:00
|
|
|
Value value = Get("enable_active_checks");
|
2013-01-22 16:01:08 +01:00
|
|
|
|
|
|
|
if (value.IsEmpty())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return static_cast<bool>(value);
|
|
|
|
}
|
|
|
|
|
2013-01-24 23:10:07 +01:00
|
|
|
void Service::SetEnablePassiveChecks(bool enabled)
|
2013-01-22 16:01:08 +01:00
|
|
|
{
|
2013-01-24 23:10:07 +01:00
|
|
|
Set("enable_passive_checks", enabled ? 1 : 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Service::GetEnablePassiveChecks(void) const
|
|
|
|
{
|
|
|
|
Value value = Get("enable_passive_checks");
|
|
|
|
|
|
|
|
if (value.IsEmpty())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return static_cast<bool>(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Service::SetEnableActiveChecks(bool enabled)
|
|
|
|
{
|
|
|
|
Set("enable_active_checks", enabled ? 1 : 0);
|
2013-01-22 16:01:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Service::GetForceNextCheck(void) const
|
|
|
|
{
|
|
|
|
Value value = Get("force_next_check");
|
|
|
|
|
|
|
|
if (value.IsEmpty())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return static_cast<bool>(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Service::SetForceNextCheck(bool forced)
|
|
|
|
{
|
|
|
|
Set("force_next_check", forced ? 1 : 0);
|
|
|
|
}
|
|
|
|
|
2013-01-23 13:21:07 +01:00
|
|
|
AcknowledgementType Service::GetAcknowledgement(void)
|
|
|
|
{
|
|
|
|
Value value = Get("acknowledgement");
|
|
|
|
|
|
|
|
if (value.IsEmpty())
|
|
|
|
return AcknowledgementNone;
|
|
|
|
|
|
|
|
int ivalue = static_cast<int>(value);
|
|
|
|
AcknowledgementType avalue = static_cast<AcknowledgementType>(ivalue);
|
|
|
|
|
|
|
|
if (avalue != AcknowledgementNone) {
|
|
|
|
double expiry = GetAcknowledgementExpiry();
|
|
|
|
|
|
|
|
if (expiry != 0 && expiry < Utility::GetTime()) {
|
|
|
|
avalue = AcknowledgementNone;
|
2013-01-23 13:46:35 +01:00
|
|
|
SetAcknowledgement(avalue);
|
|
|
|
SetAcknowledgementExpiry(0);
|
2013-01-23 13:21:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return avalue;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Service::SetAcknowledgement(AcknowledgementType acknowledgement)
|
|
|
|
{
|
|
|
|
Set("acknowledgement", static_cast<long>(acknowledgement));
|
|
|
|
}
|
|
|
|
|
|
|
|
double Service::GetAcknowledgementExpiry(void) const
|
|
|
|
{
|
|
|
|
Value value = Get("acknowledgement_expiry");
|
|
|
|
|
|
|
|
if (value.IsEmpty())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return static_cast<double>(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Service::SetAcknowledgementExpiry(double timestamp)
|
|
|
|
{
|
|
|
|
Set("acknowledgement_expiry", timestamp);
|
|
|
|
}
|
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
void Service::ApplyCheckResult(const Dictionary::Ptr& cr)
|
2012-06-25 14:13:24 +02:00
|
|
|
{
|
2012-07-24 15:38:30 +02:00
|
|
|
ServiceState old_state = GetState();
|
|
|
|
ServiceStateType old_stateType = GetStateType();
|
|
|
|
|
2012-06-25 15:42:46 +02:00
|
|
|
long attempt = GetCurrentCheckAttempt();
|
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
if (cr->Get("state") == StateOK) {
|
2012-06-25 15:54:50 +02:00
|
|
|
if (GetState() == StateOK)
|
|
|
|
SetStateType(StateTypeHard);
|
2012-06-25 15:42:46 +02:00
|
|
|
|
2012-06-25 15:54:50 +02:00
|
|
|
attempt = 1;
|
|
|
|
} else {
|
2012-06-25 15:42:46 +02:00
|
|
|
if (attempt >= GetMaxCheckAttempts()) {
|
|
|
|
SetStateType(StateTypeHard);
|
2012-06-25 15:54:50 +02:00
|
|
|
attempt = 1;
|
|
|
|
} else if (GetStateType() == StateTypeSoft || GetState() == StateOK) {
|
2012-06-25 15:42:46 +02:00
|
|
|
SetStateType(StateTypeSoft);
|
2012-06-25 15:54:50 +02:00
|
|
|
attempt++;
|
2012-06-25 15:42:46 +02:00
|
|
|
}
|
|
|
|
}
|
2012-06-25 14:13:24 +02:00
|
|
|
|
2012-06-25 15:54:50 +02:00
|
|
|
SetCurrentCheckAttempt(attempt);
|
2012-08-02 09:38:08 +02:00
|
|
|
|
|
|
|
int state = cr->Get("state");
|
|
|
|
SetState(static_cast<ServiceState>(state));
|
2012-07-24 15:38:30 +02:00
|
|
|
|
|
|
|
SetLastCheckResult(cr);
|
|
|
|
|
|
|
|
if (old_state != GetState()) {
|
2012-07-25 12:59:17 +02:00
|
|
|
double now = Utility::GetTime();
|
|
|
|
|
2012-07-24 15:38:30 +02:00
|
|
|
SetLastStateChange(now);
|
|
|
|
|
|
|
|
if (old_stateType != GetStateType())
|
|
|
|
SetLastHardStateChange(now);
|
2013-01-23 13:21:07 +01:00
|
|
|
|
|
|
|
/* remove acknowledgements */
|
|
|
|
if (GetAcknowledgement() == AcknowledgementNormal ||
|
|
|
|
(GetAcknowledgement() == AcknowledgementSticky && GetStateType() == StateTypeHard && GetState() == StateOK)) {
|
|
|
|
SetAcknowledgement(AcknowledgementNone);
|
|
|
|
SetAcknowledgementExpiry(0);
|
|
|
|
}
|
2013-01-25 16:21:21 +01:00
|
|
|
|
|
|
|
/* reschedule dependencies */
|
2013-01-27 11:49:23 +01:00
|
|
|
Dictionary::Ptr dependencies = GetDependencies();
|
2013-01-25 16:21:21 +01:00
|
|
|
|
2013-01-27 11:49:23 +01:00
|
|
|
if (dependencies) {
|
|
|
|
String svc;
|
|
|
|
BOOST_FOREACH(tie(tuples::ignore, svc), dependencies) {
|
|
|
|
if (!Service::Exists(svc))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Service::Ptr service = Service::GetByName(svc);
|
|
|
|
service->SetNextCheck(Utility::GetTime());
|
|
|
|
}
|
2013-01-25 16:21:21 +01:00
|
|
|
}
|
2012-07-24 15:38:30 +02:00
|
|
|
}
|
2013-01-31 13:57:14 +01:00
|
|
|
|
|
|
|
if (GetState() != StateOK)
|
|
|
|
DowntimeProcessor::TriggerDowntimes(GetSelf());
|
2012-06-25 14:13:24 +02:00
|
|
|
}
|
2012-06-25 15:42:46 +02:00
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
ServiceState Service::StateFromString(const String& state)
|
2012-06-27 18:43:34 +02:00
|
|
|
{
|
2012-09-07 09:47:58 +02:00
|
|
|
if (state == "ok")
|
|
|
|
return StateOK;
|
|
|
|
else if (state == "warning")
|
|
|
|
return StateWarning;
|
|
|
|
else if (state == "critical")
|
|
|
|
return StateCritical;
|
|
|
|
else if (state == "uncheckable")
|
|
|
|
return StateUncheckable;
|
2012-06-27 18:43:34 +02:00
|
|
|
else
|
2012-09-07 09:47:58 +02:00
|
|
|
return StateUnknown;
|
2012-06-27 18:43:34 +02:00
|
|
|
}
|
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
String Service::StateToString(ServiceState state)
|
2012-06-27 18:43:34 +02:00
|
|
|
{
|
|
|
|
switch (state) {
|
|
|
|
case StateOK:
|
|
|
|
return "ok";
|
|
|
|
case StateWarning:
|
|
|
|
return "warning";
|
|
|
|
case StateCritical:
|
|
|
|
return "critical";
|
|
|
|
case StateUncheckable:
|
|
|
|
return "uncheckable";
|
|
|
|
case StateUnknown:
|
|
|
|
default:
|
|
|
|
return "unknown";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
ServiceStateType Service::StateTypeFromString(const String& type)
|
2012-06-27 18:43:34 +02:00
|
|
|
{
|
|
|
|
if (type == "soft")
|
|
|
|
return StateTypeSoft;
|
|
|
|
else
|
|
|
|
return StateTypeHard;
|
|
|
|
}
|
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
String Service::StateTypeToString(ServiceStateType type)
|
2012-06-27 18:43:34 +02:00
|
|
|
{
|
|
|
|
if (type == StateTypeSoft)
|
|
|
|
return "soft";
|
|
|
|
else
|
|
|
|
return "hard";
|
|
|
|
}
|
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
bool Service::IsAllowedChecker(const String& checker) const
|
2012-06-27 18:43:34 +02:00
|
|
|
{
|
2012-07-02 16:19:43 +02:00
|
|
|
Dictionary::Ptr checkers = GetCheckers();
|
|
|
|
|
|
|
|
if (!checkers)
|
|
|
|
return true;
|
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
Value pattern;
|
2012-07-17 19:19:03 +02:00
|
|
|
BOOST_FOREACH(tie(tuples::ignore, pattern), checkers) {
|
2012-07-02 16:19:43 +02:00
|
|
|
if (Utility::Match(pattern, checker))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2012-06-27 18:43:34 +02:00
|
|
|
}
|
2012-07-09 10:09:53 +02:00
|
|
|
|
2012-07-27 16:05:02 +02:00
|
|
|
Dictionary::Ptr Service::ResolveDependencies(const Host::Ptr& host, const Dictionary::Ptr& dependencies)
|
2012-07-09 10:09:53 +02:00
|
|
|
{
|
2012-08-03 13:19:55 +02:00
|
|
|
Dictionary::Ptr services = host->Get("services");
|
2012-07-09 10:09:53 +02:00
|
|
|
|
|
|
|
Dictionary::Ptr result = boost::make_shared<Dictionary>();
|
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
Value dependency;
|
2012-07-17 19:19:03 +02:00
|
|
|
BOOST_FOREACH(tie(tuples::ignore, dependency), dependencies) {
|
2012-08-02 09:38:08 +02:00
|
|
|
String name;
|
2012-07-09 13:27:02 +02:00
|
|
|
|
2012-07-16 22:00:50 +02:00
|
|
|
if (services && services->Contains(dependency))
|
2012-08-02 09:38:08 +02:00
|
|
|
name = host->GetName() + "-" + static_cast<String>(dependency);
|
2012-07-09 10:09:53 +02:00
|
|
|
else
|
2012-08-02 09:38:08 +02:00
|
|
|
name = static_cast<String>(dependency);
|
2012-07-09 13:27:02 +02:00
|
|
|
|
2012-07-09 16:19:56 +02:00
|
|
|
result->Set(name, name);
|
2012-07-09 10:09:53 +02:00
|
|
|
}
|
|
|
|
|
2012-07-14 15:59:59 +02:00
|
|
|
return result;
|
2012-07-09 10:09:53 +02:00
|
|
|
}
|
2012-08-03 23:03:58 +02:00
|
|
|
|
|
|
|
void Service::OnAttributeChanged(const String& name, const Value& oldValue)
|
|
|
|
{
|
|
|
|
if (name == "checker")
|
|
|
|
OnCheckerChanged(GetSelf(), oldValue);
|
2013-01-22 12:44:23 +01:00
|
|
|
else if (name == "next_check")
|
|
|
|
OnNextCheckChanged(GetSelf(), oldValue);
|
2013-01-24 13:21:35 +01:00
|
|
|
else if (name == "servicegroups")
|
|
|
|
ServiceGroup::InvalidateMembersCache();
|
2013-01-24 15:10:17 +01:00
|
|
|
else if (name == "host_name")
|
|
|
|
Host::InvalidateServicesCache();
|
2013-01-29 14:19:54 +01:00
|
|
|
else if (name == "downtimes")
|
|
|
|
DowntimeProcessor::InvalidateDowntimeCache();
|
2012-08-03 23:03:58 +02:00
|
|
|
}
|
2013-01-22 11:07:09 +01:00
|
|
|
|
|
|
|
void Service::BeginExecuteCheck(const function<void (void)>& callback)
|
|
|
|
{
|
|
|
|
/* don't run another check if there is one pending */
|
2013-01-22 11:11:11 +01:00
|
|
|
if (!Get("current_task").IsEmpty()) {
|
2013-01-22 11:07:09 +01:00
|
|
|
/* we need to call the callback anyway */
|
|
|
|
callback();
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* keep track of scheduling info in case the check type doesn't provide its own information */
|
|
|
|
Dictionary::Ptr scheduleInfo = boost::make_shared<Dictionary>();
|
|
|
|
scheduleInfo->Set("schedule_start", GetNextCheck());
|
|
|
|
scheduleInfo->Set("execution_start", Utility::GetTime());
|
|
|
|
|
2013-02-01 19:27:36 +01:00
|
|
|
try {
|
|
|
|
vector<Value> arguments;
|
|
|
|
arguments.push_back(static_cast<Service::Ptr>(GetSelf()));
|
|
|
|
ScriptTask::Ptr task;
|
|
|
|
task = InvokeMethod("check", arguments, boost::bind(&Service::CheckCompletedHandler, this, scheduleInfo, _1, callback));
|
|
|
|
Set("current_task", task);
|
|
|
|
} catch (...) {
|
|
|
|
/* something went wrong while setting up the method call -
|
|
|
|
* reschedule the service and call the callback anyway. */
|
|
|
|
|
|
|
|
UpdateNextCheck();
|
2013-01-22 11:07:09 +01:00
|
|
|
|
2013-02-01 19:27:36 +01:00
|
|
|
callback();
|
|
|
|
|
|
|
|
throw;
|
|
|
|
}
|
2013-01-22 11:07:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Service::CheckCompletedHandler(const Dictionary::Ptr& scheduleInfo,
|
|
|
|
const ScriptTask::Ptr& task, const function<void (void)>& callback)
|
|
|
|
{
|
|
|
|
Set("current_task", Empty);
|
|
|
|
|
|
|
|
scheduleInfo->Set("execution_end", Utility::GetTime());
|
|
|
|
scheduleInfo->Set("schedule_end", Utility::GetTime());
|
|
|
|
|
2013-02-03 16:32:31 +01:00
|
|
|
Dictionary::Ptr result;
|
|
|
|
|
2013-01-22 11:07:09 +01:00
|
|
|
try {
|
|
|
|
Value vresult = task->GetResult();
|
|
|
|
|
2013-02-03 16:32:31 +01:00
|
|
|
if (vresult.IsObjectType<Dictionary>())
|
|
|
|
result = vresult;
|
|
|
|
} catch (const exception& ex) {
|
|
|
|
stringstream msgbuf;
|
|
|
|
msgbuf << "Exception occured during check for service '"
|
|
|
|
<< GetName() << "': " << ex.what();
|
|
|
|
String message = msgbuf.str();
|
2013-01-22 11:07:09 +01:00
|
|
|
|
2013-02-03 16:32:31 +01:00
|
|
|
Logger::Write(LogWarning, "checker", message);
|
2013-01-22 11:07:09 +01:00
|
|
|
|
2013-02-03 16:32:31 +01:00
|
|
|
result = boost::make_shared<Dictionary>();
|
|
|
|
result->Set("state", StateUnknown);
|
|
|
|
result->Set("output", message);
|
|
|
|
}
|
2013-01-22 11:07:09 +01:00
|
|
|
|
2013-02-03 16:32:31 +01:00
|
|
|
if (result) {
|
|
|
|
if (!result->Contains("schedule_start"))
|
|
|
|
result->Set("schedule_start", scheduleInfo->Get("schedule_start"));
|
2013-01-22 11:07:09 +01:00
|
|
|
|
2013-02-03 16:32:31 +01:00
|
|
|
if (!result->Contains("schedule_end"))
|
|
|
|
result->Set("schedule_end", scheduleInfo->Get("schedule_end"));
|
2013-01-22 11:07:09 +01:00
|
|
|
|
2013-02-03 16:32:31 +01:00
|
|
|
if (!result->Contains("execution_start"))
|
|
|
|
result->Set("execution_start", scheduleInfo->Get("execution_start"));
|
2013-01-28 09:01:47 +01:00
|
|
|
|
2013-02-03 16:32:31 +01:00
|
|
|
if (!result->Contains("execution_end"))
|
|
|
|
result->Set("execution_end", scheduleInfo->Get("execution_end"));
|
|
|
|
|
|
|
|
if (!result->Contains("active"))
|
|
|
|
result->Set("active", 1);
|
|
|
|
|
|
|
|
ProcessCheckResult(result);
|
2013-01-22 11:07:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* figure out when the next check is for this service; the call to
|
|
|
|
* ApplyCheckResult() should've already done this but lets do it again
|
|
|
|
* just in case there was no check result. */
|
|
|
|
UpdateNextCheck();
|
|
|
|
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Service::ProcessCheckResult(const Dictionary::Ptr& cr)
|
|
|
|
{
|
|
|
|
ApplyCheckResult(cr);
|
|
|
|
|
2013-01-28 09:01:47 +01:00
|
|
|
/* flush the current transaction so other instances see the service's
|
2013-01-31 16:49:40 +01:00
|
|
|
* new state when they receive the CheckResult message */
|
2013-01-28 09:01:47 +01:00
|
|
|
DynamicObject::FlushTx();
|
|
|
|
|
2013-01-22 11:07:09 +01:00
|
|
|
RequestMessage rm;
|
2013-01-31 16:49:40 +01:00
|
|
|
rm.SetMethod("checker::CheckResult");
|
2013-01-22 11:07:09 +01:00
|
|
|
|
|
|
|
/* TODO: add _old_ state to message */
|
2013-01-31 16:49:40 +01:00
|
|
|
CheckResultMessage params;
|
2013-01-22 11:07:09 +01:00
|
|
|
params.SetService(GetName());
|
2013-01-31 16:49:40 +01:00
|
|
|
params.SetCheckResult(cr);
|
2013-01-22 11:07:09 +01:00
|
|
|
|
|
|
|
rm.SetParams(params);
|
|
|
|
|
|
|
|
EndpointManager::GetInstance()->SendMulticastMessage(rm);
|
|
|
|
}
|
2013-01-31 16:49:40 +01:00
|
|
|
|