diff options
author | varac <varacanero@zeromail.org> | 2012-11-22 23:03:20 +0100 |
---|---|---|
committer | varac <varacanero@zeromail.org> | 2012-11-22 23:03:20 +0100 |
commit | 245a754d5b8c1e986722bc1e1534ba4acd64e0c5 (patch) | |
tree | e31d487f68acb3fefd469997b007bc5587d4dd34 | |
parent | 58a108f9d0204f76cb880f32a1d093d0fc2ad21f (diff) | |
parent | 4d6b051acd53217860e1401946e45550e20c91a0 (diff) |
Merge branch 'master' of git://labs.riseup.net/shared-nagios
Conflicts:
templates/irc_bot/nsa.cfg.erb
-rw-r--r-- | files/irc_bot/riseup-nagios-server.pl | 80 |
1 files changed, 79 insertions, 1 deletions
diff --git a/files/irc_bot/riseup-nagios-server.pl b/files/irc_bot/riseup-nagios-server.pl index 7180758..5d65e6d 100644 --- a/files/irc_bot/riseup-nagios-server.pl +++ b/files/irc_bot/riseup-nagios-server.pl @@ -4,6 +4,16 @@ # a simple IRC bot which dispatches messages received via local domain sockets # ############################################################################## + +###### +## THIS NEEDS TO BE PORTED TO THE NEW FRAMEWORKS! +## +## STICKY POINTS: the addfh() function doesn't exist in BasicBot or POE::Component::IRC +## +## people suggested we use Anyevent::IRC and POE::Kernel ->select_read and POE::Wheel namespace +## +## in the meantime, inspiration for extensions can be found here: http://svn.foswiki.org/trunk/WikiBot/mozbot.pl + use strict; use File::Basename; @@ -56,7 +66,8 @@ sub new { my $self = { socket => undef, irc => undef, - conn => undef + conn => undef, + commandfile => undef, }; return bless($self, __PACKAGE__); @@ -115,6 +126,70 @@ sub irc_on_connect { $self->join($CFG::Nsa{'channel'}); } +sub irc_on_msg { + my ($self, $event) = @_; + my $data = join(' ', $event->args); + #my $nick = quotemeta($nicks[$nick]); + my $to = $event->to; + #my $channel = &toToChannel($self, @$to); + #my $cmdChar = $commandChar{default}; # FIXME: should look up for CURRENT channel first! + #if ( exists $commandChar{$channel} ) { $cmdChar = $commandChar{$channel}; } + my $msg = parse_msg($event); + if (defined($msg)) { + #$self->privmsg($event->nick, "alright, sending this message to nagios, hope it figures it out: $msg"); + } else { + $self->privmsg($event->nick, "can't parse $data, you want 'ack host service comment'\n"); + } +} + +sub irc_on_public { + my ($self, $event) = @_; + my $data = join(' ', $event->args); + my $to = $event->to; + if ($data =~ s/([^:]*):\s+//) { + if ($1 eq $CFG::Nsa{'nickname'}) { + my $msg = parse_msg($event); + if (defined($msg)) { + #$self->privmsg($event->to, "alright, sending this message to nagios, hope it figures it out: $msg"); + } else { + $self->privmsg($event->to, "can't parse $data, you want 'ack host service comment'\n"); + } + } else { + #print STDERR "ignoring message $data, not for me (me being $1)\n"; + } + } else { + #print STDERR "ignoring message $data\n"; + } +} + +sub parse_msg { + my $event= shift; + my $data = join(' ', $event->args); + my $msg; + if ($data =~ m/([^:]*:)?\s*ack(?:knowledge)?\s+([a-zA-Z0-9\-\.]+)(?:\s+([-\w\.]+)(?:\s+([\w\s]+))?)?/) { + #print STDERR "writing to nagios scoket ". $CFG::Nsa{'commandfile'} . "\n"; + open(my $cmdfile, ">", $CFG::Nsa{'commandfile'}) || die "Can't open Nagios commandfile: $CFG::Nsa{'commandfile'}!\n"; + my $host = $2; + my ($service, $comment) = (undef, "no comment (from irc)"); + if ($4) { + $service = $3; + $comment = $4; + } elsif ($3) { + $service = $3; + } + my $user = $event->nick; + $msg = '[' . time() . '] '; + if (defined($service)) { + $msg .= "ACKNOWLEDGE_SVC_PROBLEM;$host;$service;1;1;1;$user;$comment\n"; + } else { + $msg .= "ACKNOWLEDGE_HOST_PROBLEM;$host;1;1;1;$user;$comment\n"; + } + print {$cmdfile} $msg; + close($cmdfile); + } + return $msg; +} + # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - my $bot = &new; @@ -145,6 +220,9 @@ $bot->{conn} = $bot->{irc}->newconn ( $bot->{conn}->add_global_handler(376, \&irc_on_connect); $bot->{conn}->add_global_handler('nomotd', \&irc_on_connect); +$bot->{conn}->add_global_handler('msg', \&irc_on_msg); +$bot->{conn}->add_global_handler('public', \&irc_on_public); +#$bot->{conn}->add_global_handler('notice', \&irc_on_msg); $bot->{irc}->addfh($bot->{socket}, \&socket_has_data, 'r', $bot); while ($running) { |