Un bot irc pour les notifications nagios
De April MediaWiki
Révision datée du 16 novembre 2015 à 16:50 par Polux (discussion | contributions) (→Le script perl)
Installation
Le principe est celui-ci : http://tools.suckless.org/ii/bots
- installer ii (dans le paquet debian).
- installer le script d'init d'ii en copiant le fichier ci-après dans /et/init.d/ii puis lancer update-rc.d ii defaults.
- installer le script perl (ci-après) dans les plugins de nagios. Penser aux permissions 755 car il sera executé en temps que nagios.
- faire la config nagios en adaptant les chemins (en particulier le répertoire de connexion de l'utilisateur nagios).
Ça devrait suffire. Pour les tests, penser à vérifier que l'user nagios dispose d'un shell et d'un home.
Le script d'init
#! /bin/sh
# Adapted from Nagios init script by PoluX
### BEGIN INIT INFO
# Provides: ii
# Required-Start: $local_fs $remote_fs $syslog $named $network $time
# Required-Stop: $local_fs $remote_fs $syslog $named $network
# Should-Start:
# Should-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: ii is a IRC client.
# Description: ii is a minimalist FIFO and filesystem-based IRC client.
### END INIT INFO
set -e
. /lib/lsb/init-functions
DAEMON=/usr/bin/ii
NAME="ii"
DESC="ii IRC client"
NICENESS=5
USER="nagios"
IRCSERVER="irc.freenode.net"
IRCNICK="nagios_april"
IRCFULLNAME="April IRC Bot"
DAEMON_OPT="-s $IRCSERVER -n $IRCNICK -f $IRCFULLNAME"
[ -x "$DAEMON" ] || exit 0
# this is from madduck on IRC, 2006-07-06
# There should be a better possibility to give daemon error messages
# and/or to log things
log()
{
case "$1" in
[[:digit:]]*) success=$1; shift;;
*) :;;
esac
log_action_begin_msg "$1"; shift
log_action_end_msg ${success:-0} "$*"
}
check_started () {
check_cmd="ps -e | grep '\<ii\>' -q"
eval $check_cmd >/dev/null
if [ -f "$THEPIDFILE" ]; then
pid="$(cat $THEPIDFILE)"
if [ "$pid" ] && kill -0 $pid >/dev/null 2>/dev/null; then
return 0 # Is started
fi
fi
return 1 # Isn't started
}
THEPIDFILE='/var/run/ii.pid'
start () {
if ! check_started; then
start-stop-daemon --background -m -p $THEPIDFILE --user $USER --name $DAEMON --start --quiet --chuid $USER --exec $DAEMON -- $DAEMON_OPT
ret=$?
else
log_warning_msg "already running!"
fi
return $ret
}
stop () {
killproc -p $THEPIDFILE
ret=$?
if [ `pidof ii | wc -l ` -gt 0 ]; then
echo -n "Waiting for $NAME daemon to die.."
cnt=0
while [ `pidof ii | wc -l ` -gt 0 ]; do
cnt=`expr "$cnt" + 1`
if [ "$cnt" -gt 15 ]; then
kill -9 `pidof ii`
break
fi
sleep 1
echo -n "."
done
fi
echo
if [ -n "$ret" ]; then
return $ret
else
return $?
fi
}
status()
{
log_action_begin_msg "checking $DAEMON"
if check_started; then
log_action_end_msg 0 "running"
else
if [ -e "$THEPIDFILE" ]; then
log_action_end_msg 1 "$DAEMON failed"
exit 1
else
log_action_end_msg 1 "not running"
exit 3
fi
fi
}
case "$1" in
start)
log_daemon_msg "Starting $DESC" "$NAME"
start
log_end_msg $?
;;
stop)
log_daemon_msg "Stopping $DESC" "$NAME"
stop
log_end_msg $?
;;
restart)
log_daemon_msg "Restarting $DESC" "$NAME"
stop
if [ -z "$?" -o "$?" = "0" ]; then
start
fi
log_end_msg $?
;;
status)
status
;;
*)
log_failure_msg "Usage: $0 {start|stop|restart|status}" >&2
exit 1
;;
esac
exit 0
Le script perl
Le script varie de la version suckless, pour forcer le bot à rejoindre le chan.
#!/usr/bin/perl -w
my $users = "nagios.april";
my $serverpipe = "/var/lib/nagios/irc/irc.freenode.net/in";
my $pipe = "/var/lib/nagios/irc/irc.freenode.net/#april-admin/in";
my %color = (
red => "\0034",
purple => "\0036",
yellow => "\0038",
clear => "\00315",
blue => "\0032\002",
green => "\0033",
normal => "\0031",
);
open(PIPE, '>', $serverpipe) or die "Can't write to $serverpipe: $!";
print PIPE "/j #april-admin\n";
close(PIPE);
open(PIPE, '>', $pipe) or die "Can't write to $pipe: $!";
while (<>) {
s/Host [a-z0-9_.]+ is down/$color{red}$&$color{normal}/i;
s/PROBLEM.*?CRITICAL/$color{red}$&$color{normal}/i;
s/PROBLEM.*?WARNING/$color{yellow}$&$color{normal}/i;
s/Host [a-z0-9_.]+ is up/$color{green}$&$color{normal}/i;
s/RECOVERY.*?OK/$color{green}$&$color{normal}/i;
print PIPE "$users: $_";
}
close(PIPE);