The Benefits of Setting up sSMTP or any Email Server

Setting up an email server on Nix machine is invaluable to keep an eye on multiple machines. Lets face it, often times Nix machines run so well that we sometimes forget to login and check on them every once and awhile. Why not make them check on their selves by scripting them to email you when they need some maintenance love?

We can have our Nix boxes report:

Send Email Alert on Root Login

Certain machines require only periodic root access. This is often the case on shared servers. You can setup an email alert that will immediately notify you that someone has accessed the root account.

Login as root and edit the file, ‘~/.bash_profile’. Near the bottom add this:

echo -e "Subject: Root Access\n\nALERT - Root Shell Access on: `date`, `who`\n" | ssmtp [email protected]

Now to test it, log out of root and then log back on as root. You should now get an alert that someone has logged in as root. This script shows the time and user. You can adjust the information accordingly depending on how detailed you want it.

Example:

ALERT - Root Shell Access on: Tue Jun 30 20:06:40 MDT 2009, 
nixtutor     pts/0        2009-06-30 19:47 (NixTutor)

This should work as long as your root account uses bash and you have ssmtp setup correctly.

Send Email Alert on Hardware Failure

Linux has some great hardware monitoring tools to give us an overall health status of our system. Lm-sensors can report things like, processor temperature, fan speed, and voltages of the motherboard and other components. Smartmontools can give us information for S.M.A.R.T compatible hard drives that could warn us before hard drive failure and data disaster occurs.

Using these tools and a little bit of knowledge of Bash scripting or other scripting languages like, python, perl, or ruby we can easily create automated system health checks that can report back in the event of failure via email or TXT message.

Send Email Alert to Cell Phone

There may be a time where an error on a server is so critical that it needs to be sent directly to your phone. A lot of cell phone carriers have an SMS email gateway that will forward emails to the cell phone as a text message. This allows us to use sSMTP (or any email server) to send out text messages directly to our phones.

For example if I wanted to send a text message to a phone on the Verizon network I would use, [email protected].

For a complete listing of SMS gateways you can see this article, How to: Send a TXT Message Through Email.