/ nagios

nagios checking process age for redhat

#!/bin/bash
# tested with Redhat 5/6
pid=$(ps -ef | grep java | grep -v grep | awk '{print $2}')
# returns the current time as epoche
now=$(date +%s)
# check if no pid was found
if [ -z "$pid" ]; then
  echo "OK: No process found."; exit 0
fi
#pid was found
if [ ! -z "$pid" ]; then
  #calclates the time difference
  proc_age=$(echo "$now $(date -d "$(ps -p $nl_pid -o lstart=)" +%s)" | awk '{print $1 - $2}')
  minutes=$(( $proc_age / 60 ))
  if [[ $minutes < 180 ]]; then
    echo "OK: Process runs less than 3 hours. Running for $minutes mins"; exit 0
  else
    echo "WARNING: Process runs more than 3 hours. Running for $minutes mins"; exit 1
  fi
fi