#!/bin/sh
# Copyright (C) 2019 Checkmk GmbH - License: GNU General Public License v2
# This file is part of Checkmk (https://checkmk.com). It is subject to the terms and
# conditions defined in the file COPYING, which is part of this source code package.

# This check checks the overall state of a service group.
# It fetches the current state of all services of a group
# via livestatus (the path to the livestatus socket is
# provided as a third, optional parameter).
#
# Provide the name of the service group as first
# parameter.
#
# If you specify a number NUM as second parameter,
# then the state of this check is not considered to
# be the worst service state of the group but the
# NUM'th worst state. E.g. if there is one critical
# and one warning service, then specifying 2 will
# result in a warning state and 3 in a OK state.
#
# WARN is considered to be worse than UNKNOWN.

GROUPNAME=$1
if [ -z "$GROUPNAME" ]; then
    echo "Usage: $0 SERVICE_GROUP [NUM [SOCKET] ]"
    exit 3
fi
NUM=${2:-1}
SOCKET=${3:-/var/run/nagios/rw/live}

check() {
    IFS=\; read -r COUNT OK WARN CRIT UNKNOWN
    if [ "$CRIT" -ge "$NUM" ]; then
        status=CRIT
        exitcode=2
    elif [ $((CRIT + WARN)) -ge "$NUM" ]; then
        status=WARN
        exitcode=1
    elif [ $((CRIT + WARN + UNKNOWN)) -ge "$NUM" ]; then
        status=UNKNOWN
        exitcode=3
    else
        status=OK
        exitcode=0
    fi
    echo "$status - $COUNT services: $CRIT critical, $WARN warning, $UNKNOWN unknown, $OK ok"
    exit $exitcode
}

cat <<EOF | unixcat "$SOCKET" | check
GET services
Filter: groups >= $GROUPNAME
Stats: last_hard_state >= 0
Stats: last_hard_state = 0
Stats: last_hard_state = 1
Stats: last_hard_state = 2
Stats: last_hard_state = 3
EOF
