BiffBangPow /
silverstripe-monitoring-server
| 1 | <?php |
||
| 2 | |||
| 3 | namespace BiffBangPow\SSMonitor\Server\Client; |
||
| 4 | |||
| 5 | use BiffBangPow\SSMonitor\Server\Helper\ReportingHelper; |
||
| 6 | use SilverStripe\Core\Config\Configurable; |
||
| 7 | use SilverStripe\Core\Extensible; |
||
| 8 | use SilverStripe\Core\Injector\Injector; |
||
| 9 | use SilverStripe\ORM\ArrayList; |
||
| 10 | use SilverStripe\View\ArrayData; |
||
| 11 | use SilverStripe\View\SSViewer; |
||
| 12 | use Psr\Log\LoggerInterface; |
||
| 13 | use SilverStripe\View\HTML; |
||
| 14 | |||
| 15 | class SystemInfo implements MonitoringClientInterface |
||
| 16 | { |
||
| 17 | use Configurable; |
||
| 18 | use ClientCommon; |
||
| 19 | use Extensible; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @var string |
||
| 23 | */ |
||
| 24 | private string $clientName = 'systeminfo'; |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 25 | |||
| 26 | /** |
||
| 27 | * @config |
||
| 28 | * @var string |
||
| 29 | */ |
||
| 30 | private static $client_title = 'System info'; |
||
|
0 ignored issues
–
show
|
|||
| 31 | |||
| 32 | /** |
||
| 33 | * @config |
||
| 34 | * @var array |
||
| 35 | */ |
||
| 36 | private static $data_config = []; |
||
|
0 ignored issues
–
show
|
|||
| 37 | |||
| 38 | /** |
||
| 39 | * @var array |
||
| 40 | */ |
||
| 41 | private $warnings = []; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var array |
||
| 45 | */ |
||
| 46 | private $clientData = []; |
||
| 47 | |||
| 48 | |||
| 49 | |||
| 50 | /** |
||
| 51 | * Gathers up any warning states for this module |
||
| 52 | * This should return a numeric array of warning messages |
||
| 53 | * @param array $data |
||
| 54 | * @return array|false |
||
| 55 | */ |
||
| 56 | public function getWarnings($data) |
||
| 57 | { |
||
| 58 | $this->clientData = $data; |
||
| 59 | $this->checkMemoryLimit(); |
||
| 60 | $this->checkPHPVersion(); |
||
| 61 | $allWarnings = $this->warnings; |
||
| 62 | $this->extend('updateWarnings', $allWarnings, $data); |
||
| 63 | return (count($allWarnings) > 0) ? $allWarnings : false; |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Generate the markup for the reporting page |
||
| 68 | * @param $data |
||
| 69 | * @return string |
||
| 70 | */ |
||
| 71 | public function getReport($data) |
||
| 72 | { |
||
| 73 | //Injector::inst()->get(LoggerInterface::class)->info(print_r($data, true)); |
||
| 74 | $this->clientData = $data; |
||
| 75 | return $this->generateReportsHTML(); |
||
| 76 | } |
||
| 77 | |||
| 78 | |||
| 79 | private function generateReportsHTML() { |
||
| 80 | $data = $this->clientData; |
||
| 81 | $variables = ArrayList::create(); |
||
| 82 | $envVariables = false; |
||
| 83 | |||
| 84 | $env = (isset($data['environment'])) ? $data['environment'] : false; |
||
| 85 | if ($env) { |
||
| 86 | unset($data['environment']); |
||
| 87 | $envVariables = ArrayList::create(); |
||
| 88 | foreach ($env['value'] as $variableName => $value) { |
||
| 89 | $envVariables->push(ArrayData::create([ |
||
| 90 | 'Variable' => $variableName, |
||
| 91 | 'Value' => $value |
||
| 92 | ])); |
||
| 93 | } |
||
| 94 | } |
||
| 95 | |||
| 96 | foreach ($data as $id => $values) { |
||
| 97 | $variables->push(ArrayData::create([ |
||
| 98 | 'Variable' => $values['label'], |
||
| 99 | 'Value' => $values['value'] |
||
| 100 | ])); |
||
| 101 | } |
||
| 102 | |||
| 103 | $viewer = new SSViewer('BiffBangPow/SSMonitor/Server/Module/SystemInfo'); |
||
| 104 | return $viewer->process(ArrayData::create([ |
||
| 105 | 'Title' => $this->getClientTitle(), |
||
| 106 | 'Variables' => $variables, |
||
| 107 | 'Environment' => $envVariables |
||
| 108 | ])); |
||
| 109 | } |
||
| 110 | |||
| 111 | |||
| 112 | |||
| 113 | /** |
||
| 114 | * Check the PHP version is OK |
||
| 115 | * @return void |
||
| 116 | */ |
||
| 117 | private function checkPHPVersion() |
||
| 118 | { |
||
| 119 | $config = $this->config()->get('data_config'); |
||
| 120 | if ((isset($config['warnings']['min_php_version'])) && (isset($this->clientData['php']))) { |
||
| 121 | $clientVersion = $this->clientData['php']['value']; |
||
| 122 | $thresholdVersion = $config['warnings']['min_php_version']; |
||
| 123 | if (ReportingHelper::isVersionLess($clientVersion, $thresholdVersion)) { |
||
| 124 | $this->warnings[] = _t( |
||
| 125 | __CLASS__ . '.phpwarning', |
||
| 126 | 'PHP version is below the required value. Required: {required}. Actual: {actual}', |
||
| 127 | [ |
||
| 128 | 'required' => $thresholdVersion, |
||
| 129 | 'actual' => $clientVersion |
||
| 130 | ] |
||
| 131 | ); |
||
| 132 | } |
||
| 133 | } |
||
| 134 | } |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Check that the configured memory is sufficient |
||
| 138 | * @return void |
||
| 139 | */ |
||
| 140 | private function checkMemoryLimit() |
||
| 141 | { |
||
| 142 | $config = $this->config()->get('data_config'); |
||
| 143 | if ((isset($config['warnings']['min_memory_limit'])) && (isset($this->clientData['memorylimit']))) { |
||
| 144 | $threshold = ReportingHelper::convertMemoryStringToBytes($config['warnings']['min_memory_limit']); |
||
| 145 | $actual = ReportingHelper::convertMemoryStringToBytes($this->clientData['memorylimit']['value']); |
||
| 146 | |||
| 147 | if ($actual < $threshold) { |
||
| 148 | $this->warnings[] = _t( |
||
| 149 | __CLASS__ . '.memorywarning', |
||
| 150 | 'Memory limit is below the required value. Required: {required}. Actual: {actual}', |
||
| 151 | [ |
||
| 152 | 'required' => $config['warnings']['min_memory_limit'], |
||
| 153 | 'actual' => $this->clientData['memorylimit']['value'] |
||
| 154 | ] |
||
| 155 | ); |
||
| 156 | } |
||
| 157 | } |
||
| 158 | } |
||
| 159 | |||
| 160 | |||
| 161 | } |
||
| 162 |