#! /bin/sh
# --------------------------------------------------------------------------------------
#
#  DCHANNEL MONITOR EXAMPLE FILE
#
#  Copiright (c) 1993-2001 by Eicon Networks
#
# * This program is free software; you can redistribute it and/or modify
# * it under the terms of the GNU General Public License as published by
# * the Free Software Foundation; either version 2, or (at your option)
# * any later version.
# *
# * This program is distributed in the hope that it will be useful,
# * but WITHOUT ANY WARRANTY; without even the implied warranty of
# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# * GNU General Public License for more details.
# *
# * You should have received a copy of the GNU General Public License
# * along with this program; if not, write to the Free Software
# * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
# *
#
#  Should be used as:
#  "divactrl dchannel -c X -monitor /YYY/dchanel_monitor"
#  Where:
#     "X"   is adapter number
#     "YYY" is full path to this file
#
#  Provides reporting of changes in the state of Layer 1 and Layer 2
#  via EMail.
#
#  Please set "MAILTO" variable to correct EMail address
#
# --------------------------------------------------------------------------------------

MAILTO="root"

# --------------------------------------------------------------------------------------

PATH=/sbin:/usr/sbin:/usr/bin:/bin; export PATH

# --------------------------------------------------------------------------------------

if [ $(($#)) -eq 0 ]
then
	echo "Please start 'divactrl dchannel -c X -monitor /..../dchannel_monitor'"
	echo "Where 'X' is the adapter number"
  exit 1
fi

# --------------------------------------------------------------------------------------

adapter=${1}
layer=${2}
state=${3}

# --------------------------------------------------------------------------------------
smessage="Unknown Change"
message="Unknown Change"
possible_problem=0

if [ $((layer)) -eq $((1)) ]
then
# -------------------------------------------
#  Layer 1
# -------------------------------------------
  if [ $((state)) -eq $((1)) ]
  then
    smessage="Layer 1 UP"
    message="${smessage} (OK, normal operation)"
  else
    smessage="Layer 1 FAILED"
    message="${smessage} (link problem, cable problem ?)"
		possible_problem=1
  fi
else
# -------------------------------------------
#  Layer 2
# -------------------------------------------
  if [ $((state)) -eq 1 ]
  then
    smessage="Layer 2 UP"
    message="${smessage} (connected)" 
  else
    smessage="Layer 2 FAILED"
    message="${smessage} (disconnected)"
		possible_problem=1
  fi
fi

# --------------------------------------------------------------------------------------

tmp_file="/tmp/diva_dchannel_monitor_tmp_adapter${adapter}.txt"
divactrl="/usr/lib/eicon/divas/divactrl"
if [ ! -f ${divactr} ]
then
divactrl="divactrl"
fi

rm -f ${tmp_file}

divactrl="${divactrl} load -c ${adapter}"
name=$(${divactrl} -CardName)
sn=$(${divactrl} -SerialNumber)

# --------------------------------------------------------------------------------------

echo "------------------------------" > ${tmp_file}
echo $(date) >> ${tmp_file}
echo "DIVA Adapter Nr:${adapter} ${name} SN: ${sn}" >> ${tmp_file}
echo "" >> ${tmp_file}
echo "${message}" >> ${tmp_file}
echo "" >> ${tmp_file}
echo "------------------------------" >> ${tmp_file}
echo "." >> ${tmp_file}

if [ $((possible_problem)) -eq 1 ]
then
subject="PROBLEM: Diva Card ${adapter} - ${smessage}"
else
subject="STATUS: Diva Card ${adapter} - ${smessage}"
fi

# --------------------------------------------------------------------------------------

mail -s "${subject}"  "${MAILTO}" < ${tmp_file}

# --------------------------------------------------------------------------------------

exit 0

# --------------------------------------------------------------------------------------

