#!/bin/sh -efu
#
# Copyright (C) 2020  Paul Wolneykien <manowar@altlinux.org>
#
# This file is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.

PROG="${0##*/}"
PROG_VERSION=1.0.0

PLUGINDIR="${0%/*}"
TIMEPATTERN='%s'
LOG_FILE=/var/log/nagios/nagios.log
CHECK="$PLUGINDIR/check_timed_logs"
INTERVAL=1440
WTIMES=1
CTIMES=2

show_help()
{
	cat <<EOF
Usage: $PROG [options]

$PROG is a wrapper around check_timed_logs script adjusted to inspect
the Nagios log file.

Options:

  -H HOSTNAME, --hostname=HOSTNAME      hostname to search for;
                                    normally you should pass the
                                    \$HOSTNAME\$ (the default is
                                    any host);

  -S DESC, --service=DESC   service description to search for;
                            if not given, it will search for a
                            HOST ALERT, otherwise for a SERVICE
                            ALERT;

  -s STATE, --state=STATE   event state to search for (the default
                            is any state);

  -l LOG, --log=LOG         Nagios log file; normally you should
                            pass the \$LOGFILE\$ (default is
                            $LOG_FILE);

  -i MINS, --interval=MINS  time interval in minutes (default is
                            $INTERVAL);

  -w N, --wtimes=N          number of search hits for a WARNING
                            (default is $WTIMES);

  -c N, --ctimes=N          number of search hits for a CRITICAL
                            (default is $CTIMES);

  -C CHECK, --check=CHECK   the path to check_timed_logs Nagios
                            plugin (the default is
                            $CHECK);

  -V,--version              print program version and exit;
  -h,--help                 show this text and exit.


Report bugs to http://bugs.altlinux.ru/

EOF
}

print_version()
{
	cat <<EOF
$PROG version $PROG_VERSION
Written by Paul Wolneykien <manowar@altlinux.org>

Copyright (C) 2020 Paul Wolneykien <manowar@altlinux.org>
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE.
EOF
}

show_usage()
{
	cat <<EOF
Usage: $PROG [options]

Run \`$PROG -h\` to see the help page.
EOF
}


OPTS=`getopt -n $PROG -o H:,S:,s:,l:,i:,w:,c:,C:,V,h \
             -l hostname:,service:,state:,log:,interval:,wtimes:,ctimes:,check:,version,help -- "$@"` ||
	show_usage
eval set -- "$OPTS"

hostname='[^;]\+'
service=
state='[^;]\+'
log="$LOG_FILE"
interval="$INTERVAL"
wtimes="$WTIMES"
ctimes="$CTIMES"
check="$CHECK"

while :; do
	case "$1" in
		-H|--hostname)
            shift
            hostname="$1"
            ;;
		-S|--service)
            shift
            service="$1"
            ;;
		-s|--state)
            shift
            state="$1"
            ;;
		-l|--log)
            shift
            log="$1"
            ;;
		-i|--interval)
            shift
            interval="$1"
            ;;
		-w|--wtimes)
            shift
            wtimes="$1"
            ;;
		-c|--ctimes)
            shift
            ctimes="$1"
            ;;
		-C|--check)
            shift
            check="$1"
            ;;
		-V|--version)
            print_version
            exit 0
            ;;
		-h|--help)
            show_help
            exit 0
            ;;
        --)
            shift
            break
            ;;
		*)
            echo "Unrecognized option: $1" >&2
            exit 1
            ;;
	esac
	shift
done

alert_type="${service:+SERVICE}"
[ -n "$alert_type" ] || alert_type="HOST"

$check -pattern "$alert_type\\s+ALERT:\s+$hostname;${service:+$service;}$state;" -logfile "$log" -timepattern "$TIMEPATTERN" -interval "$interval" -warning "$wtimes" -critical "$ctimes"
