#!/bin/bash
#
# Command line example for nrpe.cfg:
# commandcheck_system=/path/to/nrpe/libexec/check_process
#
# Description: This plugin determines whether the server is running properly. It will check the following: * Are all required processes running? * Are all the required TCP/IP ports open?
#
# Changes: 28.01.2006 added yellow check (FBA)
#               29.01.2006 change "px -ef" to "ps -ax" (FBA). Problems with long arguments 31.01.2006 added all OK Status with all procs and ports (FBA)
#               15.07.2006 change "ps -ax" to "ps ax" (FBA). Also problems with long arguments under RedHat 3/4 17.07.2006      Plugin rewrite and bugfixes (Magnus Glantz) 19.07.2006  Removed
#               utils.sh dependency.
#
##################################################################################
#
# Processes to check

PROCLIST_RED="sshd nrpe"
PROCLIST_YELLOW="apache2"
# Ports to check
# PORTLIST="993 5666 389 3306 587 110 143 80 55555 25 6556"
PORTLIST="22 80 5666"

##################################################################################
PATH="/usr/bin:/usr/sbin:/bin:/sbin"
STATE_OK=0
STATE_WARNING=1
STATE_CRITICAL=2
STATE_UNKNOWN=3
STATE_DEPENDENT=4

if test -n "$1"; then
        PROCLIST_RED="$1"
        PROCLIST_YELLOW="nagios"
fi
if test -n "$2"; then
         PORTLIST="$2"
PROCLIST_RED="$1"
PROCLIST_YELLOW="nrpe"

fi
if test -n "$3"; then
         PROCLIST_RED="$1"
  PORTLIST="$2"
PROCLIST_YELLOW="$3"
fi


print_gpl() {
    echo "Prozess-Checker"
}
print_help(){
        echo ""
        echo "System process and port check script for Nagios."
        echo "Parameter 1 Tasknamen"
        echo "Parameter 2 Portnummern"
        print_gpl
}

check_processes_red() {
        PROCESS="0"
        ERROR_PROCS=""
        for PROC in `echo $PROCLIST_RED`; do
        if [ `ps -ef | grep $PROC | grep -v grep | wc -l` -lt 1 ]; then
                        PROCESS=1
                        ERROR_PROCS="$ERROR_PROCS""$PROC ";
        fi
        done
        if [ $PROCESS -eq "1" ]; then
                exit_red=$STATE_CRITICAL
        elif [ $PROCESS -eq "0" ]; then
                exit_red=$STATE_OK
        fi
}
check_processes_yellow() {
        PROCESS="0"
        WARNING_PROCS=""
        for PROC in `echo $PROCLIST_YELLOW`; do
        if [ `ps -ef | grep $PROC | grep -v grep | wc -l` -lt 1 ]; then
                        PROCESS=1
                        WARNING_PROCS="$WARNING_PROCS""$PROC ";
        fi
        done
        if [ $PROCESS -eq "1" ]; then
                exit_yellow=$STATE_WARNING
        elif [ $PROCESS -eq "0" ]; then
                exit_yellow=$STATE_OK
        fi
}
check_ports() {
        PORTS="0"
        ERROR_PORTS=""
        for NUM in `echo $PORTLIST`; do
                if [ `netstat -an | grep LISTEN | grep $NUM | grep -v grep | wc -l` -lt 1 ]; then
                        PORTS=1
                        ERROR_PORTS="$ERROR_PORTS""$NUM ";
                fi
        done
        if [ $PORTS -eq "1" ]; then
                exit_ports=$STATE_CRITICAL
        elif [ $PORTS -eq "0" ]; then
                exit_ports=$STATE_OK
        fi
}
check_processes_red
check_ports
check_processes_yellow
final_exit=`expr $exit_ports + $exit_red + $exit_yellow`
if [ $final_exit -eq "0" ]; then
        echo "SYSTEM OK - All monitored resources OK. Processes: $PROCLIST_RED $PROCLIST_YELLOW. Ports: $PORTLIST.|Fehler=  0"
        exitstatus=$STATE_OK
elif [ $final_exit -eq "1" ]; then
        echo "SYSTEM WARNING - Processes DOWN.Warnings= ($WARNING_PROCS).|Fehler = 1"
        exitstatus=$STATE_WARNING
elif [ $final_exit -ge "1" ]; then
        echo "SYSTEM CRITICAL - Resources DOWN! Processes: $ERROR_PROCS $WARNING_PROCS. Ports: $ERROR_PORTS|Fehler = 5"
        exitstatus=$STATE_CRITICAL
fi
exit $exitstatus

