#!/bin/bash # # # Description: Send an alert mail to a list of people. # # Author: Philip Gwyn # Support: philip@awale.qc.ca # License: GNU General Public License (GPL) # Copyright: (C) 2007 Philp Gwyn / Awalé. # # usage: ./msg {start|stop|status} # # : Text to be sent # # An example usage in /etc/ha.d/haresources: # node1 msg::first 10.0.0.170 msg::last # This will send $MSG_start_first, then claim the IP, then # send $MSG_start_last (see msg.cfg) # # # An exmpale usage in /etc/drbd.conf # incon-degr-cmd "/etc/ha.d/resource.d/msg incon-degr"; # This will send $MSG_icon_degr (see msg.cfg) # CFGFILE="/etc/ha.d/msg.cfg" HADIR=/etc/ha.d . $HADIR/shellfuncs ############################################################### #### Parse the params if [ "$#" -eq 1 ]; then CMD='start' TEXT=$1 if [ "$TEXT" = 'status' ] ; then CMD=$TEXT TEXT="" fi else # In perl, this would be : # $CMD = pop @ARGV; # $TEXT = join ' ', @ARGV; TEXT="" while [ $# -gt 0 ] ; do if [ -n "$LAST" ] ; then if [ -z "$TEXT" ] ; then TEXT=$LAST else TEXT="$TEXT $LAST" fi fi LAST=$1 shift done CMD=$LAST fi NODE=$(uname -n) RESOURCE=$( echo -n $TEXT | sed -e 's/[^0-9A-Za-z]\+/_/g') date >>/tmp/debug echo "CMD=$CMD" >>/tmp/debug echo "TEXT=$TEXT" >>/tmp/debug echo "RESOURCE=$RESOURCE" >>/tmp/debug ############################################################### #### Load the config file if [ ! -r "$CFGFILE" ] ; then DIR=$(dirname $0) if [ -r "$DIR/msg.cfg" ] ; then CFGFILE="$DIR/msg.cfg" else echo "Unable to find msg.cfg ($CFGFILE, $DIR/msg.cfg)" >&2 exit 1; fi fi . $CFGFILE ############################################################### #### Get the text of a message get_text() { T=$1 shift name="${T}_$( echo -n "$*" | sed -e 's/[^0-9A-Za-z]\+/_/g')" M=$(eval echo \$$name) if [ "$M" ] ; then echo $M else echo "$*" fi } ############################################################### #### Send a simple message mail_send() { MSG=$(get_text MSG $1 $TEXT) SUBJECT=$(get_text SUBJ $1: $TEXT) ha_log INFO "$MSG" ha_log INFO "Node: $NODE" for n in $DEST ; do echo -e "$MSG\nNode: $NODE" | mail -s "$SUBJECT" $n done } ############################################################### #### Wall everybody do_wall() { MSG=$(get_text WALL $1 $TEXT) if [ "$MSG" = "$1 $TEXT" ] ; then MSG=$(get_text MSG $1 $TEXT) fi if [ "$MSG" ] ; then true # echo -e "$MSG\nNode: $NODE" | wall fi } ############################################################### #### Simple usage statement usage() { echo "$0 {start|stop|status}" } ############################################################### #### Claiming a resource start() { mail_send "start" do_wall "start" ha_pseudo_resource msg_$RESOURCE start } ############################################################### #### Stop using a resource stop() { mail_send "stop" do_wall "stop" ha_pseudo_resource msg_$RESOURCE stop } ############################################################### #### How's it going status() { # we are always stopped... does this cause a problem? if ha_pseudo_resource msg_$RESOURCE monitor then echo $(get_text MSG running) exit $OCF_SUCCESS else echo $(get_text MSG stopped) exit $OCF_NOT_RUNNING fi } ############################################################### #### Run the command case "$CMD" in start) start ;; stop) stop ;; status) status ;; *) usage ;; esac exit 0