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

Listing 9 send_summary

#!/bin/ksh
# Listing 9
# Program: send_summary
#
# Description:
#
# This script, typically called from cron, summarizes recently sent alerts, 
# and sends an email message with a date, an alerts header, a table of 
# contents, and the Details.  

. $(dirname $0)/send_globals

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

get_options
strip_config_file
addresses=""

while read line
do
    reset_config_variables
    read_config_block

    > $TEMP_HEADER
    > $TEMP_HEADER_2
    > $TEMP_STRINGS_FILE
    alert_num=0

    get_config < $TEMP_CONFIG_FILE

    if (( $(echo $message_types | grep -cw summary) == 0 )) || ! should_send
    then
        addresses=""
        continue
    fi

    echo "\nDetails:" > $TEMP_BODY

    #====================================
    # find all the alert files $days_old.
    #====================================

    alert_files=$(find $SENT_ALERTS_DIR -type f -mtime -$days_old | sort -r)

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

    for alert_file in $alert_files
    do
        #------------------------------------------
        # 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)

        attribute_string=$alert_host.$alert_priority.$alert_class

        if (( $(fgrep $attribute_string $TEMP_STRINGS_FILE | wc -l) > 0 ))
        then
            echo $attribute_string >> $TEMP_STRINGS_FILE
            continue
        else
            echo $attribute_string >> $TEMP_STRINGS_FILE
        fi

        #----------------------------------------------------------------
        # 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

        if should_include
        then
            alert_num=$(( $alert_num + 1 ))
            print "$alert_num. $(basename $alert_file)" >> $TEMP_HEADER
            cat >> $TEMP_BODY << EOF

======================================================================
Alert #$alert_num: $(basename $alert_file)

$(cat $alert_file)
EOF
        fi
    done

    while read number alert_file
    do

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

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

        attribute_string=$alert_host.$alert_priority.$alert_class
        times_repeated=$(echo $(fgrep $attribute_string \
          $TEMP_STRINGS_FILE | wc -l))
        echo "$number $alert_file ($times_repeated)" >> $TEMP_HEADER_2
    done < $TEMP_HEADER

    date > $TEMP_HEADER

    if (( $days_old == 1 ))
    then
        echo "\nAlerts on $THIS_HOST from the past day\n" >> $TEMP_HEADER
    else
        echo "\nAlerts on $THIS_HOST from the past $days_old days:\n" \
          >> $TEMP_HEADER
    fi

    cat $TEMP_HEADER_2 >> $TEMP_HEADER

    if (( $alert_num == 0 ))
    then
        echo "There are no alerts to summarize." > $TEMP_HEADER
        > $TEMP_BODY
    fi

    #----------------
    # Mail the file.
    #----------------

    addresses=$(remove_duplicates $addresses)
    if [[ -n $addresses ]]
    then
        cat $TEMP_HEADER $TEMP_BODY | mailx -s "SUMMARY: alerts from $(uname -n)" $addresses
        message_count=$(( $message_count + 1 ))
    fi

done < $STRIPPED_CONFIG_FILE

echo "messages sent: $message_count"

clean_up