Skip to content

Commit

Permalink
Implement the "cluster-zone" check task.
Browse files Browse the repository at this point in the history
Fixes #6204
  • Loading branch information
gunnarbeutner committed May 25, 2014
1 parent f85eacb commit b959124
Show file tree
Hide file tree
Showing 8 changed files with 177 additions and 6 deletions.
27 changes: 23 additions & 4 deletions contrib/make-agent-config.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,32 @@
print " endpoints = [ \"%s\" ]" % (agent)
print "}"
print ""

print "object Host \"%s\" {" % (agent_info["zone"])
print " check_command = \"cluster-zone\""
print "}"
print ""

print "apply Dependency \"host-zone-%s\" to Host {" % (agent_info["zone"])
print " parent_host_name = \"%s\"" % (agent_info["zone"])
print " assign where host.@zone == \"%s\"" % (agent_info["zone"])
print "}"
print ""

print "apply Dependency \"service-zone-%s\" to Service {" % (agent_info["zone"])
print " parent_host_name = \"%s\"" % (agent_info["zone"])
print " assign where service.@zone == \"%s\"" % (agent_info["zone"])
print "}"
print ""

print "zone \"%s\" {" % (agent_info["zone"])

for host, services in agent_info["repository"].items():
print "object Host \"%s\" {" % (host)
print " check_command = \"dummy\""
print "}"
print ""
if host != agent_info["zone"]:
print "object Host \"%s\" {" % (host)
print " check_command = \"dummy\""
print "}"
print ""

for service in services:
print "object Service \"%s\" {" % (service)
Expand Down
10 changes: 9 additions & 1 deletion doc/6-configuring-icinga-2.md
Original file line number Diff line number Diff line change
Expand Up @@ -1678,14 +1678,22 @@ data for the current Icinga instance.

The `icinga` check command does not support any vars.

#### <a id="itl-icinga"></a> cluster
#### <a id="itl-icinga-cluster"></a> cluster

Check command for the built-in `cluster` check. This check returns performance
data for the current Icinga instance and connected endpoints.

The `cluster` check command does not support any vars.

#### <a id="itl-icinga-cluster-zone"></a> cluster-zone

Check command for the built-in `cluster-zone` check.

Cluster Attributes:

Name | Description
-------------|---------------
cluster_zone | **Optional.** The zone name. Defaults to "$host.name$".

## <a id="plugin-check-commands"></a> Plugin Check Commands

Expand Down
6 changes: 6 additions & 0 deletions itl/command-icinga.conf
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,9 @@ object CheckCommand "cluster" {
import "cluster-check-command"
}

object CheckCommand "cluster-zone" {
import "cluster-zone-check-command"

vars.cluster_zone = "$host.name$"
}

4 changes: 4 additions & 0 deletions itl/command.conf
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ template CheckCommand "cluster-check-command" {
methods.execute = "ClusterCheck"
}

template CheckCommand "cluster-zone-check-command" {
methods.execute = "ClusterZoneCheck"
}

template CheckCommand "plugin-check-command" {
methods.execute = "PluginCheck"
}
Expand Down
1 change: 1 addition & 0 deletions lib/config/config_lexer.ll
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ in { yylval->op = &AExpression::OpIn; return T_IN; }
&& { yylval->op = &AExpression::OpLogicalAnd; return T_LOGICAL_AND; }
\|\| { yylval->op = &AExpression::OpLogicalOr; return T_LOGICAL_OR; }
[a-zA-Z_][a-zA-Z0-9\-_]* { yylval->text = strdup(yytext); return T_IDENTIFIER; }
@[a-zA-Z_][a-zA-Z0-9\-_]* { yylval->text = strdup(yytext + 1); return T_IDENTIFIER; }
\<[^\>]*\> { yytext[yyleng-1] = '\0'; yylval->text = strdup(yytext + 1); return T_STRING_ANGLE; }
-?[0-9]+(\.[0-9]+)?ms { yylval->num = strtod(yytext, NULL) / 1000; return T_NUMBER; }
-?[0-9]+(\.[0-9]+)?d { yylval->num = strtod(yytext, NULL) * 60 * 60 * 24; return T_NUMBER; }
Expand Down
3 changes: 2 additions & 1 deletion lib/methods/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ else()
endif()

