#!/bin/bash
# $Header: /cvs/WebShield/wsrc/files/mgmt/ws_status,v 1.14.2.1 2004/09/10 10:19:31 bwhittak Exp $
#
# Copyright (C) 2004 Networks Associates Technology Inc. All rights reserved.
#
# get the state of all proxies
#
if [ -f $NETAWSS/.profile.vars ]; then 
    . $NETAWSS/.profile.vars 
else 
    . /var/NAIENV/.profile.vars 
fi 

reset_proc()
{
  test -f /proc/${1}/status
  if [ $? = 0 ]
  then
       echo reset_stats 1 > /proc/${1}/status
  fi
}
read_proc ()
{
  test -f /proc/${1}/status
  if [ $? = 0 ]
  then
       cat /proc/${1}/status
  fi
}
reset()
{
    reset_proc wsstats
    reset_proc wsnat
    touch /tmp/ws_stats_time 
}

print_reset_time()
{
   test -f /tmp/ws_stats_time
   if [ $? != 0 ]
   then
       reset
       exit 0
   fi
   modtime=`date -r /tmp/ws_stats_time +%s`
   now=`date "+%s"`
   delta=$(($now - $modtime))

   start=`date -r /tmp/ws_stats_time +%Y%m%d%H%M%S`
   echo i.start ${start}
   echo i.delta ${delta}
}
print_fs_stats()
{
    total=`echo ${3} | awk ' { print $2 } '`
    free=`echo ${3} | awk ' { print $4 } '`

    echo i.${1}.${2}total $total
    echo i.${1}.${2}free  $free
}
file_system_stats()
{
    inode_stats=`df -i /${1} | tail -1`
    file_stats=`df /${1} | tail -1`

    print_fs_stats ${1} i "${inode_stats}"
    print_fs_stats ${1} d "${file_stats}"

}
cpu_info()
{
   loadavg=`cat /proc/loadavg | cut -f1 -d" "`
   echo i.loadaverage $loadavg
   cat /tmp/cpuinfo.txt
}
memory_info()
{
   meminfo=`cat /proc/meminfo| grep ^Mem:`
   freemem=`echo $meminfo | awk ' { print $4 } '`
   used=`echo $meminfo | awk ' { print $3 } '`

   echo i.mem.used $used
   echo i.mem.free $freemem
}
read_nic_state()
{
   /sbin/ethtool ${1} | grep -e "Advertised auto" -e Speed -e Link -e Duplex |
   (
     LINE=
     while read x
     do
        if [ "${LINE}" == "" ]
        then
	  LINE="${x}"
        else
	  LINE="${LINE}, ${x}"
        fi
     done 
     echo $LINE
    )|sed "s/Advertised //"
}
nic_info()
{
   HW=`ifconfig $1 |grep ^${1} | awk ' { print $5 } '`
   echo s.${1}.mac ${HW}
   echo -n "s.${1}.state "

   read_nic_state ${1}
}
ls_info()
{
   test -f /proc/wsrmgr/status
   if [ $? = 0 ]
   then
       cat /proc/wsrmgr/status
   else
	echo s.LoadShare
   fi
}
print_attr()
{
   echo "${1}" `${2}`
}
product_info()
{
    echo s.product webshield
    echo s.product.version V3
    print_attr "s.product.variant" "cat /.version"
    [[ -f /.build ]] && cat /.build
}
print_stats()
{
    print_reset_time

    # Get current appliance statistics
    read_proc wsnat
    read_proc wsstats

    file_system_stats deferred
    file_system_stats quarantine
    file_system_stats scandir
    file_system_stats logs
    cat /tmp/realtime.txt | grep ^s.
    cat /tmp/diskstatus.txt

    cpu_info

    memory_info

    ls_info

    nic_info eth0
    nic_info eth1

    product_info

    echo -n "s.uptime "
    /usr/bin/uptime

    SN=
    if [ -x /usr/sbin/dmidecode ]
    then
       test -f /tmp/hwinfo.txt
       if [ $? != 0 ]
       then
		/usr/sbin/dmidecode | grep "Serial Number" | head -1 | cut -f3 -d" "  > /tmp/hwinfo.txt
      fi
      SN=`cat /tmp/hwinfo.txt`
      SN=`echo \(${SN}\)`
    fi
    
    NAME=`/bin/uname -n`
    echo "s.appliance ${NAME} ${SN}"

    cat /tmp/scanner.txt
}

case $1 in
    reset) reset;;
    *) print_stats;;
esac
exit 0
