From 59c42e7c2c28f5cff599e2c230e34c1c57ceaefd Mon Sep 17 00:00:00 2001 From: sceox Date: Thu, 12 Nov 2020 17:18:13 -0800 Subject: [PATCH] add nagios irc bot and documentation --- README.md | 38 ++++++++++++++++++++++++++++++- ircbot.sh | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+), 1 deletion(-) create mode 100755 ircbot.sh diff --git a/README.md b/README.md index c9a3add..c76590e 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,43 @@ -Simple nagios monitoring scripts. +Simple nagios monitoring and notification scripts. `check_ircd` attempts to connect to an IRC server. `check_senderscore` checks the senderscore (a metric for whether an IP address tends to send spam email) of an IP address. +`ircbot.sh` runs as a daemon as the nagios user, connects to the configured +IRC server and channel, keeps the connection open, and relays messages from +nagios to the channel. + +## Setting up `notify_irc` + +Change the configuration parameters in the script itself. + +Edit your nagios commands configuration (eg +`/usr/local/nagios/etc/commands.cfg`) and define a new host and service +commands that write to the file `/tmp/ircmsg`. For example: + +``` + define command { + command_name notify-service-by-irc + command_line /usr/bin/printf "%b" "$NOTIFICATIONTYPE$ Service: $SERVICEDESC$ Host: $HOSTALIAS$ Address: $HOSTADDRESS$ State: $SERVICESTATE$ Additional Info: $SERVICEOUTPUT$\n" >> /tmp/ircmsg + } + define command { + command_name notify-host-by-irc + command_line /usr/bin/printf "%b" "$NOTIFICATIONTYPE$ Host: $HOSTNAME$ State: $HOSTSTATE$ Address: $HOSTADDRESS$ Info: $HOSTOUTPUT$\n" >> /tmp/ircmsg + } +``` + +Then create a contact with parameters: + + service_notification_commands notify-service-by-irc + host_notification_commands notify-host-by-irc + +Check that the new configuration is valid: + + sudo /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg + +And then restart nagios with `systemctl` or `rcctl`. + +Register `ircbot.sh` as a service with `systemd` or `rc`. (TODO: write service +files for these.) diff --git a/ircbot.sh b/ircbot.sh new file mode 100755 index 0000000..68917b7 --- /dev/null +++ b/ircbot.sh @@ -0,0 +1,67 @@ +#!/bin/sh + +# TODO: make sure only nagios can write to $inputf and $msgf, else do not run + +# configuration area +nick='nagios' +chan='#ops' +server='irc.anarchyplanet.org' +email='nagios@anarchyplanet.org' +password='mysecretpass' +port='6667' +inputf=/tmp/ircinput +msgf=/tmp/ircmsg +logf=/tmp/notify_irc.log +joinmsg="nagios bot reporting" + +touch $msgf + +connect() { + echo "NICK $nick" > $inputf + echo "USER $nick 8 * : $nick" >> $inputf + echo "PRIVMSG NickServ : identify $password" >> $inputf + echo "JOIN $chan" >> $inputf + echo "PRIVMSG $chan : $joinmsg" >> $inputf +} +reconnect() { + exec $0 +} +delay() { + while read line + do + sleep 1.5 + echo $line + done +} +register() { + echo "PRIVMSG NickServ : register $password $email" >> $inputf +} +loop() { + tail -f $msgf | delay | while read line + do + echo "PRIVMSG #ops : $line" >> $inputf + done +} + +connect + +loop & + +tail -f $inputf | delay | nc $server $port | while read msg +do + echo "$msg" | tee $logf + case "$msg" in + *'PING'*) echo "$msg" | sed 's/PING/PONG/' >> $inputf + ;; + *'is not a registered nickname.'*) register + ;; + *'You have not registered'*) register + ;; + *'You have not joined'*) echo "JOIN $chan" >> $inputf + ;; + *'Cannot join'*) sleep 10; echo "JOIN $chan" >> $inputf + ;; + *'ERROR :Closing link:'*) reconnect + ;; + esac +done