#!/bin/sh
#
# network: Sun Enterprise Server check - BB external script test
#
# version 1.0
# version 1.1 - updated downed interface section to logic that works
# version 1.2 - ported to RedHat Linux 6.1
# version 1.3 - properly uses $CAT instead of cat
# version 1.4 - properly uses $MACHINE instead of `uname -n` in bb-hosts check
# version 1.5 - properly uses $THIS_HOST instead of $MACHINE due to fqdn using comma in name - thanks Craig Cook
# version 1.6 - Updated ifconfig to work on HP and correcting ping section of
#                 ypserv contact section, which was missing the parameters
#                 AND changed grep pattern from Collis to Coll for HP
# version 1.7 - Updated ypserv contact info to work after screwing it up.
#                 AND modified the showmount to include the "-e" flag as it should have been.
# version 1.8 - Updated netstat -i section to remove HP-UX's asterisk lines
# version 1.9 - Updated ifconfig output - again - now parses for UP on field 4 instead of 6
#                 AND it properly swaps the gt and lt characters
# version 1.10 - Updated ifconfig section for linux if interface is down
#                 but no address is specified - should be acceptable cond.
# version 1.11 - Updated errors check because netra always reports 3 errors.  Now
#                 the threshold for errors is 5 instead of 0
# version 1.12 - Updated bad message output to indicate info in colorized italicized format
# version 1.13 - Updated ping check on yp server to use the proper ping parmeters for Linux
# version 1.14 - Updated Linux collision monitoring to return output
#
# BIG BROTHER / XXXXXXXXXXXXXXXX status
#
# Written by Paul A. Luzzi
#  on March 22, 2000
#

########################################
# NOTE
# This has been tested with BB 1.2b, 1.4h2, 1.5, 1.5a
# 
# Tested on 
#   Sun Ultra5/10/30/60/80,E220R/420R,E250/450,E3000/3500/4500/6500
#   AIX 43P
#   UnixWare 7,7.01
#   RedHat 5.2, 6.0, 6.1, 7.0, 7.1, 7.2, 7.3
#   Linux Turbo Cluster Server 4.0 and 6.0
#   Caldera Open Linux 1.1, 2.3, and 2.4
########################################

########################################
# INSTALLATION
#  step 1  - update the EXT section of the runbb.sh script to include this
#  ...
########################################

##################################
# CONFIGURE IT HERE
##################################

##################################
# Start of script
##################################

if test ! "$BBHOME"
then
        echo "template: BBHOME is not set"
        exit 1
fi

if test ! -d "$BBHOME"
then
        echo "template: BBHOME is invalid"
        exit 1
fi

if test ! "$BBTMP"                      # GET DEFINITIONS IF NEEDED
then
        echo "template: The BB environment is not set"
        exit 1
fi

#####
#####  Start of custom section added by Paul A. Luzzi
#####

