#!/bin/bash

#######
## Description: Extension for the check_procs plugin to exclude the php processes from the check result.
## Author: Sebastian Klee
## E-Mail: s.klee@timmehosting.de
## Version: 1.1
## Date: 16.11.2023
#######

export PATH=$PATH:/usr/lib/nagios/plugins:/usr/lib/nagios/plugins/contrib:/usr/local/lib/nagios/plugins

# Variables. The variables critical and warning get their values from the icinga monitoring.
counter=0
critical="$1"
warning="$2"
#regex for the corresponding processes
regex="kworker.*sda[2|3]"

# For-Loop, repeated for every line of the filtered check_procs command 
for i in $(check_procs -vv | awk '{print $11}'); do
      # If the output of the filtered command does not contain the string php-fpm and also does not match the regex, the value of the variable counter will be increased by one.
      if [[ ! $i =~ 'php-fpm' ]] && [[ ! $i =~ $regex ]]; then
      counter=$((counter + 1))
      fi
done

# An if-query to determine the check result, depending on the result the script returns a corresponding status to the Monitoring(warning,ok or critical).
if [[ $counter -ge $critical ]]; then
   # Critical
   echo "PROCS CRITICAL: $counter processes"
   exit 2
elif [[ $counter -ge $warning ]]; then
   # Warning
   echo "PROCS Warning: $counter processes"
   exit 1
else
   # OK
   echo "PROCS OK: $counter processes"
   exit 0
fi
