icinga2-checks/check_process.sh

58 lines
1.2 KiB
Bash
Executable File

#!/usr/bin/env bash
# Check if a process is running using Icinga2 and pgrep
# Usage: ./check_process.sh -p <process_name> -s <pretty_service_name>
# Exit codes
STATE_OK=0
STATE_WARNING=1
STATE_CRITICAL=2
STATE_UNKNOWN=3
# Check if the process name and pretty service name are provided
while getopts ":p:s:" opt; do
case $opt in
p)
process_name="$OPTARG"
;;
s)
pretty_service_name="$OPTARG"
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit $STATE_UNKNOWN
;;
:)
echo "Option -$OPTARG requires an argument." >&2
exit $STATE_UNKNOWN
;;
esac
done
if [ -z "$process_name" ]; then
echo "UNKNOWN - Process name not provided"
exit $STATE_UNKNOWN
fi
if [ -z "$pretty_service_name" ]; then
pretty_service_name="$process_name"
fi
if uname | grep -q "BSD"; then
# have to run pgrep with sudo in BSD
process_count=$(sudo pgrep -f "$process_name" 2>/dev/null | wc -l)
else
process_count=$(pgrep -f "$process_name" 2>/dev/null | wc -l)
fi
if [ $? -ne 0 ]; then
echo "UNKNOWN - Check failed"
exit $STATE_UNKNOWN
elif [ $process_count -gt 0 ]; then
echo "OK - $pretty_service_name is running"
exit $STATE_OK
else
echo "CRITICAL - $pretty_service_name is not running"
exit $STATE_CRITICAL
fi