UpdateStatusSensorPlugin.php in Monitoring 8
File
src/Plugin/monitoring/SensorPlugin/UpdateStatusSensorPlugin.php
View source
<?php
namespace Drupal\monitoring\Plugin\monitoring\SensorPlugin;
use Drupal\monitoring\Result\SensorResultInterface;
use Drupal\monitoring\SensorPlugin\SensorPluginBase;
use Drupal\update\UpdateFetcherInterface;
use Drupal\update\UpdateManagerInterface;
class UpdateStatusSensorPlugin extends SensorPluginBase {
protected $configurableValueType = FALSE;
public function runSensor(SensorResultInterface $result) {
$type = $this->sensorConfig
->getSetting('type');
$status = $this
->calculateStatus($type);
$result
->setStatus($status);
$available = update_get_available();
$project_data = update_calculate_project_data($available);
if ($type == 'core') {
$this
->checkCore($result, $project_data);
}
else {
$this
->checkContrib($result, $project_data);
}
}
protected function checkCore(SensorResultInterface $result, $project_data) {
$info = $project_data['drupal'];
$status = $this
->getStatusText($info['status']);
if ($status == 'unknown') {
$result
->addStatusMessage('Core update status unknown');
$result
->setStatus(SensorResultInterface::STATUS_INFO);
}
elseif ($status == 'current') {
$result
->addStatusMessage('Core up to date');
}
else {
$result
->addStatusMessage('Core (@current) - @status - latest @latest', array(
'@status' => $status,
'@current' => isset($info['existing_version']) ? $info['existing_version'] : NULL,
'@latest' => isset($info['latest_version']) ? $info['latest_version'] : NULL,
));
}
}
protected function checkContrib(SensorResultInterface $result, $project_data) {
unset($project_data['drupal']);
$updates = [];
$important_updates = [];
foreach ($project_data as $info) {
$status_text = $this
->getStatusText($info['status']);
if (!isset($updates[$status_text])) {
$updates[$status_text] = 0;
}
$updates[$status_text]++;
if ($status_text == 'NOT SECURE' || $status_text == 'not supported') {
if (!isset($important_updates[$status_text])) {
$important_updates[$status_text] = [];
}
$important_updates[$status_text][] = $info;
}
}
foreach ($updates as $status_text => $count) {
$result
->addStatusMessage($count . ' ' . $status_text);
}
foreach ($important_updates as $status_text => $update_info) {
foreach ($update_info as $info) {
if ($status_text == 'NOT SECURE') {
$result
->addStatusMessage('@module (@status, @current => @recommended)', array(
'@module' => $info['info']['name'],
'@status' => $status_text,
'@current' => isset($info['existing_version']) ? $info['existing_version'] : NULL,
'@recommended' => isset($info['recommended']) ? $info['recommended'] : NULL,
));
}
else {
$result
->addStatusMessage('@module (@status, @current)', array(
'@module' => $info['info']['name'],
'@status' => $status_text,
'@current' => isset($info['existing_version']) ? $info['existing_version'] : NULL,
));
}
}
}
}
protected function getStatusText($status) {
switch ($status) {
case UpdateManagerInterface::NOT_SECURE:
return 'NOT SECURE';
break;
case UpdateManagerInterface::CURRENT:
return 'current';
break;
case UpdateManagerInterface::REVOKED:
return 'version revoked';
break;
case UpdateManagerInterface::NOT_SUPPORTED:
return 'not supported';
break;
case UpdateManagerInterface::NOT_CURRENT:
return 'update available';
break;
case UpdateFetcherInterface::UNKNOWN:
case UpdateFetcherInterface::NOT_CHECKED:
case UpdateFetcherInterface::NOT_FETCHED:
case UpdateFetcherInterface::FETCH_PENDING:
return 'unknown';
break;
}
}
protected function calculateStatus($type) {
\Drupal::service('module_handler')
->loadInclude('update', 'install');
$requirements = update_requirements('runtime');
$update_info = array();
if (isset($requirements['update_' . $type])) {
$update_info = $requirements['update_' . $type];
}
$update_info += array(
'severity' => REQUIREMENT_OK,
);
if ($update_info['severity'] == REQUIREMENT_OK) {
return SensorResultInterface::STATUS_OK;
}
elseif ($update_info['severity'] == REQUIREMENT_INFO) {
return SensorResultInterface::STATUS_INFO;
}
elseif ($update_info['severity'] == REQUIREMENT_WARNING) {
return SensorResultInterface::STATUS_INFO;
}
else {
return SensorResultInterface::STATUS_CRITICAL;
}
}
}