This repository has been archived on 2025-03-03. You can view files and clone it, but cannot push or open issues or pull requests.
nagios-scripts/ircbot.sh
2020-11-12 17:18:13 -08:00

68 lines
1.4 KiB
Bash
Executable File

#!/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