From 06bba6c2b212f98c3058ff7c2610b833b4685c00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A9lix=20Su=C3=A1rez?= Date: Thu, 27 Jul 2023 11:58:06 -0600 Subject: [PATCH] Add events and notifications --- .../include/class/ConsoleSupervisor.php | 42 +++++++++++++++++++ .../include/functions_notifications.php | 1 + .../include/functions_update_manager.php | 11 +++++ pandora_server/bin/pandora_server | 16 ++++++- 4 files changed, 69 insertions(+), 1 deletion(-) diff --git a/pandora_console/include/class/ConsoleSupervisor.php b/pandora_console/include/class/ConsoleSupervisor.php index 9142f9a692..e4409e10fb 100644 --- a/pandora_console/include/class/ConsoleSupervisor.php +++ b/pandora_console/include/class/ConsoleSupervisor.php @@ -210,6 +210,12 @@ class ConsoleSupervisor $this->checkUpdateManagerRegistration(); + /* + * Check if has API access. + * NOTIF.API.ACCESS + */ + $this->checkApiAccess(); + /* * Check if there're new messages in UM. * NOTIF.UPDATEMANAGER.MESSAGES @@ -434,6 +440,12 @@ class ConsoleSupervisor $this->checkUpdateManagerRegistration(); + /* + * Check if has API access. + * NOTIF.API.ACCESS + */ + $this->checkApiAccess(); + /* * Check if event storm protection is activated. * NOTIF.MISC.EVENTSTORMPROTECTION @@ -498,6 +510,12 @@ class ConsoleSupervisor $this->checkUpdateManagerRegistration(); + /* + * Check if has API access. + * NOTIF.API.ACCESS + */ + $this->checkApiAccess(); + /* * Check if there're new messages in UM. * NOTIF.UPDATEMANAGER.MESSAGES @@ -865,6 +883,7 @@ class ConsoleSupervisor case 'NOTIF.METACONSOLE.DB_CONNECTION': case 'NOTIF.DOWNTIME': case 'NOTIF.UPDATEMANAGER.REGISTRATION': + case 'NOTIF.API.ACCESS': case 'NOTIF.MISC.EVENTSTORMPROTECTION': case 'NOTIF.MISC.DEVELOPBYPASS': case 'NOTIF.MISC.FONTPATH': @@ -2366,6 +2385,29 @@ class ConsoleSupervisor } } + /** + * Check if has access to the API + * + * @return void + */ + public function checkApiAccess() + { + global $config; + include_once $config['homedir'].'/include/functions_update_manager.php'; + + + if (update_manager_verify_api() === false) { + $this->notify( + [ + 'type' => 'NOTIF.API.ACCESS', + 'title' => __('Cannot access the Pandora FMS API '), + 'message' => __('Please check the configuration, some components may fail due to this misconfiguration.'), + ] + ); + } else { + $this->cleanNotifications('NOTIF.API.ACCESS'); + } + } /** * Check if user 'admin' is enabled and using default password. diff --git a/pandora_console/include/functions_notifications.php b/pandora_console/include/functions_notifications.php index 170612d1f4..76fdcd60e6 100644 --- a/pandora_console/include/functions_notifications.php +++ b/pandora_console/include/functions_notifications.php @@ -145,6 +145,7 @@ function notifications_get_subtypes(?string $source=null) 'NOTIF.METACONSOLE.DB_CONNECTION', 'NOTIF.DOWNTIME', 'NOTIF.UPDATEMANAGER.REGISTRATION', + 'NOTIF.API.ACCESS', 'NOTIF.MISC.EVENTSTORMPROTECTION', 'NOTIF.MISC.DEVELOPBYPASS', 'NOTIF.MISC.FONTPATH', diff --git a/pandora_console/include/functions_update_manager.php b/pandora_console/include/functions_update_manager.php index d564d363c5..9fe87cae13 100755 --- a/pandora_console/include/functions_update_manager.php +++ b/pandora_console/include/functions_update_manager.php @@ -54,6 +54,17 @@ function update_manager_verify_registration() return false; } +/** + * Verifies api state. + * + * @return boolean Status. + */ +function update_manager_verify_api() +{ + global $config; + return (isset($config['has_api_access']) === true && $config['has_api_access'] == '1'); +} + /** * Retrieves current update from DB. diff --git a/pandora_server/bin/pandora_server b/pandora_server/bin/pandora_server index 6152136a77..b161bd57bb 100755 --- a/pandora_server/bin/pandora_server +++ b/pandora_server/bin/pandora_server @@ -733,15 +733,29 @@ sub main() { my $command = $Config{'plugin_exec'}.' 30 curl --cookie-jar /tmp/cron-session-cookies '.$curl_execution.' 2>/dev/null'; my $exe_testing_api = `$command`; my @res_testing_api = split(',', $exe_testing_api); + my $has_api_access = undef; + if ( $res_testing_api[0] ne 'OK' ) { logger(\%Config, "Warning! The server does not have access to the API, this can trigger problems in the generation of reports and graphs.", 1); - pandora_event (\%Config, "Server does not have access to the API", 0, 0, 0, 0, 0, 'system', 0, $DBH); + pandora_event (\%Config, "The server " . $Config{'servername'} ." cannot access the Pandora FMS API through the supplied credentials. Please check the configuration, some components may fail due to this misconfiguration. Look in the manual for the configuration tokens related to console_api_pass.", 0, 0, 0, 0, 0, 'system', 0, $DBH); + $has_api_access = "0"; } else { # Test successful. pandora_set_tconfig_token($DBH, 'internal_user_pass', pandora_input_password(\%Config, $Config{"console_pass"}) ); pandora_set_tconfig_token($DBH, 'internal_user', $Config{"console_user"}); + $has_api_access = "1"; + } + + my $token = "has_api_access"; + my $prev_access = pandora_get_config_value($DBH, $token); + + if(defined($prev_access) && $prev_access ne "") { + db_update ($DBH, 'UPDATE tconfig SET value = ? WHERE token = ?', $has_api_access, $token); + } + else { + db_insert ($DBH, 'id_config', 'INSERT INTO tconfig (token, value) VALUES (?, ?)', $token, $has_api_access); } # Generate 'going up' events