Nristen's (g)log
2021-08-19
Monit notifications through XMPP
For my personal equipment (servers, routers, etc) I currently use monit to monitor if devices go down or if ports stop responding. I like monit because it does not use very many system resources.
Monit by default sends alerts through email so the problem I had was I would only get outage notifications when I checked my email. I have gotten into the habit of checking my email accounts daily and as such, I have removed the notifications from my computer and phone... This results in a much more peaceful day with much less distrations to take away from my focus. Since I have my own XMPP server now, I thought it would be a perfect option for getting outage notifications to me.
I found a small command line package for send XMPP messages: sendxmpp which was available through the package manager on the raspberry pi I was using for this.
Monit Config:
check host pi-hole address 192.168.1.147
if failed
port 80 protocol http
for 3 cycles
then exec /usr/local/bin/monit-xmpp
if failed
port 53 protocol dns
for 3 cycles
then exec /usr/local/bin/monit-xmpp
My script at /usr/local/bin/monit-xmpp:
#!/bin/bash echo -e "$MONIT_HOST\nService: $MONIT_SERVICE\nAlert: $MONIT_DESCRIPTION" | sendxmpp -f /root/.sendxmpprc -t myxmppaddress@xmppserver.net
A couple notes regarding the above bash script which runs the sendxmpp command.
- If I manually run the sendxmpp command by hand, it automatically finds the config file located at /root/.sendxmpprc however if I run the command from a script I have to specify the location of the config file. The script failed until I added "-d" for debug mode and also added the line at the top of the script, "exec 1> >(logger -s -t $(basename $0)) 2>&1". After this in /var/log/syslog, I found that it was not able to find the value in the $HOME environment variable. So once I specified where the config file was it ran without any problems.
- The "-t" in the sendxmpp command specifies to connect using TLS which my XMPP server requires.
So far this is working very well for me.
References
--------