From 238569ef02188d5fa893ca0b57b9f350b16d8ce3 Mon Sep 17 00:00:00 2001 From: mh Date: Sat, 18 Oct 2025 21:05:47 +0200 Subject: [PATCH] Upgrade to CMK 2.4 --- check_mk_plugin/.gitignore | 1 + check_mk_plugin/agent_based/jobwatch_check.py | 125 ------------------ .../agent_based/jobwatch_check.py | 120 +++++++++++++++++ .../graphing/jobwatch_metrics.py | 18 +++ check_mk_plugin/collect.sh | 22 --- check_mk_plugin/jobwatch-1.0.mkp | Bin 1853 -> 0 bytes check_mk_plugin/jobwatch-2.0.0.mkp | Bin 0 -> 1602 bytes 7 files changed, 139 insertions(+), 147 deletions(-) create mode 100644 check_mk_plugin/.gitignore delete mode 100755 check_mk_plugin/agent_based/jobwatch_check.py create mode 100644 check_mk_plugin/cmk_addons_plugins/agent_based/jobwatch_check.py create mode 100644 check_mk_plugin/cmk_addons_plugins/graphing/jobwatch_metrics.py delete mode 100644 check_mk_plugin/collect.sh delete mode 100644 check_mk_plugin/jobwatch-1.0.mkp create mode 100644 check_mk_plugin/jobwatch-2.0.0.mkp diff --git a/check_mk_plugin/.gitignore b/check_mk_plugin/.gitignore new file mode 100644 index 0000000..8845225 --- /dev/null +++ b/check_mk_plugin/.gitignore @@ -0,0 +1 @@ +**/__gitignore__ diff --git a/check_mk_plugin/agent_based/jobwatch_check.py b/check_mk_plugin/agent_based/jobwatch_check.py deleted file mode 100755 index 123fff1..0000000 --- a/check_mk_plugin/agent_based/jobwatch_check.py +++ /dev/null @@ -1,125 +0,0 @@ -#!/usr/bin/env python3 -# -*- coding: utf-8 -*- - - -import json -from operator import concat -from typing import Any, Dict, Generator, Union -from cmk.base.api.agent_based.checking_classes import IgnoreResults, IgnoreResultsError -from cmk.base.api.agent_based.type_defs import Parameters - -from typing import Any, Dict, Generator, Iterable -from cmk.base.api.agent_based.type_defs import Parameters, StringTable -from cmk.base.plugins.agent_based.agent_based_api.v1 import ( - register, - HostLabel -) -from cmk.base.plugins.agent_based.agent_based_api.v1 import ( - register, - render, - Result, Metric, State, - Service, ServiceLabel, - check_levels, - get_value_store, -) - - -# -# Section Plugin -# - - -def parse_jobwatch(string_table: StringTable) -> Dict: - #print("Agent-PARSE", string_table) - js = "" - for ln in string_table: - js += "".join(ln) - - data = json.loads(js) - - res = {} - for user in data: - uid = user.get("user_id") - if not uid: - continue - - entries = {} - for e in user.get("entries", []): - state = e.get("state") - if not state: - continue - - job_full_id = state.get("job_id") - if not job_full_id: - continue - - ji = state.get("job_inst_id") - if ji: - job_full_id += "_" +ji - - entries[job_full_id] = { - "state": state, - "log_entries": state.get("log_entries", []) - } - res[uid] = entries - - #print(res) - return res # return a parsed section - - -register.agent_section( - name="jobwatch", - parse_function=parse_jobwatch, -) - - - -# -# Check Plugin -# - -def discover_jobwatch(section: Any) -> Generator[Service, None, None]: - #print("D", section) - for uid, usr_data in section.items(): - for job_full_id, job_data in usr_data.items(): - state = job_data.get("state") - if not state: - continue - - item = uid + "/" + job_full_id - yield Service(item=item) - - - -def check_jobwatch(item: str, section: Any) -> Generator[Union[Result,Metric], None, None]: - uid, job_full_id = item.split("/", 2) - - state = section \ - .get(uid, {}) \ - .get(job_full_id, {}) \ - .get("state") - - if state: - yield Metric( - name="last_run", - value=state.get("last_run_age_seconds"), - ) - - yield Result( - state=State(state.get('last_run_age_state', 3)), - summary=f"last run at { state.get('last_run')}, {render.timespan(state.get('last_run_age_seconds'))} ago" - ) - - yield Result( - state=State(state.get('last_state', 3)), - summary=f"last exitcode { state.get('last_exit_code')}" - ) - -register.check_plugin( - name="jobwatch", - service_name="Job %s", - check_function=check_jobwatch, - discovery_function=discover_jobwatch, - - -) diff --git a/check_mk_plugin/cmk_addons_plugins/agent_based/jobwatch_check.py b/check_mk_plugin/cmk_addons_plugins/agent_based/jobwatch_check.py new file mode 100644 index 0000000..0764155 --- /dev/null +++ b/check_mk_plugin/cmk_addons_plugins/agent_based/jobwatch_check.py @@ -0,0 +1,120 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + + +from typing import Any, Dict + +import json +from typing import Dict + + + +from cmk.agent_based.v2 import ( + AgentSection, + CheckPlugin, + CheckResult, + DiscoveryResult, + render, + Result, + Metric, + Service, + State, + StringTable, +) + +# +# Section Plugin +# + + +def parse_jobwatch(string_table: StringTable) -> Dict: + # print("Agent-PARSE", string_table) + js = "" + for ln in string_table: + js += "".join(ln) + + data = json.loads(js) + + res = {} + for user in data: + uid = user.get("user_id") + if not uid: + continue + + entries = {} + for e in user.get("entries", []): + state = e.get("state") + if not state: + continue + + job_full_id = state.get("job_id") + if not job_full_id: + continue + + ji = state.get("job_inst_id") + if ji: + job_full_id += "_" + ji + + entries[job_full_id] = { + "state": state, + "log_entries": state.get("log_entries", []), + } + res[uid] = entries + + # print(res) + return res # return a parsed section + + +agent_section_jobwatch = AgentSection( + name="jobwatch", + parse_function=parse_jobwatch, +) + + +# +# Check Plugin +# + + +def discover_jobwatch(section: Any) -> DiscoveryResult: + # print("D", section) + for uid, usr_data in section.items(): + for job_full_id, job_data in usr_data.items(): + state = job_data.get("state") + if not state: + continue + + item = uid + "/" + job_full_id + yield Service(item=item) + + +def check_jobwatch( + item: str, section: Any +) -> CheckResult: + uid, job_full_id = item.split("/", 2) + + state = section.get(uid, {}).get(job_full_id, {}).get("state") + + if state: + yield Metric( + name="last_run", + value=state.get("last_run_age_seconds"), + ) + + yield Result( + state=State(state.get("last_run_age_state", 3)), + summary=f"last run at { state.get('last_run')}, {render.timespan(state.get('last_run_age_seconds'))} ago", + ) + + yield Result( + state=State(state.get("last_state", 3)), + summary=f"last exitcode { state.get('last_exit_code')}", + ) + + +check_plugin_jobwatch = CheckPlugin( + name="jobwatch", + service_name="Job %s", + check_function=check_jobwatch, + discovery_function=discover_jobwatch, +) diff --git a/check_mk_plugin/cmk_addons_plugins/graphing/jobwatch_metrics.py b/check_mk_plugin/cmk_addons_plugins/graphing/jobwatch_metrics.py new file mode 100644 index 0000000..8aeb5e5 --- /dev/null +++ b/check_mk_plugin/cmk_addons_plugins/graphing/jobwatch_metrics.py @@ -0,0 +1,18 @@ +#!/usr/bin/env python3 + +from cmk.graphing.v1 import Title +from cmk.graphing.v1.metrics import ( + Color, + TimeNotation, + Metric, + Unit, +) + +UNIT_S = Unit(TimeNotation()) + +metric_last_run = Metric( + name="last_run", + title=Title("Last run"), + unit=UNIT_S, + color=Color.CYAN, +) diff --git a/check_mk_plugin/collect.sh b/check_mk_plugin/collect.sh deleted file mode 100644 index 4270225..0000000 --- a/check_mk_plugin/collect.sh +++ /dev/null @@ -1,22 +0,0 @@ -#!/bin/bash - -SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )" -cd $SCRIPT_DIR - -files=( - "local/lib/check_mk/base/plugins/agent_based/jobwatch_check.py:agent_based" - "local/share/check_mk/web/plugins/metrics/jobwatch_metrics.py:web/plugins/metrics" -) - - -for f in ${files[*]}; do - s="$(echo $f | cut -d ":" -f 1)" - d="$(echo $f | cut -d ":" -f 2)" - src=$OMD_ROOT/$s - dest=$d - echo "$src -> $dest" - mkdir -p $dest - cp -L $src $dest -done - -mkp pack jobwatch \ No newline at end of file diff --git a/check_mk_plugin/jobwatch-1.0.mkp b/check_mk_plugin/jobwatch-1.0.mkp deleted file mode 100644 index bc43332fc2fa3c1fd7a6a30ad0033c3b97d7cf5b..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1853 zcma*hc{~#e0KjpC5Um`~axKaoF*mtZ?#G;!$uLH4a!12cBXYeWN9Kc%-ke)R%^dTX zjpllC=gzq;cgwl=_xtbt_4|DP`+>6f_}=b_Ah_5;;fOe&Xy3ia1~=S7e2f&o<7^&4VZ5cSM7)+;^@17n?q6y~YUz>T7+L7o#OgAo%UNk4diT_lCsY zkPiQ}E2BA+*Bv{h3?*%12b(8-2r_Wt=c*+V35==xyCBbooZMi6gcwMrI{^!f3&4Z$yCkFK_;krz_xSp%jc;WqWNY!G7O~ z>fGU4i)rQjn)o)9q`l4Hnv$UlZkuz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