You can set up your own email notification in Ubuntu Server. This is achieved by installing Postfix and periodically running a custom script.
In my example, I wrote a script to notify the storage usage, the memory usage, Geth status, and Lighthouse validator status. You can modify the script to your liking.
To make the process simple, we’re going to use Gmail as SMTP (“Simple Mail Transfer Protocol”).
Installation Steps
- Create a dummy Gmail account. This account will be used to send emails. After creating an account, go to app access settings and turn on “Allow less secure app access”. If this setting is not turned on, your ubuntu server cannot access the Gmail account.
- Install
mailutils
andpostfix
on Ubuntu Server. In the installation screen, chooseInternet with smarthost
. Input any domain name, such asrandomemaildomain.com
, in System Mail Name. Input[smtp.gmail.com]:587
as SMTP relay host.
sudo apt update && sudo apt install mailutils postfix -y
- Create and open file
sasl_passwd
.
sudo nano /etc/postfix/sasl_passwd
- Copy and paste the following line and save the file. Don’t forget to substitute
username
andpassword
with the dummy Gmail account credentials.
[smtp.gmail.com]:587 <username>@gmail.com:<password>
- Create .db file and change the permissions.
sudo postmap /etc/postfix/sasl_passwd
sudo chmod 600 /etc/postfix/sasl_passwd
- Open the Postfix setting file.
sudo nano /etc/postfix/main.cf
- Copy and paste the following lines at the end and save the file.
smtp_use_tls = yes
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_tls_security_options = noanonymous
- Reload Postfix.
sudo systemctl reload postfix
- Test if you can send email from Ubuntu Server. Check the log file
/var/log/mail.log
for errors.
echo "Test" | mail <your receive email address>
# example
echo "Test" | mail test@gmail.com
- Create and open a shell script file
sendemailnotification.sh
.
sudo nano /root/sendemailnotification.sh
- Copy and paste the following code.
Don’t forget to substitute<your SSD>
with your SSD partition. Mine is nvme0n1p2. You can find yours by running the commanddf -h
.
Also, substitute<receiveaddress@test.com>
with your receive email address.
#!/bin/bash
# SSD partition to monitor
CHECK_PARTITION=<your SSD>
# Your receive email address
MAILADDRESS="<receiveaddress@test.com>"
# Check partition usage
CHECK_PERCENT=`df -h | grep $CHECK_PARTITION | awk '{print $5}' | sed -e '$s/.$//'`
# Check memory usage
MEM_USED=`free -m |sed -n 2P | awk '{print $3}'`
MEM_CAP=`free -m |sed -n 2P | awk '{print $2}'`
MEM_PERCENT=$((MEM_USED*100/MEM_CAP))
# Check Geth status
GETH_STATUS=`journalctl -u geth -n 9 | if grep -q "Imported new chain"; then echo "Geth OK"; else echo "Check Geth!!!!!"; fi`
# Check Lighthouse Beacon Chain status
BEACON_STATUS=`journalctl -u lighthousebeacon -n 9 | if grep -q "Synced"; then echo "Beacon OK"; else echo "Check Beacon!!!!!"; fi`
# Check Lighthouse Validator status
VALIDATOR_STATUS=`journalctl -u lighthousevalidator -n 9 | if grep -q "All validators active"; then echo "Validator OK"; else echo "Check Validators!!!!!"; fi`
# Create email body
{
echo -e "Storage ${CHECK_PERCENT}%\nMemory ${MEM_PERCENT}%\n"
echo $GETH_STATUS
echo $BEACON_STATUS
echo $VALIDATOR_STATUS
} > /var/log/email.txt
# Send email
COUNT=`grep -c "OK" /var/log/email.txt`
if [ $COUNT = 3 ]
then
cat /var/log/email.txt | mail -s "Server OK" $MAILADDRESS
else
cat /var/log/email.txt | mail -s "Check server!!!!!" $MAILADDRESS
fi
- Cron will be used to periodically run the script.
sudo crontab -e
- Copy and paste the following line at the end and save the file. The script will run once everyday.
If you want to modify and test the script, you can edit the timing to*/1 * * * *
. This way, the script will run every minute.
0 1 * * * bash /root/sendemailnotification.sh