Article Listing 1 Listing 2 Listing 3 Listing 4
Listing 5 Listing 6 Listing 7 Listing 8 Listing 9
Listing 10 apr2004.tar

Listing 4 send_alerts

#!/bin/ksh
#**************************************************************
# Listing 4:
# Program: send_alerts
#
# Description:
#
# Processes alert files in $NEW_ALERTS_DIR and sends email
# according to configuration settings in $CONFIG_FILE.
#
#**************************************************************

. $(dirname $0)/send_globals

#==============================
# Get the command line options.
#==============================

get_options

#============================
# Move redundant alert files.
#============================

filter_alerts | tee -a $MONITOR_LOGFILE

#=========================
# Process each alert file.
#=========================

for alert_file in $(find $NEW_ALERTS_DIR -type f)
do
    #-------------------------------
    # Initializes the address lists.
    #-------------------------------

    pager_addresses=""
    email_addresses=""

    #------------------------------------------
    # Get meta-data from standard header block.
    #------------------------------------------

    alert_host=$(get_alert_attribute $alert_file host)
    alert_priority=$(get_alert_attribute $alert_file priority)
    alert_class=$(get_alert_attribute $alert_file class)

    #----------------------------------------------------------------
    # Compare the priority and class options specified on the command
    # line with the priority and class of the alert file. Go to the
    # next alert file if we don't want to process this one.
    #----------------------------------------------------------------

    check_priority $alert_priority
    check_class $alert_class

    #-----------------------------
    # Read the configuration file.
    #-----------------------------

    strip_config_file

    while read line
    do
        reset_config_variables
        read_config_block
        get_config < $TEMP_CONFIG_FILE

        if should_include && should_send \
        && (( $(echo $message_types | grep -c -w alert) > 0 ))
        then
            addresses=$(echo $addresses)
            if [[ -n $addresses ]]
            then
                case "$address_type" in
                pager)
                    pager_addresses=$(echo $pager_addresses $addresses)
                    ;;
                email)
                    email_addresses=$(echo $email_addresses $addresses)
                    ;;
                *)
                    print "Error: Unexpected address type!" >&2
                esac
            fi
        fi
    done < $STRIPPED_CONFIG_FILE

    #--------------------------------
    # Send alerts to pager addresses.
    #--------------------------------

    pager_addresses=$(remove_duplicates "$pager_addresses")

    if [[ -n $pager_addresses ]]
    then
        subject="PAGE from $THIS_HOST: $alert_class"
        head -5 $alert_file | mailx -s "$subject" $pager_addresses
        print "sent: $(basename $alert_file)" | tee -a $MONITOR_LOGFILE
        print "to: $pager_addresses" | tee -a $MONITOR_LOGFILE
        message_count=$(( $message_count + 1 ))
    fi

    #--------------------------------
    # Send alerts to email addresses.
    #--------------------------------

    email_addresses=$(remove_duplicates "$email_addresses")

    if [[ -n $email_addresses ]]
    then
        subject="ALERT from $THIS_HOST: $alert_class"
        cat $alert_file | mailx -s "$subject" $email_addresses
        echo "sent: $(basename $alert_file)" | tee -a $MONITOR_LOGFILE
        echo "to: $email_addresses" | tee -a $MONITOR_LOGFILE
        message_count=$(( $message_count + 1 ))
    fi

    #--------------------------------------------------
    # Move the alert file to the sent alerts directory.
    #--------------------------------------------------

    mv $alert_file $SENT_ALERTS_DIR/$(basename $alert_file).$(date \
     '+%y%m%d.%H%M%S')
    file_count=$(( $file_count + 1 ))

done

#=========================
# Print a summary message.
#=========================

if (( $file_count > 0 ))
then

    tee -a $MONITOR_LOGFILE << EOF

messages sent: $message_count
alert files processed: $file_count
finished: $(date)

EOF

else
    print "no alerts were processed"
fi

#==========================
# Clean up temporary files.
#==========================

clean_up