summaryrefslogtreecommitdiffstats
path: root/application/forms
diff options
context:
space:
mode:
Diffstat (limited to 'application/forms')
-rw-r--r--application/forms/Config/BackendConfigForm.php112
-rw-r--r--application/forms/Config/GlobalConfigForm.php32
-rw-r--r--application/forms/Config/MonitoringConfigForm.php72
-rw-r--r--application/forms/Event/EventCommentForm.php144
-rw-r--r--application/forms/Events/AckFilterForm.php80
-rw-r--r--application/forms/Events/SeverityFilterForm.php224
6 files changed, 664 insertions, 0 deletions
diff --git a/application/forms/Config/BackendConfigForm.php b/application/forms/Config/BackendConfigForm.php
new file mode 100644
index 0000000..a9c85c1
--- /dev/null
+++ b/application/forms/Config/BackendConfigForm.php
@@ -0,0 +1,112 @@
+<?php
+/* Icinga Web 2 | (c) 2016 Icinga Development Team | GPLv2+ */
+
+namespace Icinga\Module\Eventdb\Forms\Config;
+
+use Exception;
+use Icinga\Data\ResourceFactory;
+use Icinga\Forms\ConfigForm;
+
+/**
+ * Form for managing the connection to the EventDB backend
+ */
+class BackendConfigForm extends ConfigForm
+{
+ /**
+ * {@inheritdoc}
+ */
+ public function init()
+ {
+ $this->setSubmitLabel($this->translate('Save'));
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function createElements(array $formData)
+ {
+ $resources = array();
+ foreach (ResourceFactory::getResourceConfigs() as $name => $config) {
+ if ($config->type === 'db') {
+ $resources[] = $name;
+ }
+ }
+
+ $this->addElement(
+ 'select',
+ 'backend_resource',
+ array(
+ 'description' => $this->translate('The resource to use'),
+ 'label' => $this->translate('Resource'),
+ 'multiOptions' => array_combine($resources, $resources),
+ 'required' => true
+ )
+ );
+
+ if (isset($formData['skip_validation']) && $formData['skip_validation']) {
+ $this->addSkipValidationCheckbox();
+ }
+ }
+
+ /**
+ * Return whether the given values are valid
+ *
+ * @param array $formData The data to validate
+ *
+ * @return bool
+ */
+ public function isValid($formData)
+ {
+ if (! parent::isValid($formData)) {
+ return false;
+ }
+
+ if (($el = $this->getElement('skip_validation')) === null || ! $el->isChecked()) {
+ $resourceConfig = ResourceFactory::getResourceConfig($this->getValue('backend_resource'));
+
+ if (! $this->isValidEventDbSchema($resourceConfig)) {
+ if ($el === null) {
+ $this->addSkipValidationCheckbox();
+ }
+
+ return false;
+ }
+ }
+
+ return true;
+ }
+
+ public function isValidEventDbSchema($resourceConfig)
+ {
+ try {
+ $db = ResourceFactory::createResource($resourceConfig);
+ $db->select()->from('event', array('id'))->fetchOne();
+ } catch (Exception $_) {
+ $this->error($this->translate(
+ 'Cannot find the EventDB schema. Please verify that the given database '
+ . 'contains the schema and that the configured user has access to it.'
+ ));
+ return false;
+ }
+ return true;
+ }
+
+ /**
+ * Add a checkbox to the form by which the user can skip the schema validation
+ */
+ protected function addSkipValidationCheckbox()
+ {
+ $this->addElement(
+ 'checkbox',
+ 'skip_validation',
+ array(
+ 'description' => $this->translate(
+ 'Check this to not to validate the EventDB schema of the chosen resource'
+ ),
+ 'ignore' => true,
+ 'label' => $this->translate('Skip Validation'),
+ 'order' => 0
+ )
+ );
+ }
+}
diff --git a/application/forms/Config/GlobalConfigForm.php b/application/forms/Config/GlobalConfigForm.php
new file mode 100644
index 0000000..5b4bf2a
--- /dev/null
+++ b/application/forms/Config/GlobalConfigForm.php
@@ -0,0 +1,32 @@
+<?php
+/* Icinga Web 2 - EventDB | (c) 2018 Icinga Development Team | GPLv2+ */
+
+namespace Icinga\Module\Eventdb\Forms\Config;
+
+use Icinga\Forms\ConfigForm;
+
+class GlobalConfigForm extends ConfigForm
+{
+ /**
+ * {@inheritdoc}
+ */
+ public function init()
+ {
+ $this->setSubmitLabel($this->translate('Save'));
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function createElements(array $formData)
+ {
+ $this->addElement(
+ 'text',
+ 'global_default_filter',
+ array(
+ 'description' => $this->translate('Filter to be used by the menu link for EventDB by default'),
+ 'label' => $this->translate('Default Filter')
+ )
+ );
+ }
+}
diff --git a/application/forms/Config/MonitoringConfigForm.php b/application/forms/Config/MonitoringConfigForm.php
new file mode 100644
index 0000000..e1c7638
--- /dev/null
+++ b/application/forms/Config/MonitoringConfigForm.php
@@ -0,0 +1,72 @@
+<?php
+/* Icinga Web 2 - EventDB | (c) 2017 Icinga Development Team | GPLv2+ */
+
+namespace Icinga\Module\Eventdb\Forms\Config;
+
+use Icinga\Forms\ConfigForm;
+
+/**
+ * Form for managing settings for the integration into monitoring module
+ */
+class MonitoringConfigForm extends ConfigForm
+{
+ /**
+ * {@inheritdoc}
+ */
+ public function init()
+ {
+ $this->setSubmitLabel($this->translate('Save'));
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function createElements(array $formData)
+ {
+ $this->addElement(
+ 'text',
+ 'monitoring_custom_var',
+ array(
+ 'description' => $this->translate('Name of the custom variable to enable EventDB integration for (usually "edb")'),
+ 'label' => $this->translate('Custom Variable')
+ )
+ );
+
+ $this->addElement(
+ 'checkbox',
+ 'monitoring_always_on_host',
+ array(
+ 'description' => $this->translate('Always enable the integration on hosts, even when the custom variable is not set'),
+ 'label' => $this->translate('Always enable for hosts')
+ )
+ );
+
+ $this->addElement(
+ 'checkbox',
+ 'monitoring_always_on_service',
+ array(
+ 'description' => $this->translate('Always enable the integration on services, even when the custom variable is not set'),
+ 'label' => $this->translate('Always enable for services')
+ )
+ );
+
+ $this->addElement(
+ 'checkbox',
+ 'monitoring_detailview_disable',
+ array(
+ 'description' => $this->translate('Disable the detail view inside the monitoring module'),
+ 'label' => $this->translate('Disable the detail view')
+ )
+ );
+
+ $this->addElement(
+ 'text',
+ 'monitoring_detailview_filter',
+ array(
+ 'description' => $this->translate('Filter events in the detail view area inside the monitoring module'),
+ 'label' => $this->translate('Filter the detail view'),
+ 'value' => 'ack=0', // Also see DetailviewExtension
+ )
+ );
+ }
+}
diff --git a/application/forms/Event/EventCommentForm.php b/application/forms/Event/EventCommentForm.php
new file mode 100644
index 0000000..130910c
--- /dev/null
+++ b/application/forms/Event/EventCommentForm.php
@@ -0,0 +1,144 @@
+<?php
+/* Icinga Web 2 | (c) 2016 Icinga Development Team | GPLv2+ */
+
+namespace Icinga\Module\Eventdb\Forms\Event;
+
+use Exception;
+use Icinga\Data\Filter\Filter;
+use Icinga\Module\Eventdb\Eventdb;
+use Icinga\Web\Form;
+
+/**
+ * Form for managing the connection to the EventDB backend
+ */
+class EventCommentForm extends Form
+{
+ /**
+ * @var Eventdb
+ */
+ protected $db;
+
+ protected $filter;
+
+ protected static $types = array(
+ 'comment',
+ 'ack',
+ 'revoke'
+ );
+
+ /**
+ * {@inheritdoc}
+ */
+ public static $defaultElementDecorators = array(
+ array('ViewHelper', array('separator' => '')),
+ array('Errors', array('separator' => '')),
+ );
+
+ /**
+ * {@inheritdoc}
+ */
+ public function init()
+ {
+ $this->setSubmitLabel($this->translate('Submit'));
+ }
+
+ public function setDb(Eventdb $db)
+ {
+ $this->db = $db;
+ return $this;
+ }
+
+ public function setFilter(Filter $filter)
+ {
+ $this->filter = $filter;
+ return $this;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function createElements(array $formData)
+ {
+ $this->addElement(
+ 'select',
+ 'type',
+ array(
+ 'label' => $this->translate('Type'),
+ 'multiOptions' => array(
+ 0 => $this->translate('Comment'),
+ 1 => $this->translate('Acknowledge'),
+ 2 => $this->translate('Revoke')
+ ),
+ 'required' => true,
+ 'value' => 1,
+ )
+ );
+ $this->addElement(
+ 'text',
+ 'comment',
+ array(
+ 'label' => $this->translate('Comment'),
+ 'required' => true
+ )
+ );
+ }
+
+ public function onSuccess()
+ {
+ $type = $this->getValue('type');
+ $comment = $this->getValue('comment');
+ $username = $this->Auth()->getUser()->getUsername();
+
+ $events = $this->db->select()->from('event', array('id'))->applyFilter($this->filter);
+
+ $dbAdapter = $this->db->getDataSource()->getDbAdapter();
+
+ $dbAdapter->beginTransaction();
+ try {
+ foreach ($events as $event) {
+ $this->db->insert('comment', array(
+ 'event_id' => $event->id,
+ 'type' => $type,
+ 'message' => $comment,
+ 'created' => date(Eventdb::DATETIME_FORMAT),
+ 'modified' => date(Eventdb::DATETIME_FORMAT),
+ 'user' => $username
+ ));
+
+ if ($type !== '0') {
+ $ackFilter = Filter::expression('id', '=', $event->id);
+ if ($this->db->hasCorrelatorExtensions()) {
+ $ackFilter = Filter::matchAny($ackFilter, Filter::where('group_leader', $event->id));
+ }
+ $this->db->update('event', array(
+ 'ack' => $type === '1' ? 1 : 0
+ ), $ackFilter);
+ }
+ }
+ $dbAdapter->commit();
+ return true;
+ } catch (Exception $e) {
+ $dbAdapter->rollback();
+ $this->error($e->getMessage());
+ return false;
+ }
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function loadDefaultDecorators()
+ {
+ parent::loadDefaultDecorators();
+
+ $this->removeDecorator('FormHints');
+ }
+
+ public function addSubmitButton()
+ {
+ parent::addSubmitButton();
+
+ $btn = $this->getElement('btn_submit');
+ $btn->removeDecorator('HtmlTag');
+ }
+}
diff --git a/application/forms/Events/AckFilterForm.php b/application/forms/Events/AckFilterForm.php
new file mode 100644
index 0000000..829c6ce
--- /dev/null
+++ b/application/forms/Events/AckFilterForm.php
@@ -0,0 +1,80 @@
+<?php
+/* Icinga Web 2 | (c) 2016 Icinga Development Team | GPLv2+ */
+
+namespace Icinga\Module\Eventdb\Forms\Events;
+
+use Icinga\Data\Filter\Filter;
+use Icinga\Web\Form;
+
+class AckFilterForm extends Form
+{
+ /**
+ * {@inheritdoc}
+ */
+ public function init()
+ {
+ $this->setAttrib('class', 'inline ack-filter-form');
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function addSubmitButton()
+ {
+ if ((bool) $this->getRequest()->getUrl()->getParams()->get('ack', true)) {
+ $icon = 'ok';
+ $title = $this->translate('Hide acknowledged events');
+ } else {
+ $icon = 'cancel';
+ $title = $this->translate('Show also acknowledged events');
+ }
+
+ $this->addElements(array(
+ array(
+ 'button',
+ 'btn_submit',
+ array(
+ 'class' => 'link-button spinner',
+ 'decorators' => array(
+ 'ViewHelper',
+ array('HtmlTag', array('tag' => 'div', 'class' => 'control-group form-controls'))
+ ),
+ 'escape' => false,
+ 'ignore' => true,
+ 'label' => $this->getView()->icon($icon) . $this->translate('Ack'),
+ 'type' => 'submit',
+ 'title' => $title,
+ 'value' => $this->translate('Ack')
+ )
+ )
+ ));
+
+ return $this;
+ }
+
+ public function onSuccess()
+ {
+ $redirect = clone $this->getRequest()->getUrl();
+ $params = $redirect->getParams();
+ $modifyFilter = $params->shift('modifyFilter');
+ $columns = $params->shift('columns');
+ if (! (bool) $this->getRequest()->getUrl()->getParams()->get('ack', true)) {
+ $params->remove('ack');
+ } else {
+ $redirect->setQueryString(
+ Filter::fromQueryString($redirect->getQueryString())
+ ->andFilter(Filter::expression('ack', '=', 0))
+ ->toQueryString()
+ );
+ }
+ $params = $redirect->getParams();
+ if ($modifyFilter) {
+ $params->add('modifyFilter');
+ }
+ if ($columns) {
+ $params->add('columns', $columns);
+ }
+ $this->setRedirectUrl($redirect);
+ return true;
+ }
+}
diff --git a/application/forms/Events/SeverityFilterForm.php b/application/forms/Events/SeverityFilterForm.php
new file mode 100644
index 0000000..bd1ec5e
--- /dev/null
+++ b/application/forms/Events/SeverityFilterForm.php
@@ -0,0 +1,224 @@
+<?php
+/* Icinga Web 2 | (c) 2016 Icinga Development Team | GPLv2+ */
+
+namespace Icinga\Module\Eventdb\Forms\Events;
+
+use Icinga\Data\Filter\Filter;
+use Icinga\Data\Filter\FilterAnd;
+use Icinga\Data\Filter\FilterChain;
+use Icinga\Data\Filter\FilterExpression;
+use Icinga\Data\Filter\FilterMatch;
+use Icinga\Module\Eventdb\Event;
+use Icinga\Web\Form;
+
+class SeverityFilterForm extends Form
+{
+ protected $includedPriorities;
+ protected $excludedPriorities;
+
+ /**
+ * @var FilterChain
+ */
+ protected $filter;
+
+ /**
+ * @var array
+ */
+ protected $filterEditorParams = array('modifyFilter', 'addFilter');
+
+ /**
+ * {@inheritdoc}
+ */
+ public function init()
+ {
+ $this->setAttrib('class', 'inline severity-filter-form');
+ }
+
+ protected function findPriorities(Filter $filter, $sign, &$target)
+ {
+ if ($filter->isEmpty()) {
+ return;
+ }
+
+ if ($filter->isChain()) {
+ /** @var FilterChain $filter */
+ foreach ($filter->filters() as $part) {
+ /** @var Filter $part */
+ if (! $part->isEmpty() && $part->isExpression()) {
+ /** @var FilterMatch $part */
+ if (strtolower($part->getColumn()) === 'priority' && $part->getSign() === $sign) {
+ $expression = $part->getExpression();
+ if (is_array($expression)) {
+ foreach ($expression as $priority) {
+ $target[(int) $priority] = $part;
+ }
+ } else {
+ $target[(int) $expression] = $part;
+ }
+ }
+ } else {
+ /** @var FilterChain $part */
+ foreach ($part->filters() as $or) {
+ /** @var FilterExpression $or */
+ if (strtolower($or->getColumn()) === 'priority' && $or->getSign() === $sign) {
+ $expression = $or->getExpression();
+ if (is_array($expression)) {
+ foreach ($expression as $priority) {
+ $target[(int) $priority] = $or;
+ }
+ } else {
+ $target[(int) $expression] = $or;
+ }
+ }
+ }
+ }
+ }
+ } else {
+ /** @var FilterMatch $filter */
+ if (strtolower($filter->getColumn()) === 'priority' && $filter->getSign() === $sign) {
+ $expression = $filter->getExpression();
+ if (is_array($expression)) {
+ foreach ($expression as $priority) {
+ $target[(int) $priority] = $filter;
+ }
+ } else {
+ $target[(int) $expression] = $filter;
+ }
+ }
+ }
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function createElements(array $formData)
+ {
+ $includedPriorities = array();
+ $excludedPriorities = array();
+ $params = $this->getRequest()->getUrl()->getParams()
+ ->without($this->filterEditorParams)
+ ->without('columns')
+ ->without('page');
+
+ $filter = Filter::fromQueryString((string) $params);
+
+ $this->findPriorities($filter, '=', $includedPriorities);
+ $this->findPriorities($filter, '!=', $excludedPriorities);
+
+ foreach (Event::$priorities as $id => $priority) {
+ $class = $priority;
+ if (
+ (empty($includedPriorities) or isset($includedPriorities[$id]))
+ && ! isset($excludedPriorities[$id])
+ ) {
+ $class .= ' active';
+ $title = $this->translate('Filter out %s');
+ } else {
+ $title = $this->translate('Filter in %s');
+ }
+
+ $label = ucfirst(substr($priority, 0, 1));
+ if ($id === 3) {
+ $label .= substr($priority, 1, 1);
+ }
+ $this->addElement(
+ 'submit',
+ $priority,
+ array(
+ 'class' => $class,
+ 'label' => $label,
+ 'title' => sprintf($title, ucfirst($priority)),
+ )
+ );
+ }
+
+ $this->includedPriorities = $includedPriorities;
+ $this->excludedPriorities = $excludedPriorities;
+
+ $this->filter = $filter;
+ }
+
+ public function onSuccess()
+ {
+ $postData = $this->getRequest()->getPost();
+ unset($postData['formUID']);
+ unset($postData['CSRFToken']);
+ reset($postData);
+ $priority = Event::getPriorityId(key($postData));
+ $redirect = clone $this->getRequest()->getUrl();
+
+ // convert inclusion to exclusion
+ if (! empty($this->includedPriorities)) {
+ if (empty($this->excludedPriorities)) {
+ $this->excludedPriorities = array();
+ }
+ // set exclusion with for all not included values
+ foreach (array_keys(Event::$priorities) as $id) {
+ if (! isset($this->includedPriorities[$id])
+ && ! isset($this->excludedPriorities[$id])
+ ) {
+ $this->excludedPriorities[$id] = true;
+ }
+ }
+ // purge from inclusions from filter
+ if ($this->filter instanceof FilterChain) {
+ foreach ($this->includedPriorities as $filter) {
+ if ($filter instanceof Filter) {
+ /** @var Filter $filter */
+ $this->filter = $this->filter->removeId($filter->getId());
+ }
+ }
+ }
+ }
+
+ if ($this->filter instanceof FilterChain) {
+ // purge existing exclusions from a complex filter
+ foreach ($this->excludedPriorities as $filter) {
+ if ($filter instanceof Filter) {
+ /** @var Filter $filter */
+ $this->filter = $this->filter->removeId($filter->getId());
+ }
+ }
+ } elseif (! empty($this->excludedPriorities)) {
+ // empty the filter - because it only was a simple exclusion
+ $this->filter = new FilterAnd;
+ }
+
+ // toggle exclusion
+ if (isset($this->excludedPriorities[$priority])) {
+ // in exclusion: just remove
+ unset($this->excludedPriorities[$priority]);
+ } else {
+ // not set: add to exclusion
+ $this->excludedPriorities[$priority] = true;
+ }
+
+ $priorityFilter = Filter::matchAll();
+ foreach (array_keys($this->excludedPriorities) as $id) {
+ $priorityFilter->andFilter(Filter::expression('priority', '!=', $id));
+ }
+
+ if ($this->filter->isEmpty()) {
+ // set the Filter
+ $this->filter = $priorityFilter;
+ } else {
+ // append our filter to the rest of the existing Filter
+ $this->filter = $this->filter->andFilter($priorityFilter);
+ }
+
+ $redirect->setQueryString($this->filter->toQueryString());
+
+ $requestParams = $this->getRequest()->getUrl()->getParams();
+ $redirectParams = $redirect->getParams();
+ foreach ($this->filterEditorParams as $filterEditorParam) {
+ if ($requestParams->has($filterEditorParam)) {
+ $redirectParams->add($filterEditorParam);
+ }
+ }
+ if ($requestParams->has('columns')) {
+ $redirectParams->add('columns', $this->getRequest()->getUrl()->getParam('columns'));
+ }
+ $this->setRedirectUrl($redirect);
+ return true;
+ }
+}