#!/bin/ksh

##
## Script: chk_ctlds_procs.ksh
## Purpose: Check the number of ctlds processes on the pweb servers and also the number of database connections on port 4100
## Invoked From:  Normally run on pdb01 or pdb00
## Parameters:  none
## Depends on: 
## Requires:  
## Date Written:  01/08/2001
## Written By:  Michael O'Sullivan
## Revision History:
##

## Summary:  If the number of ctlds processes exceeds 10 on any of the pweb servers then that server will not function properly.
## The maximum number of database connections is set to 7000 and the database needs to be restarted if it exceeds this figure.

_4100_LIMIT="6000"
_ctlds_LIMIT="11"

echo "counting number of ctld processes ...\n"
echo "Server  ctlds count \t\tStatus"
echo "======  =========== \t\t==============================================================="
for i in 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 23 24 25 26 27 28 29 30 31 32
do
_ctlds_cnt="$(rsh pweb$i 'ps -ef | grep [c]tlds | wc -l')"
if [ _ctlds_cnt -le ${_ctlds_LIMIT} ]; then
   _ctlds_status="ok"
else
   _ctlds_status="BAD - please reboot pweb$i"
fi

echo "pweb$i  ${_ctlds_cnt}\t\t ${_ctlds_status}"
done

echo "\n\n"

_4100_connections="$(netstat -na | grep 4100 | grep ESTABLISHED | wc -l)"

if [ "${_4100_connections}" -le "${_4100_LIMIT}" ]; then
   _4100_status="ok"
else
   _4100_status="BAD - too many connections (> ${_4100_LIMIT}) on port 4100.  Reboot some web servers NOW!!!"
fi

echo "$(hostname) has ${_4100_connections} connections on port 4100 established."
echo "Status:  ${_4100_status}"

if [ "${_ctlds_status}" == "ok" ]; then
   if [ "${_4100_status}" == "ok" ]; then
      exit 0
   else 
      exit 2
   fi
elif [ "${_4100_status}" == "ok" ]; then
      exit 1
else  exit 3
fi
