icingaweb2-module-businessp.../library/Businessprocess/BusinessProcess.php

411 lines
9.8 KiB
PHP
Raw Normal View History

<?php
2014-11-30 09:56:58 -05:00
namespace Icinga\Module\Businessprocess;
use Icinga\Module\Monitoring\Backend\MonitoringBackend;
2014-12-02 05:35:21 -05:00
use Icinga\Data\Filter\Filter;
use Exception;
class BusinessProcess
{
const SOFT_STATE = 0;
2015-02-06 18:13:37 -05:00
const HARD_STATE = 1;
/**
* Monitoring backend to retrieve states from
*
* @var MonitoringBackend
*/
protected $backend;
2015-02-06 18:13:37 -05:00
/**
* Business process name
*
* @var string
*/
2014-12-01 08:07:30 -05:00
protected $name;
2015-02-06 18:13:37 -05:00
/**
* Business process title
*
* @var string
*/
protected $title;
2015-02-06 18:13:37 -05:00
/**
* State type, soft or hard
*
* @var int
*/
protected $state_type = self::HARD_STATE;
2015-02-06 18:13:37 -05:00
/**
* Warnings, usually filled at process build time
*
* @var array
*/
protected $warnings = array();
2015-02-06 18:13:37 -05:00
/**
* All used node objects
*
* @var array
*/
protected $nodes = array();
2015-02-06 18:13:37 -05:00
/**
* Root node objects
*
* @var array
*/
protected $root_nodes = array();
2015-02-06 18:13:37 -05:00
/**
* All check names { 'hostA;ping' => true, ... }
*
* @var array
*/
protected $all_checks = array();
2015-02-06 18:13:37 -05:00
/**
* All host names { 'hostA' => true, ... }
*
* @var array
*/
protected $hosts = array();
2015-02-06 18:13:37 -05:00
/**
* Whether we are in simulation mode
*
* @var boolean
*/
protected $simulationMode = false;
2015-02-06 18:13:37 -05:00
/**
* Whether we are in edit mode
*
* @var boolean
*/
protected $editMode = false;
public function __construct()
{
}
2014-12-01 08:07:30 -05:00
public function setName($name)
{
$this->name = $name;
return $this;
}
public function getName()
{
return $this->name;
}
public function setTitle($title)
{
$this->title = $title;
return $this;
}
public function getTitle()
{
return $this->title ?: $this->getName();
}
public function loadBackendByName($name)
{
$this->backend = MonitoringBackend::createBackend($name);
return $this;
}
public function setBackend(MonitoringBackend $backend)
{
$this->backend = $backend;
return $this;
}
public function getBackend()
{
if ($this->backend === null) {
$this->backend = MonitoringBackend::createBackend();
}
return $this->backend;
}
public function hasBackend()
{
return $this->backend !== null;
}
public function hasBeenChanged()
{
return false;
}
public function setSimulationMode($mode = true)
{
$this->simulationMode = (bool) $mode;
return $this;
}
public function isSimulationMode()
{
return $this->simulationMode;
}
public function setEditMode($mode = true)
{
$this->editMode = (bool) $mode;
return $this;
}
public function isEditMode()
{
return $this->editMode;
}
public function useSoftStates()
{
$this->state_type = self::SOFT_STATE;
return $this;
}
public function useHardStates()
{
$this->state_type = self::HARD_STATE;
return $this;
}
public function addRootNode($name)
{
$this->root_nodes[$name] = $this->getNode($name);
return $this;
}
public function retrieveStatesFromBackend()
{
$backend = $this->getBackend();
// TODO: Split apart, create a dedicated function.
// Separate "parse-logic" from "retrieve-state-logic"
// Allow DB-based backend
// Use IcingaWeb2 Multi-Backend-Support
$check_results = array();
$hostFilter = array_keys($this->hosts);
2015-02-06 18:13:37 -05:00
if ($this->state_type === self::HARD_STATE) {
2015-01-28 14:02:50 -05:00
$hostStateColumn = 'host_hard_state';
$hostStateChangeColumn = 'host_last_hard_state_change';
$serviceStateColumn = 'service_hard_state';
$serviceStateChangeColumn = 'service_last_hard_state_change';
} else {
2015-01-28 14:02:50 -05:00
$hostStateColumn = 'host_state';
$hostStateChangeColumn = 'host_last_state_change';
$serviceStateColumn = 'service_state';
$serviceStateChangeColumn = 'service_last_state_change';
}
2014-12-02 05:35:21 -05:00
$filter = Filter::matchAny();
foreach ($hostFilter as $host) {
2015-02-06 18:01:30 -05:00
$filter->addFilter(Filter::where('host_name', $host));
2014-12-02 05:35:21 -05:00
}
2015-02-06 18:01:30 -05:00
$hostStatus = $backend->select()->from('hostStatus', array(
2015-01-28 14:02:50 -05:00
'hostname' => 'host_name',
'last_state_change' => $hostStateChangeColumn,
'in_downtime' => 'host_in_downtime',
'ack' => 'host_acknowledged',
'state' => $hostStateColumn
2014-12-02 05:35:21 -05:00
))->applyFilter($filter)->getQuery()->fetchAll();
$serviceStatus = $backend->select()->from('serviceStatus', array(
2015-01-28 14:02:50 -05:00
'hostname' => 'host_name',
'service' => 'service_description',
'last_state_change' => $serviceStateChangeColumn,
'in_downtime' => 'service_in_downtime',
'ack' => 'service_acknowledged',
'state' => $serviceStateColumn
2014-12-02 05:35:21 -05:00
))->applyFilter($filter)->getQuery()->fetchAll();
foreach ($serviceStatus + $hostStatus as $row) {
$key = $row->hostname;
if ($row->service) {
$key .= ';' . $row->service;
} else {
$key .= ';Hoststatus';
}
2015-01-28 14:02:50 -05:00
// We fetch more states than we need, so skip unknown ones
if (! $this->hasNode($key)) continue;
$node = $this->getNode($key);
if ($row->state !== null) {
$node->setState($row->state)->setMissing(false);
}
if ($row->last_state_change !== null) {
$node->setLastStateChange($row->last_state_change);
}
if ((int) $row->in_downtime === 1) {
$node->setDowntime(true);
}
if ((int) $row->ack === 1) {
$node->setAck(true);
}
}
ksort($this->root_nodes);
return $this;
}
public function getChildren()
{
return $this->getRootNodes();
}
public function countChildren()
{
return count($this->root_nodes);
}
public function getRootNodes()
{
2014-12-02 05:35:21 -05:00
ksort($this->root_nodes);
return $this->root_nodes;
}
public function getNodes()
{
return $this->nodes;
}
public function hasNode($name)
{
return array_key_exists($name, $this->nodes);
}
public function createService($host, $service)
{
$node = new ServiceNode(
$this,
(object) array(
'hostname' => $host,
'service' => $service
)
);
$this->nodes[$host . ';' . $service] = $node;
2015-02-06 18:01:30 -05:00
$this->hosts[$host] = true;
return $node;
}
public function createHost($host)
{
$node = new HostNode($this, (object) array('hostname' => $host));
$this->nodes[$host . ';Hoststatus'] = $node;
2015-02-06 18:01:30 -05:00
$this->hosts[$host] = true;
return $node;
}
public function getNode($name)
{
if (array_key_exists($name, $this->nodes)) {
return $this->nodes[$name];
}
// Fallback: if it is a service, create an empty one:
$this->warn(sprintf('The node "%s" doesn\'t exist', $name));
$pos = strpos($name, ';');
if ($pos !== false) {
$host = substr($name, 0, $pos);
$service = substr($name, $pos + 1);
2015-01-28 14:02:50 -05:00
return $this->createService($host, $service);
}
throw new Exception(
sprintf('The node "%s" doesn\'t exist', $name)
);
}
public function addObjectName($name)
{
$this->all_checks[$name] = 1;
return $this;
}
public function addNode($name, Node $node)
{
if (array_key_exists($name, $this->nodes)) {
$this->warn(
sprintf(
'Node "%s" has been defined twice',
$name
)
);
}
2015-02-06 18:01:30 -05:00
$this->nodes[$name] = $node;
return $this;
}
public function hasWarnings()
{
return ! empty($this->warnings);
}
public function getWarnings()
{
return $this->warnings;
}
protected function warn($msg)
{
if (isset($this->parsing_line_number)) {
$this->warnings[] = sprintf(
'Parser waring on %s:%s: %s',
$this->filename,
$this->parsing_line_number,
$msg
);
} else {
$this->warnings[] = $msg;
}
}
2014-11-30 05:30:59 -05:00
public function toLegacyConfigString()
{
$conf = sprintf(
"### Busines Process Config File ###\n"
. "#\n"
. "# Title : %s\n"
// . "# Owner : %s\n"
. "# Backend : %s\n"
. "# Statetype : %s\n",
$this->getTitle(),
$this->backend->getName(),
$this->state_type = self::SOFT_STATE ? 'soft' : 'hard'
);
if (false) $conf .= sprintf("# xSLA Hosts: %s\n", implode(', ', array()));
$conf .= sprintf(
"#\n"
. "### %s - autogenerated ###\n\n",
date('Y-m-d H:i:s')
);
foreach ($this->getChildren() as $child) {
$conf .= $child->toLegacyConfigString();
}
return $conf;
}
2014-11-30 05:30:59 -05:00
public function renderHtml($view)
{
$html = '<div class="bp">';
foreach ($this->getRootNodes() as $name => $node) {
// showNode($this, $node, $this->slas, $this->opened, 'bp_')
$html .= $node->renderHtml($view);
}
$html .= '</div>';
return $html;
}
}