You are here

public function SettingsForm::buildForm in Nagios Monitoring 8

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides ConfigFormBase::buildForm

File

src/Form/SettingsForm.php, line 50

Class

SettingsForm
Allows admins to configure the Nagios module.

Namespace

Drupal\nagios\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  _nagios_update_os_user();
  $group = 'modules';
  $config = $this
    ->config('nagios.settings');
  $form['nagios_status_page'] = [
    '#type' => 'fieldset',
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#title' => $this
      ->t('Status page settings'),
  ];
  $form['nagios_status_page']['nagios_enable_status_page'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Enable HTTP status page'),
    '#default_value' => $config
      ->get('nagios.statuspage.enabled'),
    '#description' => $this
      ->t('Even when the status page is ' . 'disabled, you can use `$ drush nagios` to get the status from the ' . 'command line.') . ' ' . $this
      ->t('This integrates well with NRPE.'),
  ];
  $only_enabled_if_page = [
    'visible' => [
      '#edit-nagios-enable-status-page' => [
        'checked' => TRUE,
      ],
    ],
  ];
  $form['nagios_status_page']['nagios_page_path'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Nagios page path'),
    '#description' => $this
      ->t('Enter the path for the Nagios HTTP status page. It must be a valid Drupal path.'),
    '#default_value' => $config
      ->get('nagios.statuspage.path'),
    '#states' => $only_enabled_if_page,
  ];
  $form['nagios_status_page']['nagios_ua'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Unique ID'),
    '#default_value' => $config
      ->get('nagios.ua'),
    '#description' => $this
      ->t('Restrict sending information to requests identified by this Unique ID. You should change this to some unique string for your organization, and configure Nagios accordingly. This makes Nagios data less accessible to curious users. See the README.txt for more details.'),
    '#states' => $only_enabled_if_page,
  ];
  $form['nagios_status_page']['nagios_page_controller'] = [
    '#type' => 'hidden',
    '#title' => $this
      ->t('Nagios page controller'),
    '#description' => $this
      ->t('Enter the name of the controller and function to be used by the Nagios status page.') . ' ' . $this
      ->t('Take care and be sure this function exists before submitting this form!'),
    '#default_value' => $config
      ->get('nagios.statuspage.controller'),
  ];
  $form['nagios_status_page']['nagios_enable_status_page_get'] = [
    '#type' => 'radios',
    '#default_value' => (int) $config
      ->get('nagios.statuspage.getparam'),
    '#options' => [
      0 => $this
        ->t('The HTTP User Agent has to be exactly the Unique ID'),
      1 => $this
        ->t('Enable Unique ID checking via GET parameter in the URL status page'),
    ],
    '#description' => $this
      ->getUserAgentRadioDesc(),
    '#states' => $only_enabled_if_page,
  ];
  $form['nagios_error_levels'] = [
    '#type' => 'details',
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#title' => $this
      ->t('Error levels'),
    '#description' => $this
      ->t('Set the values to be used for error levels when reporting to Nagios.'),
  ];
  $form['nagios_error_levels']['nagios_status_ok_value'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Status OK'),
    '#description' => $this
      ->t('The value to send to Nagios for a Status OK message.'),
    '#default_value' => $config
      ->get('nagios.status.ok'),
  ];
  $form['nagios_error_levels']['nagios_status_warning_value'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Warning'),
    '#description' => $this
      ->t('The value to send to Nagios for a Warning message.'),
    '#default_value' => $config
      ->get('nagios.status.warning'),
  ];
  $form['nagios_error_levels']['nagios_status_critical_value'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Critical'),
    '#description' => $this
      ->t('The value to send to Nagios for a Critical message.'),
    '#default_value' => $config
      ->get('nagios.status.critical'),
  ];
  $form['nagios_error_levels']['nagios_status_unknown_value'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Unknown'),
    '#description' => $this
      ->t('The value to send to Nagios for an Unknown message.'),
    '#default_value' => $config
      ->get('nagios.status.unknown'),
  ];
  $form[$group] = [
    '#type' => 'fieldset',
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#title' => $this
      ->t('Modules'),
    '#description' => $this
      ->t('Select the modules that should report their data into Nagios.'),
  ];
  $modules = nagios_invoke_all('nagios_info');
  foreach ($modules as $module => $data) {
    $form[$group]['nagios_enable_' . $module] = [
      '#type' => 'checkbox',
      '#title' => $data['name'] . ' (' . $module . ')',
      '#default_value' => self::getModuleHookEnabled($module, $config),
    ];
  }
  if (count($modules) < 2) {
    $form[$group]['#access'] = FALSE;
  }
  if (\Drupal::moduleHandler()
    ->loadInclude('dblog', 'admin.inc')) {
    $form['watchdog'] = [
      '#type' => 'fieldset',
      '#collapsible' => TRUE,
      '#collapsed' => FALSE,
      '#title' => $this
        ->t('Watchdog settings'),
      '#description' => $this
        ->t('Controls how watchdog messages are retrieved and displayed when watchdog checking is set.'),
      '#states' => [
        'visible' => [
          '#edit-nagios #edit-nagios-func-watchdog' => [
            'checked' => TRUE,
          ],
          '#edit-nagios-enable-nagios' => [
            'checked' => TRUE,
          ],
        ],
      ],
      '#weight' => 5,
    ];
    $form['watchdog']['limit_watchdog_display'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Limit watchdog display'),
      '#default_value' => $config
        ->get('nagios.limit_watchdog.display'),
      '#description' => $this
        ->t('Limit watchdog messages to only those that are new since the last check.'),
    ];
    $form['watchdog']['channel_filter_group'] = [
      '#type' => 'fieldgroup',
      '#title' => $this
        ->t('Channel filter'),
    ];
    $channel_filter = $config
      ->get('nagios.limit_watchdog.channel_filter') ?: [];
    $filters = dblog_filters();
    $options = empty($filters['type']['options']) ? [
      'php' => 'php',
    ] : $filters['type']['options'];
    $options += $channel_filter;
    $negate = $config
      ->get('nagios.limit_watchdog.negate');
    $form['watchdog']['channel_filter_group']['channel_filter'] = [
      '#type' => 'select',
      '#title' => $negate ? $this
        ->t('Message types to include') : $this
        ->t('Message types to ignore'),
      '#default_value' => $channel_filter,
      '#options' => $options,
      '#multiple' => TRUE,
      '#size' => 8,
      '#description' => $this
        ->t('Types are also known as channels. You can ignore multiple with the Ctrl or &#8984; key on your keyboard.'),
    ];
    $form['watchdog']['channel_filter_group']['negate'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Negate condition'),
      '#default_value' => $negate,
      '#description' => $this
        ->t('If checked, the ignored message types are those that are not selected in the list above.'),
    ];
  }
  foreach (nagios_invoke_all('nagios_settings') as $module => $module_settings) {
    $form[$module] = [
      '#type' => 'fieldset',
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#title' => $module,
      '#states' => [
        'visible' => [
          '#edit-nagios-enable-' . str_replace('-', '_', $module) => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    foreach ($module_settings as $element => $data) {
      $form[$module][$element] = $data;

      // Set #defaultvalue from #configname for first level form elements:
      if (!isset($data['#default_value']) && isset($data['#configname'])) {
        $form[$module][$element]['#default_value'] = $config
          ->get($module . '.' . $data['#configname']);
      }

      // Set #defaultvalue from #configname for second level form elements:
      if (isset($data['#type']) && $data['#type'] == 'fieldset') {
        foreach ($data as $fieldset_element => $fieldset_data) {
          if (is_array($fieldset_data) && !isset($fieldset_data['#default_value']) && isset($fieldset_data['#configname'])) {
            $form[$module][$element][$fieldset_element]['#default_value'] = $config
              ->get($module . '.' . $fieldset_data['#configname']);
          }
        }
      }
    }
  }
  return parent::buildForm($form, $form_state);
}