core/source/logic/check_kinds/script.ts

142 lines
2.4 KiB
TypeScript
Raw 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>
{
const nm_child_process = require("child_process");
type type_result = {
return_code : int;
stdout : string;
stderr : string;
};
/**
* @see https://nodejs.org/api/child_process.html#child_processspawncommand-args-options
*/
const result : type_result = await new Promise<type_result>(
(resolve, reject) => {
nm_child_process.spawnSync(
parameters["path"],
parameters["arguments"],
{
},
(result) => {
if (result.error) {
reject(result.error);
}
else {
resolve(
{
"return_code": result.status,
"stdout": result.stdout,
"stderr": result.stdin,
}
);
}
}
);
}
);
let condition : _heimdall.enum_condition;
switch (result.return_code) {
default: {
lib_plankton.log.notice(
lib_plankton.translate.get("check_kind_script_invalid_return_code"),
{
"return_code": 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;
}
}
return {
"condition": condition,
"info": {
"result": result,
},
};
2023-07-06 16:13:26 +02:00
}
/**
*/
export function check_kind_implementation(
) : type_check_kind
{
return {
"parameters_schema": parameters_schema,
"normalize_order_node": normalize_order_node,
"run": run,
};
}
}