#!/bin/csh -f
#+
#OSIRIS library of scripts
#
#NAME
#    lastalive - confirm health of the keyword servers
#
#SYNOPSIS
#     lastalive
#
#DESCRIPTION
#
#OPTIONS
#     -t #
#          ID number to attach to all script generated errors, warnings, and
#          questions that are passed to the OGS.  The ID number is separated
#          from the rest of the message with a ";".
#
#     -m
#          simulate calls to RPC servers.  during simulated calls, the 
#          command is echoed (instead of being executed).
#
#     others?
#
#EXAMPLES
#     script_name junk1 -t 123
#          What would happen?
#
#ENVIRONMENT VARIABLES
#     list of environment variables used
#
#FILES
#     list of files used
#
#SERVERS & KEYWORDS
#     list of the servers and keywords used, for example:
#
#     service = all
#          keywords: lastalive
#
#SCRIPTS CALLED
#     list of the scripts called by this script, for example:
#
#     help, syncheck
#
#EXIT STATUS
#     0 - normal exit, no error
#     1 - script aborted by an interrupt
#     2 - syncheck error
#     3 - error parsing command line input
#     other errors...
#
#SEE ALSO
#     ???
#-
#
# Modification History:
# 20160317 - jlyke: Initial version created
#

# Boiler plate for "-h" support for command autohelp.

if ("$1" == "-h") then
    help $0 | more
    exit $status
endif

# Set up to trap interrupts (Ctrl-C, etc.)
onintr abort

# Set default variable values
set cmdpre = ""
set cmdsuf = ""
set idnum = 0

# Check for flags
set noglob
while ($#argv != 0)
##echo "${0} ${mode}: Checking ${1}..."
    switch ($1)
	case -t:
	    set CheckStatus = `syncheck -command $1 $2 -pattern int`
	    if ("$CheckStatus" == "OK") then
		set idnum = $2
		shift
	    else
		echo "${0} ${mode}: Invalid script ID number specified <${2}> - using ${idnum}."
	    endif
	    unset Checkstatus
	    breaksw
	case -m:
	    set sim
	    set cmdpre = "echo ${0}: sim:"
	    set cmdsuf = "-m"
	    breaksw
	default:
	    echo "${0}: Invalid command line flag $1 specified."
	    echo "${0}: Usage: $0 [-t #] [-m]"
	    # set the error code for an error with command line input
	    exit 3
	    breaksw
    endsw
    shift
end
unset noglob

### BODY OF SCRIPT HERE

# set cmd variable
set buf = $0
set cmd = $buf:t

# Set echo to /usr/bin/echo so we can ring the bell
alias echo /usr/bin/echo

# Set server lists
set service_list = ( osiris op1s op2s osds oids oprs otcs ot1s ot2s om1s om2s om3s om4s om5s om6s )

set warnings = 0
set wlist = ""
set errors = 0
set elist = ""
set key = lastalive
# display header
set line = "=============================="
#echo "-------------------------------------------------------------------"
echo $line
echo "Service  LastAlive "
echo $line

foreach service ( $service_list )
  set val = `show -s $service -terse lastalive`
printf "%-8s %-25s\n" $service $val 
end

echo $line
set now = `date "+%y/%m/%d,%T"`
printf "%-8s %-25s\n" "TimeNow" $now
echo $line




goto done

abort:
# Block of code to handle interrupts.
exit 1

done:
# is there anything that needs to go here?
exit