#! /bin/bash

# Nagios plugin
# created 09.01.2011 by symphonic.mushroom@gmail.com
# modified 04.24.2012 by symphonic.mushroom@gmail.com with the advices from formwandler
# modified 07.22.2017 by symphonic.mushroom@gmail.com with the help from Toby Wahlers toby@100.rpm.com
# check if processes matching to a pattern are exceeding a given elapsed time
# return a Nagios exit code depending on the result
# 0 = OK
# 1 = WARNING
# 2 = CRITICAL
# 3 = UNKNOWN


# This program 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 3 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, see <http://www.gnu.org/licenses/>.


# for help printing
print_help() {
        echo "This Nagios plugin check if processes matching to a pattern are exceeding a given elapsed time"
        echo "Usage : $0 -p <process_name> -w <seconds> -c <seconds> "
        echo " -p parameter : name of the monitoring process. For granularity, quote commands with spaces."
        echo " -w parameter : minimal elapsed time for status WARNING on NAGIOS, in seconds."
        echo " -c parameter : minimal elapsed time for status CRITICAL on NAGIOS, in seconds."
	echo "returned performance data : number of process; oldest time in minutes; warning time in minutes; critical time in minutes; 0;"
        exit 3
}

# check if there is at least one argument
if [ -z $1 ]
        then echo "Missing arguments"
        echo "try \'$0 --help\' for help"
        exit 3
fi

# print help
if [[ ( $1 = "--help" || $1 = "-h" ) ]]
        then print_help
        exit 3
fi

# assign value to arguments
# print an error in case of unkown argument
while getopts ":w:c:p:" options
do
    case $options in
        w ) warning=$OPTARG ;;
        c ) critical=$OPTARG ;;
        p ) proc=$OPTARG ;;
        * ) echo "Unknown argument"
        echo "try \'$0 --help\' for help"
        exit 3 ;;
    esac
done

# check if all arguments are present
if [[ ( -z $warning || -z $critical || -z $proc ) ]]
        then echo "Missing argument"
        echo "try \'$0 --help\' for help"
        exit 3
fi

#calculate number of process
nbproc=$(ps -A -o args | grep -w "$proc" | grep -v $0 | grep -v grep | wc -l)
if [ $nbproc -gt 0 ]
        then

#calculate age of oldest process
        ageproc=$(ps -A -o etime,comm,args | grep "$proc" | grep -v $0 | grep -v grep | gawk '{split($1,t,":");split(t[1],td,"-");if (td[2]) {ta=td[1]*86400; t[1]=td[2]} else {ta=0}; if (t[3]) {$1=(t[1]*60+t[2])*60+t[3]+ta} else {$1=t[1]*60+t[2]};if (NR==1) {maxi=$1;} else {if ($1>maxi){maxi=$1;}}};END {print maxi}')
        case $ageproc in
                ?|[0-5]? ) maxage=$ageproc" Seconds";;
                ??|???|[0-2]???|3[0-5]?? ) maxage=$(($ageproc/60))" Minutes";;
                * ) maxage=$(($ageproc/3600))" Hours "$(($ageproc % 3600 / 60))" minutes";;
        esac
         msg="there are $nbproc process $proc, oldest has got $maxage age"
         perfmaxage=$(($ageproc/60))
         perfdata="Processes=${nbproc:-0} MaxAge=${perfmaxage:-0}Minutes;$(($warning/60));$(($critical/60));0;"
                if [ $ageproc -gt $critical ]
                        then echo "CRITICAL: $msg | $perfdata"
                        exit 2
                elif [ $ageproc -gt $warning ]
                        then echo "WARNING: $msg | $perfdata"
                        exit 1
                else echo "OK: $msg | $perfdata"
                exit 0
                fi
        else
        echo "OK: there is no process matching $proc"
        exit 0
fi
