#!/bin/bash
# UP script -- WWH -- created
# On start:  send SNMP trap ColdStart  (0) to TRAPHOST
# On stop:   send SNMP trap Enterprise (0) to TRAPHOST
#
# chkconfig: - 99 99 
# description: Last thing to be run when system started, first when stopped
#
# processname: 
# config: 
#
# Notes:
# 1)  This generates a BOOT record in $LOGFILE each time the
#     system is booted.  This boot record consists of the
#     uname -a information (see man uname), and the date.
# 2)  This also generates SNMP traps when started/stopped:
#       start:  snmp ColdStart(0)  trap to $TRAPHOST
#       stop:   snmp Enterprise(0) trap to $TRAPHOST
# 3)  The name of this is aaaup, but this really should
#     get a better name....
# 4)  This should be linked to the following in /etc/rc.d/init.d:
#        cd /etc/rc.d/rc0.d
#        ln -s ../init.d/aaaup K01aaaup
#        cd /etc/rc.d/rc1.d
#        ln -s ../init.d/aaaup S99aaaup
#        cd /etc/rc.d/rc2.d
#        ln -s ../init.d/aaaup S99aaaup
#        cd /etc/rc.d/rc3.d
#        ln -s ../init.d/aaaup S99aaaup
#        cd /etc/rc.d/rc4.d
#        ln -s ../init.d/aaaup S99aaaup
#        cd /etc/rc.d/rc5.d
#        ln -s ../init.d/aaaup S99aaaup
#        cd /etc/rc.d/rc6.d
#        ln -s ../init.d/aaaup K01aaaup
#
# 5)  Configuration items are below, but really should be in
#     a file like /etc/conf.aaaup:

# Setup variables: 
# A) TRAPHOST -- host to send snmptraps to (see man snmptrapd)
#TRAPHOST=yourhost.yourdomain
TRAPHOST=localhost

# B) LOGFILE  -- file to log boots/shutdowns....
#                typically /var/log/bootlog.txt 
LOGFILE=/var/log/bootlog.txt

# C) LOCKFILE -- where the lock for this item should go
#                typically /var/lock/subsys/aaaup 
LOCKFILE=/var/lock/subsys/aaaup

# D) TRAPPGM  -- program/script to generate the trap
#                typically /usr/bin/snmptrap
TRAPPGM=/usr/bin/snmptrap

# Set some variables for information reporting....
BOOTDATE=`date`
UNAME=`uname -a`
HNAME=`hostname`

# source function library
. /etc/rc.d/init.d/functions

case "$1" in
  start)
	echo -n "Starting System: "
        cat >>$LOGFILE <<EOF
$BOOTDATE System started
  $UNAME
EOF
$TRAPPGM $TRAPHOST public 0 0 "$HNAME restart $BOOTDATE"
	touch $LOCKFILE
	echo
	;;
  stop)
	echo -n "Stopping System: "
        cat >>$LOGFILE <<EOF
$BOOTDATE System shutdown
  $UNAME
EOF
$TRAPPGM $TRAPHOST public 6 0 "$HNAME shutdown $BOOTDATE"
	rm -f $LOCKFILE
	echo
	;;
  restart)
        $0 stop
        $0 start
        ;;
  status)
        if [ -f $LOCKFILE ]
        then
          echo "UP -- $HNAME UP, sending traps to $TRAPHOST"
        else 
          echo "DOWN -- $HNAME DOWN, sending traps to $TRAPHOST"
        fi
        ;;
  *)
	echo "Usage: aaaup {start|stop|restart|status}"
	exit 1
esac

exit 0
