#! /bin/sh
# @(#)getcpload.sh		Version  1.20	11/08/04  Dow Corning

# this file is   getcpload.sh
#
# This script file uses OM commands to produce a report of loading
# measurements for control stations (CP, MG, and AB stations). It
# was written to replace a Prelude spreadsheet that accomplishes
# the same task using OM function calls in Prelude.	
#
# Revision History:
#
#  5/05/98  DMD         Original concept and script file
#  5/12/98  DMD  v1.00  working script
#  5/13/98  DMD  v1.01  make sure the supplied CP letterbug is in
#                       uppercase
#  1/10/00  DMD  v1.02  restructure the script a bit to accommodate
#                       CP60 (hw type C101)
#  8/22/00  DMD  v1.03  add FoxGuard type (C001) and correct the
#                       CP60 count
# 10/20/00  DMD  v1.10  check the command line parameter to see if the
#                       input is a file and use it, this is so we can
#                       use a list of CPs other than /etc/cplns such
#                       as /etc/wpcplns (which we pruned to show only
#                       the actual CPs)	
# 11/08/04  DMD  v1.20  change omget to omgetimp to optimize the calls

activate()
{
    ACTIVE=`/opt/fox/bin/tools/omgetimp -v $1_STA:STATION.LODSW | \
           awk '{print $3}'`
    if [ $ACTIVE = FALSE ]
    then
        /opt/fox/bin/tools/omset -b TRUE $1_STA:STATION.LODSW
        sleep 5		# give the station block time to become active
    fi
}

getparm1()
{
    param=`/opt/fox/bin/tools/omgetimp -v $1_STA:STATION.$2 | \
           awk '{printf("%7.1f"), $3}'`
}

getparm2()
{
    param=`/opt/fox/bin/tools/omgetimp -v $1_STA:STATION.$2 | \
           awk '{printf("%5d"), $3}'`
}

if [ $# -eq 0 ]
then
    echo "\nUsage: `basename $0` CPlbug [CPlbug CPlbug ...] | all\n"
    exit
elif [ "$1" = "all" ]   # if told all, use the complete list
then
    CPlist="`sort /etc/cplns`"
    PrintCount=1
elif [ -f $1 ]          # if supplied with a filename, use it
then
    CPlist="`sort $1`"
    PrintCount=1
else
    CPlist="`echo $* | tr '[a-z]' '[A-Z]'`"
    PrintCount=0
fi

PARM10="CPLOAD IOLOAD BPLOAD SQLOAD"
PARM30="CPLOAD IOLOAD BPLOAD SQLOAD OMLDAV IDLETM"
PARMALL="PP_TOT PP_DSC PP_DEL PP_NFD"
CP10=0
CP30=0
CP40=0
CP60=0
FG=0

# some of the station types we are concerned about:
# 201  CP-10
# 203  CP-30
# 204  AB station
# 205  CP-40
# 3001 MG-10
# 3002 MG-30
# C101 CP-60
# C001 FoxGuard (Triconex)

echo "CP Loading Report created on `date '+%A, %B %e, %Y at %r'` by DMDo\n"
echo "                        CP Loading (%)                Peer-to-Peer Status"
echo "         ------------------------------------------   --------------------"
echo "           Total    I/O   Cont    Seq    OM    Idle   Total  Pts  Pts  Pts"
echo "         Control   Scan   Blks   Blks   Scan   Time    Pts  Disc  Del   NF\n"

for sta in $CPlist
do
    unknown=""
    activate $sta
    TYPE=`grep $sta /usr/fox/sp/hldb | awk '{print $2}'`
    if [ $TYPE = 201 -o $TYPE = 3001 ]		# CP-10 or MG-10
    then
        echo "$sta   \c"; CP10=`expr $CP10 + 1`
        for parm in $PARM10
        do
            getparm1 $sta $parm
            echo "$param\c"
        done
        echo "                \c"
        sleep 1
    elif [ $TYPE = 203 -o $TYPE = 204 -o $TYPE = 205 -o $TYPE = C001 \
        -o $TYPE = C101 -o $TYPE = 3002 ]		# the rest
    then
        case $TYPE in
            203|204|3002) echo "$sta * \c"; CP30=`expr $CP30 + 1`;;
            205)          echo "$sta **\c"; CP40=`expr $CP40 + 1`;;
            C101)         echo "$sta # \c"; CP60=`expr $CP60 + 1`;;
            C001)         echo "$sta ##\c"; FG=`expr $FG + 1`;;
        esac
        for parm in $PARM30
        do
            getparm1 $sta $parm
            echo "$param\c"
        done
        echo "  \c"
        sleep 1
    else
        echo "we know nothing about the hw type ($TYPE) for $sta"
        unknown=1
    fi
    for parm in $PARMALL
    do
        if [ "$unknown" = "" ]
        then
            getparm2 $sta $parm
            echo "$param\c"
        fi
    done
    echo
    sleep 1
done
if [ $PrintCount -eq 1 ]
then
    if [ $CP10 -lt 10 ]; then CP10=" $CP10"; fi
    if [ $CP30 -lt 10 ]; then CP30=" $CP30"; fi
    if [ $CP40 -lt 10 ]; then CP40=" $CP40"; fi
    if [ $CP60 -lt 10 ]; then CP60=" $CP60"; fi
    if [ $FG   -lt 10 ]; then   FG=" $FG"; fi
    echo "\n                               -- Counts --"
    echo "Notes: unmarked: CP-10/MG-10   $CP10 FT Modules"
    echo "              *: CP-30/MG-30   $CP30 FT Modules"
    echo "             **: CP-40         $CP40 FT Modules"
    echo "              #: CP-60         $CP60 FT Modules"
    echo "             ##: FoxGuard      $FG Triconex units"
else
    echo "\n\nNotes: unmarked: CP-10/MG-10"
    echo "              *: CP-30/MG-30"
    echo "             **: CP-40"
    echo "              #: CP-60"
    echo "             ##: FoxGuard (Triconex)"
fi
