#!/bin/ksh

#####################################################################
## saafsfmon.ksh - A script to clean up the SAAgent logs as needed.
## Gary L. Armstrong 02-16-01
#####################################################################
#
# Variables
version=1
NodeName=$(uname -n)
TimeTxt=`date "+%H:%M:%S"`
DateTxt=`date "+%D"`
FileSystem="/opt/saagent"
SAAuser="<USERNAME>"
OnCall="<ONCALLEMAIL>"
OutTxt="Text not set"
CleanCount=0

# The percentage... final number TBD
Threshold=60

# Is /opt/saagent there?
SaaExists="`mount | grep saagent`"
if [[ -z $SaaExists ]]; then
        print "/opt/saagent not mounted on $NodeName"
        exit 1
else # the rest of the script is inside the else

# Can you believe this was easier than using a cut command?
PercentUsed="`df -k /opt/saagent | awk '{print int((100-($3/$2)*100))}'|tail -1`"

# Compare and run cleanup if needed
if [[ $PercentUsed -ge $Threshold ]]; then
        CleanLogs="su - $SAAuser -c /opt/saagent/scripts/control-sa/scripts/clean-logs.sh"
        CleanCount=1
        ExitCode=$?
else
        ExitCode=0
fi

# Did it work?
if [[ ExitCode -eq 0 ]]; then
        print "On $NodeName, saafsmon.ksh executed normally, $DateTxt $TimeTxt"
        if [[ CleanCount -eq 0 ]]; then
                print "No logs were cleaned, $FileSystem is $PercentUsed% full."
        else
                print "Logs were cleaned"
        fi
else
        OutTxt="On $NodeName, saafsfmon.ksh exited badly, return code $ExitCode on $DateTxt $TimeTxt"
        print $OutTxt
        print $OutTxt | mail -s "saafsmon.ksh died" $OnCall
        exit $ExitCode
fi
exit 0

fi