#!/bin/bash
#
# Omoti structured alert local check [Power: 700]
# Version: 1.1.0
# Date:    2026.07.06

set -u

# Stable service to component mapping.
SERVICES=(
	"omoti_license licensing"
	"omoti_update updater"
	"omoti_geoip geoip"
	"omoti_blocklist blocklist"
	"omoti_config config"
	"omoti_events events"
)

# Emit UNKNOWN for every logical service.
emit_unknown_lines() {
	local code="$1"
	local message="$2"
	local entry=""
	local service=""
	local component=""

	for entry in "${SERVICES[@]}"
	do
		service="${entry%% *}"
		component="${entry##* }"
		echo "3 ${service} severity=unknown code=${code} component=${component} - ${message}"
	done
}

# Ask the application to render the current state.
binary="/usr/bin/omoti-server"
if ! [[ -x "${binary}" ]]
then
	emit_unknown_lines "omoti_server_missing" "The omoti-server binary is missing"
	exit 0
fi

if output="$("${binary}" checkmk-local 2>/dev/null)"
then
	if [[ -n "${output}" ]]
	then
		printf '%s\n' "${output}"
		exit 0
	fi
fi

# Fall back to UNKNOWN when rendering fails.
emit_unknown_lines "checkmk_local_failed" "Failed to render structured Checkmk output"
exit 0
