#!/bin/bash # # health-report.sh # - Basic monitoring tool to produce various server health reports # - Emails the reports to the provided email address # - Can aggregate data into a log file, which can be requested # # Changelog # 2014-05-02 - Nathan Isburgh # * Added aggregation ability, and memory report # 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 [-tmdc] [-r date] [email] Simple reporting tool to gather machine health information and email it to the supplied user Options: -t Generate a top report -m Generate a memory report -d Generate a df report -c Collect-only mode ( no email, just log reports ) -r date Email logs for requested date ( any date command supported format ) email Recipient for emailed reports eof } reportheader() { echo '+================================================+' echo ' ' $(date) " - $@" echo '+================================================+' } # # topreport # - Produce a top output and send to email # topreport() { reportheader Top Report top -bn 1 } # # topreport # - Produce a df output and send to email # dfreport() { reportheader DF Report df -h } # # memreport # - Produce a memory report and send to email # memreport() { reportheader Memory Report ps -Ao comm,'%mem' | tail -n +2 | sort -rnk 2 } # Main body RUNTOPREPORT=0 RUNDFREPORT=0 RUNMEMREPORT=0 COLLECT=0 REPORTDATE='' NEEDEMAIL=0 # Process command line arguments... while getopts ":tdmcr:" opt; do case $opt in t) RUNTOPREPORT=1 NEEDEMAIL=1 ;; d) RUNDFREPORT=1 NEEDEMAIL=1 ;; m) RUNMEMREPORT=1 NEEDEMAIL=1 ;; c) COLLECT=1 COLLECTFILE=/tmp/health-report.$(date +%F) ;; r) #echo $REPORTDATE | egrep -q '^[0-9]{4}-[0-9]{2}-[0-9]{2}$' REPORTDATE=$(date -d "$OPTARG" +%F) 2> /dev/null if [ $? -ne 0 ] then usage fail 5 "Invalid date format for -r ($OPTARG). Please use YYYY-MM-DD." fi ;; \?) fail 1 "Invalid option: -$OPTARG" ;; :) fail 1 "Option -$OPTARG requires an argument." ;; esac done debug "\$@: '$@' \$OPTIND: $OPTIND \$#: $#" eval EMAIL=\${$OPTIND} debug "\$EMAIL: $EMAIL" debug "\$REPORTDATE: '$REPORTDATE'" # Additional argument checks.. if [ $# -eq 0 ] then usage fail fi if [ $NEEDEMAIL -eq 1 -a $COLLECT -eq 0 ] then usage fail 8 "Please supply an email address for one-shot report mode" fi if [ "$RUNTOPREPORT" -eq 1 ] then if [ $COLLECT -eq 1 ] then topreport >> $COLLECTFILE else topreport | mailx -s "top Report" $EMAIL fi fi if [ "$RUNDFREPORT" -eq 1 ] then if [ $COLLECT -eq 1 ] then dfreport >> $COLLECTFILE else dfreport | mailx -s "df Report" $EMAIL fi fi if [ "$RUNMEMREPORT" -eq 1 ] then if [ $COLLECT -eq 1 ] then memreport >> $COLLECTFILE else memreport | mailx -s "memory Report" $EMAIL fi fi if [ -n "$REPORTDATE" ] then if [ -z "$EMAIL" ] then usage fail 9 "Please supply an email address for report mode" fi if [ -e "/tmp/health-report.$REPORTDATE" ] then mailx -s "Aggregated report for $REPORTDATE" $EMAIL < /tmp/health-report.$REPORTDATE else fail 10 "Report information not available for date $REPORTDATE" fi fi succeed