#!/bin/bash

#######
## Description: Simple Check to detect "defunct" processes 
## Author: Sebastian Klee
## E-Mail: s.klee@timmehosting.de
## Version: 1.1
## Date: 26.06.2024
#######

# We use the "exit_code" variable to set the exit code of the script when "defunct" processes are found, the variable gets its value from the Monitoring.
exit_code=$1

# Count processes which contains the string "defunct>"
number_defunct=$(ps axo pid,stat | awk '$2 ~ /^Z/ { print $1 }' | wc -l)

# End the script with exit code 0(ok), if there are no "defunct" processes, otherwise with exit code 1(warning)
if [[ $number_defunct == 0 ]]; then
   echo "No \"defunct\" Processes detected"
   exit 0
else
   echo  "The Check detected \"Defunct\" Processes(${number_defunct})"
   exit "$exit_code"
fi
