Linux Nagios Running Proces

Uit Rolandow
Versie door Rolandow (Overleg | bijdragen) op 1 jun 2012 om 12:11 (Nieuwe pagina aangemaakt met '= Goal = Check if a proces is running, on a remote host. This is done by checking a given command against a given PID (or pidfile). Actually it just checks if /proc/<…')

(wijz) ← Oudere versie | Huidige versie (wijz) | Nieuwere versie → (wijz)
Ga naar: navigatie, zoeken

Goal

Check if a proces is running, on a remote host. This is done by checking a given command against a given PID (or pidfile). Actually it just checks if /proc/<pid>/cmdline has the command that you check with.

Bash script

Script /var/lib/nagios/plugins/check_proc_running

The include of utils.sh makes sure we have the $STATE_OK variables.

#! /bin/bash
#
# Check if specified process is running at specified PID
#
# Usage: ./check_proc_running -P <pid> -PF <pidfile> -C <script> -x [exitstatus]
#
# Example:
#  ./check_proc_running -P 16802 -C /home/soapserver/productDaemon.php
#
# or:
#  ./check_proc_running -PF /var/run/stumpel/productDaemon.pid -C /home/soapserver/productDaemon.php
#
# Return alternative status when running process is found, for example
# state critical:
#
#  ./check_proc_running -P 16801 -C /home/soapserver/productDaemon.php -x 2
#
# Default state is OK when process is running on PID.
#
PROGNAME=`/usr/bin/basename $0`
PROGPATH=`echo $0 | sed -e 's,[\\/][^\\/][^\\/]*$,,'`
REVISION="1.0.0"

. $PROGPATH/utils.sh

print_usage() {
    echo "Usage: $PROGNAME -P pid -C command"
    echo "Usage: $PROGNAME --help"
    echo "Usage: $PROGNAME --version"
}

print_help() {
    print_revision $PROGNAME $REVISION
    echo ""
    print_usage
    echo ""
    echo "Running process checker"
    echo ""
    support
}

# Make sure the correct number of command line
# arguments have been supplied

if [ $# -lt 4 ]; then
    print_usage
    exit $STATE_UNKNOWN
fi

# Grab the command line arguments

exitstatus=$STATE_OK #default
verbosity=0
while test -n "$1"; do
    case "$1" in
        --help)
            print_help
            exit $STATE_OK
            ;;
        -h)
            print_help
            exit $STATE_OK
            ;;
        --version)
            print_revision $PROGNAME $REVISION

            exit $STATE_OK
            ;;
        -V)
            print_revision $PROGNAME $REVISION
            exit $STATE_OK
            ;;
        --pid)
            checkpid=$2
            shift
            ;;
        -P)
            checkpid=$2
            shift
            ;;
        --pidfile)
            pidfile=$2
            shift
            ;;
        -PF)
            pidfile=$2
            shift
            ;;
        --command)
            checkcommand=$2
            shift
            ;;
        -C)
            checkcommand=$2
            shift
            ;;
        -x)
            exitstatus=$2
            shift
            ;;
        --exitstatus)
            exitstatus=$2
            shift
            ;;
        -v)
            verbosity=$2
            shift
            ;;
        *)
            echo "Unknown argument: $1"
            print_usage
            exit $STATE_UNKNOWN
            ;;
    esac
    shift
done

f_VERB() {
  if [ "$verbosity" -ge 3 ]; then
    echo $@
  fi
}

if [ -n $pidfile ]; then
  f_VERB "Loading pidfile $pidfile"
  if [ ! -e $pidfile ]; then
    echo "Error: file $pidfile does not exist\n"
    exit $STATE_UNKNOWN
  fi


  checkpid=`cat $pidfile`
fi

f_VERB "Checking $checkpid for command $checkcommand"

# if pid not set, exit
if [ -z $checkpid ]; then
  echo "Error: PID not given\n"
  exit $STATE_UNKNOWN
fi

# if command not set, exit
if [ -z $checkcommand ]; then
  echo "Error: command to check not set"
  exit $STATE_UNKNOWN
fi

f_VERB "grep '$checkcommand' /proc/$checkpid/cmdline | wc -l"

is_running=`cat /proc/$checkpid/cmdline | grep $checkcommand | wc -l`
if [ "$is_running" -ge "1" ]; then
  echo "$checkcommand is running\n"
  exit $exitstatus
fi

echo "$checkcommand is NOT running\n"
if [ "$exitstatus" -eq "0" ]; then
  exit $STATE_CRITICAL
fi

exit $STATE_OK

nrpe_local.cfg

On the host that runs the processes that need to be checked, the nrpe_local.cfg needs to be configured.

command[check_stumpel_daemon]=/usr/lib/nagios/plugins/check_proc_running -PF /var/run/stumpel/$ARG1$.pid -C /home/soapserver/htdocs/$ARG1$.php

services.conf

On the monitoring host a service should be defined. For example:

define service {
        use                             generic-service
        host_name                       soap.stumpel.nl
        service_description             shippingDaemon
        check_command                   check_nrpe_alt!check_stumpel_daemon!shippingDaemon
}

Note that check_nrpe_alt is being used here to connect to a different port.