Introduction

This post builds on part 2: NAS – Part 2: Software and services. It’s a detection script to see if your RAID is failing. In the past I’ve had my fair share of failed RAID configurations.

I do know the package mdadm can send alerts, however this small script which can be extended to detect specific changes in RAID/system configuration without using the built in reporting.

Implementation

First let’s start by installing mailutils. This package is needed

sudo apt-get install mailutils

Next up is the ‘ssmtp’ package. This package will allow you to send a mail.

sudo apt-get install ssmtp

Create the ssmtp directory (if it doesn’t exists).

sudo mkdir /etc/ssmtp/

And create an ssmtp.conf file.

sudo nano /etc/ssmtp/ssmtp.conf

This ssmtp.conf requires a username(author) and password(authpass). Also a mail hub (smtp, example: mailhub=smtp.gmail.com:587)

AuthUser=<your-email-adres>
AuthPass=<password>
FromLineOverride=YES
mailhub=<smtp-mailserver>
UseSTARTTLS=YES

To test your configuration you can try to send a test mail. Just change ’email@mail.com’ to your email adress.

echo "This is a test" | mail -s "Test" email@mail.com

If everything works you are ready to create your cron job script. (I will create this script in my user directory, however you can create this wherever you want.)

cd ~
nano health-mdstat.sh

The underscore of ‘cat /proc/mdstat’ is used by mdadm to notify you of any failing RAID disks. So I’ll be checking for this character.

#!/bin/bash
SUBJECT="---RAID IN DEGRADED STATE---"
EMAIL="<target email>"
FROM="<from email>"
EMAILMESSAGE="/tmp/cron-email"
 
cat /proc/mdstat > /tmp/cron-email
 
if grep -q "_" "$EMAILMESSAGE"; then
   mail -aFrom:$FROM -s "$SUBJECT" "$EMAIL" < $EMAILMESSAGE
fi

Let’s assign execute rights to our script.

sudo chmod +x ./health-mdstat.sh

That’s it! Now assign this to a cron job. I assigned my cron job to run daily.
Also happy scripting (when extending this script).

Introduction

The final step to my NAS is keeping my dynamic IP bound to a DNS host. I am using http://afraid.org/ to manage and handle the dynamic DNS.

This script is adapted and based on th script found at: http://adambuchanan.me/post/25473551700/dynamic-dns-with-bash-afraid-org.

Script

This following script will change all hosts assigned to your account to the current IP you are running this script from.

#!/bin/bash
 
#insert SHA-1 hash here (format): username|password
hash=""
 
info_url="http://freedns.afraid.org/api/?action=getdyndns&sha=$hash"
 
echo "Calling $info_url ..."
 
ip=$(dig @208.67.222.220 myip.opendns.com | grep "myip.opendns.com." | grep "0" | awk '{ print $5} ')
echo "Current IP is: $ip"
 
# get the current dns settings...
for each in `curl -s "$info_url"`
do
        domain=`echo "$each" | cut -d"|" -f1`
        dns_ip=`echo "$each" | cut -d"|" -f2`
 
        update_url=`echo "$each" | cut -d"|" -f3`
 
        echo "$domain ..."
        if [ "$ip" != "$dns_ip" ]
        then
                echo "Updating $dns_ip =>$ip ..."
                curl "$update_url" >> log
        fi
        echo "OK"
done

Now run this with a job in crontab to update your afraid.org DNS.

crontab -e