#! /bin/sh
#
# Copyright (c) 1992 MCNC, CONCERT Network
# All rights reserved.
#
# Permission to use, copy, modify, and distribute this software and
# its documentation for any purpose and without fee is hereby granted,
# provided that the above copyright notice appears in all copies and
# that both the copyright notice and this permission notice appear in
# supporting documentation.  MCNC makes no representations about the
# suitability of this software for any purpose. It is provided "as is"
# without express or implied warranty.
#

#
#-	ttalarm		Concert Trouble Ticket System Alarm Monitor
#-
#	Author:		Tom Sandoski
#	Released:	04/30/92
#
#


#---------------------------------------------------------------#
#             Check for options (only debug right now)          #
#---------------------------------------------------------------#
while (test -n "$1")
do
        case $1 in
		-D)	set -x; set -v; break;;
		*)	continue;
        esac
	shift;
done


#---------------------------------------------------------------#
# Set up some basic stuff                                       #
#---------------------------------------------------------------#

tempfile="/tmp/tt-temp.$$"
mailout="/tmp/tt-mailout.$$"
mailout1="/tmp/tt-mailout1.$$"

MAIL="/usr/ucb/mail"
MAILLIST="trouble"

TTADMIN="tom"

DBASE="nticketdb"
DBALARM="/usr/postgres/bin/dbalarm -db $DBASE"
MONITOR="/usr/postgres/bin/monitor -TN -c"

subject1="Trouble Ticket Alarm Processing"
echo "Starting alarm processing of open tickets...." > $mailout1
echo "" >> $mailout1

#---------------------------------------------------------------#
# Fetch open tickets by oid sequence                            #
#---------------------------------------------------------------#
params="ticket.status=\"open\""
oids=`$MONITOR "retrieve (ticket.oid) where $params sort by oid using <" $DBASE`

if [ -z "$oids" ]; then echo "No open tickets found" >> $mailout1; fi

noids=0
for n in $oids
do
noids=`expr $noids + 1`
done

echo "$noids open tickets found" >> $mailout1

#----------------------------------------------------------------#
# Process each open ticket in the database                       #
#----------------------------------------------------------------#

for n in $oids	# start loop
do

# Process the next ticket
$DBALARM -o ${tempfile} $n
if [ $? -ne 0 ]; then echo "Database error on ticket.oid $n" >> $mailout1; continue; fi

chmod +x ${tempfile}
. ${tempfile}
/bin/rm -f ${tempfile}

# If alarm not set or blank, skip this ticket
if [ -z "$d_alarm" ]; then continue; fi
if [ "$d_alarm" = " " ]; then continue; fi


#----------------------------------------------------------------#
# If the alarm will have gone to zero, send mail message               #
#----------------------------------------------------------------#
if [ $d_alarm = "1" ]; then
subject="Trouble Ticket Alarm"
echo "An alarm has gone off for the following ticket:" > $mailout
echo "" >> $mailout
echo "Ticket Number:		$d_number" >> $mailout
echo "Type:			$d_type" >> $mailout
echo "Source:			$d_source" >> $mailout
echo "Scope:			$d_scope" >> $mailout
echo "Site:			$d_site" >> $mailout
echo "Circuit:		$d_line" >> $mailout
echo "Priority:		$d_priority" >> $mailout
echo "Owner:			$d_owner" >> $mailout
echo "Ticket Opened:		$d_opened" >> $mailout
echo "Problem Start:		$d_start" >> $mailout
echo "Last Update:		$d_update" >> $mailout
echo "" >> $mailout
echo "Problem:" >> $mailout
echo "$d_pline" >> $mailout
echo "" >> $mailout
echo "Site Contact:		$d_contact" >> $mailout
echo "" >> $mailout

$MAIL -s "$subject" $MAILLIST < $mailout

echo "Alarm mailed for ticket.oid $n" >> $mailout1

fi

echo "Alarm updated for ticket.oid $n" >> $mailout1

done

echo "" >> $mailout1
echo "Alarm processing complete." >> $mailout1
$MAIL -s "$subject1" "$TTADMIN" < $mailout1

/bin/rm -f ${mailout}
/bin/rm -f ${mailout1}
