Script for Periodic Email Notification of unRAID Status


Recommended Posts

Attached is a script I created for smtp notification of unRAID status.  As you can see, I used Joe L.'s excellent script as a base.

 

Joe L.'s original script and instructions can be found here: http://lime-technology.com/forum/index.php?topic=140.msg814#msg814.  I created two versions of the script for myself.  One with emsg="" and the other with emsg="unRaid is OK" with the first called smtp_check_unraid_hourly.sh and the other called smtp_check_unraid_weekly.sh.  I copied the hourly script to cron.hourly and the other script to cron.weekly.  That way I will get a quick notification if something fails, but I won't get a ton of spam in my inbox (only weekly status reports).

 

This script supports multiple "to" addresses (i.e. one for email, and one for text messages to your phone, etc.).  The script also includes the option to include the output of /proc/mdcmd, which  as Joe L. pointed out contains information about your unRAID server (a lot of information; I keep this enabled for both versions of the script that I use).

 

You will need to use the nc (netcat) binary located at http://packages.slackware.it/package.php?q=current/nc-1.10-i386-1 (also attached below) as the behavior exhibited by the one Tom uploaded and the commands it accepts are very different. 

 

I attempted to make the script very easy to read.  A summary, as well as instructions are included as inline comments.

 

-Kenshin

 

EDIT: Obviously you need an smtp server for this to work!  The current implementation uses anonymous access, but can be modified to authenticate.

 

 

#!/bin/bash
#
# Title: smtp_check_status.sh
#
# Summary:
# The purpose of this script is to check the status of the unRAID
# array by searching the /proc/mdcmd file for known bad states.
# This script was originally created by Joe. L for use with Yak
# and has been modified by kenshin (twelston) on 08-17-2007 to add smtp
# functionality, and increase the information transmitted by the script
# (i.e. adding ip, date, and the output of the /proc/mdcmd file if
# elected by the user)
# 
# If you have any questions or problems while using this script please
# post them to the unRaid forum:
#
# http://lime-technology.com/forum/
#
# DISCLAIMER:
# Your use of this script is at your sole risk. This script is provided
#  "as -is", without any warranty
#
# Further, the authors of this script shall not be liable for any damages
# you may sustain by using this information, whether direct, indirect, 
# special, incidental or consequential, even if they have been advised of
# the possibility of such damages.

# Define the following six values to setup this script

# The name of your SMTP server
sSmtpServer="samplehost"

# Your domain name
sDomain="www.sample.com"

# The "from" email address
sMailFrom="[email protected]"

# The "to" mail address (The addressess must be separated by a space, a 
# single address requires no spaces)
sRcptTo="[email protected] [email protected]"

# The email subject
sSubject="unRaid Status Notification"

# Choose whether to include the contents of /proc/mdcmd in your status 
# notifications (Setting this value to anything but "True" is the same
# as setting it to "False")
bIncludeMdcmd="True"

#PATH=$PATH:/boot/

# initialize the error message to an empty string if no hourly OK message is desired
# like this:
# emsg=""
emsg="unRaid is OK"

# request that status be updated
echo "status" >/proc/mdcmd
# now check the status and report as needed
grep "mdState=STARTED" /proc/mdcmd >/dev/null 2>&1
if [ $? != 0 ]
then
emsg="unRaid array not started"
fi 

egrep "=DISK_INVALID|=DISK_DSBL" /proc/mdcmd >/dev/null 2>&1
if [ $? = 0 ]
then
emsg="The unRaid array needs attention. One or more disks are disabled or invalid."
fi 

# if an error message was set, broadcast it to all the machines
# in the notify list in turn
if [ "$emsg" != "" ]
then
# look up the ip address given the machine name
ip_addr=`net lookup $sSmtpServer 2>/dev/null`
if [ $? = 0 ]
then

# Build the echo string
sEcho="ehlo $sDomain\r\nmail from:$sMailFrom\r\n"

# Add each recipient to the RCPT list
for i in $sRcptTo
do
	sEcho+="RCPT TO:"$i"\r\n"
done

# Message header
sEcho+="data\r\nFrom:$sMailFrom\r\nTo:"

# Add each recipient to the message header
for i in $sRcptTo
do
	sEcho+=$i";"
done

sEcho+="\r\n"

# Message Subject
sEcho+="Subject:$sSubject\r\n"

