#!/bin/bash
# SYNOPSIS
#	check_pbsserver
#	check_pbsserver [<port>]
#
# DESCRIPTION
#	This NAGIOS plugin checks whether: 1) pbs_server is running
#	and 2) the host is listening on the given port.  If no port
#	number is specified TCP/UDP port 15001 is checked.
#
# AUTHOR
#	Wayne.Mallett@jcu.edu.au

OK=0
WARN=1
CRITICAL=2
PATH="/bin:/sbin:/usr/bin:/usr/sbin"

# Default listening port is TCP/UDP 15001.
if [ $# -lt 1 ] ; then
  port=15001
else
  port=$1
fi

if [ `ps -C pbs_server | wc -l` -lt 2 ]; then
  echo "PBS_SERVER CRITICAL:  Daemon is NOT running!"
  exit $CRITICAL
else
  if [ `netstat -ln | grep -E "tcp.*:$port" | wc -l` -lt 1 ]; then
    echo "PBS_SERVER CRITICAL:  Host is NOT listening on TCP port $port!"
    exit $CRITICAL
  fi
  if [ `netstat -ln | grep -E "udp.*:$port" | wc -l` -lt 1 ]; then
    echo "PBS_SERVER CRITICAL:  Host is NOT listening on UDP port $port!"
    exit $CRITICAL
  fi
  echo "PBS_SERVER OK:  Daemon is running.  Host is listening."
  exit $OK
fi
