Skip to content

Commit

Permalink
New feature: Allow to write passive CheckCommand to Icinga2 services
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian Bläul committed Apr 4, 2020
1 parent 4a3ad9d commit b9790d2
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/De/Uniwue/RZ/Api/Icinga2/Icinga2.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
<?php

/**
* Icinga2 Class is the main API interface for the icinga library. At the moment only reads from the server is
* possible.
* Icinga2 Class is the main API interface for this library.
*
* Created by PhpStorm.
* User: poa32kc
Expand Down Expand Up @@ -391,6 +390,7 @@ public function decodeResult(Response $response, $type)
$params = array(
$icingaRow["name"],
$icingaRow["type"],
$this,
$icingaRow["attrs"],
$icingaRow["meta"],
$icingaRow["joins"]
Expand Down
12 changes: 11 additions & 1 deletion src/De/Uniwue/RZ/Api/Icinga2/Icinga2Object/Icinga2Object.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
namespace De\Uniwue\RZ\Api\Icinga2\Icinga2Object;


use De\Uniwue\RZ\Api\Icinga2\Icinga2;

class Icinga2Object
{
/**
Expand Down Expand Up @@ -36,18 +38,26 @@ class Icinga2Object
*/
private $type;

/**
* @var \De\Uniwue\RZ\Api\Icinga2\Icinga2
*/
protected $icinga2;

/**
* Icinga2Object constructor.
*
* @param string $name The name of the given Icinga2Object
* @param string $type The type of the given Icinga2Object
* @param \De\Uniwue\RZ\Api\Icinga2\Icinga2 $icinga2
* @param array $attributes The attributes for the given Icinga2Object
* @param array $meta The meta information for the given Icinga2Object
* @param array $joins The joins for the given query result
*/
public function __construct($name, $type, $attributes = array(), $meta = array(), $joins = array())
public function __construct($name, $type, Icinga2 $icinga2, $attributes = array(), $meta = array(), $joins = array())
{
$this->name = $name;
$this->type = $type;
$this->icinga2 = $icinga2;
$this->attributes = $attributes;
$this->meta = $meta;
$this->joins = $joins;
Expand Down
48 changes: 48 additions & 0 deletions src/De/Uniwue/RZ/Api/Icinga2/Icinga2Object/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,52 @@ class Service extends Icinga2Object implements Icinga2ObjectInterface
public function getChildren()
{
}

/**
* This is useful if the service has a passive CheckCommand, i.e.
* when the service definition contains enable_active_checks = false.
*
* See
* -
* https://icinga.com/docs/icinga2/latest/doc/12-icinga2-api/#process-check-result
* - https://itgix.com/blog/post/icinga2-api-and-passive-checks/
*
* @param int $exit_status
* 0=OK, 1=WARNING, 2=CRITICAL, 3=UNKNOWN
* @param string $plugin_output
* @param string[] $performance_data
* Structure of the individual strings; sections are separated by ;
* 1. Section: Everything before the '=' is the name of the value.
* For example 'disk_space_percent'
* The first field after the '=' is meant to be the current state of
* the
* service always followed by ';'. That's the least amount of
* information required to receive performance data which can be
* interpreted by the monitoring. Additionally, you can add the unit to
* make sure the monitoring understands what the value represents.
* Examples are MB, ms or %
*
* 2. Section: warning level
* 3. Section: critical level
*
* 4. Section: theoretical minimum of the scale, usually 0
* 5. Section: maximum amount possible for this performance number.
*
* @return bool
* True if Icinga accepted the request.
*/
public function processCheckResult(int $exit_status, string $plugin_output, array $performance_data): bool
{
$request = $this->icinga2->buildRequest('v1/actions/process-check-result?service=' . $this->getName());
$attrs = $this->getAttributes();
$request->payload = json_encode([
'type' => 'Service',
'exit_status' => $exit_status,
'plugin_output' => $plugin_output,
'performance_data' => $performance_data,
]);
$response = $request->send();
return $response->code == 200;
}

}

0 comments on commit b9790d2

Please sign in to comment.