#!/bin/sh
#
# check_nagios_latency
#
# Checks the Nagios latency
#
# Copyright (c) 2007-2010 ETH Zurich.
#
# This module is free software; you can redistribute it and/or modify it
# under the terms of GNU general public license (gpl) version 3.
# See the LICENSE file for details.
#
# RCS information
# enable substitution with:
#   $ svn propset svn:keywords "Id Revision HeadURL Source Date"
#
#   $Id: check_ssl_cert 1212 2010-12-16 06:08:19Z corti $
#   $Revision: 1212 $
#   $HeadURL: https://svn.id.ethz.ch/nagios_plugins/check_ssl_cert/check_ssl_cert $
#   $Date: 2010-12-16 07:08:19 +0100 (Thu, 16 Dec 2010) $

################################################################################
# Constants

VERSION=1.0.0
SHORTNAME="LATENCY"

################################################################################
# Functions

################################################################################
# Prints usage information
# Params
#   $1 error message (optional)
usage() {

    if [ -n "$1" ] ; then
        echo "Error: $1" 1>&2
    fi
    
    echo
    echo "Usage: check_nagios_latency [-hvV?] -w warning -c critical [-n path ] [-f cfg_file ]"
    echo
    echo "   -c         critical threshold"
    echo "   -f path    nagios config file path"
    echo "   -h, -?     this help message"
    echo "   -n path    nagiostats path"
    echo "   -v         verbose output"
    echo "   -w         warning threshold"
    echo "   -V         version"
    echo
    echo "Report bugs to: Matteo Corti <matteo.corti@id.ethz.ch>"
    echo

    exit 3

}

################################################################################
# Checks if a given program is available and executable
# Params
#   $1 program name
check_prog() {

    if [ -z "$PROG" ] ; then
        PROG=$(command -v $1)
    fi

    if [ -z "$PROG" ] ; then
        echo "${SHORTNAME} CRITICAL - cannot find $1"
        exit 2
    fi

    if [ ! -x "$PROG" ] ; then
        echo "${SHORTNAME} CRICTICAL - $PROG is not executable"
        exit 2
    fi

}

################################################################################
# Main
################################################################################

# initialize local variable (ovverride external definitions)
CONFIG=
CRITICAL=
PROG=
VERBOSE=
WARNING=

# process command line options
while getopts "vh?Vc:w:n:f:" opt; do
    case $opt in
        c )      CRITICAL=$OPTARG;  ;;
        f )      CONFIG=$OPTARG;    ;;
        h | \? ) usage ; exit 3;    ;;
        n )      PROG=$OPTARG ;;
        V )      echo "check_nagios_latency version ${VERSION}"; exit 3; ;;
        v )      VERBOSE=1;         ;;
        w )      WARNING=$OPTARG;   ;;
    esac
done
shift $(($OPTIND - 1))

################################################################################
# sanity checks

###############
# Check options
if [ -z "${CRITICAL}" ] ; then
    usage "No critical threshold specified"
fi
if [ -z "${WARNING}" ] ; then
    usage "No warning threshold specified"
fi

######################
# Check number formats

if ! echo $WARNING | grep -qE '^[0-9]+(\.[0-9]+)?$' ; then
    echo "${SHORTNAME} UNKOWN - Wrong number: $WARNING"
    exit 3
fi

if ! echo $CRITICAL | grep -qE '^[0-9]+(\.[0-9]+)?$' ; then
    echo "${SHORTNAME} UNKOWN - Wrong number: $WARNING"
    exit 3
fi

#######################
# Check needed programs

check_prog nagiostats

test -n "$CFG" && PROG="$PROG -c $CFG"

# Check the latency

LATENCY=$($PROG | grep "Active Service Latency" |cut -f3 -d'/' | awk '{print $1}' | tr -d '\n');

if [ -n "${VERBOSE}" ] ; then echo "latency: ${LATENCY}"; fi

####################
# Perform the checks

PERF="Latency=${LATENCY};${WARNING};${CRITICAL};;"

COMPARISON=$(echo "if($LATENCY>$CRITICAL) 1 else 0;" | bc)
if [ $COMPARISON -eq 1 ] ; then
    echo "${SHORTNAME} CRITICAL ${LATENCY}s | $PERF"
    exit 2
fi

COMPARISON=$(echo "if($LATENCY>$WARNING) 1 else 0;" | bc)
if [ $COMPARISON -eq 1 ] ; then
    echo "${SHORTNAME} WARNING ${LATENCY}s | $PERF"
    exit 1
fi

echo "${SHORTNAME} OK ${LATENCY}s| $PERF"
exit 0;
