Implement first commands, fix obsessing state

refs #4441
This commit is contained in:
Jannis Moßhammer 2013-07-31 17:03:21 +02:00 committed by Marius Hein
parent d6bbed3a54
commit 2091f63c3b
13 changed files with 126 additions and 39 deletions

View File

@ -136,11 +136,14 @@ class CommandPipe
* @param $state * @param $state
* @param $output * @param $output
*/ */
public function submitCheckResult($objects, $state, $output) public function submitCheckResult($objects, $state, $output, $perfdata = "")
{ {
if ($perfdata) {
$output = $output."|".$perfdata;
}
foreach ($objects as $object) { foreach ($objects as $object) {
if (isset($object->service_description)) { if (isset($object->service_description)) {
$this->send("PROCESS_SVC_CHECK_RESULT;$object->host_name;$object->service_description;$state;$output"); $this->send("PROCESS_SERVICE_CHECK_RESULT;$object->host_name;$object->service_description;$state;$output");
} else { } else {
$this->send("PROCESS_HOST_CHECK_RESULT;$object->host_name;$state;$output"); $this->send("PROCESS_HOST_CHECK_RESULT;$object->host_name;$state;$output");
} }
@ -349,7 +352,7 @@ class CommandPipe
*/ */
public function disableActiveChecks($objects) public function disableActiveChecks($objects)
{ {
$this->modifyMonitoringProperties( $this->setMonitoringProperties(
$objects, $objects,
new PropertyModifier( new PropertyModifier(
array( array(
@ -379,7 +382,7 @@ class CommandPipe
*/ */
public function disablePassiveChecks($objects) public function disablePassiveChecks($objects)
{ {
$this->modifyMonitoringProperties( $this->setMonitoringProperties(
$objects, $objects,
new PropertyModifier( new PropertyModifier(
array( array(
@ -536,6 +539,32 @@ class CommandPipe
); );
} }
public function startObsessing($objects)
{
foreach ($objects as $object) {
$type = $this->getObjectType($object);
$msg = "START_OBSESSING_OVER_". (($type == self::TYPE_SERVICE) ? 'SVC' : 'HOST');
$msg .= ';'.$object->host_name;
if ($type == self::TYPE_SERVICE) {
$msg .= ';'.$object->service_description;
}
$this->send($msg);
}
}
public function stopObsessing($objects)
{
foreach ($objects as $object) {
$type = $this->getObjectType($object);
$msg = "STOP_OBSESSING_OVER_". (($type == self::TYPE_SERVICE) ? 'SVC' : 'HOST');
$msg .= ';'.$object->host_name;
if ($type == self::TYPE_SERVICE) {
$msg .= ';'.$object->service_description;
}
$this->send($msg);
}
}
/** /**
* Return the transport handler that handles actual sending of commands * Return the transport handler that handles actual sending of commands
* *

View File

@ -74,6 +74,7 @@ class PropertyModifier
*/ */
const FRESHNESS = "%s_FRESHNESS_CHECKS"; const FRESHNESS = "%s_FRESHNESS_CHECKS";
/** /**
* *
*/ */
@ -88,6 +89,7 @@ class PropertyModifier
self::PASSIVE => self::STATE_KEEP, self::PASSIVE => self::STATE_KEEP,
self::NOTIFICATIONS => self::STATE_KEEP, self::NOTIFICATIONS => self::STATE_KEEP,
self::FRESHNESS => self::STATE_KEEP, self::FRESHNESS => self::STATE_KEEP,
self::OBSESSING => self::STATE_KEEP,
self::EVENTHANDLER => self::STATE_KEEP self::EVENTHANDLER => self::STATE_KEEP
); );

View File

@ -29,8 +29,10 @@
// {{{ICINGA_LICENSE_HEADER}}} // {{{ICINGA_LICENSE_HEADER}}}
use Icinga\Application\Benchmark; use Icinga\Application\Benchmark;
use Icinga\Application\Icinga;
use Icinga\Backend; use Icinga\Backend;
use Icinga\Application\Config; use Icinga\Application\Config;
use Icinga\Application\Logger;
use Icinga\Authentication\Manager; use Icinga\Authentication\Manager;
use Icinga\Web\Form; use Icinga\Web\Form;
use Icinga\Web\ModuleActionController; use Icinga\Web\ModuleActionController;
@ -113,6 +115,11 @@ class Monitoring_CommandController extends ModuleActionController
*/ */
public function init() public function init()
{ {
$this->objects = $this->selectCommandTargets();
if (empty($this->objects) && ! $this->isGlobalCommand()) {
throw new \Exception("No objects found for your command");
}
if ($this->_request->isPost()) { if ($this->_request->isPost()) {
$instance = $this->_request->getPost("instance"); $instance = $this->_request->getPost("instance");
@ -142,6 +149,11 @@ class Monitoring_CommandController extends ModuleActionController
} }
} }
private function isGlobalCommand()
{
return false;
}
/** /**
* Retrieve all existing targets for host- and service combination * Retrieve all existing targets for host- and service combination
* @param string $hostname * @param string $hostname
@ -149,21 +161,38 @@ class Monitoring_CommandController extends ModuleActionController
* @return array * @return array
* @throws Icinga\Exception\MissingParameterException * @throws Icinga\Exception\MissingParameterException
*/ */
private function selectCommandTargets($hostname, $servicename = null) private function selectCommandTargets()
{ {
$target = "hostlist"; $query = null;
$fields = array(
'host_name',
'host_state'
);
try {
$hostname = $this->getParam('host', null);
$servicename = $this->getParam('service', null);
$filter = array(); $filter = array();
if (!$hostname && !$servicename) { if (!$hostname && !$servicename) {
throw new MissingParameterException("Missing host and service definition"); throw new MissingParameterException("No target given for this command");
} }
if ($hostname) { if ($hostname) {
$filter["host_name"] = $hostname; $filter["host_name"] = $hostname;
} }
if ($servicename) { if ($servicename) {
$filter["service_description"] = $servicename; $filter["service_description"] = $servicename;
$target = "servicelist"; $fields[] = "service_description";
$fields[] = "service_state";
}
;
$query = Backend::getInstance()->select()->from("status", $fields);
return $query->applyFilters($filter)->fetchAll();
} catch (\Exception $e) {
Logger::error(
"CommandController: SQL Query '%s' failed (message %s) ",
$query ? (string) $query->getQuery()->dump() : '--', $e->getMessage()
);
return array();
} }
return Backend::getInstance()->select()->from($target)->applyFilters($filter)->fetchAll();
} }
/** /**
@ -193,6 +222,7 @@ class Monitoring_CommandController extends ModuleActionController
*/ */
public function disableactivechecksAction() public function disableactivechecksAction()
{ {
$form = new ConfirmationForm(); $form = new ConfirmationForm();
$form->setRequest($this->getRequest()); $form->setRequest($this->getRequest());
$form->setSubmitLabel(t('Disable active checks')); $form->setSubmitLabel(t('Disable active checks'));
@ -200,7 +230,7 @@ class Monitoring_CommandController extends ModuleActionController
$this->setForm($form); $this->setForm($form);
if ($form->isPostAndValid() === true) { if ($form->isPostAndValid() === true) {
throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__); $this->target->disableActiveChecks($this->objects);
} }
} }
@ -217,7 +247,7 @@ class Monitoring_CommandController extends ModuleActionController
$this->setForm($form); $this->setForm($form);
if ($form->isPostAndValid() === true) { if ($form->isPostAndValid() === true) {
throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__); $this->target->enableActiveChecks($this->objects);
} }
} }
@ -233,7 +263,7 @@ class Monitoring_CommandController extends ModuleActionController
$this->setForm($form); $this->setForm($form);
if ($form->isPostAndValid() === true) { if ($form->isPostAndValid() === true) {
throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__); $this->target->scheduleCheck($this->objects);
} }
} }
@ -252,7 +282,7 @@ class Monitoring_CommandController extends ModuleActionController
$this->setForm($form); $this->setForm($form);
if ($form->isPostAndValid() === true) { if ($form->isPostAndValid() === true) {
throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__); $this->target->submitCheckResult($this->objects, $form->getState(), $form->getOutput(), $form->getPerformancedata());
} }
} }
@ -269,7 +299,7 @@ class Monitoring_CommandController extends ModuleActionController
$this->setForm($form); $this->setForm($form);
if ($form->isPostAndValid() === true) { if ($form->isPostAndValid() === true) {
throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__); $this->target->stopObsessing($this->objects);
} }
} }
@ -286,7 +316,7 @@ class Monitoring_CommandController extends ModuleActionController
$this->setForm($form); $this->setForm($form);
if ($form->isPostAndValid() === true) { if ($form->isPostAndValid() === true) {
throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__); $this->target->startObsessing($this->objects);
} }
} }

