]> git.ipfire.org Git - thirdparty/bacula.git/commitdiff
baculum: Add option to interpret Bacula error codes by API
authorMarcin Haba <marcin.haba@bacula.pl>
Fri, 24 Nov 2023 14:53:48 +0000 (15:53 +0100)
committerMarcin Haba <marcin.haba@bacula.pl>
Fri, 24 Nov 2023 14:53:48 +0000 (15:53 +0100)
15 files changed:
gui/baculum/protected/API/Lang/en/messages.mo
gui/baculum/protected/API/Lang/en/messages.po
gui/baculum/protected/API/Lang/pl/messages.mo
gui/baculum/protected/API/Lang/pl/messages.po
gui/baculum/protected/API/Lang/pt/messages.mo
gui/baculum/protected/API/Lang/pt/messages.po
gui/baculum/protected/API/Lang/ru/messages.mo
gui/baculum/protected/API/Lang/ru/messages.po
gui/baculum/protected/API/Modules/BaculaError.php [new file with mode: 0644]
gui/baculum/protected/API/Modules/Bconsole.php
gui/baculum/protected/API/Pages/API/config.xml
gui/baculum/protected/API/Pages/Panel/APIInstallWizard.php
gui/baculum/protected/API/Pages/Panel/APISettings.page
gui/baculum/protected/API/Pages/Panel/APISettings.php
gui/baculum/protected/Common/Modules/Errors/BaculaError.php [new file with mode: 0644]

index 4308a1686640e03cd05191b7a30fb9986e508fd3..3e410be2e5065afe92ce396ad8aa899b8fb60b35 100644 (file)
Binary files a/gui/baculum/protected/API/Lang/en/messages.mo and b/gui/baculum/protected/API/Lang/en/messages.mo differ
index e1f21b03adfde2a61d06e0abf4793223b16a6384..b7978cbe3e9549db34fd4659cc7ae5e88c723759 100644 (file)
@@ -691,3 +691,6 @@ msgstr "Dedicated Bconsole config"
 
 msgid "Audit log:"
 msgstr "Audit log:"
+
+msgid "Interpret Bacula errors:"
+msgstr "Interpret Bacula errors:"
index b2b349dadce4eefd70115f4f558c02d65aa7d5e1..36e257dee9ccff3e396cd99249f10ba9841e9f0a 100644 (file)
Binary files a/gui/baculum/protected/API/Lang/pl/messages.mo and b/gui/baculum/protected/API/Lang/pl/messages.mo differ
index a25f107d03db335d25e1620b8d792b8f8d538325..3ee6b39aced7f06b6709af93d9d6430a3ac56bcb 100644 (file)
@@ -697,3 +697,6 @@ msgstr "Dedykowana konfiguracja Bconsole"
 
 msgid "Audit log:"
 msgstr "Audit log:"
+
+msgid "Interpret Bacula errors:"
+msgstr "Interpret Bacula errors:"
index 410645c728138e1dc690a9770def34dd65b7d57b..7417a22f521535f10e72e3eddb538b3cad1755ee 100644 (file)
Binary files a/gui/baculum/protected/API/Lang/pt/messages.mo and b/gui/baculum/protected/API/Lang/pt/messages.mo differ
index 1d4583c726fde3e41a79c87d45742c248289a9da..ecf231e86e0e7569d474784efbed0736ac07530d 100644 (file)
@@ -698,3 +698,6 @@ msgstr "Configuração do Bconsole dedicado"
 
 msgid "Audit log:"
 msgstr "Audit log:"
+
+msgid "Interpret Bacula errors:"
+msgstr "Interpret Bacula errors:"
index e5e25c04dda26be1e5e027e20e00e7ecc072c467..388e16fedd1ad8b951383eea46ba0969accdfe86 100644 (file)
Binary files a/gui/baculum/protected/API/Lang/ru/messages.mo and b/gui/baculum/protected/API/Lang/ru/messages.mo differ
index ef9f0a25db9889d61c66ed2c740d598ad84c10ca..c57c270419158839bacb1a6c5c4a87279d698d17 100644 (file)
@@ -698,3 +698,6 @@ msgstr "Выделенная Bconsole"
 
 msgid "Audit log:"
 msgstr "Audit log:"