add_library(methods SHARED
castfuncs.cpp clusterchecktask.cpp icingachecktask.cpp nullchecktask.cpp nulleventtask.cpp
castfuncs.cpp clusterchecktask.cpp clusterzonechecktask.cpp
icingachecktask.cpp nullchecktask.cpp nulleventtask.cpp
pluginchecktask.cpp plugineventtask.cpp pluginnotificationtask.cpp
randomchecktask.cpp timeperiodtask.cpp ${WindowsSources}
)
Expand Down
88 changes: 88 additions & 0 deletions lib/methods/clusterzonechecktask.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/******************************************************************************
* 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. *
******************************************************************************/

#include "methods/clusterzonechecktask.hpp"
#include "icinga/checkcommand.hpp"
#include "icinga/macroprocessor.hpp"
#include "remote/apilistener.hpp"
#include "remote/endpoint.hpp"
#include "remote/zone.hpp"
#include "base/scriptfunction.hpp"
#include <boost/foreach.hpp>

using namespace icinga;

REGISTER_SCRIPTFUNCTION(ClusterZoneCheck, &ClusterZoneCheckTask::ScriptFunc);

void ClusterZoneCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr)
{
ApiListener::Ptr listener = ApiListener::GetInstance();

if (!listener) {
cr->SetOutput("No API listener is configured for this instance.");
cr->SetState(ServiceUnknown);
checkable->ProcessCheckResult(cr);
return;
}

CheckCommand::Ptr commandObj = checkable->GetCheckCommand();
Value raw_command = commandObj->GetCommandLine();

Host::Ptr host;
Service::Ptr service;
tie(host, service) = GetHostService(checkable);

MacroProcessor::ResolverList resolvers;
if (service)
resolvers.push_back(std::make_pair("service", service));
resolvers.push_back(std::make_pair("host", host));
resolvers.push_back(std::make_pair("command", commandObj));
resolvers.push_back(std::make_pair("icinga", IcingaApplication::GetInstance()));

String zoneName = MacroProcessor::ResolveMacros("cluster_zone", resolvers, checkable->GetLastCheckResult());

if (zoneName.IsEmpty()) {
cr->SetOutput("Zone '" + zoneName + "' does not exist.");
cr->SetState(ServiceUnknown);
checkable->ProcessCheckResult(cr);
return;
}

Zone::Ptr zone = Zone::GetByName(zoneName);

bool connected = false;

BOOST_FOREACH(const Endpoint::Ptr& endpoint, zone->GetEndpoints()) {
if (endpoint->IsConnected()) {
connected = true;
break;
}
}

if (!connected) {
cr->SetState(ServiceCritical);
cr->SetOutput("Zone '" + zoneName + "' is not connected.");
} else {
cr->SetState(ServiceOK);
cr->SetOutput("Zone '" + zoneName + "' is connected.");
}

checkable->ProcessCheckResult(cr);
}

44 changes: 44 additions & 0 deletions lib/methods/clusterzonechecktask.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/******************************************************************************
* 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. *
******************************************************************************/

#ifndef CLUSTERZONECHECKTASK_H
#define CLUSTERZONECHECKTASK_H

#include "icinga/service.hpp"

namespace icinga
{

/**
* Cluster zone check type.
*
* @ingroup methods
*/
class ClusterZoneCheckTask
{
public:
static void ScriptFunc(const Checkable::Ptr& service, const CheckResult::Ptr& cr);

private:
ClusterZoneCheckTask(void);
};

}

#endif /* CLUSTERZONECHECKTASK_H */

0 comments on commit b959124

Please sign in to comment.