CMK version:
RAW 2.3.0p29
OS version:
Red Hat Enterprise Linux 8.10
Error message:
check_mk return (null) message in Summary field. However script working well in CLI. I do not understand whyi I think there is no issue with output format etc.
Script below:
#!/usr/bin/env python3
import sys
import dbm.gnu as dbm
import os
import argparse
DB_PATH = "/var/tmp/mycmdb.pag"
EXIT_OK = 0
EXIT_WARN = 1
EXIT_CRIT = 2
EXIT_UNKNOWN = 3
def exit_with(code, message):
print(f"{code} cmdb-check - {message}")
sys.exit(code)
def normalize_hostname(name):
return name.lower().split('.')[0]
def parse_args():
hostname = None
alias = None
args = sys.argv[1:]
for i in range(len(args)):
if args[i] in ("-H", "--hostname") and i + 1 < len(args):
hostname = normalize_hostname(args[i + 1])
if args[i] in ("-A", "--alias") and i + 1 < len(args):
alias = normalize_hostname(args[i + 1])
if not hostname:
exit_with(EXIT_UNKNOWN, "Usage: check_cmdb.py -H <hostname> [-A <alias>]")
return hostname, alias
def check_cmdb_entry(db, key, is_alias=False):
try:
value = db[key].decode("utf-8")
fields = value.split(";")
customer = fields[1] if len(fields) > 1 else ""
status = fields[8] if len(fields) > 8 else ""
service_window = fields[9] if len(fields) > 9 else ""
name_type = "Alias" if is_alias else "Hostname"
name = key.decode()
if status == "Disposed":
exit_with(EXIT_WARN, f"{name_type} {name} is in CMDB with status 'Disposed'")
elif not service_window:
exit_with(EXIT_WARN, f"{name_type} {name} is in CMDB but has no Service Window")
elif not customer:
exit_with(EXIT_WARN, f"{name_type} {name} is in CMDB but has no Customer name")
exit_with(EXIT_OK, f"{name_type} {name} found in CMDB (Customer: {customer})")
except Exception as e:
exit_with(EXIT_UNKNOWN, f"Error reading CMDB data for {key.decode()}: {e}")
def main():
hostname, alias = parse_args()
if not os.path.exists(DB_PATH):
exit_with(EXIT_UNKNOWN, f"DBM file not found: {DB_PATH}.pag")
# try:
# db = dbm.open(DB_PATH, 'r')
# except Exception as e:
# exit_with(EXIT_UNKNOWN, f"Cannot open CMDB DBM file: {e}")
try:
db = dbm.open(DB_PATH, 'r')
except Exception as e:
exit_with(3, f"Cannot open CMDB DBM file: {e}")
key_host = hostname.encode("utf-8")
key_alias = alias.encode("utf-8") if alias else None
if key_host in db:
check_cmdb_entry(db, key_host, is_alias=False)
elif key_alias and key_alias in db:
check_cmdb_entry(db, key_alias, is_alias=True)
else:
msg = f"Hostname {hostname}"
if alias:
msg += f" or alias {alias}"
msg += " not found in CMDB"
exit_with(EXIT_WARN, msg)
db.close()
if __name__ == "__main__":
main()
And here you have Rule defined in GUI for using that plugin:
Under host view it is looks lik that:
CLI Output:
Can someone help me fix that ?