# Message Body (You can change this to look how you want.  Just
# 	       to include \r\n at the end of each line.)
sEcho+="This message is a status update for unRAID  $HOSTNAME\r\n"
sEcho+="-----------------------------------------------------------------\r\n"
sEcho+="Server Name: $HOSTNAME\r\n"
sEcho+="Server IP: `net lookup $HOSTNAME 2>/dev/null`\r\n"
sEcho+="Status: $emsg\r\n"
sEcho+="Date: "
sEcho+=`date 2>/dev/null`
sEcho+="\r\n"

# Add the contents of /proc/mdcmd if elected by the user
if [ $bIncludeMdcmd="True" ]
then
	sEcho+="\r\n"
	sEcho+="Output of /proc/mdcmd:\r\n"
	sEcho+="-----------------------------------------------------------------\r\n"
       
        # Add <CRLF> to each line
                file=/proc/mdcmd

	x=0

	while [ $x -lt $(wc -l <$file) ]
	do
		let x=x+1
		sEcho+=`head -n $x $file | tail -n 1`"\r\n"
	done

	sEcho+="\r\n"
fi 

        # End the message
sEcho+=".\r\n"
sEcho+="QUIT\r\n"

# Send the message we just built
echo -e $sEcho|nc -i 1 -q 1 $ip_addr 25

# Use the following line for debug
#echo -e $sEcho|nc -w 1 -q 2 -i 1 -vv $ip_addr 25
fi
fi

 

 

Link to comment
  • Replies 87
  • Created
  • Last Reply

Top Posters In This Topic

Very nice script.  Glad you could build on my earlier work. I like your coding style... it is a bit like mine. Grin

 

Thank you :D

 

I never thought to use netcat to route email to an smtp server's port 25.  Seems so obvious, now that you thought of it.  Wink

 

I actually didn't think of it until I saw you using it to talk to Yac  ::)  (I just realized I spelled Yac with a K in the script.  :o)

 

-kenshin

Link to comment
  • 2 weeks later...

kenshin,

 

I put a copy of your e-mail alert script on my unRaid server and added it to my cron.hourly folder as described in my original thread.  It works great.

 

I made a few minor changes to set the flag that includes the output of mdcmd if an error is detected, even if it is not normally included when all is OK.

 

I also made one change to simplify your script.  You had a loop using a counter set by wc, and head and tail -1 to get each line in turn from /proc/mdcmd so you could append a CR/LF to the end of each line.  My version is way more efficient. 

 

My version uses the stream editor (sed) to do exactly the same thing, but using one line of code instead of a loop. 

 

To install this on your server:

Download and unzip the smtp_check_unraid.zip file to your PC.  Also download and unzip the netcat command (nc.zip) from kenshin's original post.

Open the smtp_check_unraid.sh script in wordpad (do NOT use notepad) and edit variables near the top to change the smtp server name and e-mail addresses to those suitable for your use.  If you only have one email address to send to, then it can be the only one listed.  If you have more than one address to send to, separate the addresses with spaces.

 

Now, copy the modified file to your flash drive.  Also copy the "nc" command.  They should exist in /boot

 

now... Log onto the unRaid server using telnet and type the following unix commands:

 

cd /boot

chmod +x smtp_check_unraid.sh

chmod +x nc

 

Once you have completed the commands above you can test your installation.

(If you modified the script to NOT get hourly alerts when all is OK, you will need to stop your unRaid array and then perform your test, that way it will have something to report.)

To test type:

smtp_check_unraid.sh

 

At this point all that is left is to copy the smtp_check_unraid.sh script to a folder where it will be executed hourly by the linux cronological scheduler (cron).  To do this type the following command:

 

cp /boot/smtp_check_unraid.sh /etc/cron.hourly/

 

You will want this copy to occur automatically each time you restart your server.  To do this, you can add a line to your "go" script.

First, make a backup copy of the "go" script using the following command:

 

On any but the oldest of unRaid releases, the "go" script is in the /boot/config/ folder.  Make a copy of it by typing the following commands:

cd /boot/config/

cp go go.original

One last step is to add a line to the end of the "go" script to copy the "smtp_check_unraid.sh script to appropriate folders each time you restart the unRaid server.

 

You can use wordpad in windows to do this.  The line shown below in red is the line you will be adding to the "go" script.

 

The last few lines in the "go" script will then be:

 

# Start the management utility

emhttp &

cp /boot/smtp_check_unraid.sh /etc/cron.hourly/

 

This will automatically install (copy) the smtp_check_unraid.sh script to the /etc/cron.hourly folder every time the unRaid server is restarted.

 

Thanks again for your contribution.  I've attached a copy of my modified version of the script to this post.

 

Joe L.

 

