From ceb6233f66f90cd90e4d26afaca180dfb183e77a Mon Sep 17 00:00:00 2001 From: Antoine Beaupre Date: Mon, 27 Feb 2012 01:28:19 -0500 Subject: add ack functionality --- files/irc_bot/riseup-nagios-server.pl | 70 ++++++++++++++++++++++++++++++++++- 1 file changed, 69 insertions(+), 1 deletion(-) (limited to 'files') diff --git a/files/irc_bot/riseup-nagios-server.pl b/files/irc_bot/riseup-nagios-server.pl index 7180758..89a4c35 100644 --- a/files/irc_bot/riseup-nagios-server.pl +++ b/files/irc_bot/riseup-nagios-server.pl @@ -56,7 +56,8 @@ sub new { my $self = { socket => undef, irc => undef, - conn => undef + conn => undef, + commandfile => undef, }; return bless($self, __PACKAGE__); @@ -115,6 +116,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+))?)?/) { + print STDERR "writing to nagios scoket ". $CFG::Nsa{'nagios_socket'} . "\n"; + open(my $cmdfile, ">", $CFG::Nsa{'nagios_socket'}) || die "Can't open Nagios commandfile: $CFG::Nsa{'nagios_socket'}!\n"; + my $host = $2; + my ($service, $comment) = (undef, ""); + 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 +210,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) { -- cgit v1.2.3 From f56b96b55ce609df4a950f203bf2635906741ed8 Mon Sep 17 00:00:00 2001 From: Antoine Beaupre Date: Mon, 27 Feb 2012 01:29:12 -0500 Subject: note that this bot needs to be ported to the new framework --- files/irc_bot/riseup-nagios-server.pl | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'files') diff --git a/files/irc_bot/riseup-nagios-server.pl b/files/irc_bot/riseup-nagios-server.pl index 89a4c35..9fbc8c5 100644 --- a/files/irc_bot/riseup-nagios-server.pl +++ b/files/irc_bot/riseup-nagios-server.pl @@ -4,6 +4,14 @@ # 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 + use strict; use File::Basename; -- cgit v1.2.3 From cf6462a91be41d325711d5ebdb910ff95202dac7 Mon Sep 17 00:00:00 2001 From: Antoine Beaupre Date: Mon, 27 Feb 2012 01:37:14 -0500 Subject: add commandfile path to configuration Conflicts: templates/irc_bot/nsa.cfg.erb --- files/irc_bot/riseup-nagios-server.pl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'files') diff --git a/files/irc_bot/riseup-nagios-server.pl b/files/irc_bot/riseup-nagios-server.pl index 9fbc8c5..04e3054 100644 --- a/files/irc_bot/riseup-nagios-server.pl +++ b/files/irc_bot/riseup-nagios-server.pl @@ -165,8 +165,8 @@ sub parse_msg { my $data = join(' ', $event->args); my $msg; if ($data =~ m/([^:]*:)?\s*ack(?:knowledge)?\s+([a-zA-Z0-9\-\.]+)(?:\s+(\w+)(?:\s+(\w+))?)?/) { - print STDERR "writing to nagios scoket ". $CFG::Nsa{'nagios_socket'} . "\n"; - open(my $cmdfile, ">", $CFG::Nsa{'nagios_socket'}) || die "Can't open Nagios commandfile: $CFG::Nsa{'nagios_socket'}!\n"; + 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, ""); if ($4) { -- cgit v1.2.3 From 974fc1015a39a618352317933de14c36c3fe3a41 Mon Sep 17 00:00:00 2001 From: Antoine Beaupre Date: Mon, 27 Feb 2012 01:49:24 -0500 Subject: add reference for good material i found --- files/irc_bot/riseup-nagios-server.pl | 2 ++ 1 file changed, 2 insertions(+) (limited to 'files') diff --git a/files/irc_bot/riseup-nagios-server.pl b/files/irc_bot/riseup-nagios-server.pl index 04e3054..4ccec47 100644 --- a/files/irc_bot/riseup-nagios-server.pl +++ b/files/irc_bot/riseup-nagios-server.pl @@ -11,6 +11,8 @@ ## 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; -- cgit v1.2.3 From 7610f67b70f1ae78f6e7bcf73769de4615d61d15 Mon Sep 17 00:00:00 2001 From: Antoine Beaupre Date: Mon, 27 Feb 2012 11:13:11 -0500 Subject: always add a comment, allow spaces in comments --- files/irc_bot/riseup-nagios-server.pl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'files') diff --git a/files/irc_bot/riseup-nagios-server.pl b/files/irc_bot/riseup-nagios-server.pl index 4ccec47..92d0114 100644 --- a/files/irc_bot/riseup-nagios-server.pl +++ b/files/irc_bot/riseup-nagios-server.pl @@ -166,11 +166,11 @@ 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+))?)?/) { + 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, ""); + my ($service, $comment) = (undef, "no comment"); if ($4) { $service = $3; $comment = $4; -- cgit v1.2.3 From 967e9dd3a09697afc7a2fb84f193d5c89bf6c1fe Mon Sep 17 00:00:00 2001 From: Antoine Beaupre Date: Mon, 27 Feb 2012 11:13:37 -0500 Subject: remove debugging message --- files/irc_bot/riseup-nagios-server.pl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'files') diff --git a/files/irc_bot/riseup-nagios-server.pl b/files/irc_bot/riseup-nagios-server.pl index 92d0114..c1a1e0d 100644 --- a/files/irc_bot/riseup-nagios-server.pl +++ b/files/irc_bot/riseup-nagios-server.pl @@ -167,7 +167,7 @@ sub parse_msg { 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"; + #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"); -- cgit v1.2.3 From 2e732d4a7f7288c2c89f79621391ddf43311028d Mon Sep 17 00:00:00 2001 From: root Date: Mon, 27 Feb 2012 17:16:12 -0500 Subject: accept dots in services --- files/irc_bot/riseup-nagios-server.pl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'files') diff --git a/files/irc_bot/riseup-nagios-server.pl b/files/irc_bot/riseup-nagios-server.pl index c1a1e0d..faa0eee 100644 --- a/files/irc_bot/riseup-nagios-server.pl +++ b/files/irc_bot/riseup-nagios-server.pl @@ -166,7 +166,7 @@ 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]+))?)?/) { + 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; -- cgit v1.2.3 From 4211e9f9a994f1b19b76eeb9d7037204f7dfc996 Mon Sep 17 00:00:00 2001 From: root Date: Mon, 27 Feb 2012 17:16:35 -0500 Subject: be silent when we send stuff to nagios, it will answer back --- files/irc_bot/riseup-nagios-server.pl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'files') diff --git a/files/irc_bot/riseup-nagios-server.pl b/files/irc_bot/riseup-nagios-server.pl index faa0eee..2bf5130 100644 --- a/files/irc_bot/riseup-nagios-server.pl +++ b/files/irc_bot/riseup-nagios-server.pl @@ -136,7 +136,7 @@ sub irc_on_msg { #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"); + #$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"); } @@ -150,7 +150,7 @@ sub irc_on_public { 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"); + #$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"); } -- cgit v1.2.3 From 16e168b9a670b117c4093558603bf5dc63ca42ba Mon Sep 17 00:00:00 2001 From: root Date: Mon, 27 Feb 2012 17:17:13 -0500 Subject: make default comment mention irc --- files/irc_bot/riseup-nagios-server.pl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'files') diff --git a/files/irc_bot/riseup-nagios-server.pl b/files/irc_bot/riseup-nagios-server.pl index 2bf5130..a7bad28 100644 --- a/files/irc_bot/riseup-nagios-server.pl +++ b/files/irc_bot/riseup-nagios-server.pl @@ -170,7 +170,7 @@ sub parse_msg { #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"); + my ($service, $comment) = (undef, "no comment (from irc)"); if ($4) { $service = $3; $comment = $4; -- cgit v1.2.3 From e00811faa08243267fbefc5d0133facbe5f0086f Mon Sep 17 00:00:00 2001 From: Antoine Beaupre Date: Mon, 5 Mar 2012 11:56:25 -0500 Subject: allow dashes in service names --- files/irc_bot/riseup-nagios-server.pl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'files') diff --git a/files/irc_bot/riseup-nagios-server.pl b/files/irc_bot/riseup-nagios-server.pl index a7bad28..5d65e6d 100644 --- a/files/irc_bot/riseup-nagios-server.pl +++ b/files/irc_bot/riseup-nagios-server.pl @@ -166,7 +166,7 @@ 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]+))?)?/) { + 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; -- cgit v1.2.3