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

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 'squid'"
	echo "syntax: $0 [--help]"
	exit 3
fi

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

# get local squid port
port=3128
squidCfg=/etc/squid/squid.conf
if [ -e "$squidCfg" ]; then
	cfgPort=$(sed -nre 's/^\s*http_port\s+//p' "$squidCfg")
	if [ -n "$cfgPort" ]; then
		port=$cfgPort
	fi
fi

# check if authentication is required
auth=""
for i in ntlmauth basicauth krb5auth; do
	if [ -z "$auth" ] ; then
		tmp=$(/usr/sbin/univention-config-registry get squid/$i)
		if [ "yes" = "$tmp" -o "true" = "$tmp" ]; then
			auth="-e 407"
		fi
	fi
done

# check HTTP service
MSG="$(/usr/lib/nagios/plugins/check_http -I 127.0.0.1 -p $port -u http://127.0.0.1 $auth)"
nagiosExit "$?" "$MSG"
