core/source/logic/check_kinds/script.ts

131 lines
2.3 KiB
TypeScript
Raw Permalink Normal View History

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
}