My version of the logic used to add the mdcmd contents.

        # Add the contents of /proc/mdcmd if elected by the user
        if [ "$bIncludeMdcmd" = "True" ]
        then
                sEcho+="\r\n"
                sEcho+="Output of /proc/mdcmd:\r\n"
                sEcho+="-----------------------------------------------------------------\r\n"

                # Add <CRLF> to each line
                file=/proc/mdcmd
                sEcho+=`sed "s/$/\\r\\n/" <$file`
                sEcho+="\r\n"
        fi

 

Your original version

    # Add the contents of /proc/mdcmd if elected by the user
    if [ $bIncludeMdcmd="True" ]
    then
        sEcho+="\r\n"
        sEcho+="Output of /proc/mdcmd:\r\n"
        sEcho+="-----------------------------------------------------------------\r\n"
       
        # Add <CRLF> to each line
        file=/proc/mdcmd

        x=0

        while [ $x -lt $(wc -l <$file) ]
        do
            let x=x+1
            sEcho+=`head -n $x $file | tail -n 1`"\r\n"
        done

        sEcho+="\r\n"
    fi 

 

 

Link to comment

I'm glad it worked.  That is definitely a cleaner way to fix the file for SMTP.  Thanks for spending the time on it.

 

I replaced the original version attached in my post with your new version, so that no one downloads old code if they miss your post ;)

 

-kenshin

Link to comment
  • 2 weeks later...

I followed the install steps but I get the following error when running the test:

 

root@Tower:/boot# smtp_check_unraid.sh

-bash: ./smtp_check_unraid.sh: /bin/bash^M: bad interpreter: No such file or directory

 

Any ideas?

It is acting as if it cannot locate /bin/bash.

 

What version of unRaid are you using?  if you type

ls -l /bin/bash

 

What do you get as your output?

 

It should look at lot like this:

root@Tower:~# ls -l /bin/bash

-rwxr-xr-x 1 root root 679620 Sep  7 22:00 /bin/bash*

Lastly, did you use "wordpad" to do your editing to define your mail server, etc.?  (I hope)

or notepad (bad when editing UNIX files, ok for stuff on windows)

The ^M at the end of the line in your error message makes me think you used notepad, and it appended control-M (a carriage return character) to the end of all your lines in the script.  Windows uses a carriage return character, followed by a newline. 

Unix (unRaid is unix) uses only a newline character.

notepad tacks on the carriage returns.  wordpad usually does not.  In wordpad, save the file as a "text file"

Do not save  it as a "dos text file" or you'll get those carriage returns again that you do not want.

 

Basically, it looks like the very first line in the script was trying to find /bin/bash^M  as the command interpreter.  (including the extra control-M carriage return as part of the name) and it did not find it since no command exists with a carriage return as part of its name on the unRaid server.

 

Before you start again fresh using wordpad, you might try these commands (assuming the smtp script is in the /boot folder)  They make a new copy of your script using the "tr" (translate) command to delete the control-M and control-Z characters (MS-dos sometimes pads the ends of files with these)  The last three lines re-name the original file (the bad one) to smtp_check_unraid.orig and then renames the newly translated/filtered file, without the carriage returns, to the correct name. and finally, change its mode to make it executable.

 

cd /boot

tr -d '\15\32' < smtp_check_unraid.sh > smtp_check_unraid.new

mv smtp_check_unraid.sh smtp_check_unraid.orig

mv smtp_check_unraid.new smtp_check_unraid.sh

chmod +x smtp_check_unraid.sh

 

Joe L.

Link to comment

Joe,

 

I did use word to edit the file. I used the set of commands in your last msg and that fixed the error message.

 

Thanks

 

Thats a typo on my part. I mean to say that I used "WordPad" to edit the file.

I just checked, booth of the zip files attached to this thread have only linefeed characters.  Somehow, when you edited the file, whatever editor you used added the carriage returns.  "WordPad" has a "Save As Text" and also a "Save As MS-Dos Text"

You don't want to save as a MS-DOS text, as it will certainly add the carriage returns.  As Tom indicated, usually "WordPAd" will detect the linefeed chars and not add carriage returns.

 

In any case, since you used the "tr" command to get rid of the extra characters it does not matter.

 

Most importantly, does your installed e-mail notification now work?

 

Joe L.

Link to comment

Joe,

 

I did use word to edit the file. I used the set of commands in your last msg and that fixed the error message.

 

Thanks

 

Thats a typo on my part. I mean to say that I used "WordPad" to edit the file.

I just checked, booth of the zip files attached to this thread have only linefeed characters.  Somehow, when you edited the file, whatever editor you used added the carriage returns.   "WordPad" has a "Save As Text" and also a "Save As MS-Dos Text"

