#!/bin/bash
#
# Univention Nagios Plugin
#
# SPDX-FileCopyrightText: 2010-2025 Univention GmbH
# SPDX-License-Identifier: AGPL-3.0-only

function nagiosExit () {

	local state="$1"
	local msg="$2"

	case $state in
	0)
		echo "OK: $msg"
		exit 0
		;;
	1)
		echo "WARNING: $msg"
		exit 1
		;;
	2)
		echo "CRITICAL: $msg"
		exit 2
		;;
	*)
		echo "UNKNOWN: $msg"
		exit 3
		;;
	esac
}

if [ "$1" = "-h" -o "$1" = "--help" ] ; then
	echo "$0 checks local service 'opsi'"
	echo "syntax: $0 [--help]"
	exit 3
fi


# check for at least one opsiconfd process
MSG="$(/usr/lib/nagios/plugins/check_procs -w 1: -c 1: -C opsiconfd)"
RET="$?"
if [ ! "$RET" = "0" ] ; then
	nagiosExit "$RET" "$MSG"
fi

# check for at least one opsiconfd-guard process
MSG="$(/usr/lib/nagios/plugins/check_procs -w 1: -c 1: -C opsiconfd-guard)"
RET="$?"
if [ ! "$RET" = "0" ] ; then
	nagiosExit "$RET" "$MSG"
fi

# check HTTP service
port=4447
opsiCfg=/etc/opsi/opsiconfd.conf
if [ -e "$opsiCfg" ]; then
	cfgPort=$(sed -nre 's/^\s*https port\s+=\s+//p' "$opsiCfg")
	if [ -n "$cfgPort" ]; then
		port=$cfgPort
	fi
fi
MSG="$(/usr/lib/nagios/plugins/check_http -I 127.0.0.1 -p $port -S)"
nagiosExit "$?" "$MSG"