View File

@ -68,12 +68,14 @@ class ConfirmationForm extends Form
/** /**
* Array of messages * Array of messages
*
* @var string[] * @var string[]
*/ */
private $notes = array(); private $notes = array();
/** /**
* Setter for cancel label * Setter for cancel label
*
* @param string $cancelLabel * @param string $cancelLabel
*/ */
public function setCancelLabel($cancelLabel) public function setCancelLabel($cancelLabel)
@ -83,6 +85,7 @@ class ConfirmationForm extends Form
/** /**
* Getter for cancel label * Getter for cancel label
*
* @return string * @return string
*/ */
public function getCancelLabel() public function getCancelLabel()
@ -92,6 +95,7 @@ class ConfirmationForm extends Form
/** /**
* Setter for submit label * Setter for submit label
*
* @param string $submitLabel * @param string $submitLabel
*/ */
public function setSubmitLabel($submitLabel) public function setSubmitLabel($submitLabel)
@ -101,6 +105,7 @@ class ConfirmationForm extends Form
/** /**
* Getter for submit label * Getter for submit label
*
* @return string * @return string
*/ */
public function getSubmitLabel() public function getSubmitLabel()
@ -110,6 +115,7 @@ class ConfirmationForm extends Form
/** /**
* Add message to stack * Add message to stack
*
* @param string $message * @param string $message
*/ */
public function addNote($message) public function addNote($message)
@ -119,6 +125,7 @@ class ConfirmationForm extends Form
/** /**
* Purge messages from stack * Purge messages from stack
*
*/ */
public function clearNotes() public function clearNotes()
{ {
@ -127,6 +134,7 @@ class ConfirmationForm extends Form
/** /**
* Getter for notes * Getter for notes
*
* @return string[] * @return string[]
*/ */
public function getNotes() public function getNotes()
@ -148,6 +156,7 @@ class ConfirmationForm extends Form
/** /**
* Add elements to this form (used by extending classes) * Add elements to this form (used by extending classes)
*
* @see Form::create * @see Form::create
*/ */
protected function create() protected function create()
@ -191,15 +200,16 @@ class ConfirmationForm extends Form
/** /**
* Get the author name * Get the author name
* TODO(mh): This should work on the request, at present it's fix
*/ */
protected function getAuthorName() protected function getAuthorName()
{ {
return 'Iwan IV. Wassiljewitsch, der Schreckliche'; return $this->getRequest()->getUser()->getUsername();
} }
/** /**
* Creator for author field * Creator for author field
*
* @return Zend_Form_Element_Hidden * @return Zend_Form_Element_Hidden
*/ */
protected function createAuthorField() protected function createAuthorField()

View File

@ -168,4 +168,20 @@ class SubmitPassiveCheckResultForm extends ConfirmationForm
parent::create(); parent::create();
} }
public function getState()
{
return intval($this->getValue('pluginstate'));
}
public function getOutput()
{
return $this->getValue('checkoutput');
}
public function getPerformancedata()
{
return $this->getValue('performancedata');
}
} }

