Receive Linux system stats via email

Receive Linux system stats via email

If you run your own Linux servers like I do, you like to know what’s happening without having to manually login and check for yourself. For example, you might want to know if there are any important packages that need to be updated.

Most sysadmins will set a fixed schedule for updating packages, for example, every mondays. The problem with this approach is that if there is a patch for a serious security vulnerability on Wednesday, your system will remain vulnerable until next Monday.

Instead, it’s better if you get notifications about important updates from the system directly so that you can schedule updates earlier.

In this post, I’ll show you how to configure msmtp to email you periodically.

Example #1: Receiving emails about upgradeable packages

I like to keep my system up to date but I don’t want to always be updating packages unless they’re important because updating packages for no reason can cause downtime. Instead, I’ll only update if there are important security or stability patches. For example, I will always update when there are new Linux kernel releases.

That’s why I have a simply script on all my servers to email me the list of upgradeable packages every day.

Configuring msmtp

msmtp is a simply smtp client that you’ll use to send yourself emails.

First, install it:

apt install msmtp

Then you need to create a configuration file at /etc/msmtprc. Inside that file, enter the following:

defaults
auth on
tls  on
tls_trust_file /etc/ssl/certs/ca-certificates.crt

# Your email account
account myaccount
host    smtp.gmail.com
port    587
from    [email protected]
user    [email protected]
password Caput_Draconis

account default: myaccount
  1. Change myaccount to a name that you like. It’s for your own use.
  2. Change host to your SMTP server. If you use Gmail, then you’ll need to create an app specific password.
  3. from will be used as the from address when sending the email.
  4. user is the user that will login to the SMTP server.
  5. password is obvious.

You can send yourself a test email now:

printf "Subject: Test\n\nTesting 1234" | msmtp [email protected]

Of course, change [email protected] to the address you want to send the email to. If you receive an email at [email protected], then you did the steps correctly. If not, then you did something working and you need to start over.

Scheduling the emails

Now that you have msmtp configured, you need to setup a cron job to email yourself on a daily basis. I like to use anacron as my cron job tool because it will automatically attempt to run cron jobs that failed to start. For example, if your server crashes and needs to restart at the time that your cron job needed to run, the next time it starts up, anacron will execute the job. Pretty handy.

Install it:

apt install anacron

Then you need a simple shell script as your job. Try this one:

#!/bin/bash

apt update >/dev/null 2>/dev/null && \
printf "Subject: $HOSTNAME packages $(date +%Y-%m-%d)\n\n$(apt list --upgradable 2>/dev/null)" \
| msmtp {{ package_notify_email }}

If this runs, it will send you an email containing a list of upgradable packages as provided by apt. The subject line of the email will contain your system’s hostname and a date in the form YYYY-MM-DD

Place this script at /etc/cron.daily/myscript. This will cause the script to fire once a day. There’s also cron.hourly, cron.weekly, and cron.monthly that you can use to run scripts at different intervals.

Don’t forget to make the script executable:

chmod +x /etc/cron.daily/myscript

Now you’re set to receive a list of package updates on a daily basis.

Example #2: Receiving emails about disk failures

Most people use cloud servers instead of running a whole dedicated server. However, I have a few dedicated servers that I run and I like to know when a disk is failing. These days, servers come with NVMe disks with SMART capability. SMART keeps track of a number of attributes that tell you how healthy your disks are.

Instead of logging on the system, running SMART tests, then checking the healthy manually, I want the system to do all of that and email me only if there is an issue with my disks. Let’s do this.

First, you need to configure msmtp as the default mail application on your system. Create a file /etc/mail.rc containing the following:

set sendmail="/usr/bin/msmtp -t"

Now all programs that use sendmail to send email will use msmtp under the hood because msmtp is compatible with sendmail.

Then, install smart tools:

apt install smartmontools

The tools contain a daemon called smartd that can periodically run SMART tests for you. Configure it by creating a file at /etc/smartd.conf containing the following:

DEVICESCAN -a -o on -S on -n standby,q -s (S/../.././02|L/../../6/03) -W 4,35,40 -m [email protected]

Replace [email protected] with the email that will receive the notifications.

The SMART Archlinux wiki and smartd(8) manpage contain information on how to tweak this command.

Your turn

Now that you’ve seen how easy it it to setup Linux to email you about stats automatically, go and do it for your own tasks so that you can avoid wasting time doing things manually.

Since most Linux commands output text to stdout or stderr, you can simply pipe them to msmtp and receive them as emails. In the packages example, I chose to receive only stdout output by redirecing stderr to /dev/null (2>/dev/null).

Using this basic Unix principles, you can easily automate any notification sending task.