You don't want to save as a MS-DOS text, as it will certainly add the carriage returns.  As Tom indicated, usually "WordPAd" will detect the linefeed chars and not add carriage returns.

 

In any case, since you used the "tr" command to get rid of the extra characters it does not matter.

 

Most importantly, does your installed e-mail notification now work?

 

Joe L.

 

When I run "smtp_check_unraid.sh" it gives me an unrecognized domain name. I still received the e-mail so I am not sure this is an issue.

It will only alert you if there is an issue correct?

 

Link to comment

When you run it on the command line you get exactly the same mail as when it is invoked hourly by the chronological scheduler.

 

You will get the e-mail stating all was OK unless you edited these lines from this

# initialize the error message to an empty string if no hourly OK message is desired

# like this:

#emsg=""

emsg="unRaid is OK"

 

to this  (basically, uncommenting the line without the unRaid is OK message, and commenting out the one with it.)

# initialize the error message to an empty string if no hourly OK message is desired

# like this:

emsg=""

#emsg="unRaid is OK"

Setting it to an empty string will cause the script to NOT send an email message UNLESS the unRaid server is stopped or a disk has failed.

 

It sounds as if your mail server is complaining about how you have you "domain" configured in the script.

How do you have these lines configured?

# Your domain name

sDomain="www.yourdomain.com"

 

In my case, since I am on a roadrunner cable account for my e-mail, I set it to be

sDomain="www.XXXXXXXXX.rr.com"

 

Where XXXXXXXXXX = my local roadrunner node. 

I would expect your mail server to complain if some other domain attempted to relay mail through it. (think spam, and you

will understand why)

 

If you set up the cron entry by copying the file to /etc/cron.hourly/smtp_check_unraid.sh it will be executed at 47 minutes past every hour.  If emsg="" you will not get any mail unless your server is stopped or a disk has failed.  You might want to stop the server (using the button on the management array) and then execute the script (or wait tilll 47 minutes past the hour)

When the server is stopped, you will get an e-mail message alerting you to its need for some attention.

 

Although I did not state it, adding the line to the end of the "go" script does nothing until the next time the "go" script is invoked by re-booting your server.  So...

type

cd /boot

cp smtp_check_unraid.sh /etc/cron.hourly/

to put the smtp email script into place withuot have to reboot your server.

 

Joe L.

Link to comment

you would add two lines to the script as shown below in red:

 

Those lines would go after the lines defining the date in the mail header, and before the potential inclusion of the /proc/mdcmd output.

 

Glad it worked first time for you.  In my opinion, it is critical to know that a disk has failed, since the array will run and the contents would appear to be there supplied by parity and the remaining drives.  This script will help anyone who installs it by making a proactive notification of a failure.

 

sEcho+="Date: "

sEcho+=`date 2>/dev/null`

sEcho+="\r\n"

 

sEcho+=`df -h | sed "s/$/\\r\\n/"`

sEcho+="\r\n"

# Add the contents of /proc/mdcmd if elected by the user

if [ "$bIncludeMdcmd" = "True" ]

then

        sEcho+="\r\n"

        sEcho+="Output of /proc/mdcmd:\r\n"

        sEcho+="----------------------------------------------------------\r\n"

 

        # Add <CRLF> to each line

        file=/proc/mdcmd

        sEcho+=`sed "s/$/\\r\\n/" <$file`

        sEcho+="\r\n"

fi

Link to comment

If you have elected daily status reports you might want a different subject line on the e-mail to make it stand out when a failure occurs.

 

Add the line show below in red, it will change the e-mail subject line when the unRaid array needs your attention.

 

Joe L.

 

egrep "=DISK_INVALID|=DISK_DSBL" /proc/mdcmd >/dev/null 2>&1

if [ $? = 0 ]

then

      sSubject="*** unRaid server needs your attention. One or more disks are disabled or invalid. ****"

        emsg="The unRaid array needs attention. One or more disks are disabled or invalid."

        bIncludeMdcmd="True"

fi

Link to comment
  • 2 weeks later...

Could someone explain how you would and SMTP authentication to the script?

 

Judd

The answer is trickier than you might first think because there are quite a few different authentication methods, and some SMTP servers support only a few.

 

To determine the authentication methods supported by your SMTP server, you can invoke a dialog with it using "telnet" and it will tell you.  Only then can we figure out what it will accept.

 

open up a dos-window on your windows box and then type the following

telnet your-smtp-server.name.com 25

If you have the correct server name, it will answer.  Don't forget the "25" at the end of the telnet command to specify the port to connect to.

 

