From 4f5455151700ed1c2cdac1cfff13764fbbd87b43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Fra=C3=9F?= Date: Thu, 6 Jul 2023 16:13:26 +0200 Subject: [PATCH] [add] check_kind:script --- source/logic/check_kinds/file_state.ts | 66 +++++++++-- source/logic/check_kinds/http_request.ts | 9 +- source/logic/check_kinds/script.ts | 141 +++++++++++++++++++++++ source/logic/helpers/json_schema.ts | 8 +- source/logic/main.ts | 1 + source/logic/order.ts | 46 +++++--- tools/heimdall.prj.json | 1 + 7 files changed, 238 insertions(+), 34 deletions(-) create mode 100644 source/logic/check_kinds/script.ts diff --git a/source/logic/check_kinds/file_state.ts b/source/logic/check_kinds/file_state.ts index 86cf1a2..e6764d8 100644 --- a/source/logic/check_kinds/file_state.ts +++ b/source/logic/check_kinds/file_state.ts @@ -25,26 +25,54 @@ namespace _heimdall.check_kinds.file_state }, "age_threshold_concerning": { "description": "in seconds; ignored if 'exist_mode' is set to false", - "type": ["null", "integer"], - "exclusiveMinimum": 0, + "anyOf": [ + { + "type": "null", + }, + { + "type": "integer", + "exclusiveMinimum": 0, + }, + ], "default": null, }, "age_threshold_critical": { "description": "in seconds; ignored if 'exist_mode' is set to false", - "type": ["null", "integer"], - "exclusiveMinimum": 0, + "anyOf": [ + { + "type": "null", + }, + { + "type": "integer", + "exclusiveMinimum": 0, + }, + ], "default": null, }, "size_threshold_concerning": { "description": "in bytes; ignored if 'exist_mode' is set to false", - "type": ["null", "integer"], - "exclusiveMinimum": 0, + "anyOf": [ + { + "type": "null", + }, + { + "type": "integer", + "exclusiveMinimum": 0, + }, + ], "default": null, }, "size_threshold_critical": { "description": "in bytes; ignored if 'exist_mode' is set to false", - "type": ["null", "integer"], - "exclusiveMinimum": 0, + "anyOf": [ + { + "type": "null", + }, + { + "type": "integer", + "exclusiveMinimum": 0, + }, + ], "default": null, }, @@ -64,15 +92,29 @@ namespace _heimdall.check_kinds.file_state "age_threshold": { "deprecated": true, "description": "", - "type": ["null", "integer"], - "exclusiveMinimum": 0, + "anyOf": [ + { + "type": "null", + }, + { + "type": "integer", + "exclusiveMinimum": 0, + }, + ], "default": null, }, "size_threshold": { "deprecated": true, "description": "", - "type": ["null", "integer"], - "exclusiveMinimum": 0, + "anyOf": [ + { + "type": "null", + }, + { + "type": "integer", + "exclusiveMinimum": 0, + }, + ], "default": null, }, }, diff --git a/source/logic/check_kinds/http_request.ts b/source/logic/check_kinds/http_request.ts index 8f09636..9c240f5 100644 --- a/source/logic/check_kinds/http_request.ts +++ b/source/logic/check_kinds/http_request.ts @@ -47,7 +47,14 @@ namespace _heimdall.check_kinds.http_request "properties": { "status_code": { "description": "checks whether the response status code is this", - "type": ["null", "integer"], + "anyOf": [ + { + "type": "null", + }, + { + "type": "integer", + }, + ], "default": 200 }, "headers": { diff --git a/source/logic/check_kinds/script.ts b/source/logic/check_kinds/script.ts new file mode 100644 index 0000000..38f7356 --- /dev/null +++ b/source/logic/check_kinds/script.ts @@ -0,0 +1,141 @@ +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( + (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, + }, + } + } + + + /** + */ + export function check_kind_implementation( + ) : type_check_kind + { + return { + "parameters_schema": parameters_schema, + "normalize_order_node": normalize_order_node, + "run": run, + }; + } + +} diff --git a/source/logic/helpers/json_schema.ts b/source/logic/helpers/json_schema.ts index 5729a11..7276721 100644 --- a/source/logic/helpers/json_schema.ts +++ b/source/logic/helpers/json_schema.ts @@ -3,6 +3,7 @@ namespace _heimdall.helpers.json_schema /** */ + /* type type_name = ( "null" | @@ -18,6 +19,7 @@ namespace _heimdall.helpers.json_schema | "object" ); + */ /** @@ -88,16 +90,12 @@ namespace _heimdall.helpers.json_schema } | { - type ?: "array"; + type : "array"; items ?: type_schema; default ?: Array; enum ?: Array>; } | - { - type ?: Array; - } - | { anyOf ?: Array; } diff --git a/source/logic/main.ts b/source/logic/main.ts index 1e2c564..49ac0dc 100644 --- a/source/logic/main.ts +++ b/source/logic/main.ts @@ -245,6 +245,7 @@ async function main( "console": _heimdall.notification_kinds.console.notification_kind_implementation(), }; const check_kind_implementations : Record = { + "script": _heimdall.check_kinds.script.check_kind_implementation(), "http_request": _heimdall.check_kinds.http_request.check_kind_implementation(), "file_state": _heimdall.check_kinds.file_state.check_kind_implementation(), }; diff --git a/source/logic/order.ts b/source/logic/order.ts index 52ffb3d..2c74233 100644 --- a/source/logic/order.ts +++ b/source/logic/order.ts @@ -49,23 +49,37 @@ namespace _heimdall.order ) : _heimdall.helpers.json_schema.type_schema { return { - "anyOf": [ - { - "description": "in seconds", - "type": (allow_null ? "integer" : ["null", "integer"]), - "exclusiveMinimum": 0, - }, - { - "description": "as text", - "type": "string", - "enum": [ - "minute", - "hour", - "day", - "week", + "anyOf": ( + [] + .concat( + allow_null + ? [ + { + "type": "null", + } ] - }, - ], + : [] + ) + .concat( + [ + { + "description": "in seconds", + "type": "integer", + "exclusiveMinimum": 0, + }, + { + "description": "as text", + "type": "string", + "enum": [ + "minute", + "hour", + "day", + "week", + ] + }, + ] + ) + ), "default": default_, }; } diff --git a/tools/heimdall.prj.json b/tools/heimdall.prj.json index be39629..2a1bd00 100644 --- a/tools/heimdall.prj.json +++ b/tools/heimdall.prj.json @@ -25,6 +25,7 @@ "source/logic/notification_kinds/_abstract.ts", "source/logic/notification_kinds/console.ts", "source/logic/check_kinds/_abstract.ts", + "source/logic/check_kinds/script.ts", "source/logic/check_kinds/http_request.ts", "source/logic/check_kinds/file_state.ts", "source/logic/state_repository.ts",