[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/se3master/usr/share/se3/scripts-alertes/ -> check_procs_pgrep (source)

   1  #! /bin/bash 
   2  #
   3  #  This is a Nagios plugin.
   4  #  Uses pgrep command to find processes by their name.
   5  #  This plugin has been developed as a drop-in replacement
   6  #  for the official check_procs plugin. 
   7  #
   8  #  check_procs_pgrep -h    will give syntax.
   9  #
  10  #
  11  
  12  STATE_OK=0
  13  STATE_WARNING=1
  14  STATE_CRITICAL=2
  15  STATE_UNKNOWN=3
  16  STATE_DEPENDENT=4
  17  
  18  DEFAULT_LO_RANGE=0     # if low range not specified
  19  DEFAULT_HI_RANGE=99999  # if high range not specified 
  20  
  21  print_help() {
  22    echo ""
  23    echo ""
  24    echo "check_procs_pgrep (v1.0)"
  25    echo ""
  26    echo ""
  27    echo "This plugin checks the number of currently running processes and"
  28    echo "generates WARNING or CRITICAL states if the process count is outside"
  29    echo "the specified threshold ranges. The process count can be filtered by"
  30    echo "process owner or may be the total number of running processes if process name"
  31    echo "arguments are not specified. The plugin is most often used to check if"
  32    echo "a specific process is running."
  33    echo ""
  34    echo "Note to check_procs users: check_procs_pgrep is a drop-in replacement"
  35    echo "for the check_procs plugin from the official Nagios plugin distribution."
  36    echo "It is a simple bash script that use pgrep command to find processes."
  37    echo "Compared to the official plugin only the 'number of processes' metric"
  38    echo "is suppported by this plugin. Apart from this the two plugins accept"
  39    echo "same parameters and have identical functionality."
  40    echo ""
  41    echo "Usage: check_procs_pgrep -w <range> -c <range> [-u user] "
  42    echo "                               [-a argument-array] [-C command] "
  43    echo "                               [-h]"
  44    echo ""
  45    echo "Required Arguments:"
  46    echo " -w, --warning=RANGE"
  47    echo "   Generate warning state if metric is outside this range"
  48    echo " -c, --critical=RANGE"
  49    echo "   Generate critical state if metric is outside this range"
  50    echo ""
  51    echo "Optional Filters:"
  52    echo " -u, --user=USER"
  53    echo "   Only scan for processes with user name or ID indicated."
  54    echo "   (this translates into -U parameter on pgrep"
  55    echo " -a, --argument-array=STRING"
  56    echo "   Only scan for processes with args that contain STRING."
  57    echo " -C, --command=COMMAND"
  58    echo "   Only scan for exact matches of COMMAND (without path)."
  59    echo ""
  60    echo "Other:"
  61    echo " -h, --help"
  62    echo "   This text."
  63    echo " -v, --verbose"
  64    echo "   Print debug information."
  65    echo ""
  66    echo ""
  67    echo "RANGEs are specified 'min:max' or 'min:' or ':max' (or 'max'). If"
  68    echo "specified 'max:min', a warning status will be generated if the"
  69    echo "count is inside the specified range"
  70    echo ""
  71    echo ""
  72    echo "Examples:"
  73    echo " check_procs_pgrep -w 2:2 -c 2:1024 -C portsentry"
  74    echo "   Warning if not two processes with command name portsentry. Critical"
  75    echo "   if < 2 or > 1024 processes"
  76    echo ""
  77    echo " check_procs_pgrep -w 10 -c 10 -a '/usr/local/bin/perl' -u root"
  78    echo "   Critical alert if > 10 processes with command arguments containing"
  79    echo "   '/usr/local/bin/perl' and owned by root. (critical takes precedence"
  80    echo "   over warning in this case)"
  81    echo ""
  82    echo "No support for this plugin since it is dead simple. Feel free to change."
  83  }
  84  
  85  
  86  
  87  
  88  
  89  # Grab the command line arguments
  90  
  91  while test -n "$1"; do
  92      case "$1" in
  93          --help)
  94              print_help
  95              exit $STATE_OK
  96              ;;
  97          -h)
  98              print_help
  99              exit $STATE_OK
 100              ;;
 101          -u)
 102              username=$2
 103              shift
 104              ;;
 105          --user)
 106              username=$2
 107              shift
 108              ;;
 109          -C)
 110              command=$2
 111              shift
 112              ;;
 113          --command)
 114              command=$2
 115              shift
 116              ;;
 117          -a)
 118              argarr=$2
 119              shift
 120              ;;
 121          --argument-array)
 122              argarr=$2
 123              shift
 124              ;;
 125          -w)
 126              warningrange=$2
 127              shift
 128              ;;
 129          --warning)
 130              warningrange=$2
 131              shift
 132              ;;
 133          -c)
 134              criticalrange=$2
 135              shift
 136              ;;
 137          --critical)
 138              criticalrange=$2
 139              shift
 140              ;;
 141          -v)
 142              verbose="yes"
 143              ;;
 144          --verbose)
 145              verbose="yes"
 146              ;;
 147          *)
 148              echo "Unknown argument: $1"
 149              print_usage
 150              exit $STATE_UNKNOWN
 151              ;;
 152      esac
 153      shift
 154  done
 155  
 156  
 157  
 158  if [ -z "$criticalrange" ] || [ -z "$warningrange" ] ; then
 159    echo "Syntax error: both -c and -w arguments must be specified."
 160    echo "Help on syntax: check_procs_grep -h"
 161    exit $STATE_UNKNOWN
 162  fi
 163  
 164  # ****************
 165  # Parse ranges
 166  # ****************
 167  
 168  critrange1=`echo $criticalrange | awk -F: '{print $1}'`
 169  critrange2=`echo $criticalrange | awk -F: '{print $2}'`
 170  warnrange1=`echo $warningrange | awk -F: '{print $1}'`
 171  warnrange2=`echo $warningrange | awk -F: '{print $2}'`
 172  
 173  # if no colon in string
 174  if [ "$criticalrange" == "$critrange1" ] ; then
 175    critrange2="$critrange1"
 176    critrange1=$DEFAULT_LO_RANGE 
 177  fi
 178  
 179  if [ -n "$verbose" ] ; then echo "C1 = $critrange1   , C2 = $critrange2" ; fi
 180  
 181  if [ -z "$critrange1" ] ; then
 182    critrange1=$DEFAULT_LO_RANGE
 183  fi
 184  if [ -z "$critrange2" ] ; then
 185    critrange2=$DEFAULT_HI_RANGE
 186  fi
 187  
 188  if [ -n "$verbose" ] ; then echo "C1 = $critrange1   , C2 = $critrange2" ; fi
 189  
 190  # if no colon in string
 191  if [ "$warningrange" == "$warnrange1" ] ; then
 192    warnrange2="$warnrange1"
 193    warnrange1=$DEFAULT_LO_RANGE
 194  fi
 195  if [ -n "$verbose" ] ; then echo "W1 = $warnrange1   , W2 = $warnrange2" ; fi
 196  
 197  if [ -z "$warnrange1" ] ; then
 198    warnrange1=$DEFAULT_LO_RANGE
 199  fi
 200  if [ -z "$warnrange2" ] ; then
 201    warnrange2=$DEFAULT_HI_RANGE
 202  fi
 203  
 204  if [ -n "$verbose" ] ; then echo "W1 = $warnrange1   , W2 = $warnrange2" ; fi
 205  
 206  # ****************
 207  # end of Parse ranges 
 208  # ****************
 209  
 210  # Find out which pgrep command to use
 211  PGREP_CMD="pgrep"
 212  if [ -z "$argarr" ] && [ -z "$command" ] ; then
 213    argarr="."
 214  fi
 215  
 216  if [ -n "$username" ] ; then
 217    PGREP_CMD="$PGREP_CMD -U $username"
 218  fi
 219  if [ -n "$argarr" ] ; then
 220    PGREP_CMD="$PGREP_CMD -f"
 221    PGREP_ARG="$argarr"
 222  fi
 223  if [ -n "$command" ] ; then
 224    PGREP_CMD="$PGREP_CMD"
 225    PGREP_ARG="$command"
 226  fi
 227  
 228  if [ -n "$verbose" ] ; then echo "Executing    :   $PGREP_CMD \"$PGREP_ARG\"" ; fi    # debug
 229  
 230  pgrep_result=`$PGREP_CMD} "$PGREP_ARG}"`
 231  
 232  
 233  if [ -n "$verbose" ] ; then echo "pgrep_result : $pgrep_result" ; fi    # debug
 234  
 235  num_procs=`echo $pgrep_result | wc -w`
 236  
 237  
 238  if [ "$critrange1" -gt "$num_procs" ] || [ "$critrange2" -lt "$num_procs" ] ; then
 239    echo "Could not find process(es) according to search criteria. Process(es) possibly not running."
 240    exit $STATE_CRITICAL
 241  fi
 242  
 243  if [ "$warnrange1" -gt "$num_procs" ] || [ "$warnrange2" -lt "$num_procs" ] ; then
 244    echo "Could not find process(es) according to search criteria. Process(es) possibly not running."
 245    exit $STATE_WARNING
 246  fi
 247  
 248  if [ "$num_procs" -gt "1" ] ; then 
 249    echo "OK. Process(es) exist."
 250  else
 251    echo "OK. Process exist."
 252  fi
 253  exit $STATE_OK
 254  


Generated: Tue Mar 17 22:47:18 2015 Cross-referenced by PHPXref 0.7.1