View source
<?php
namespace Drupal\monitoring\Sensor\Sensors;
use Drupal\monitoring\Result\SensorResultInterface;
use Drupal\monitoring\Sensor\SensorConfigurable;
class SensorEnabledModules extends SensorConfigurable {
public function settingsForm($form, &$form_state) {
$form = parent::settingsForm($form, $form_state);
module_load_include('inc', 'system', 'system.admin');
$form['allow_additional'] = array(
'#type' => 'checkbox',
'#title' => t('Allow additional modules to be enabled'),
'#description' => t('If checked the additional modules being enabled will not be considered as an error state.'),
'#default_value' => $this->info
->getSetting('allow_additional'),
);
$modules = system_rebuild_module_data();
$visible_modules = array();
$hidden_modules = array();
foreach ($modules as $module => $module_data) {
if (strpos(drupal_get_path('module', $module), 'profiles') === 0) {
continue;
}
if (!isset($module_data->info['name'])) {
$module_data->info['name'] = '- No name -';
}
if (!empty($module_data->info['hidden'])) {
$module_data->info['name'] .= ' [' . t('Hidden module') . ']';
$hidden_modules[$module] = $module_data;
}
else {
$visible_modules[$module] = $module_data;
}
}
uasort($visible_modules, 'system_sort_modules_by_info_name');
uasort($hidden_modules, 'system_sort_modules_by_info_name');
$default_value = $this->info
->getSetting('modules');
if (empty($default_value)) {
$default_value = module_list();
}
$options = array();
foreach (array_merge($visible_modules, $hidden_modules) as $module => $module_data) {
$options[$module] = $module_data->info['name'] . ' (' . $module . ')';
}
$form['modules'] = array(
'#type' => 'checkboxes',
'#options' => $options,
'#title' => t('Modules expected to be enabled'),
'#description' => t('Check all modules that are supposed to be enabled.'),
'#default_value' => $default_value,
);
return $form;
}
public function runSensor(SensorResultInterface $result) {
$module_data = db_select('system')
->fields('system')
->execute()
->fetchAllAssoc('name');
$result
->setExpectedValue(0);
$delta = 0;
$monitoring_enabled_modules = array();
foreach (module_list() as $module) {
$path_parts = explode('/', drupal_get_path('module', $module));
if ($path_parts[0] != 'profiles') {
$monitoring_enabled_modules[$module] = $module;
}
}
$expected_modules = array_filter($this->info
->getSetting('modules'));
if (empty($expected_modules)) {
$expected_modules = $monitoring_enabled_modules;
$settings = monitoring_sensor_settings_get('monitoring_enabled_modules');
$settings['modules'] = $monitoring_enabled_modules;
monitoring_sensor_settings_save('monitoring_enabled_modules', $settings);
}
$non_installed_modules = array_diff($expected_modules, $monitoring_enabled_modules);
if (!empty($non_installed_modules)) {
$delta += count($non_installed_modules);
$non_installed_modules_info = array();
foreach ($non_installed_modules as $non_installed_module) {
$info = unserialize($module_data[$non_installed_module]->info);
$non_installed_modules_info[] = $info['name'] . ' (' . $non_installed_module . ')';
}
$result
->addStatusMessage('Following modules are expected to be installed: @modules', array(
'@modules' => implode(', ', $non_installed_modules_info),
));
}
$unexpected_modules = array_diff($monitoring_enabled_modules, $expected_modules);
if (!$this->info
->getSetting('allow_additional') && !empty($unexpected_modules)) {
$delta += count($unexpected_modules);
$unexpected_modules_info = array();
foreach ($unexpected_modules as $unexpected_module) {
$info = unserialize($module_data[$unexpected_module]->info);
$unexpected_modules_info[] = $info['name'] . ' (' . $unexpected_module . ')';
}
$result
->addStatusMessage('Following modules are NOT expected to be installed: @modules', array(
'@modules' => implode(', ', $unexpected_modules_info),
));
}
$result
->setValue($delta);
}
}