#!/bin/ksh # # $Id: sendxmpp.ksh,v 1.1 2006/08/28 02:48:04 mike Exp $ # # Hobbit to XMPP client pager script # - Sends an XMPP message containing a Hobbit alert. # - Requires a properly formatted .sendxmpprc file. # - Add the following to your hobbit-alerts.cfg: # SCRIPT /path/to/sendxmpp.ksh FORMAT=SCRIPT # # The script gets the following environment variables pre-defined from Hobbit # so that it can send a meaningful alert: # # BBCOLORLEVEL - The color of the alert: "red", "yellow" or "purple" # BBALPHAMSG - The full text of the status log triggering the alert # ACKCODE - The "cookie" that can be used to acknowledge the alert # RCPT - The recipient, from the SCRIPT entry # BBHOSTNAME - The name of the host that the alert is about # MACHIP - The IP-address of the host that has a problem # BBSVCNAME - The name of the service that the alert is about # BBSVCNUM - The numeric code for the service. From SVCCODES definition. # BBHOSTSVC - HOSTNAME.SERVICE that the alert is about. # BBHOSTSVCCOMMAS - As BBHOSTSVC, but dots in the hostname replaced with commas # BBNUMERIC - A 22-digit number made by BBSVCNUM, MACHIP and ACKCODE. # RECOVERED - Is "1" if the service has recovered. # DOWNSECS - Number of seconds the service has been down. # DOWNSECSMSG - When recovered, holds the text "Event duration : N" where # N is the DOWNSECS value. # # EXIT CODE: # 0 = success # 1 = sendxmpp error # 10 = print_help function (or incorrect commandline) # 11 = ERROR: Missing Hobbit environment variables. See above. # 20 = ERROR: Must be root. # AUTHOR="Mike Arnold " VERSION=20060827 # if [ $DEBUG ]; then set -x; fi # ##### START CONFIG ################################################### #OPTIONS="--tls" SENDDEBUG= ##### STOP CONFIG #################################################### PATH=/usr/bin:/usr/sbin:/bin:/sbin # Function to print the help screen. print_help () { print "Usage: $1" print " $1 [-h|--help]" print " $1 [-v|--version]" exit 10 } # Function to check for root priviledges. check_root () { if [[ `/usr/bin/id | awk -F= '{print $2}' | awk -F"(" '{print $1}' 2>/dev/null` -ne 0 ]]; then print "You must have root priviledges to run this program." exit 20 fi } # If the variable DEBUG is set, then turn on tracing. # http://www.research.att.com/lists/ast-users/2003/05/msg00009.html if [ $DEBUG ]; then # This will turn on the ksh xtrace option for mainline code set -x # This will turn on the ksh xtrace option for all functions typeset +f | while read F junk do typeset -ft $F done unset F junk fi # Process arguments. while [[ $1 = -* ]]; do case $1 in -h|--help) print_help "$(basename $0)" ;; -v|--version) print "\tHobbit to XMPP client pager script" print "\tVersion: $VERSION" print "\tWritten by: $AUTHOR" exit 0 ;; *) print_help "$(basename $0)" ;; esac shift done # Check to see if we have no parameters. #if [[ ! $# -eq 1 ]]; then print_help "$(basename $0)"; fi # Lets not bother continuing unless we have the privs to do something. #check_root ## main if [ -z $RCPT ]; then echo "ERROR: Hobbit environment not found. This script must be run by hobbit." exit 11 fi if [ $SENDDEBUG ]; then # Send a debug message echo "BBCOLORLEVEL - $BBCOLORLEVEL BBALPHAMSG - $BBALPHAMSG ACKCODE - $ACKCODE RCPT - $RCPT BBHOSTNAME - $BBHOSTNAME MACHIP - $MACHIP BBSVCNAME - $BBSVCNAME BBSVCNUM - $BBSVCNUM BBHOSTSVC - $BBHOSTSVC BBHOSTSVCCOMMAS - $BBHOSTSVCCOMMAS BBNUMERIC - $BBNUMERIC RECOVERED - $RECOVERED DOWNSECS - $DOWNSECS DOWNSECSMSG - $DOWNSECSMSG" | sendxmpp $OPTIONS --subject "Hobbit - DEBUG" $RCPT RETVAL=$? else echo "$BBALPHAMSG" | sendxmpp $OPTIONS --subject "Hobbit [${ACKCODE}] ${BBHOSTSVC}:${BBSVCNUM} $BBCOLORLEVEL" $RCPT RETVAL=$? fi exit $RETVAL