#!/bin/csh -f
#+
#NAME
#       ctx - check for missing servers and list GUIs 
#
#PURPOSE
#       Cleans output from ct command.  
#USAGE
#       ctx
# 
#ARGUMENTS
#	None.
# 
#OUTPUT
#	Stdout
# 
#RESTRICTIONS
#	Runs on (OSIRIS_HOST) as osiris numbered account
# 
#EXIT VALUES
#       0 = normal completion
#       1 = problem
#
#EXAMPLE
#       1) ctx
#
#
#-
# Modification history:
#       2005 Nov 02     jlyke   original version
#	2005 Nov 15 	jlyke 	changed name to ctx from ctx2
#       2017 oct 05     jlyke   removed imager servers temporarily
#       2020 jun 30     jlyke   upgrade to computers
#       2020 aug 28     jlyke   stop lying to summit staff
#
#-----------------------------------------------------------------------

# Set echo to /usr/bin/echo so we can ring the bell
alias echo /usr/bin/echo
#alias ct /kroot/rel/default/bin/ct

# how many of each process?
set expected_count = 1
# Write normal ct output to a tmp file
#ct > /tmp/ct.$$
touch /tmp/ct.$$

# Get the status of the detector servers on puunoa and kuiaha
# and add to the tmp file
foreach comp (vm-osiris napili puunoa kuiaha)
  ssh osiris@$comp ct >> /tmp/ct.$$
end

# Set process lists
set motor_list = ( om1s om2s om3s om4s om5s om6s )
#set motor_list = ( om1s om2s om3s om4s )
set power_list = ( op1s op2s oprs )
set  temp_list = ( ot1s ot2s otcs )
#set  tnet_list = ( Motor_1 Motor_2 Motor_3 Motor_4 Motor_5 Motor_6 \
#			Power_Control Temperature_1 Temperature_2 \
#			Temperature_Control )
set  tnet_list = ( Motor_1 Motor_2 Motor_3 Motor_4  \
                        Power_Control Temperature_1 Temperature_2 \
                        Temperature_Control )

set letter_list = ( m p t g i s tnet )

# Break out errors, counts, totals, and computers based on type of server

set m_error = 0
set p_error = 0
set t_error = 0
set g_error = 0
set i_error = 0
set s_error = 0
set tnet_error = 0

set m_count = 0
set p_count = 0
set t_count = 0
set g_count = 0
set i_count = 0
set s_count = 0
set tnet_count = 0

set m_total = $#motor_list
set p_total = $#power_list
set t_total = $#temp_list
set g_total = 1
set i_total = 1
set s_total = 1
set tnet_total = $#tnet_list

set m_comp = napili
set p_comp = napili
set t_comp = napili
set g_comp = napili 
set i_comp = kuiaha
set s_comp = puunoa
set tnet_comp = napili

### Now for the code

# check that we get $expected_count pid's for each motor
foreach motor ($motor_list)
  set count = 0
  set count = `grep -c $motor /tmp/ct.$$`
  if ( $count != $expected_count ) then
    echo "\aWarning -- $motor is missing a required process."
    @ m_error++
  else
    @ m_count++
  endif
end

# check that we get $expected_count pid's for each power
foreach power ($power_list)
  set count = 0
  set count = `grep -c $power /tmp/ct.$$`
  if ( $count != $expected_count ) then
    echo "\aWarning -- $power is missing a required process."
    @ p_error++
  else
    @ p_count++
  endif
end

# check that we get $expected_count pid's for each temp
foreach temp ($temp_list)
  set count = 0
  set count = `grep -c $temp /tmp/ct.$$`
  if ( $count != $expected_count ) then
    echo "\aWarning -- $temp is missing a required process."
    @ t_error++
  else
    @ t_count++
  endif
end

# check that we get 1 pid for global, $expected_count pid's for oids, osds
set pid = osiris
set count = 0
# challenge is that the osiris account runs the osiris global server
#set count = `grep -c $pid /tmp/ct.$$`
set count = `awk '{if ($4 ~ /'$pid'/ && $1 ~ /'$pid'/) print}' /tmp/ct.$$ | wc -l`
if ( $count != $expected_count ) then
  echo "\aWarning -- $pid is missing a required process."
  @ g_error++
else
  @ g_count++
endif

set pid = oids
set count = 0
set count = `grep -c $pid /tmp/ct.$$`
#set count = 2
if ( $count != $expected_count ) then
  echo "\aWarning -- $pid is missing a required process."
  @ i_error++
else
  @ i_count++
endif

set pid = osds
set count = 0
set count = `grep -c $pid /tmp/ct.$$`
if ( $count != $expected_count ) then
  echo "\aWarning -- $pid is missing a required process."
  @ s_error++
else
  @ s_count++
endif

# tnet processes no longer used
#foreach tnet ($tnet_list)
#  set count = 0
#  set count = `grep -c $tnet /tmp/ct.$$`
#  if ( $count != 1 ) then
#    echo "\aWarning -- tnet $tnet is missing a required process."
#    @ tnet_error++
#  else
#    @ tnet_count++
#  endif
#end

# Notify the user how many of each server are running
echo "============================================================"
echo ""
echo " $m_count of  $m_total motor servers running on $m_comp"
echo " $p_count of  $p_total power/pressure servers running on $p_comp"
echo " $t_count of  $t_total temperature servers running on $t_comp"
echo " $g_count of  $g_total global servers running on $g_comp"
echo " $i_count of  $i_total image servers running on $i_comp"
echo " $s_count of  $s_total spec servers running on $s_comp"
#echo "$tnet_count of $tnet_total tnet processes running on $tnet_comp"

# Notify the user which GUIs are running
set gui = `grep -c "\- O" /tmp/ct.$$`
if ( $gui == 0 ) then
  echo ""
  echo "There are no GUIs currently running."
else
  echo ""
  echo "The following GUIs are currently running:"
  echo "username    pid     service"
  echo "-------- --------   -------"
  grep "\- O" /tmp/ct.$$
endif

# Notify the user of the 'ct' command
echo ""
echo "To view the running processes, execute the command: ct"
echo ""

# Print out the results to the screen
if ( $m_error == 0 && $p_error == 0 && $t_error == 0 && \
     $g_error == 0 && $i_error == 0 && $s_error == 0 && \
     $tnet_error == 0) then
  echo "============================================================"
  echo "All the required servers are running."
  echo "============================================================"
else
  echo "============================================================"
  echo "\a\aWarning -- There are missing server processes."
  echo "============================================================"
endif


# Clean up
unalias echo
\rm /tmp/ct.$$




