#!/bin/bash # # health-report.sh [-td] email # - Simple reporting tool to email top and df reports # # Changelog: # * 2014-05-22 - Nathan Isburgh # - Initial version of script # - Implemented first two reports: top and df # # Debug flag - 1 for verbose debugging DEBUG=1 # # NO EDITS BELOW THIS LINE # # # succeed() # - gracefully exit from program successfully # succeed() { cleanup exit 0 } # # fail( code, msg ) # - gracefully exit from program in failure state # fail() { cleanup if [ -n "$2" ] then echo "$2" > /dev/stderr fi exit ${1:-1} } # # cleanup() # - clean up log/lock files, etc # cleanup() { return } # # debug( msg ) # - Prints standard debug message to stderr # - Only print if debugging is enabled # debug() { if [ "$DEBUG" -ne 1 ] then return fi echo $(date +%T) " $@" > /dev/stderr } # # usage() # - Print a basic usage statement for user to understand program use # usage() { cat << eof Usage: $0 [-td] email Sends requested reports to the given email address -t Send a top report -d Send a df report email Email recipient for the generated reports eof } # # topreport() # - Sends an email of the top command output to $EMAIL # topreport() { top -bn 1 | mailx -s "Top report" $EMAIL } # # dfreport() # - Sends an email of the df command output to $EMAIL # dfreport() { df -h | mailx -s "df report" $EMAIL } # Main code body # ... TOPREPORT=0 DFREPORT=0 while getopts ":td" opt; do case $opt in t) debug "top report requested" TOPREPORT=1 ;; d) debug "df report requested" DFREPORT=1 ;; \?) usage fail 1 "Invalid option: -$OPTARG" ;; :) usage fail 2 "Option -$OPTARG requires an argument." ;; esac done eval EMAIL=\${$OPTIND} # After processing the above command line, the resulting command # line becomes: # eval EMAIL=${8} debug "\$#: $# \$OPTIND: $OPTIND \$EMAIL: '$EMAIL'" # Make sure the user supplied an email address if [ -z "$EMAIL" ] then usage fail 3 "Please supply an email address for the reports" fi if [ "$TOPREPORT" -eq 1 ] then topreport fi if [ "$DFREPORT" -eq 1 ] then dfreport fi succeed