+
+msgid "Interpret Bacula errors:"
+msgstr "Interpret Bacula errors:"
diff --git a/gui/baculum/protected/API/Modules/BaculaError.php b/gui/baculum/protected/API/Modules/BaculaError.php
new file mode 100644 (file)
index 0000000..aabac54
--- /dev/null
@@ -0,0 +1,81 @@
+<?php
+/*
+ * Bacula(R) - The Network Backup Solution
+ * Baculum   - Bacula web interface
+ *
+ * Copyright (C) 2013-2023 Kern Sibbald
+ *
+ * The main author of Baculum is Marcin Haba.
+ * The original author of Bacula is Kern Sibbald, with contributions
+ * from many others, a complete list can be found in the file AUTHORS.
+ *
+ * You may use this file and others of this release according to the
+ * license defined in the LICENSE file, which includes the Affero General
+ * Public License, v3.0 ("AGPLv3") and some additional permissions and
+ * terms pursuant to its AGPLv3 Section 7.
+ *
+ * This notice must be preserved when any source code is
+ * conveyed and/or propagated.
+ *
+ * Bacula(R) is a registered trademark of Kern Sibbald.
+ */
+
+namespace Baculum\API\Modules;
+
+use Baculum\Common\Modules\Errors\BaculaError as BError;
+
+/**
+ * Parse and handle Bacula errors.
+ *
+ * @author Marcin Haba <marcin.haba@bacula.pl>
+ * @category Error
+ * @package Baculum API
+ */
+class BaculaError extends APIModule {
+
+       const DIRECTOR_ERROR_PATTERN = '/^\s*\[(?P<code>D[A-Z]\d{4})\]/';
+       const STORAGE_ERROR_PATTERN = '/^\s*\[(?P<code>S[A-Z]\d{4})\]/';
+       const CLIENT_ERROR_PATTERN = '/^\s*\[(?P<code>F[A-Z]\d{4})\]/';
+       const CONSOLE_ERROR_PATTERN = '/^\s*\[(?P<code>C[A-Z]\d{4})\]/';
+
+       /**
+        * Find first error in output.
+        *
+        * @param array $output bconsole output
+        * @return array with code, error code and message
+        */
+       public function checkForErrors($output) {
+               $code = null;
+               $error = BError::ERROR_NO_ERRORS;
+               $errmsg = BError::MSG_ERROR_NO_ERRORS;
+               for ($i = 0; $i < count($output); $i++) {
+                       if (preg_match(self::DIRECTOR_ERROR_PATTERN, $output[$i], $match) === 1) {
+                               $code = $match['code'];
+                               $error = BError::ERROR_BACULA_DIRECTOR_ERROR;
+                               $errmsg = BError::MSG_ERROR_BACULA_DIRECTOR_ERROR;
+                               break;
+                       } elseif (preg_match(self::STORAGE_ERROR_PATTERN, $output[$i], $match) === 1) {
+                               $code = $match['code'];
+                               $error = BError::ERROR_BACULA_STORAGE_ERROR;
+                               $errmsg = BError::MSG_ERROR_BACULA_STORAGE_ERROR;
+                               break;
+                       } elseif (preg_match(self::CLIENT_ERROR_PATTERN, $output[$i], $match) === 1) {
+                               $code = $match['code'];
+                               $error = BError::ERROR_BACULA_CLIENT_ERROR;
+                               $errmsg = BError::MSG_ERROR_BACULA_CLIENT_ERROR;
+                               break;
+                       } elseif (preg_match(self::CONSOLE_ERROR_PATTERN, $output[$i], $match) === 1) {
+                               $code = $match['code'];
+                               $error = BError::ERROR_BACULA_CONSOLE_ERROR;
+                               $errmsg = BError::MSG_ERROR_BACULA_CONSOLE_ERROR;
+                               break;
+                       }
+               }
+               $result = [
+                       'code' => $code,
+                       'error' => $error,
+                       'errmsg' => $errmsg
+               ];
+               return $result;
+       }
+}
index acfa03ef4f08b12222ae1de553fe8f23cd9c6e54..a1b6c80daefdcfe0038060a9469e3525a407f24c 100644 (file)
@@ -185,7 +185,26 @@ class Bconsole extends APIModule {
                        }
                }
                $output = array_values($output);
