Jun 222015
 

I had to build my own email sender script for Zabbix as I needed some mangling done on the subjects that was being sent out.
I used python to build it so that I could then use some regex to strip out the garbage that I needed stripped out.

First thing you need to do is to create the email script for Zabbix to run when it receives an alert.
The default location for these scripts is /usr/lib/zabbix/alertscripts/ but this can be customised from the Zabbix server configuration file.

This is the python script that I used, I named it emailalert.py, so the full path for this script should be /usr/lib/zabbix/alertscripts/emailalert.py.

In this example script, I’m only removing text <EMAIL> from any subjects that may have it. This is because some alerts that are being generated have an <EMAIL> tag to enable Zabbix to recognise that the alert needs to go through email, however the team receiving the alert doesn’t want the <EMAIL> tag on the subject line of the email they receive.

#!/usr/bin/python
import smtplib
import sys
import re

email = sys.argv[1]
subject = sys.argv[2]
body = sys.argv[3]

subject = re.sub('<EMAIL>',"",subject)

sender = "Zabbix <zabbix@example.com>"

message = """From: Zabbix <zabbix@example.com>
To: """+email+"""
Subject: """+subject+"""

"""+body+"""
"""

try:
smtpObj = smtplib.SMTP("smtpserver.example.com")
smtpObj.sendmail(sender,email,message)
print "Success"
except SMTPException:
print "Unable to send email"

Once the script has been created, you’ll need to add the script to the Media Types under Administration in Zabbix.
Create a new media type, and give it a name. I’ve called mine Email Script.
For the type, choose Script, and then for the Script Name, use the name that you’ve given the script. In my case, I’ve called it emailalert.py

My Media type configuration

My Media type configuration

Once this is done, you’ll need to set up email addresses for users that need emails through this new script by going to each user, and setting up a new media entry with the type being Email Script

Share
Mar 012013
 

One of my headless wireless devices have been freezing lately, and since it’s in a somewhat isolated location, I have no idea when it freezes.
So I wrote up a quick script to check to see whether it’s up and I’m running it every hour so that it alerts me when it goes down. It will also alert me once it comes back up, in case it comes back up by itself !

The script will also save the last state that the device was in so that it will only email me once when the device goes down.


#!/bin/bash
email=YOUR@EMAIL.HERE

ip=$1
if [[ -z $1 ]]; then
echo Usage: ./pingcheck.sh \
exit
fi
if [ ! -f /tmp/$1.status ]; then
touch /tmp/$1.status
fi

oldstatus=`cat /tmp/$1.status`
reply=`ping -c 1 $ip | grep 64\ bytes | wc -l`
echo $reply > /tmp/$1.status

if [[ $oldstatus -ne $reply ]]; then
if [[ $reply -eq 1 ]]; then
echo $1 is online | mail -s "Node is online" $email
else
echo $1 is offline | mail -s "Node is offline" $email
fi
fi

Share
Feb 192013
 

Just finished my first video tutorial !

This will guide you from a fresh install of Raspbian Server Edition all the way through to getting snapshots in your email.

Would love to hear some feedback on this, positive or negative 🙂

Share
Feb 182013
 

I recently setup motion on my Raspberry Pi so I could stream a Logitech c110 to my Android phone. That was successfully setup in this post.

Next step was to start sending the snapshots to my email address when motion was detected. That way I could see if anything was being detected without having to log into the Raspberry Pi itself.

As I started off with RSE, there is no mail daemon installed, so that’s the first thing I need to do.
sudo apt-get install postfix

Configuration for postfix will depend on where you want email to be routed to. I will be sending it to GMail, and my ISP needs the mail to go through their mail server, so I selected Internet with smarthost
Next thing to setup is the local mail client. There are a few options, but in this post we will use heirloom-mailx.
sudo apt-get install heirloom-mailx

We can test out the Postfix install by attempting to send a test email with our newly installed mail client now !
echo 'hi' | mail -s Test <Your Email Address>
This will send an email to your email address with a body text of “hi” and the subject of “Test”.
If you receive it, congratulations ! Your Pi is now emailing out.
If not then you may need to check your junk mail to see if it got junked.
If there are any issues getting the mail out, running mail on the Pi will launch the mail client in which you will see any failed attempts to send an email.

Once Postfix and the local email client is setup, it’s time to configure motion.
If you’re only running one camera, in /etc/motion/motion.conf at approximately line 521, you should see this
; on_picture_save value
This is the line that configures what motion will do to a file once it is saved.

What we will do, is setup motion to attach the file to an email, and send it to your email address when motion is detected and a file is saved.
To do that, change line 521 to the following –
on_picture_save echo 'webcam alert' | mail -a %f -s "Webcam Alert" <Your Email Address>
Make sure you remove the ; at the beginning as that denotes a commented out line.
The ‘webcam alert’ text can be changed to whatever you want in your version.
Running mail with the -a command line argument will attach the file specified, in this case it is %f, which is motion’s variable for the saved file.

Once that is done, restart motion with /etc/init.d/motion restart and whenever you get motion detected, it will email you the picture as an attachment !

Share
Mar 112012
 

Recently I had some issues with my free DNS service, it wasn’t updating my DNS so I couldn’t access my server remotely.
After this happened a few times, I decided to modify one of the configuration files so that it would email me my external IP address each time the PPP connection came up. This ensured that I would get the latest IP address emailed to me ASAP.

What You Will Need :

Sendmail or equivalent on the linux box that you doing this on.
The PPP connection needs to be on the linux box also for this tutorial, however I will write up a guide that uses an external website instead later on.
A commandline mail sending program, I have used mailx in this example, which is symlinked to mail.

How To Do It :

To do this, I added a file in this directory ( on my Debian System ) –

/etc/ppp/ip-up.d/

which I have named “EmailIP”.
Scripts in this directory are run when the ppp connection goes up.

The file contains the following line which does the gruntwork

echo Connection has come up - New IP Address : `ifconfig ppp0 | grep inet | awk -F' ' '{ print $2 }'|awk -F: '{print $2}'` | /usr/bin/mail -r <From@example.com> -s IP-Address <Email@example.com>

What that command does is pipe the text “New IP Address : ” followed by the output of ifconfig ppp0 which is then grepped down to the inet line, and then awked down to the actual IP address and pipe it into the mail command which can send an email via sendmail or whatever mail daemon you have installed.
The sendmail command has the following parameters specified –
-r : specifies the address that the email appears to be coming from (From@example.com in the example)
-s : specifies the subject of the email (IP-Address in the example)
Email Address (Email@example.com in the example)

Then once you have the script in place, when the ppp connection goes down then comes back up, you should get an email to the email address you specified with the text “Connection has come up – New IP Address : <Your New IP Address>

Share