I wanted to build a menu item in Zabbix to open a ticket against a host in Zabbix so that we could flag one for attention.
Continue reading »
I’ve finally gotten around to working on my projects again after doing some exams.
I wanted to get some ADSL monitoring going so I could get some history on the state of my line, get sync rates etc.
The first thing I needed to do was get the modem stats via a script as Zabbix couldn’t login to the modem properly.
I did a quick python script that can be called from Zabbix to get the modem stats.
Continue reading »
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
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
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
As a follow up this this post, the script that I used had a few issues running on the server that I was using it on.
For some reason, on my server, the http interface would randomly die on me and then the script would not be able to stop or start motion.
So I modified the old one to this one, but it necessitated some modifications to the startup script for motion as trying to start motion via a shell script did not work due to the paths used within the startup script.
In the startup script /etc/init.d/motion, the full path is not used for start-stop-daemon. This means that when motion is started from a shell script, the command start-stop-daemon is not found. To rectify this, add /sbin/ to all instances of start-stop-daemon. The following example shows one of the instances of start-stop-daemon. The example begins on line 50, and the start-stop-daemon command is the last line of the example.
case "$1" in
start)
if check_daemon_enabled ; then
if ! [ -d /var/run/motion ]; then
mkdir /var/run/motion
fi
chown motion:motion /var/run/motion
log_daemon_msg "Starting $DESC" "$NAME"
if /sbin/start-stop-daemon --start --oknodo --exec $DAEMON -b --chuid motion ; then
In the updated script, I have added a few checks to ensure that the http interface is still running, and if it isn’t the script will kill motion altogether, and then restart motion to get everything up and running again.
The updated script is below –
#!/bin/bash
PeopleAround=0
people=( 192.168.2.72 192.168.2.117 192.168.2.129 )
wget -o getlog -O /usr/scripts/status http://localhost:8080/0/detection/status
if [ -f /usr/scripts/status ]; then
echo "SettingMotion"
MotionStarted=`cat /usr/scripts/status | sed -e 's/<[^>]*>//g' | tail -n 3 | head -n -2`
else
echo "Status file not available"
echo "Attempting to start motion"
/etc/init.d/motion start
exit
fi
sleep 5
if [ -f /usr/scripts/status ]; then
echo Checking Size
StatusSize=$(stat -c%s /usr/scripts/status)
if [[ $StatusSize -eq 0 ]]; then
echo Size is zero
MotionStarted=`pidof motion | wc -l`
if [[ $MotionStarted -eq 1 ]]; then
echo Motion web interface failed. Restarting motion
killall -9 motion
echo "HTTP interface has failed. killing motion and restarting" | mail -s 'Motion killed'
/etc/init.d/motion restart
else
echo Motion not started.
echo Starting Motion
/etc/init.d/motion start
fi
fi
else
echo "Status file not available"
exit
fi
for ip in "${people[@]}"; do
PersonAround=`ping -c 1 $ip | grep '64 bytes' | wc -l`
if [[ PersonAround -eq 1 ]]; then
PeopleAround=1
break
fi
done
if [[ $PeopleAround -eq 1 ]]; then
if [[ "$MotionStarted" =~ "ACTIVE" ]]; then
wget -O /usr/scripts/pause http://127.0.0.1:8080/0/detection/pause
echo "People around, stopping motion" | mail -s 'Motion stopped'
echo "People around, stopping motion"
fi
else
if [[ "$MotionStarted" =~ "PAUSE" ]]; then
wget -O /usr/scripts/start http://127.0.0.1:8080/0/detection/start
echo "No One around, Starting motion" | mail -s 'Motion started'
echo "No One around, Starting motion"
fi
fi
if [ -e /usr/scripts/status ]; then
rm /usr/scripts/status
fi