From ef8b58df2926993736aac053a693040ade2dbc40 Mon Sep 17 00:00:00 2001 From: Marcin Haba Date: Mon, 13 Nov 2023 10:28:31 +0100 Subject: [PATCH] baculum: Add endpoint to get device disk usage on storage daemon host --- .../API/Pages/API/PluginCoreDiskUsage.php | 163 ++++++++++++++++++ .../protected/API/Pages/API/endpoints.xml | 1 + .../protected/API/openapi_baculum.json | 41 +++++ 3 files changed, 205 insertions(+) create mode 100644 gui/baculum/protected/API/Pages/API/PluginCoreDiskUsage.php diff --git a/gui/baculum/protected/API/Pages/API/PluginCoreDiskUsage.php b/gui/baculum/protected/API/Pages/API/PluginCoreDiskUsage.php new file mode 100644 index 000000000..fcdbd07f6 --- /dev/null +++ b/gui/baculum/protected/API/Pages/API/PluginCoreDiskUsage.php @@ -0,0 +1,163 @@ + + * @category API + * @package Baculum API + */ +class PluginCoreDiskUsage extends ConsoleOutputQueryPage { + + /** + * Usage properties taken into account on parsing output. + */ + private static $parse_props = [ + 'mount_point', + 'free_space', + 'total_space' + ]; + + public function get() { + $misc = $this->getModule('misc'); + $storageid = $this->Request->contains('id') && $misc->isValidInteger($this->Request['id']) ? (int)$this->Request['id'] : 0; + $result = $this->getModule('bconsole')->bconsoleCommand( + $this->director, + ['.storage'], + null, + true + ); + if ($result->exitcode === 0) { + $storage_val = $this->getModule('storage')->getStorageById($storageid); + if (is_object($storage_val) && in_array($storage_val->name, $result->output)) { + $storage = $storage_val->name; + } else { + $this->output = StorageError::MSG_ERROR_STORAGE_DOES_NOT_EXISTS; + $this->error = StorageError::ERROR_STORAGE_DOES_NOT_EXISTS; + return; + } + } else { + $this->output = PluginError::MSG_ERROR_WRONG_EXITCODE; + $this->error = PluginError::ERROR_WRONG_EXITCODE; + return; + } + $params = [ + 'storage' => $storage + ]; + + + $out_format = ConsoleOutputPage::OUTPUT_FORMAT_RAW; + if ($this->Request->contains('output') && $this->isOutputFormatValid($this->Request['output'])) { + $out_format = $this->Request['output']; + } + + $out = new \StdClass; + $out->output = []; + if ($out_format === ConsoleOutputPage::OUTPUT_FORMAT_RAW) { + $out = $this->getRawOutput($params); + } elseif($out_format === ConsoleOutputPage::OUTPUT_FORMAT_JSON) { + $out = $this->getJSONOutput($params); + } + $this->output = $out->output; + if ($out->exitcode != PluginError::ERROR_EXECUTING_PLUGIN_QUERY_COMMAND) { + if ($out->exitcode != 0) { + $this->error = PluginError::ERROR_WRONG_EXITCODE; + $this->output = PluginError::MSG_ERROR_WRONG_EXITCODE; + } else { + $this->error = PluginError::ERROR_NO_ERRORS; + } + } + } + + /** + * Get diskusage command output in raw format. + * + * @param array $params command parameters + * @return StdClass object with output and exitcode + */ + protected function getRawOutput($params = []) { + $ret = $this->getModule('bconsole')->bconsoleCommand( + $this->director, + [ + '.query', + 'plugin="core:"', + 'storage="' . $params['storage'] . '"', + 'parameter="diskusage"' + ], + null, + true + ); + if ($ret->exitcode != 0) { + $this->getModule('logging')->log( + Logging::CATEGORY_EXECUTE, + 'Wrong output from RAW .query diskusage: ' . implode(PHP_EOL, $ret->output) + ); + $ret->output = []; // don't provide errors to output, only in logs + } elseif ($this->isError($ret->output)) { + $ret->exitcode = PluginError::ERROR_EXECUTING_PLUGIN_QUERY_COMMAND; + } + return $ret; + } + + /** + * Get query diskusage command output in JSON format. + * + * @param array $params command parameters + * @return StdClass object with output and exitcode + */ + protected function getJSONOutput($params = []) { + $result = $this->getRawOutput($params); + if ($result->exitcode === 0) { + $result->output = $this->getItemRows($result->output); + } + return $result; + } + + /** + * Parse and get rows with items from output. + * + * @param array $output dot query command output + * @return array parsed output + */ + private function getItemRows(array $output) { + $out = []; + $part = []; + $pattern = '/^(?P\w+)=(?.+)$/i'; + for ($i = 0; $i < count($output); $i++) { + if (preg_match($pattern, $output[$i], $match) === 1 && in_array($match['key'], self::$parse_props)) { + $part[$match['key']] = $match['val']; + } elseif (count($part) > 0) { + $out[] = $part; + $part = []; + } + } + return $out; + } +} diff --git a/gui/baculum/protected/API/Pages/API/endpoints.xml b/gui/baculum/protected/API/Pages/API/endpoints.xml index 9943b5f48..12e1a4f9c 100644 --- a/gui/baculum/protected/API/Pages/API/endpoints.xml +++ b/gui/baculum/protected/API/Pages/API/endpoints.xml @@ -151,6 +151,7 @@ + diff --git a/gui/baculum/protected/API/openapi_baculum.json b/gui/baculum/protected/API/openapi_baculum.json index c36297ef7..fd8f95641 100644 --- a/gui/baculum/protected/API/openapi_baculum.json +++ b/gui/baculum/protected/API/openapi_baculum.json @@ -10355,6 +10355,47 @@ ] } }, + "/api/v2/plugins/core/{storageid}/storage/diskusage": { + "get": { + "tags": ["plugins"], + "summary": "Check usage of the storage daemon disk devices",, + "description": "Check usage of the storage daemon disk devices.", + "responses": { + "200": { + "description": "Check usage of the storage daemon disk devices.", + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "output": { + "type": "array", + "items": { + "description": "Usage results per archive device", + "type": "string" + } + }, + "error": { + "type": "integer", + "description": "Error code", + "enum": [0, 1, 2, 3, 4, 5, 6, 7, 9, 11, 20, 150, 1000] + } + } + } + } + } + } + }, + "parameters": [ + { + "$ref": "#/components/parameters/StorageId" + }, + { + "$ref": "#/components/parameters/Output" + } + ] + } + }, "/api/v2/plugins/m365/{clientid}/tenants": { "get": { "tags": ["plugins"], -- 2.47.3