From 44ff66445d9fe3decd60d642b301a6e4e5ae4073 Mon Sep 17 00:00:00 2001 From: iglocska Date: Mon, 2 Mar 2020 23:06:31 +0100 Subject: [PATCH] new: [dashboard] Resource widget added --- app/Lib/Dashboard/MispAdminResourceWidget.php | 8 ++- app/Lib/Dashboard/MispAdminSyncTestWidget.php | 62 ++++++++++++++++++ app/Lib/Dashboard/MispAdminWorkerWidget.php | 63 +++++++++++++++++++ 3 files changed, 132 insertions(+), 1 deletion(-) create mode 100644 app/Lib/Dashboard/MispAdminSyncTestWidget.php create mode 100644 app/Lib/Dashboard/MispAdminWorkerWidget.php diff --git a/app/Lib/Dashboard/MispAdminResourceWidget.php b/app/Lib/Dashboard/MispAdminResourceWidget.php index 2728a4d72..a3d499560 100644 --- a/app/Lib/Dashboard/MispAdminResourceWidget.php +++ b/app/Lib/Dashboard/MispAdminResourceWidget.php @@ -8,6 +8,7 @@ class MispAdminResourceWidget public $height = 2; public $params = array(); public $description = 'Basic widget showing some server statistics in regards to MISP.'; + public $cacheLifetime = 5; public function handler($user, $options = array()) @@ -18,7 +19,12 @@ class MispAdminResourceWidget if ($redis) { $memory_stats = round($redis->rawCommand('memory', 'stats')[3] / 1024 / 1024) . 'M'; $data[] = array( - 'title' => __('Redis memory usage'), + 'title' => __('Current Redis memory usage'), + 'value' => h($memory_stats) + ); + $memory_stats = round($redis->rawCommand('memory', 'stats')[1] / 1024 / 1024) . 'M'; + $data[] = array( + 'title' => __('Peak Redis memory usage'), 'value' => h($memory_stats) ); } diff --git a/app/Lib/Dashboard/MispAdminSyncTestWidget.php b/app/Lib/Dashboard/MispAdminSyncTestWidget.php new file mode 100644 index 000000000..f61989731 --- /dev/null +++ b/app/Lib/Dashboard/MispAdminSyncTestWidget.php @@ -0,0 +1,62 @@ +Server = ClassRegistry::init('Server'); + $servers = $this->Server->find('all', array( + 'fields' => array('id', 'url', 'name', 'pull', 'push', 'caching_enabled'), + 'conditions' => array('OR' => array('pull' => 1, 'push' => 1, 'caching_enabled' => 1)), + 'recursive' => -1 + )); + $syncTestErrorCodes = $this->Server->syncTestErrorCodes; + foreach ($servers as $server) { + $result = $this->Server->runConnectionTest($server['Server']['id']); + if ($result['status'] === 1) { + $message = __('Connected.'); + $colour = 'green'; + $flags = json_decode($result['message'], true); + if (empty($flags['perm_sync'])) { + $colour = 'orange'; + $message .= ' ' . __('No sync access.'); + } + if (empty($flags['perm_sighting'])) { + $colour = 'orange'; + $message .= ' ' . __('No sighting access.'); + } + } else { + $colour = 'red'; + $message = $syncTestErrorCodes[$result['status']]; + } + $data[] = array( + 'title' => sprintf( + 'Server #%s (%s - %s)', + h($server['Server']['id']), + h($server['Server']['name']), + h($server['Server']['url']) + ), + 'value' => h($message), + 'class' => $colour + ); + } + return $data; + } + + public function checkPermissions($user) + { + if (empty($user['Role']['perm_site_admin'])) { + return false; + } + return true; + } +} diff --git a/app/Lib/Dashboard/MispAdminWorkerWidget.php b/app/Lib/Dashboard/MispAdminWorkerWidget.php new file mode 100644 index 000000000..588474293 --- /dev/null +++ b/app/Lib/Dashboard/MispAdminWorkerWidget.php @@ -0,0 +1,63 @@ +Server = ClassRegistry::init('Server'); + $workerIssueCount = array(); + $results = $this->Server->workerDiagnostics($workerIssueCount); + $data = array(); + foreach ($results as $queueName => $queue) { + if (in_array($queueName, array('controls', 'proc_accessible'))) { + continue; + } + $total = 0; + $alive = 0; + foreach ($queue['workers'] as $worker) { + if ($worker['alive']) { + $alive += 1; + } + $total += 1; + } + $colour = 'green'; + if ($alive == 0) { + $colour = 'red'; + } else { + if ($alive != $total) { + $colour = 'orange'; + } + } + $data[] = array( + 'title' => h($queueName) . ' workers alive', + 'value' => sprintf('[%s/%s]', $alive, $total), + 'class' => $colour + ); + $data[] = array( + 'title' => h($queueName) . ' jobs pending', + 'value' => empty($queue['jobCount']) ? '0' : h($queue['jobCount']) + ); + $data[] = array( + 'type' => 'gap' + ); + } + return $data; + } + + public function checkPermissions($user) + { + if (empty($user['Role']['perm_site_admin'])) { + return false; + } + return true; + } +}