#!/bin/ksh

##
## Script: chk_web_servers.ksh
## Purpose: Check the number of ctlds processes on the pweb servers and also the number of database connections on port 4100
##          It also checks the size of the Netscape error log file.  If this goes over 2GB then the web server does not 
##          server pages any more.  The script also counts the number of HTTP connections established.
## 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="17"
NS_ERROR_FILE="/u0/netscape/suitespot/https-genie42/logs/errors"
_overall_status="OK"

count_Story_procs() {

   _Story_cnt="$(rsh pweb$i 'ps -ef | grep Stor[y] | wc -l')"

}

count_ctlds_procs() {

   _ctlds_cnt="$(rsh pweb$i 'ps -ef | grep [c]tlds | wc -l')"
   if [ _ctlds_cnt -le ${_ctlds_LIMIT} ]; then
      _overall_status="OK"
      _ctlds_status="ok"
   else
      _ctlds_status="BAD - please reboot pweb$i"
      _overall_status="REBOOT"
   fi
#   printf "pweb$i  ${_ctlds_cnt}\t\t ${_ctlds_status}"
}

check_errors_file() {

error_size=$(rsh pweb$i "du -ks ${NS_ERROR_FILE} | cut -f1")

if [ ${error_size} -ge 2048000 ]; then
   rsh pweb$i "> ${NS_ERROR_FILE}"
   #_error_file_status="WARNING - ${NS_ERROR_FILE} was over 2GB."
   _error_file_status="> 2GB."
elif
   [ ${error_size} -ge 1500000 ]; then
   rsh pweb$i "> ${NS_ERROR_FILE}"
   #_error_file_status="WARNING - truncated ${NS_ERROR_FILE}."
   _error_file_status="zeroed"
else
   #_error_file_status="error file size = ${error_size} KB"
   _error_file_status="${error_size} KB"
fi
#printf "pweb$i - \t\t\t\t ${_error_file_status}"

}

check_http_connections() {

est_http=$(rsh pweb$i "/usr/bin/netstat -na | grep '\.80 ' | grep ESTABLISHED | wc -l")

if [ ${error_size} -ge 2048000 ] && [ ${est_http} -eq 0 ]; then
   est_http_status="pweb$i - ERROR - error file too big, need to restart server"
   _overall_status="REBOOT"
   printf ${est_http_status}
#   rsh pweb$i "/usr/sbin/sync; /usr/sbin/sync; /usr/sbin/init 6"
fi

#printf "${est_http} HTTP connections"

}

get_uptime() {

cpu_load=$(rsh pweb$i "uptime")

}


print_results() {

printf "%5s%13d%11d%11d\t" pweb$i ${_ctlds_cnt} ${est_http} ${_Story_cnt}
print -n ${_error_file_status}
print -n "\t\t"
print -n "${_overall_status}\t"
print ${cpu_load}


}

#printf "counting number of ctld processes ...\n"
printf "Server  ctlds count HTTP count Story Count Error File \t\tStatus\n"
printf "======  =========== ========== =========== ========== \t\t================================================================\n"
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
ping pweb$i 1 > /dev/null
if [ $? != 0 ] ; then
   printf "$i - Not responding"
else
   #count_ctlds_procs
   #check_errors_file
   #check_http_connections
   #get_uptime
   print_results
   rsh pweb$i "ps -ef | grep Stor[y]"
fi
done

printf "\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

print "$(hostname) has ${_4100_connections} connections on port 4100 established."
print "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
