#!/bin/bash # # health-report.sh # - Basic monitoring tool to produce various server health reports # - Emails the reports to the provided email address # # Changelog # 2014-05-01 - Nathan Isburgh # * Initial version of health-report.sh # * Provides basic top and df reports # # Debug - 1 = on, 0 = off DEBUG=1 # # NO EDITS AFTER THIS LINE # # # fail ecode msg # - gracefully exit with ecode and printing optional msg # fail() { cleanup if [ -n "$2" ] then echo $2 > /dev/stderr fi exit ${1:-1} } # # succeed # - cleanup and exit 0 # succeed() { cleanup exit 0 } # # cleanup # - clean up after script ( remove tmp files, etc ) # cleanup() { return } # # debug msg # - print msg to stderr with simple timestamp # debug() { if [ "$DEBUG" -eq 1 ] then echo $(date +%T) $@ fi } # # usage # - Print simple usage/synopsis type message to stdout # usage() { cat << eof health-report.sh [-td] email Simple reporting tool to gather machine health information and email it to the supplied user Options: -t Generate a top report -d Generate a df report eof } # # topreport # - Produce a top output and send to email # topreport() { top -bn 1 | mailx -s "top Report" $EMAIL } # # topreport # - Produce a df output and send to email # dfreport() { df -h | mailx -s "df Report" $EMAIL } # Main body RUNTOPREPORT=0 RUNDFREPORT=0 # Process command line arguments... while getopts ":td" opt; do case $opt in t) RUNTOPREPORT=1 ;; d) RUNDFREPORT=1 ;; \?) fail 1 "Invalid option: -$OPTARG" ;; :) fail 1 "Option -$OPTARG requires an argument." ;; esac done debug "\$@: '$@' \$OPTIND: $OPTIND \$#: $#" eval EMAIL=\${$OPTIND} debug "\$EMAIL: $EMAIL" # Additional argument checks.. if [ $# -eq 0 ] then usage fail fi if [ "$RUNTOPREPORT" -eq 1 ] then topreport fi if [ "$RUNDFREPORT" -eq 1 ] then dfreport fi succeed