core/source/logic/check_kinds/script.ts

150 lines
3 KiB
TypeScript
Raw Normal View History

2024-07-02 15:02:35 +02:00
/*
Copyright 2016-2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
<info@greenscale.de>
»heimdall« is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
»heimdall« is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with »heimdall«. If not, see <http://www.gnu.org/licenses/>.
*/
2023-07-06 16:13:26 +02:00
namespace _heimdall.check_kinds.script
{
/**
*/
function parameters_schema(
) : _heimdall.helpers.json_schema.type_schema
{
return {
"type": "object",
"additionalProperties": false,
"properties": {
"path": {
"type": "string"
},
"arguments": {
"type": "array",
"items": {
"type": "string"
}
},
},
"required": [
"path",
]
};
}
/**
*/
function normalize_order_node(
node : any
) : any
{
if (! ("path" in node)) {
throw new Error("missing mandatory field 'path'");
}
else {
const node_ = Object.assign(
{
"arguments": [],
},
node
);
return node_;
}
}
/**
*/
async function run(
parameters
) : Promise<_heimdall.type_result>
{
2023-07-27 17:37:45 +02:00
let shell_exec_result : (null | _heimdall.helpers.misc.type_shell_exec_result);
let error : any;
try {
shell_exec_result = await _heimdall.helpers.misc.shell_exec(
parameters["path"],
parameters["arguments"],
);
error = null;
}
catch (error_) {
shell_exec_result = null;
error = error_;
}
2023-07-06 16:13:26 +02:00
let condition : _heimdall.enum_condition;
2023-07-27 17:37:45 +02:00
if ((error !== null) || (shell_exec_result === null)) {
lib_plankton.log.notice(
lib_plankton.translate.get("checks.script.execution_failed"),
{
"error": String(error),
}
);
condition = _heimdall.enum_condition.unknown;
}
else {
switch (shell_exec_result.return_code) {
default: {
lib_plankton.log.notice(
lib_plankton.translate.get("checks.script.invalid_return_code"),
{
"return_code": shell_exec_result.return_code,
}
);
condition = _heimdall.enum_condition.unknown;
break;
}
case 0: {
condition = _heimdall.enum_condition.ok;
break;
}
case 1: {
condition = _heimdall.enum_condition.unknown;
break;
}
case 2: {
condition = _heimdall.enum_condition.concerning;
break;
}
case 3: {
condition = _heimdall.enum_condition.critical;
break;
}
2023-07-06 16:13:26 +02:00
}
}
return {
"condition": condition,
"info": {
2023-07-27 17:37:45 +02:00
"result": shell_exec_result,
2023-07-06 16:13:26 +02:00
},
};
2023-07-06 16:13:26 +02:00
}
/**
*/
2023-08-03 08:34:33 +02:00
register_implementation(
"script",
{
2023-07-06 16:13:26 +02:00
"parameters_schema": parameters_schema,
"normalize_order_node": normalize_order_node,
"run": run,
2023-08-03 08:34:33 +02:00
}
);
2023-07-06 16:13:26 +02:00
}