When it answers, you can then type the following commands, each followed by the "enter" key.  You might not see your own keystrokes as you type them, but don't worry, it is just that they are not being echoed back by the smtp-server.

 

EHLO

help

QUIT

 

The QUIT command should end the telnet session.  As I said, you will be typing blind, but don't let that bother you.

 

My session looks like this (I typed the lines in RED):

220 hrndva-omtalb.mail.rr.com ESMTP Welcome to Road Runner.  WARNING: *** FOR AUTHORIZED USE ONLY! ***

EHLO

250-hrndva-omtalb.mail.rr.com

250-HELP

250-VRFY

250-XREMOTEQUEUE

250-ETRN

250-PIPELINING

250-DSN

250-8BITMIME

250 SIZE 30996480

help

214-This SMTP server is a part of the Email Mx E-mail system.  For

214-information about Email Mx, please see http://www.openwave.com

214-

214-      Supported commands:

214-

214-          EHLO    HELO    MAIL    RCPT    DATA

214-          VRFY    RSET    NOOP    QUIT

214-

214-      SMTP Extensions supported through EHLO:

214-

214-          EXPN    HELP    SIZE

214-

214-For more information about a listed topic, use "HELP <topic>"

214 Please report mail-related problems to Postmaster at this site.

221 hrndva-omtalb.mail.rr.com ESMTP server closing connection

QUIT

 

Connection to host lost.

 

C:\>

 

Notice my server did not return any lines starting with "AUTH"  If it did, it would also have given the types of authentication is supported.

 

Try the above, cut and paste the response here, and we can see if we can use the LOGIN authorization method. (probably easiest for our script)

 

According to this page: http://www.fehcom.de/qmail/smtpauth.html the most common session would look something like this:

S: 220 esmtp.example.com ESMTP

C: ehlo client.example.com

S: 250-esmtp.example.com

S: 250-PIPELINING

S: 250-8BITMIME

S: 250-SIZE 255555555

S: 250 AUTH LOGIN PLAIN CRAM-MD5          <----- this mail server tells us the types of authentication is supports

C: auth login                                <- we respond with "auth login" telling smtp server what authentication we will use

S: 334 VXNlcm5hbWU6                                <- the server prompts us (in base 64) for the Login

C: avlsdkf                                                  <-  we respond with our "login ID" in base 64.

S: 334 UGFzc3dvcmQ6                                <- the server prompts us (in base 64) for the "password"

C: lkajsdfvlj                                                <- we respond back with the password (in base 64 encoding)

S: 535 authentication failed (#5.7.1)        <- a ID/bad password was supplied for this demo, so login failed

 

Oh yes, I've never done this authentication, so... if you accidentally reformat your hard disk with some weird command...sorry...

 

Joe L.

 

PS.  only kidding about the disk reformat.. ;D

 

Link to comment

Here is my output.

 

250-AUTH CRAM-MD5 PLAIN LOGIN DIGEST-MD5 NTLM

250-STARTTLS

250-ENHANCEDSTATUSCODES

250-8BITMIME

250-PIPELINING

250-ETRN

250-DSN

250 HELP

help

500 5.5.1 Command unrecognized

quit

221 2.0.0 SMTP closing connection

As you suspected, authorization is supported.

 

So... you would probably need to do something like this:

EHLO

AUTH LOGIN

xyzzyddffgg

abcdefffrrrggg

 

where the last two lines above are your login and password encoded in base64.

 

The current script has these lines

# Build the echo string

sEcho="ehlo $sDomain\r\nmail from:$sMailFrom\r\n"

 

you would change them to something like this:

 

login="yourBase64Login"

password="yourBase64password"

 

# Build the echo string

sEcho="ehlo $sDomain\r\n"

sEcho+="auth login\r\n"

sEcho+="$login\r\n"

sEcho+="$password\r\n"

sEcho+="mail from:$sMailFrom\r\n"

 

Now, how to get your login and password... well, those should be the ones you use on your current email client to SEND mail.

Note: they may be different than the ones you use to log in to GET your personal mail.  Your ISP should be able to help you with the ID and password needed to SEND mail if you do not know it.  Don't expect them to help you with this script however, odds are high they will only help you configure MS-Outlook or some other popular mail client.

 

Now... how to base64 encode your ID and password... well, try this page : http://makcoder.sourceforge.net/demo/base64.php

remember, capitalization matters with most login and passwords.

 

As I said before, I've never done this, so it might work, or might not... I am only going by documents I found online.

 

Good Luck.

 

Joe L.

Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.