#####
#####  Get Status proc - used to get all responses
#####
get_status() 
{

  #####
  #####  Setup any and all variables before beginning.
  #####

  #####
  #####    Modified extensively on 03-22-2000 by Paul A. Luzzi
  #####    for use with Big Brother
  #####
  #####  Purpose is to report back to a central server, all "standard"
  #####    detailed information about network, disk, volume, cpu, memory
  #####    which is not reported thru regular "easy" checks.
  #####
  #####  Any problem areas should be highlighted in :
  #####    bold, italics, and red font.
  #####

  #####
  #####  Setup some variables for use later
  #####
  COLOR="green"
  
  #####
  #####  List NFS client mounts  
  #####    Needs to be specially modified it you want an error status on bad exit.
  #####    This is because we may run this script on a machine that doesnt do nfs,
  #####    and if thats the case, that should not be an error, if no nfs appears.
  #####    Therefore we will just use this for status.
  #####
  echo "<P><DIV ALIGN=\"CENTER\"><HR>" 
  echo "<B>============== NFS Mounts ==============</B>"
  echo "<B>--- (grep nfs $FSTAB ) ---</B>"
  echo "<HR></DIV>" 
  echo "<BLOCKQUOTE>" 
  CATCH_COMMAND=` $GREP nfs $FSTAB `
  if [ "$?" = "0" ]
    then
      echo "$CATCH_COMMAND"
     else
      echo "No NFS mounts on this machine."
    fi
  echo "</BLOCKQUOTE>" 

  #####
  #####  List exported file Systems 
  #####    Needs to be specially modified it you want an error status on bad exit.
  #####    This is because we may run this script on a machine that doesnt do this,
  #####    and if thats the case, that should not be an error, if no nfs appears.
  #####    Therefore we will just use this for status.
  #####
  echo "<P><DIV ALIGN=\"CENTER\"><HR>" 
  echo "<B>============== Exported Filesystems ==============</B>"
  echo "<B>--- ($SHOWMOUNT -e) ---</B>"
  echo "<HR></DIV>" 
  echo "<BLOCKQUOTE>"
  CATCH_COMMAND=` $SHOWMOUNT -e `
  if [ "$?" = "0" ]
    then
      echo "$CATCH_COMMAND"
     else
      echo "No exported filesystems on this machine."
    fi
  echo "</BLOCKQUOTE>" 
   
  #####
  #####  Network Information 
  #####
  echo "<P><DIV ALIGN=\"CENTER\"><HR>" 
  echo "<B>============== Lan Interface Info ==============</B>"
  echo "<B>--- (ifconfig -a) ---</B>"
  echo "<HR></DIV>" 
  echo "<BLOCKQUOTE>" 

  if [ "$OS_TYPE" = "HP-UX" ]
    then
      CATCH_COMMAND=`for IFACE in \`$LANSCAN | $AWK ' \$4 ~ /UP/ { print \$5 }' \`
        do
          echo "@@" ; $IFCONFIG ${IFACE} 2> /dev/null
          for VIRTUAL in 1 2 3 4
            do
              NON_ZERO_IFACE=\`$IFCONFIG ${IFACE}:${VIRTUAL} 2> /dev/null | $GREP -v 0.0.0.0 | $WC -l \`
              if [ "$NON_ZERO_IFACE" -gt "1" ]
                then
                  echo "@@" ; $IFCONFIG ${IFACE}:${VIRTUAL} 2> /dev/null
                fi
             done
        done`
      FULL_IFCONFIG="echo $CATCH_COMMAND "
     else
      FULL_IFCONFIG=" $IFCONFIG -a "
      CATCH_COMMAND=` $IFCONFIG -a `
    fi

  if [ "$?" = "0" ]
    then
      CATCH_COMMAND_FORMATTED=`$FULL_IFCONFIG | $SED s/"<"/"\&lt;"/g | $SED s/">"/"\&gt;"/g `
      #CATCH_COMMAND_FORMATTED=`$FULL_IFCONFIG `
      echo "$CATCH_COMMAND_FORMATTED" | $SED s/\@\@\ //g 
     else
      COLOR="red"
      echo "\n<FONT COLOR=\"$COLOR\"><I>Problem with network interfaces !!!</I></FONT>"
    fi
  if [ -z "$LINUX_TOP" ]
    then
      ## CATCH_COMMAND2=`echo "$CATCH_COMMAND" | $GREP "^[a-z|A-Z]" | $AWK '{print $1" "$2}' `
      CATCH_COMMAND2=`echo "$CATCH_COMMAND" | $GREP DOWN`
      if [ ! -z "$CATCH_COMMAND2" ]
        then
          COLOR="red"
          echo "\n<FONT COLOR=\"$COLOR\"><I>Problem with network interfaces.  $CATCH_COMMAND2 is/are down !!!</I></FONT>"
        fi
     else
      for INTERFACE in `$FULL_IFCONFIG | $GREP "^[a-z|A-Z]" | $AWK '{print $1}' `
        do
          STATUS=`$IFCONFIG $INTERFACE | $EGREP "UP |NONE SET" `
          if [ -z "$STATUS" ]
            then
              COLOR="red"
              echo "\n<FONT COLOR=\"$COLOR\"><I>Problem with $INTERFACE - its DOWN</I></FONT>"
            fi
        done
    fi
  echo "</BLOCKQUOTE>"

  #####
  #####  Display the routing tables
  #####
  echo "<P><DIV ALIGN=\"CENTER\"><HR>" 
  echo "<B>============== Routing Table Info ==============</B>"
  echo "<B>--- (netstat -r) ---</B>"
  echo "<HR></DIV>" 
  echo "<BLOCKQUOTE>" 
  CATCH_COMMAND=` $NETSTAT -r `
  if [ "$?" = "0" ]
    then
      echo "$CATCH_COMMAND"
     else
      COLOR="red"
      echo "\n<FONT COLOR=\"$COLOR\"><I>Problem with routing table !!!</I></FONT>"
    fi
  echo "</BLOCKQUOTE>" 

  #####
  #####  Display the state of interfaces 
  #####
  echo "<P><DIV ALIGN=\"CENTER\"><HR>" 
  echo "<B>============== State of Network Interfaces ==============</B>"
  echo "<B>--- ($NETSTAT -i) ---</B>"
  echo "<HR></DIV>" 
  echo "<BLOCKQUOTE>" 
  STATUS="green"
  CATCH_COMMAND=` $NETSTAT -i | $GREP -v "*" `
  if [ -z "$LINUX_TOP" ]
    then
      CATCH_COMMAND2=`echo "$CATCH_COMMAND" | $AWK '{print $8}' | $GREP -v Oerrs `
     else
      CATCH_COMMAND2=`echo "$CATCH_COMMAND" | $TAIL +3 | $AWK '{print $5" "$9}' | $GREP -v 0 `
    fi
  for ERRORS in $CATCH_COMMAND2
    do
      if [ ! "$ERRORS" -lt "5" ]
        then
          COLOR="red"
          STATUS="red"
          echo "$CATCH_COMMAND"
          echo "\n<BR><P><FONT COLOR=\"$COLOR\"><I>Problem with network."
          echo "At least one interface has too many errors !!!</I></FONT>"
        fi
    done

  if [ -z "$LINUX_TOP" ]
    then
      CATCH_COMMAND3=`echo "$CATCH_COMMAND" | $AWK '{print $1","$7","$9}' | $GREP -v Coll `
      CATCH_COMMAND_TRAILER=""
      for LINE in $CATCH_COMMAND3
      do
        COLLISIONS=`echo $LINE | $AWK -F, '{print $3}' `
        if [ ! "$COLLISIONS" = "0" ]
          then
            IF_NAME=`echo $LINE | $AWK -F, '{print $1}' `
            TOTAL_OPKTS=`echo $LINE | $AWK -F, '{print $2}' `
            FIVE_PCT=` $EXPR $TOTAL_OPKTS \* 5 / 100 `
            if [ "$COLLISIONS" -gt "$FIVE_PCT" ]
              then
                 if [ "$COLOR" = "red" ]
                   then
                     echo ""
                     echo "<FONT COLOR=\"$COLOR\"><I>And collisions exist on $IF_NAME, and are greater than 5 percent - $COLLISIONS is more than 5 percent which is $FIVE_PCT (as shown above) !!!</I></FONT>"
                    else
                     COLOR="red"
                         STATUS="red"
                     echo "$CATCH_COMMAND"
                     echo "\n<FONT COLOR=\"$COLOR\"><I><P>Problem with network."
                     echo ""
                     echo "Collisions exist on $IF_NAME, and are greater than 5 percent - $COLLISIONS is more than 5 percent which is $FIVE_PCT (as shown above) !!!</I></FONT>"
                   fi
               else
                 CATCH_COMMAND_TRAILER="Collisions exist on $IF_NAME, but $COLLISIONS is less than 5 percent which is $FIVE_PCT."
              fi
          fi
      done
     else
      for INTERFACE in `$IFCONFIG -a | $GREP "^[a-z|A-Z]" | $AWK '{print $1}' `
        do
          COLLISIONS=`$IFCONFIG $INTERFACE | $GREP collisions | $AWK -F: '{print $2}' | $AWK '{print $1}' `
          if [ ! "$COLLISIONS" = "0" ]
            then            
              TOTAL_OPKTS=`$IFCONFIG $INTERFACE | $GREP "TX packets" | $AWK -F: '{print $2}' | $AWK '{print $1}' `
              FIVE_PCT=` $EXPR $TOTAL_OPKTS \* 5 / 100 `
              if [ "$COLLISIONS" -gt "$FIVE_PCT" ]
                then
                  if [ "$COLOR" = "red" ]
                    then
                      echo ""
                      echo "<FONT COLOR=\"$COLOR\"><I>And collisions exist on $INTERFACE, and are greater than 5 percent - $COLLISIONS is more than 5 percent which is $FIVE_PCT !!!</I></FONT>"
                    else
                     COLOR="red"
                     STATUS="red"
                     ## echo "$CATCH_COMMAND" 
                     $IFCONFIG $INTERFACE | $EGREP "$INTERFACE|inet|coll"
                     echo ""
                     echo "<FONT COLOR=\"$COLOR\"><I><P>Problem with network."
                     echo ""
                     echo "Collisions exist on $INTERFACE, and are greater than 5 percent - $COLLISIONS is more than 5 percent which is $FIVE_PCT !!!</I></FONT>"
                   fi
               else
                 CATCH_COMMAND_TRAILER="Collisions exist on $INTERFACE, but $COLLISIONS is less than 5 percent which is $FIVE_PCT."
              fi
          fi

        done
    fi

  if [ "$STATUS" = "green" ]
    then
      echo "$CATCH_COMMAND"
      echo ""
      echo "$CATCH_COMMAND_TRAILER"
    fi
  echo "</BLOCKQUOTE>" 

  #####
  #####  Contact the NIS Server and verify network connectivity
  #####
  NIS="yes"
  echo "<P><DIV ALIGN=\"CENTER\"><HR>" 
  echo "<B>============== NIS Server Info ==============</B>"
  echo "<B>--- (ypwhich) ---</B>"
  echo "<HR></DIV>" 
  echo "<BLOCKQUOTE>" 
  YPSERV=`$YPWHICH 2> /dev/null `
  if [ "$?" = "0" ]
    then
      echo "$YPSERV"
     else
      echo "No NIS used on this server."
      ## echo "Actual response was : $YPSERV."
      NIS="no"
    fi
  echo "</BLOCKQUOTE>" 

  #####
  #####  Now contact the ypserver
  #####
  if [ "$NIS" = "yes" -a ! -z "$YPSERV" ]
    then 
      echo "<P><DIV ALIGN=\"CENTER\"><HR>" 
      echo "<B>============== Contacting $YPSERV ==============</B>"
      echo "<B>--- (ping $YPSERV) ---</B>"
      echo "<HR></DIV>" 
      echo "<BLOCKQUOTE>" 
      if [ "$OS_TYPE" = "HP-UX" -o "$OS_TYPE" = "Linux" ]
        then
          CATCH_COMMAND=` $PING $YPSERV $PINGPAR1 $PINGPAR2 `
         else
          CATCH_COMMAND=` $PING $YPSERV `
        fi
      if [ "$?" = "0" ]
        then
          echo "$CATCH_COMMAND"
         else
          COLOR="red"
          echo "\n<FONT COLOR=\"$COLOR\"><I>Problem contacting $YPSERV !!!"
          echo "Actual response was : $CATCH_COMMAND.</I></FONT>"
        fi
      echo "</BLOCKQUOTE>" 
    fi
   
  #####
  #####  Make sure to export COLOR so that it gets back to "central"
  #####
  export COLOR

#####
#####  End of get_status proc
#####
}

#####
#####  Main body
#####
touch /tmp/$THIS_HOST.network
$GREP $THIS_HOST $BBHOSTS | $GREP "network" | 
while read line
  do
    if [ ! -z "$line" ]
      then
        echo "<BR><DIV ALIGN=\"CENTER\"><HR></DIV>" > /tmp/$THIS_HOST.network
        echo "Status of network on $THIS_HOST " >> /tmp/$THIS_HOST.network
        echo "Captured : `$DATE` " >> /tmp/$THIS_HOST.network
        get_status >> /tmp/$THIS_HOST.network
        $BB $BBDISP "status $THIS_HOST.network $COLOR `$DATE` `$CAT /tmp/$THIS_HOST.network` "
      else
        COLOR="clear"
        $BB $BBDISP "status $THIS_HOST.network $COLOR `$DATE` `$CAT /tmp/$THIS_HOST.network` "
      fi
    done


#####
#####  End of custom section added by Paul A. Luzzi
#####
  
##############################################
# end of script
##############################################
