#!/bin/ksh

DSP_list=`/opt/platform7/lbin/list_processors -d |sort `

WORKDIR="/h/bmetzger/s"

clear

function out_of_comms {


echo "-------------------"
echo "Stuff Out of Comms  "
echo "-------------------"
   rm /var/opt/platform7/tmp/eng_logging_C32_0
   p7dumpstatus c32 > /dev/null 2>&1 

   if [ ! -f /var/opt/platform7/tmp/eng_logging_C32_0 ]
   then
         echo "everything is in comms"
   else
         grep not /var/opt/platform7/tmp/eng_logging_C32_0 
   fi
   echo " "

} #end out_of_comms


function datastore_disk_health {

JUNKFILE=${WORKDIR}/junkfile
integer total_bad=0           # initialize

   echo " "
   echo "--------------------------------------- "
   echo " Looking for failed datastore disks  "
   echo "--------------------------------------- "

#for f in  s004ds02
for f in  ${DSP_list}
do
    if [ -f ${JUNKFILE} ]; then rm ${JUNKFILE}; fi

    print -n "."
    ssh $f "/sbin/lvmdiskscan | grep failed" > ${JUNKFILE} 2>&1

    numb_bad=`sort -u ${JUNKFILE} | wc -l`

    if ((${numb_bad} > 0))
    then
        ((total_bad = $total_bad + 1))

        sort -u ${JUNKFILE} | sed -e 's/\://g' | awk ' {printf "\ncheck: %s  disk: %s\n", "'$f'", $1  }'
    fi
done

if ((${total_bad} < 1))   # equals zero
   then  echo "All datastore disk are okay "
fi

echo " "

if [ -f ${JUNKFILE} ]; then rm ${JUNKFILE}; fi

} # datastore_disk_health


function check_for_stopped_dsp_sessions   {
   echo " "
   echo "------------------------------------------ "
   echo " Checking for Inactive Datastore sessions  "
   echo "------------------------------------------ "

   ignore_list=      # list of BPPs with no links installd (there will be no r75)

   stopped_junk=${WORKDIR}/stopped.junk
   stopped_remote=${WORKDIR}/stopped.remote.sh
   stopped_results=${WORKDIR}/stopped.results

   integer total_stopped=0   # 

# Build the file to be copied to remote boxes  
cat > ${stopped_remote} <<EOF
#! /usr/bin/ksh 
. /opt/platform7/lbin/p7profile; 
integer r75_running ;
r75_running=\`ps  -ef|grep r75.run|grep -v grep |grep -v bash | wc -l\`;
if  ((r75_running > 0)) ; 
 then   
     if [[ -f /var/opt/platform7/tmp/eng_logging_R75_0 ]];
      then rm /var/opt/platform7/tmp/eng_logging_R75_0;
     fi  
     /opt/platform7/bin/p7dumpstatus r75;
     cat /var/opt/platform7/tmp/eng_logging_R75_0;
 fi
EOF

for f in  ${DSP_list}
#for f in s028ds02 
   do
      if [ -f ${stopped_junk} ]; then rm ${stopped_junk}; fi
     
      print -n "."     
      scp ${stopped_remote} $f:/tmp/wm  2> /dev/null   # hide scp progress  window
      ssh $f  "chmod +x /tmp/wm/stopped*; /tmp/wm/stopped*" >  ${stopped_junk}
      ssh $f  "if [ -f /tmp/wm/stop*} ]; then rm /tmp/wm/stop* ; fi"     # cleanup

      stopped_count=`grep STOPPED ${stopped_junk} | wc -l`  # how many stopped sessions di r80 find?

      if ((${stopped_count} > 1))   # at least one card stopped
         then 
              cat ${stopped_junk} |
              awk '{ if ( $1 ~ /DCC/ ) {dcc_id = $NF}  
                   if ( /State/ ) {card_state = $NF}
                   if ( /^$/ && card_state ~ /STOPPED/ ) 
                         {printf ("%8s  %-18s  %s\n", "'$f'", dcc_id, card_state)
                          dcc_id = ""
                          card_state = ""
                         } 
              }'  >> ${stopped_results}
        fi
   done

   if [ -f ${stopped_results} ]; then sort ${stopped_results}; rm ${stopped_results}; fi
   if [ -f ${stopped_remote} ]; then rm ${stopped_remote}; fi
   if [ -f ${stopped_junk} ]; then rm ${stopped_junk}; fi

   echo " "
   echo " "
} #check_for_stopped_dsp_sessions


out_of_comms
#top_processes
#swapinfo
#ntp_check
#rpc_check
#bpp_check
#diskspace_check
#overload_check
#amc_lan_check
#bad_ping_results
#bad_links_summary
#check_alarm_log
#check_error_log
#find_corrupt_bootptab
#find_corrupt_inetd_sec
datastore_disk_health
#check_for_stale_mirrors
#check_hardware_mirror
#ntp_sync_loss_history
#bpp_overload_loss_history
#alarmlog_rollover_period   #broke needs work
#links_per_bpp
#check_for_stopped_dsp_sessions  # check.  needs work
#r75_running_check
#dcc_status_check
