#!/usr/bin/bash
#< sccli show events mailer for past seven days (StorEdge 35xx)
# REQUIRES: lastSevenDays.pl
# Designed to be scheduled via cron, to mail a report, e.g.
# 0 8 * * * /root/sccli_stats.sh | /usr/bin/mailx -s "SCCLI show events Report for `/usr/bin/hostname`" user@hostname.domain.com /dev/null 2>&1
# Executable paths
CUT="/usr/bin/cut"
ECHO="/usr/bin/echo"
GREP="/usr/bin/grep"
ID="/usr/xpg4/bin/id"
PRINTF="/usr/bin/printf"
RM="/usr/bin/rm"
SCCLI="/usr/sbin/sccli"
SED="/usr/bin/sed"
TAIL="/usr/bin/tail"
TR="/usr/bin/tr"
WC="/usr/bin/wc"
XPG_GREP="/usr/xpg4/bin/grep"
# Dependencies
LAST_SEVEN_DAYS="/root/lastSevenDays.pl"
# Master dependants variable
DEPENDANTS="${LAST_SEVEN_DAYS}"
# Temporary Files
EVENT_FILE="/tmp/sccli_show_events.$$"
# Exit codes
SUCCESS=0
ENOTROOT=1
ENOSCCLI=1
ENOINTRP=1
EDEP=1
EABORT=1
# Function: cleanup()
# Arguments: None
# Returns: Nothing
# Description: Removes all temporary event file
function cleanup {
${RM} -f ${EVENT_FILE}
}
trap "cleanup(); exit ${EABORT}" 1 2 3 15
# Function: print_error()
# Arguments: $@
# Returns: Nothing
# Description: Print arguments to STDERR prefixed by "Error: "
function print_error {
${ECHO} "Error: $@" >&2
}
# Function: check_root_user()
# Arguments: none
# Returns: Nothing
# Description: Check that this script is being run by root
function check_root_user {
MYUID=$( ${ID} -u )
if [ "${MYUID}" -ne "0" ]; then
print_error "This script must be run as root"
exit ${ENOTROOT}
fi
}
# Function: check_sccli()
# Arguments: none
# Returns: Nothing
# Description: Ensure that the sccli binary is present and executable
function check_sccli {
if [ ! -e "${SCCLI}" ]; then
print_error "${SCCLI} does not exist"
exit ${ENOSCCLI}
elif [ ! -x "${SCCLI}" ]; then
print_error "Cannot execute ${SCCLI}"
exit ${ENOSCCLI}
fi
}
# Function: check_dependencies()
# Arguments: none
# Returns: Nothing
# Description: Check that all dependant scripts exists and are executable
function check_dependencies {
for DEPENDANT in ${DEPENDANTS}; do
if [ ! -e "${DEPENDANT}" ]; then
print_error "${DEPENDANT} does not exist"
exit ${EDEP}
elif [ ! -x "${DEPENDANT}" ]; then
print_error "Cannot execute ${DEPENDANT}"
exit ${EDEP}
fi
done
}
# Function: check_interpreter()
# Arguments: none
# Returns: Nothing
# Description: Make sure our dependent scripts have a valid interpreter
function check_interpreter {
for DEPENDANT in ${DEPENDANTS}; do
# Assumption: Interpreter is on first line of file
SHEBANG=$( ${SED} -n '1p' ${DEPENDANT} )
INTERPRETER=$( ${ECHO} "${SHEBANG}" | ${SED} 's@^#![ ]*\(.*\)$@\1@' )
if [ ! -e "${INTERPRETER}" ]; then
print_error "Cannot find interpreter (${INTERPRETER}) for ${DEPENDANT}"
exit ${ENOINTRP}
elif [ ! -x "${INTERPRETER}" ]; then
print_error "Interpreter (${INTERPRETER}) for ${DEPENDANT} is not executable"
exit ${ENOINTRP}
fi
done
}
# Function: get_attached_enclosures()
# Arguments: none
# Returns: Nothing
# Description: Set variable ATTACHED_ENCLOSURES to contain a space seperated list of
# of supported enclosures visible to this system
function get_attached_enclosures {
ATTACHED_ENCLOSURES=$( ${SCCLI} --list | ${CUT} -d' ' -f1 | ${TR} '\n' )
}
# Function: print_attached_enclosures()
# Arguments: none
# Returns: Nothing
# Description: Print the contents of the ATTACHED_ENCLOSURES variable
function print_attached_enclosures {
${ECHO} "${ATTACHED_ENCLOSURES}"
}
# Function: print_header()
# Arguments: none
# Returns: Nothing
# Description: As this report is intended to be mailed, print a header
function print_header {
${ECHO} "
Hello,
This is the daily StorEdge 3510 Event report for ${HOSTNAME}. The last seven
days worth of events are shown.
"
}
# Function: show_event_log()
# Arguments: none
# Returns: Nothing
# Description: For each enclosure, print the last seven days worth of events
function show_event_log {
print_attached_enclosures | while read ENCLOSURE; do
${ECHO} "==[ Enclosure: ${ENCLOSURE} ]======================================="
${PRINTF} "\n"
# Save ourselves multiple invocations of (slow) sccli....
${SCCLI} ${ENCLOSURE} show events > ${EVENT_FILE} 2>/dev/null
LINE_LIST=$( ${XPG_GREP} -nf <( ${LAST_SEVEN_DAYS} ) ${EVENT_FILE} |\
${CUT} -d':' -f1 | ${TR} '\n' ' ' )
NUM_MATCHES=$( ${ECHO} "${LINE_LIST}" | ${WC} -w )
if [ "${NUM_MATCHES}" -eq "0" ]; then
${ECHO} " No events in past seven days"
${PRINTF} "\n\n"
continue
fi
for LINE in ${LINE_LIST}; do
DATE_FROM_FILE=$( ${SED} -n "${LINE} p" ${EVENT_FILE} )
${ECHO} "--> Date: ${DATE_FROM_FILE}"
EVENT_DATA=$( ${SED} -n "$(( LINE + 1 )) p" ${EVENT_FILE} )
${ECHO} " ${EVENT_DATA}"
done
${PRINTF} "\n"
done
}
#
# main
#
check_root_user
check_sccli
check_dependencies
check_interpreter
get_attached_enclosures
print_header
show_event_log
cleanup
exit ${SUCCESS}