Get Notified When Your Linux Server Boots.
Getting notified of things is great, and when one of your servers boots up, it’s always good to know when. With that in mind, it’s easy to…
Getting notified of things is great, and when one of your servers boots up, it’s always good to know when. With that in mind, it’s easy to add this feature to most any Linux or BSD server without much trouble, especially since I’ve already done the work for you :)
Here’s an example message, from one of my servers:
Box has booted!
Date and time:
Thu Jul 9 20:43:49 EDT 2015
Uptime (length of boot):
20:43:49 up 1 min, 0 users, load average: 2.94, 0.98, 0.35
Server disk usage:
Filesystem Size Used Avail Use%
Mounted on/dev/mapper/Box — vg-root 1.8T 1.5T 266G 85% /
By pasting my Boot Notification Script into your /etc/rc.local, you can use your local mail server to send boot notifications with useful statistics whenever your system comes up, with minimal and straightforward configuration.
Note that this script requires a functioning mail server to be running on the system (such as Postfix), as well as the mailx utility, which can be installed from the mailutils or bsd-mailx packages. In a future post, I’ll explain how you can easily send mail via postfix using Gmail as a relay, while also keeping your domain name intact.
For inital setup, which will produce the message shown above, you only have to set 2 variables, EMAIL and LANIFACE. There is a third variable, HOST, which is automatically set using the machine’s hostname.
EMAIL should contain the email address to which messages should be sent. If you like, you can add multiple addresses, separated by commas. This can be any email address, including your phone’s if you’d like to get notifications via SMS.
The second is HOST. This is set automatically from the machines hostname and requires no manual configuration.
The third is LANIFACE, which is the network interface that the LAN IP should be retrieved from. Usually, this is eth0, but it changes depending on your OS. You can run the command ip link show to get a list of your available network interfaces, if you’re unsure.
**Note: On some systems, /sbin/ifconfig is not installed. This command is usually replaced with /sbin/ip, with addresses shown using the command “ip addr”.
_Should this be the case, replace the code on line 52 with the following:
/bin/ip addr | /bin/grep inet | /usr/bin/tail -n 2 | /usr/bin/head -n 1
_Which should produce something along the lines of:
inet 192.168.0.12/24 brd 10.0.1.255 scope global eth0
# Boot email notification by Charlton Trezevant # Version 3.0 ###SETUP: # Just paste it right in to /etc/rc.local # Note that this depends on having mailx and postfix installed. # See below for more info. ###DEPENDENCIES # This script requires mailx and postfix to be installed. # Install mailx like so: sudo apt-get install bsd-mailx # You can then create an email address for your pi and send outgoing mail through that # (see https://www.linode.com/docs/email/postfix/postfix-smtp-debian7), Or, if you prefer, # you could also use your own mailserver at your domain. ###CONFIGURATION # At the top of the script are 3 different variables. # -- # The first is EMAIL. This should contain the email address to which messages should be sent. If you like, you can add multiple # addresses, separated by commas. This can either be your email, or your phone's SMS email (http://goo.gl/hhAABm for SMS-to-email info) # -- # The second is HOST. This automatically gets the machines hostname and requires no configuration on the part of the user. # -- # The third is the LAN interface that the LAN IP should be pulled from. Usually, this is eth0, but it could also be em0. # run ifconfig to see the list of available interfaces. Change this if you want to get the IP of a different network interface than # the default. # Also, on some systems, /sbin/ifconfig is not installed. This command is usually replaced with /sbin/ip, with addresses shown using # The command "ip addr". # Should this be the case, try using the following command: # /bin/ip addr | /bin/grep inet | /usr/bin/tail -n 2 | /usr/bin/head -n 1 # Which should produce somethinf along the lines of: # inet 192.168.0.12/24 brd 10.0.1.255 scope global eth0 EMAIL='<your_email>@example.com' HOST="$(cat /etc/hostname)" LANIFACE='eth0' echo " $HOST has booted! System information: Date and time: `date +"%A, %e %B %Y, %r"` Uptime (length of boot): `uptime` Network addresses: LAN: `/sbin/ifconfig $LANIFACE | /bin/grep "inet addr" | /usr/bin/cut -d ":" -f 2 | /usr/bin/cut -d " " -f 1` WAN: `timeout 6 dig +short myip.opendns.com @resolver1.opendns.com` Disk usage: `df -kh /` " | mailx -s $HOST' Has Booted!' $EMAIL
Originally published at blog.ctis.me on July 23, 2015.