#!/bin/bash # # health-report.sh # Simple shell script to email basic server health information # # Changelog # 2014-03-27 - Nathan Isburgh # * Initial version # * Implements a top report, and a df report # # Debug flag? 1=on, 0=off DEBUG=1 # Additional user-customized variables... # # END OF CONFIGURATION VARIABLES # NO MORE EDITS BEYOND THIS POINT # # Functions... # # fail [exit code] [exit message] # # Common exit codes: # 1 - Invalid arguments # 2 - Missing arguments # 3 - Missing software # fail() { cleanup ECODE=${1:-1} EMSG=$2 if [ -n "$EMSG" ] then echo $EMSG > /dev/stderr fi exit $ECODE } succeed() { cleanup exit } cleanup() { # Clean up temp files # Close out logs # Disconnect from remote resources return } # # debug "message" # debug() { if [ $DEBUG -eq 1 ] then printf "*** [%s] %s\n" "$(date +%r)" "$1" > /dev/stderr fi } usage() { cat << eof $0 [-tdmc] [-r date] [email] -t Generate a top report -d Generate a df -h report -m Generate a memory report -c Run in collect-only mode -r date Email report for given date email Address to send report eof } check-email() { # Check that email is supplied when required if [ -z "$EMAIL" ] then usage fail 2 "Please supply an email address for the report" fi } top-report() { echo -e "\nTop Report\n--------------------------------------" >> $TMPFILE top -bn 1 >> $TMPFILE } df-report() { echo -e "\nDF Report\n---------------------------------------" >> $TMPFILE df -h >> $TMPFILE } mem-report() { echo -e "\nMemory Report\n-----------------------------------" >> $TMPFILE ps -Ao comm -o pmem | tail -n +2 | sort -nrk 2 >> $TMPFILE } # Main body... TOPREPORT=0 DFREPORT=0 MEMREPORT=0 COLLECT=0 REPORT=0 DATE=$(date +%F) while getopts ":tdmcr:" opt; do case $opt in t) TOPREPORT=1 ;; d) DFREPORT=1 ;; m) MEMREPORT=1 ;; c) COLLECT=1 ;; r) REPORT=1 DATE="$OPTARG" ;; \?) usage fail 1 "Invalid option: -$OPTARG" ;; :) usage fail 1 "Option -$OPTARG requires an argument." ;; esac done # Sanity checks.. if [ $REPORT -eq 1 -a $COLLECT -eq 1 ] then fail 4 "Can not collect and report at same time" fi # Sanity checks.. echo "$DATE" | egrep -q '^[0-9]{4}-[0-9]{2}-[0-9]{2}$' if [ $? -ne 0 ] then fail 20 "Invalid date ( Need: YYYY-MM-DD ): $DATE" fi eval EMAIL=\$$OPTIND TMPFILE=$(mktemp) LOGFILE="/tmp/health-report.$DATE" # Runs the reports, collecting results into TMPFILE if [ $TOPREPORT -eq 1 ] then top-report fi if [ $DFREPORT -eq 1 ] then df-report fi if [ $MEMREPORT -eq 1 ] then mem-report fi # # DONE 1) No -c and no -r ( original form ) ( email required ) # DONE 2) Yes -c and no -r ( collect only mode ) ( email NOT required ) # 3) No -c and yes -r ( report only mode ) ( email required ) # XXX NOT PRACTICAL 4) Yes -c and yes -r ( Collect *and* report ) ( email required ) # # Collection mode -- append current reports to LOGFILE if [ $COLLECT -eq 1 ] then date >> $LOGFILE echo '===========================================' >> $LOGFILE cat $TMPFILE >> $LOGFILE elif [ $REPORT -eq 0 ] then # Not collection, nor reporting mode ( original mode ) check-email cat $TMPFILE | mailx -s "Health report - $DATE" $EMAIL else # Report mode LOGFILE="/tmp/health-report.$DATE" if [ -e "$LOGFILE" ] then check-email cat $LOGFILE | mailx -s "Health report - $DATE" $EMAIL else fail 10 "Invalid report date: $DATE" fi fi succeed