#!/bin/bash
# 
# Server Update Script
#
# Script to send you a simple administrative update via e-mail.  Useful on
# servers for which you have a short attention span.  This basically takes
# output you can easily get at the shell and packages it nicely for your
# review, and with cron it can make a good daily update.  This tells the
# system's uptime, lists users who are logged in, shows disk useage for all
# partitions (important to watch), lists large mail spools, lists
# infrequently-modified mail spools, shows all running processes and then
# sends the results to a user of your choice.
#
# Copyright 2003 Ed Holden; 20031215, version 2.0
# Permission is granted to use this software under the GNU General Public
# License version 2 or later.

# Set e-mail address (or username, if local) of the administrator
address=root
# Set output file location and name
file=data.txt
# Set mail output file location
mailfile=mail.txt
# Set who output file location
whofile=who.txt

# Output data to the file, starting with the uptime command
  echo "Current time and server uptime" > $file
  echo "" >> $file
  uptime >> $file
  echo "____________________________________________________________________________" >> $file; echo "" >> $file

# Then output the current authenticated users, or an alternate message if there are none
  who > $whofile
  if [ -s $whofile ]; then
    echo "Current authenticated users on this server" >> $file
    echo "" >> $file
    cat $whofile >> $file
  else
    echo "No users are currently authenticated on this server." >> $file
  fi
  echo "____________________________________________________________________________" >> $file; echo "" >> $file

# Then get disk utilization
  echo "Disk usage by partition" >> $file
  echo "" >> $file
  df -h >> $file
  echo "____________________________________________________________________________" >> $file; echo "" >> $file

# Then get a list of big mail files  
  echo "Top Ten largest mail spools in /var/spool/mail" >> $file
  echo "" >> $file
  # Create file containing list sorted by size, biggest on top.
    ls -lapS /var/spool/mail > $mailfile
  # Print first 11 lines of that file (includes the total bytes line).
    head -11 $mailfile >> $file
  echo "____________________________________________________________________________" >> $file; echo "" >> $file
  
# Now a list of old mail files  
  echo "Top Twenty least-frequently used mail spools in /var/spool/mail" >> $file
  echo "" >> $file
  # Create file containing list sorted by modification date, oldest on top.
    ls -laptr /var/spool/mail > $mailfile
  # Print first 21 lines of that file (includes the total bytes line).
    head -21 $mailfile >> $file
  echo "____________________________________________________________________________" >> $file; echo "" >> $file

# Running processes - in case anything is awry
  echo "Processes running on $HOSTNAME" >> $file
  echo "" >> $file
  ps -aux >> $file
  echo "____________________________________________________________________________" >> $file; echo "" >> $file

# Wrap it up
echo "Report complete." >> $file

# Mail the text file to administrator (change address as needed)                 
mail -s "Server Update on `date +%m/%d/%Y` at `date +%H:%M` - $HOSTNAME" $address < $file

# Clean up extra files
rm $file $mailfile $whofile

