#!/bin/bash
#
NAGIOS_OK=0
NAGIOS_ERROR=2
#
NAGIOS_NAME="$2"
PROCESS_NAME="$4"
PROCESS_REGEX="$5"
#
PIDs=$(pidof $PROCESS_NAME)
if [[ -z $PIDs ]]; then
    # no such process running
    echo "ERROR - No $NAGIOS_NAME process is running!"
    exit $NAGIOS_ERROR
else
    # there is such a process
    ps x -q "$PIDs" | grep "$PROCESS_REGEX" 2>&1 > /dev/null
    if [[ $? -eq 0 ]]; then
        echo "OK - The required $NAGIOS_NAME process is running"
        exit $NAGIOS_OK
    else
        echo "ERROR - There are processes fitting to $PROCESS_NAME, but none of them fit to the required regex"
        exit $NAGIOS_ERROR
    fi
fi