#!/bin/bash # # health-report2.sh [-tdm email] [-c] [-r YYYY-MM-DD email] # - Simple reporting tool to email top, df and memory reports # - Supports collect only mode for aggregation # # Changelog: # * 2014-05-22 - Nathan Isburgh # - Initial version of script # # 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() { rm -f $TEMPFILE 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 [-tdm email] [-c] [-r YYYY-MM-DD email] Sends requested reports to the given email address -t Send a top report -d Send a df report -m Send a memory report -c Collect only mode, logging to /tmp -r Report mode, email requested log to supplied address email Email recipient for the generated reports eof } # # topreport() # - Sends an email of the top command output to $EMAIL # topreport() { reportheader "Top Report" top -bn 1 } # # dfreport() # - Sends an email of the df command output to $EMAIL # dfreport() { reportheader "df Report" df -h } # # memreport() # - Sends an email of memory usage to $EMAIL # memreport() { reportheader "Memory Report" ps -Ao comm= -o %mem= | sort -nrk 2 } # # reportheader(header) # - spit out a basic report header # reportheader() { printf '******************************************************\n' printf '* %-12s %-37s *\n' $(date +%T) "$1" printf '******************************************************\n' } # Main code body # ... TOPREPORT=0 DFREPORT=0 MEMREPORT=0 COLLECT=0 REPORT=0 while getopts ":tdmcr:" opt; do case $opt in t) debug "top report requested" TOPREPORT=1 ;; d) debug "df report requested" DFREPORT=1 ;; m) debug "mem report requested" MEMREPORT=1 ;; c) debug "collect mode requested" COLLECT=1 ;; r) debug "report mode requested" REPORT=1 REPORTDATE=$(date -d "$OPTARG" +%F 2>&1) if [ $? -ne 0 ] then usage fail 10 "$REPORTDATE" fi ;; \?) 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 ( not in collect mode ) if [ "$COLLECT" -eq 0 -a -z "$EMAIL" ] then usage fail 3 "Please supply an email address for the reports" fi TEMPFILE=$(mktemp) # This code block actually runs/aggregates all of the reports # in to the TEMPFILE ( if [ "$TOPREPORT" -eq 1 ] then topreport fi if [ "$DFREPORT" -eq 1 ] then dfreport fi if [ "$MEMREPORT" -eq 1 ] then memreport fi ) > $TEMPFILE if [ "$COLLECT" -eq 1 ] then cat $TEMPFILE >> /tmp/health-report.$(date +%F) else if [ "$REPORT" -eq 1 ] then if [ -e /tmp/health-report.$REPORTDATE ] then mailx -s "Health reports for $REPORTDATE" $EMAIL < /tmp/health-report.$REPORTDATE else usage fail 20 "No information available for requested date ( $REPORTDATE )" fi else mailx -s "Health reports" $EMAIL < $TEMPFILE fi fi succeed