add nagios irc bot and documentation

This commit is contained in:
sceox 2020-11-12 17:18:13 -08:00
parent a0f31b1439
commit 59c42e7c2c
2 changed files with 104 additions and 1 deletions

View File

@ -1,7 +1,43 @@
Simple nagios monitoring scripts. Simple nagios monitoring and notification scripts.
`check_ircd` attempts to connect to an IRC server. `check_ircd` attempts to connect to an IRC server.
`check_senderscore` checks the senderscore (a metric for whether an IP address `check_senderscore` checks the senderscore (a metric for whether an IP address
tends to send spam email) of 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.)

67
ircbot.sh Executable file
View File

@ -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