View File

@ -39,7 +39,7 @@ class Zend_View_Helper_MonitoringFlags extends Zend_View_Helper_Abstract
private static $keys = array( private static $keys = array(
'passive_checks_enabled' => 'Passive checks', 'passive_checks_enabled' => 'Passive checks',
'active_checks_enabled' => 'Active checks', 'active_checks_enabled' => 'Active checks',
'obsess_over_host' => 'Obsessing', 'obsessing' => 'Obsessing',
'notifications_enabled' => 'Notifications', 'notifications_enabled' => 'Notifications',
'event_handler_enabled' => 'Event handler', 'event_handler_enabled' => 'Event handler',
'flap_detection_enabled' => 'Flap detection', 'flap_detection_enabled' => 'Flap detection',

View File

@ -149,7 +149,7 @@ class AbstractBackend implements DatasourceInterface
'host_scheduled_downtime_depth', 'host_scheduled_downtime_depth',
'host_failure_prediction_enabled', 'host_failure_prediction_enabled',
'host_process_performance_data', 'host_process_performance_data',
'host_obsess_over_host', 'host_obsessing',
'host_modified_host_attributes', 'host_modified_host_attributes',
'host_event_handler', 'host_event_handler',
'host_check_command', 'host_check_command',
@ -230,7 +230,7 @@ class AbstractBackend implements DatasourceInterface
'service_scheduled_downtime_depth', 'service_scheduled_downtime_depth',
'service_failure_prediction_enabled', 'service_failure_prediction_enabled',
'service_process_performance_data', 'service_process_performance_data',
'service_obsess_over_service', 'service_obsessing',
'service_modified_service_attributes', 'service_modified_service_attributes',
'service_event_handler', 'service_event_handler',
'service_check_command', 'service_check_command',

View File

@ -60,7 +60,7 @@ class StatusQuery extends AbstractQuery
'host_scheduled_downtime_depth' => 'hs.scheduled_downtime_depth', 'host_scheduled_downtime_depth' => 'hs.scheduled_downtime_depth',
'host_failure_prediction_enabled' => 'hs.failure_prediction_enabled', 'host_failure_prediction_enabled' => 'hs.failure_prediction_enabled',
'host_process_performance_data' => 'hs.process_performance_data', 'host_process_performance_data' => 'hs.process_performance_data',
'host_obsess_over_host' => 'hs.obsess_over_host', 'host_obsessing' => 'hs.obsess_over_host',
'host_modified_host_attributes' => 'hs.modified_host_attributes', 'host_modified_host_attributes' => 'hs.modified_host_attributes',
'host_event_handler' => 'hs.event_handler', 'host_event_handler' => 'hs.event_handler',
'host_check_command' => 'hs.check_command', 'host_check_command' => 'hs.check_command',
@ -160,7 +160,7 @@ class StatusQuery extends AbstractQuery
'service_scheduled_downtime_depth' => 'ss.scheduled_downtime_depth', 'service_scheduled_downtime_depth' => 'ss.scheduled_downtime_depth',
'service_failure_prediction_enabled' => 'ss.failure_prediction_enabled', 'service_failure_prediction_enabled' => 'ss.failure_prediction_enabled',
'service_process_performance_data' => 'ss.process_performance_data', 'service_process_performance_data' => 'ss.process_performance_data',
'service_obsess_over_service' => 'ss.obsess_over_service', 'service_obsessing' => 'ss.obsess_over_service',
'service_modified_service_attributes' => 'ss.modified_service_attributes', 'service_modified_service_attributes' => 'ss.modified_service_attributes',
'service_event_handler' => 'ss.event_handler', 'service_event_handler' => 'ss.event_handler',
'service_check_command' => 'ss.check_command', 'service_check_command' => 'ss.check_command',

View File

@ -729,8 +729,8 @@ class Meta
} else { } else {
unset($commands[self::CMD_START_ACCEPTING_PASSIVE_CHECKS]); unset($commands[self::CMD_START_ACCEPTING_PASSIVE_CHECKS]);
} }
$obsess = 'obsess_over_'.$type;
if ($object->$obsess === '1') { if ($object->obsessing === '1') {
unset($commands[self::CMD_START_OBSESSING]); unset($commands[self::CMD_START_OBSESSING]);
} else { } else {
unset($commands[self::CMD_STOP_OBSESSING]); unset($commands[self::CMD_STOP_OBSESSING]);

View File

@ -54,7 +54,7 @@ class HostStruct extends \stdClass
public $host_scheduled_downtime_depth = '1'; public $host_scheduled_downtime_depth = '1';
public $host_failure_prediction_enabled = '1'; public $host_failure_prediction_enabled = '1';
public $host_process_performance_data = '1'; public $host_process_performance_data = '1';
public $host_obsess_over_host = '1'; public $host_obsessing = '1';
public $host_modified_host_attributes = '14'; public $host_modified_host_attributes = '14';
public $host_event_handler = ''; public $host_event_handler = '';
public $host_normal_check_interval = '5'; public $host_normal_check_interval = '5';

View File

@ -13,7 +13,7 @@ class MonitoringFlagsTest extends \PHPUnit_Framework_TestCase
$testArray = array( $testArray = array(
'host_passive_checks_enabled' => '0', 'host_passive_checks_enabled' => '0',
'host_active_checks_enabled' => '0', 'host_active_checks_enabled' => '0',
'host_obsess_over_host' => '1', 'host_obsessing' => '1',
'host_notifications_enabled' => '0', 'host_notifications_enabled' => '0',
'host_event_handler_enabled' => '1', 'host_event_handler_enabled' => '1',
'host_flap_detection_enabled' => '1', 'host_flap_detection_enabled' => '1',
@ -41,7 +41,7 @@ class MonitoringFlagsTest extends \PHPUnit_Framework_TestCase
$testArray = array( $testArray = array(
'service_passive_checks_enabled' => '0', 'service_passive_checks_enabled' => '0',
'service_active_checks_enabled' => '1', 'service_active_checks_enabled' => '1',
'service_obsess_over_host' => '0', 'service_obsessing' => '0',
'service_notifications_enabled' => '1', 'service_notifications_enabled' => '1',
'service_event_handler_enabled' => '1', 'service_event_handler_enabled' => '1',
'service_flap_detection_enabled' => '0', 'service_flap_detection_enabled' => '0',
@ -68,7 +68,7 @@ class MonitoringFlagsTest extends \PHPUnit_Framework_TestCase
{ {
$testArray = array( $testArray = array(
'service_active_checks_enabled' => '1', 'service_active_checks_enabled' => '1',
'service_obsess_over_host' => '1', 'service_obsessing' => '1',
'DING DING' => '$$$', 'DING DING' => '$$$',
'DONG DONG' => '###' 'DONG DONG' => '###'
); );

View File

@ -50,7 +50,7 @@ class HostStruct4Properties extends \stdClass
public $host_scheduled_downtime_depth = '1'; public $host_scheduled_downtime_depth = '1';
public $host_failure_prediction_enabled = '1'; public $host_failure_prediction_enabled = '1';
public $host_process_performance_data = '1'; public $host_process_performance_data = '1';
public $host_obsess_over_host = '1'; public $host_obsessing = '1';
public $host_modified_host_attributes = '14'; public $host_modified_host_attributes = '14';
public $host_event_handler = ''; public $host_event_handler = '';
public $host_normal_check_interval = '5'; public $host_normal_check_interval = '5';

View File

@ -51,7 +51,7 @@ class HostStruct extends \stdClass
public $host_scheduled_downtime_depth = '1'; public $host_scheduled_downtime_depth = '1';
public $host_failure_prediction_enabled = '1'; public $host_failure_prediction_enabled = '1';
public $host_process_performance_data = '1'; public $host_process_performance_data = '1';
public $host_obsess_over_host = '1'; public $host_obsessing = '1';
public $host_modified_host_attributes = '14'; public $host_modified_host_attributes = '14';
public $host_event_handler = ''; public $host_event_handler = '';
public $host_normal_check_interval = '5'; public $host_normal_check_interval = '5';
@ -158,7 +158,7 @@ class MetaTest extends \PHPUnit_Framework_TestCase
$object = new HostStruct(); $object = new HostStruct();
$object->host_obsess_over_host = '0'; $object->host_obsessing = '0';
$object->host_flap_detection_enabled = '0'; $object->host_flap_detection_enabled = '0';
$object->host_active_checks_enabled = '0'; $object->host_active_checks_enabled = '0';