-               return (object)array('output' => $output, 'exitcode' => (integer)$exitcode);
+               $result = [
+                       'output' => $output,
+                       'exitcode' => (integer)$exitcode
+               ];
+               if (key_exists('interpret_bacula_errors', $this->config) && $this->config['interpret_bacula_errors'] == 1) {
+                       $berror = $this->getModule('bacula_error')->checkForErrors($output);
+                       if ($berror['error'] != 0) {
+                               $result = [
+                                       'output' => sprintf(
+                                               'Error: %s, BaculaCode: %s, APIError: %s, Output: %s',
+                                               $berror['errmsg'],
+                                               $berror['code'],
+                                               $berror['error'],
+                                               implode(PHP_EOL, $output)
+                                       ),
+                                       'exitcode' => $berror['error']
+                               ];
+                       }
+               }
+               return (object)$result;
        }
 
        public function bconsoleCommand($director, array $command, $ptype = null, $without_cmd = false) {
index a37b9b0eb55a787d0d44adb9e3bbcc51782810e6..4f14a0ac63a738f1d2cfe60c0f74354304619c09 100644 (file)
@@ -59,6 +59,7 @@
                <module id="delete" class="Baculum\API\Modules\Delete" />
                <module id="list" class="Baculum\API\Modules\BList" />
                <module id="time" class="Baculum\API\Modules\TimeManager" />
+               <module id="bacula_error" class="Baculum\API\Modules\BaculaError" />
                <!-- changer command modules -->
                <module id="changer_command" class="Baculum\API\Modules\ChangerCommand" />
                <!-- plugin modules -->
index cc038205a7d09ffd7c200b756efa380d33fb24f7..cb7f0e4f1da12b2127144679205ab39bffacb592 100644 (file)
@@ -205,6 +205,7 @@ class APIInstallWizard extends BaculumAPIPage {
                $cfg_data['bconsole']['bin_path'] = $this->BconsolePath->Text;
                $cfg_data['bconsole']['cfg_path'] = $this->BconsoleConfigPath->Text;
                $cfg_data['bconsole']['use_sudo'] = (integer)($this->UseSudo->Checked === true);
+               $cfg_data['bconsole']['interpret_bacula_errors'] = 1; // it is enabled by default;
                $cfg_data['jsontools']['enabled'] = (integer)($this->ConfigYes->Checked === true);
                $cfg_data['jsontools']['use_sudo'] = (integer)($this->BJSONUseSudo->Checked === true);
                $cfg_data['jsontools']['bconfig_dir'] = $this->BConfigDir->Text;
index 724d0489c5d87005a6fdcfe050a80c14d24d4c57..c07b91324bc418b6612ea8e4ac8f88cabcdc93e5 100644 (file)
                                        </table>
                                </div>
                        </div>
+                       <div class="w3-row w3-section">
+                               <div class="w3-col w3-quarter">
+                                       <com:TLabel
+                                               ForControl="BconsoleInterpretBaculaErrors"
+                                               Text="<%[ Interpret Bacula errors: ]%>"
+                                       />
+                               </div>
+                               <div class="w3-col w3-threequarter">
+                                       <com:TCheckBox
+                                               ID="BconsoleInterpretBaculaErrors"
+                                               CssClass="w3-check"
+                                       />
+                               </div>
+                       </div>
                </div>
                <div class="w3-row w3-section">
                        <div class="w3-col w3-center">
index 6009e53df4359fc5d54a00d8cec55a9203e20ee6..cfeea719e3c0e677920b19cd2e7b179528c3627a 100644 (file)
@@ -92,6 +92,12 @@ class APISettings extends BaculumAPIPage {
                $this->BconsolePath->Text = $this->config['bconsole']['bin_path'];
                $this->BconsoleConfigPath->Text = $this->config['bconsole']['cfg_path'];
                $this->BconsoleUseSudo->Checked = ($this->config['bconsole']['use_sudo'] == 1);
+               if (key_exists('interpret_bacula_errors', $this->config['bconsole'])) {
+                       $this->BconsoleInterpretBaculaErrors->Checked = ($this->config['bconsole']['interpret_bacula_errors'] == 1);
+               } else {
+                       // Default value if option not specified
+                       $this->BconsoleInterpretBaculaErrors->Checked = true;
+               }
        }
 
        private function loadConfigSettings() {
@@ -423,7 +429,8 @@ class APISettings extends BaculumAPIPage {
                        'enabled' => ($this->BconsoleEnabled->Checked ? 1 : 0),
                        'bin_path' => $this->BconsolePath->Text,
                        'cfg_path' => $this->BconsoleConfigPath->Text,
-                       'use_sudo' => ($this->BconsoleUseSudo->Checked ? 1 : 0)
+                       'use_sudo' => ($this->BconsoleUseSudo->Checked ? 1 : 0),
+                       'interpret_bacula_errors' => ($this->BconsoleInterpretBaculaErrors->Checked ? 1 : 0)
                ];
                $this->config['bconsole'] = $cfg;
                $this->getModule('api_config')->setConfig($this->config);
diff --git a/gui/baculum/protected/Common/Modules/Errors/BaculaError.php b/gui/baculum/protected/Common/Modules/Errors/BaculaError.php
new file mode 100644 (file)
index 0000000..be6ea87
--- /dev/null
@@ -0,0 +1,43 @@
+<?php
+/*
+ * Bacula(R) - The Network Backup Solution
+ * Baculum   - Bacula web interface
+ *
+ * Copyright (C) 2013-2023 Kern Sibbald
+ *
+ * The main author of Baculum is Marcin Haba.
+ * The original author of Bacula is Kern Sibbald, with contributions
+ * from many others, a complete list can be found in the file AUTHORS.
+ *
+ * You may use this file and others of this release according to the
+ * license defined in the LICENSE file, which includes the Affero General
+ * Public License, v3.0 ("AGPLv3") and some additional permissions and
+ * terms pursuant to its AGPLv3 Section 7.
+ *
+ * This notice must be preserved when any source code is
+ * conveyed and/or propagated.
+ *
+ * Bacula(R) is a registered trademark of Kern Sibbald.
+ */
+
+namespace Baculum\Common\Modules\Errors;
+
+/**
+ * Bacula error class.
+ *
+ * @author Marcin Haba <marcin.haba@bacula.pl>
+ * @category Errors
+ * @package Baculum Common
+ */
+class BaculaError extends GenericError {
+
+       const ERROR_BACULA_DIRECTOR_ERROR = 600;
+       const ERROR_BACULA_STORAGE_ERROR = 601;
+       const ERROR_BACULA_CLIENT_ERROR = 602;
+       const ERROR_BACULA_CONSOLE_ERROR = 603;
+
+       const MSG_ERROR_BACULA_DIRECTOR_ERROR = 'Bacula Director error.';
+       const MSG_ERROR_BACULA_STORAGE_ERROR = 'Bacula Storage error.';
+       const MSG_ERROR_BACULA_CLIENT_ERROR = 'Bacula Client error.';
+       const MSG_ERROR_BACULA_CONSOLE_ERROR = 'Bacula Console error.';
+}