nagios-fifo.pl 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  1. #!/usr/bin/perl
  2. # What this script does :
  3. # -----------------------
  4. # 1/ check nagios.log
  5. # 2/ setup an array of alerts
  6. # 3/ ability to remove-change alerts on new notification for same host/[service]
  7. # 4/ dialog with livestatus to replay full alerts on load/setup : irssi command /nagrefresh
  8. # 5/ define !recheck to nagios
  9. # 6/ ability to define chan/server on irssi configuration instead of bot config.
  10. #
  11. # What it should do :
  12. # -------------------
  13. # TODO
  14. # define !ack irc command
  15. # use %ACK_INDEX by alert type HOST / SERVICE
  16. # use %ACK_INDEX_LEVEL by alert type CRIT/UP/DOWN/WARN/UNK…
  17. use strict;
  18. use vars qw($VERSION %IRSSI);
  19. use Irssi;
  20. use POSIX "sys_wait_h";
  21. use POSIX qw(strftime);
  22. use Term::ANSIColor qw/ :constants /;
  23. use Fcntl; # provides `O_NONBLOCK' and `O_RDONLY' constants
  24. ##########################################################################
  25. # Part A : script configuration / Prototypes
  26. # Put signals to irssi ###################################################
  27. our ( $FIFO, # fifo absolute filename (expanded from Irssi config)
  28. $FIFO_HANDLE, # fifo filehandle for `open' et al.
  29. $FIFO_TAG ); # fifo signal tag for `input_add'
  30. $VERSION = "0.3.0";
  31. %IRSSI = (
  32. authors => 'asr',
  33. contact => 'root@lautre.net',
  34. name => 'nagios-ack',
  35. description => 'ack nagios alerts in irc / follow nagios log',
  36. license => 'GPLv2',
  37. url => 'http://www.lautre.net/',
  38. changed => '20141006',
  39. modules => ''
  40. );
  41. # Put signals to irssi ###################################################
  42. Irssi::settings_add_str($IRSSI{name}, # default fifo_remote_file
  43. 'fifo_remote_file', '/home/tc-14/var/nagios-fifo'); #
  44. Irssi::settings_add_str("nagios_ack", "nagios_ack_channel", "#main_channel"); # see also etc/config.template
  45. Irssi::settings_add_str("nagios_ack", "nagios_ack_nick", ""); # any user (none by default)
  46. Irssi::settings_add_str("nagios_ack", "nagios_command", "/var/lib/nagios3/rw/live");
  47. #Irssi::command_bind( 'ack', \&nagios_ack );
  48. #Irssi::command_bind( 'nagstat', \&nagios_status );
  49. Irssi::command_bind( 'nagrefresh', \&nagios_query_status );
  50. # CONFIG: point this where your Nagios configuration files live
  51. #my $nagioslog = "/var/log/nagios3/nagios.log";
  52. # To be used to check immediately / hosts-stats
  53. my $nagioscmd = Irssi::settings_get_str("nagios_command");
  54. my $nagios_ack_channel= Irssi::settings_get_str("nagios_ack_channel");
  55. my $nagios_ack_nick= Irssi::settings_get_str("nagios_ack_nick");
  56. my %renot; # { "host" or "host:service" => time_last_notification }
  57. my @cmdqueue = ();
  58. my %ignore = ();
  59. my @ACKS;
  60. my %ACK_Ind;
  61. my %C = (
  62. K => "\x0301", # 00. White
  63. B => "\x0302", # 01. Black
  64. G => "\x0303", # 02. Blue (Navy)
  65. R => "\x0304", # 03. Green
  66. # 04. Red
  67. V => "\x0306", # 05. Brown (Maroon)
  68. O => "\x0307", # 06. Purple
  69. Y => "\x0308", # 07. Orange
  70. Gg => "\x0309", # 08. Yellow
  71. # 09. Light Green (Lime)
  72. C => "\x0311", # 10. Teal (Green/Blue Cyan)
  73. Bb => "\x0312", # 11. Light Cyan (Cyan) (Aqua)
  74. Ma => "\x0313", # 12. Light Blue (Royal)
  75. Gr => "\x0314", # 13. Pink (Light Purple) (Fuchsia)
  76. W => "\x0315", # 14. Grey
  77. # 15. Light Grey (Silver)
  78. Z => "\x03",
  79. );
  80. # Numeric to string conversion of nagios status
  81. # 0..4 : hosts
  82. # 10..14 : services
  83. my @NagStates = qw/UP DOWN WARNING UNKNOWN .. .. .. .. .. ..
  84. OK WARNING CRITICAL UNKNOWN/;
  85. # Bold: U+0002 ("0x02") — Example: ^Bold Text^ whereas ^ represents the control character.
  86. # Italics: U+001D ("0x1D") — Example: ^Italicized Text^ whereas ^ represents the control character.
  87. # Underline: U+001F ("0x1F") — Example: ^Underlined Text^ whereas ^ represents the control character.
  88. # The control character used for color is U+0003 ("0x03").
  89. my $state_to_color = {
  90. OK => $C{G},
  91. UP => $C{G},
  92. WARNING => $C{Y},
  93. CRITICAL => $C{R},
  94. DOWN => $C{V},
  95. UNKNOWN => $C{Gr},
  96. };
  97. # Global use to limit memory use. Probably a bad idea : better to check
  98. # where is the memory leak.
  99. my ($status_line);
  100. my @match;
  101. my ($msg,$num);
  102. my ($message,$i);
  103. my ($type,$data);
  104. my ($d,$type,$host,$service,$state,$output);
  105. my ( $host, $svc, $state, $msg, $id );
  106. my ($server, $msg, $nick, $addr, $target);
  107. my ( $param, $server, $window );
  108. my (@issue, $issue);
  109. #my $state_to_color = { OK => '', UP => '', WARNING => '', CRITICAL => '', DOWN => '', UNKNOWN => '' };
  110. # Alert Acknowledment ####################################################
  111. my $last_alert;
  112. my $DEBUG=Irssi::settings_get_str("nagios_ack_channel");
  113. # Simple subs ############################################################
  114. sub TRUE() { 1 } # some constants [perlsyn(1)
  115. sub FALSE() { "" } # "Constant Functions"]
  116. sub DEBUG(@) { print "%B", join(":", @_),"%n" }# DEBUG thingy
  117. sub time2date($) {
  118. my ($d)=@_;
  119. return strftime("%d/%m/%y %H:%M", localtime($d));
  120. }
  121. ##########################################################################
  122. # Part B : user Actions ##################################################
  123. # Acknowledge alerts #####################################################
  124. # Global use to limit memory use. Probably a bad idea : better to check
  125. # where is the memory leak.
  126. # Four unused subs. Still there to be implemented later
  127. sub parse_status();
  128. sub nagios_ack($$$);
  129. sub nagios_check($$$);
  130. sub nagios_inject;
  131. #sub nagios_status();
  132. # Return alert ID "[FF01]"
  133. # F : caracter to display (! : ; C : change ; + : new alert ; - : green flag ; ? : green but not found)
  134. # I : numeric ID
  135. # state : state string, from @NagStates or %state_to_color
  136. sub format_alert_id($$$); # ($Flag,$I,$state)
  137. # Add alert to array / index
  138. sub insert_alert($$$$$); #( $host, $svc, $state, $msg, $d )
  139. ##########################################################################
  140. # Part C : Logfile management ############################################
  141. # disable fifo and erase fifo file
  142. sub destroy_fifo($); # [2004-08-14]
  143. # Open logfile/fifo
  144. sub open_fifo($); # [2004-08-14]
  145. # read from fifo
  146. # (called by fifo input signal)
  147. sub read_fifo(); # [2004-08-14]
  148. # Read logfile ###########################################################
  149. # create named fifo and open it for input
  150. # (called on script load and fifo name changes)
  151. sub create_fifo($); # [2004-08-14]
  152. # Query nagios ###########################################################
  153. sub nagios_query_status($);
  154. ##########################################################################
  155. # Part D : Alert processing ##############################################
  156. # Search for services/host informations, and post them to IRC ############
  157. # Delimiter : ; for logs; @ for direct nagios custom notification command
  158. # one parameter : the logline to parse.
  159. sub parse_nagios_log($);
  160. # Use alert fields to print and insert it
  161. # timestamp, alert-type, hostname, state, plugin output, service_name
  162. sub handle_alert($$$$$$$); # $d,$type,$host,$state,$output,$service
  163. ##########################################################################
  164. # IRSSI Events ###########################################################
  165. # create new fifo (erase any old) and get command prefix
  166. # (called on script loading and on user /set)
  167. sub setup(); # [2004-08-13]
  168. # Interact with IRC chan users
  169. sub event_privmsg ($$$$);
  170. # REFERENCES
  171. # ----------
  172. #
  173. # This script is mainly adapted from 3 other scripts related to nagios :
  174. #
  175. # https://github.com/zorkian/nagios-irc-bot/blob/master/nagiosirc.pl
  176. # http://www.update.uu.se/~zrajm/programs/irssi-scripts/fifo_remote.pl-0.5
  177. # https://github.com/mikegrb/irssi-scripts/blob/master/nagios-ack.pl
  178. ##########################################################################
  179. # CODE ###################################################################
  180. ##########################################################################
  181. ##########################################################################
  182. # Part B : user Actions ##################################################
  183. sub parse_status() {
  184. return $1 if $last_alert =~ /^PROBLEM - (\S+) is DOWN/;
  185. return ($1, $2) if $last_alert =~ /^PROBLEM - (\S+) on (\S+) is/;
  186. return;
  187. }
  188. sub nagios_ack($$$) {
  189. ( $param, $server, $window ) = @_;
  190. @issue = parse_status();
  191. if (!@issue) {
  192. #$window->print("Failed to parse last status: $last_alert");
  193. }
  194. my $message = " ACK ".$param . join ' ', reverse @issue;
  195. Irssi::active_server->command('MSG ' . $DEBUG . $message);
  196. }
  197. sub nagios_check($$$) {
  198. #1412375470;filer2;DISK_all;2;W=10% C=5%
  199. ( $param, $server, $window ) = @_;
  200. @issue = parse_status();
  201. if (!@issue) {
  202. #$window->print("Failed to parse last status: $last_alert");
  203. }
  204. my $message = " CHECK ".$param . join ' ', reverse @issue;
  205. Irssi::active_server->command('MSG ' . $DEBUG . $message);
  206. # handle_alert($$$$$$) { # $d,$type,$host,$state,$output,$service }
  207. }
  208. sub nagios_inject {
  209. $last_alert = shift;
  210. }
  211. #sub nagios_status {
  212. # (undef, undef, $window) = @_;
  213. # $issue = join ',', map { "'" . $_ . "'" } reverse parse_status();
  214. # $window->print("Last issue: '$last_alert' ($issue)");
  215. #
  216. #}
  217. # Return alert ID
  218. sub format_alert_id($$$) {
  219. my ($Flag,$I,$state)=@_;
  220. my $M=sprintf('%02d', scalar @ACKS % 100);
  221. my $N=sprintf('%02d', $I % 100);
  222. if (($I == 0) && (scalar @ACKS == 0)) {
  223. return "[".$Flag.$Flag." ]/$M ";
  224. } else {
  225. return "[".$Flag.$Flag.$state_to_color->{$state}.$N.$C{Z}."]/$M ";
  226. }
  227. }
  228. # Add alert to array / index
  229. sub insert_alert($$$$$) {
  230. #Irssi::print(">> On vire $host/$svc");
  231. my $i;
  232. ( $host, $svc, $state, $msg, $d ) = @_;
  233. # ALERT
  234. if ( $state eq 'WARNING' || $state eq 'CRITICAL' || $state eq 'UNKNOWN' || $state eq 'DOWN' ) {
  235. while ($i <= $#ACKS) {
  236. # Update, or insert ?
  237. if (($ACKS[$i]->[0] eq $host) && ($ACKS[$i]->[1] eq $svc)) {
  238. $ACKS[$i]->[4]=$d;
  239. Irssi::print(">> found $i");
  240. if ($ACKS[$i]->[2] ne $state) {
  241. # Same alert, but different level
  242. $ACKS[$i]->[2]=$state;
  243. return format_alert_id('C', $i, $state);
  244. } else {
  245. # Same alert
  246. return format_alert_id('!', $i, $state);
  247. # No need to display...
  248. }
  249. }
  250. $i++;
  251. }
  252. # New alert, insert.
  253. push (@ACKS, [ $host, $svc, $state, $msg, $d ] );
  254. return format_alert_id('+',scalar @ACKS,$state);
  255. # Clear alert
  256. } else {
  257. # Irssi::print(">> On vire $host/$svc");
  258. $i=0;
  259. # Find alert (need to be used by service/host index)
  260. while ($i <= $#ACKS) {
  261. if (($ACKS[$i]->[0] eq $host) && ($ACKS[$i]->[1] eq $svc)) {
  262. $ACKS[$i] = pop @ACKS;
  263. $i=$#ACKS;
  264. Irssi::print(">> found $i");
  265. return format_alert_id('-',$i+1,$state);
  266. }
  267. $i++;
  268. }
  269. return format_alert_id('?',0,$state);
  270. }
  271. }
  272. ##########################################################################
  273. # Part C : Logfile management ############################################
  274. # disable fifo and erase fifo file
  275. sub destroy_fifo($) { # [2004-08-14]
  276. my ($fifo) = @_; # get args
  277. if (defined $FIFO_TAG) { # if fifo signal is active
  278. Irssi::input_remove($FIFO_TAG); # disable fifo signal
  279. undef $FIFO_TAG; # and forget its tag
  280. } #
  281. if (defined $FIFO_HANDLE) { # if fifo is open
  282. close $FIFO_HANDLE; # close it
  283. undef $FIFO_HANDLE; # and forget handle
  284. } #
  285. if (-p $fifo) { # if named fifo exists
  286. unlink $fifo; # erase fifo file
  287. undef $FIFO; # and forget filename
  288. } #
  289. return 1; # return
  290. } #
  291. # Open logfile/fifo
  292. sub open_fifo($) { # [2004-08-14]
  293. my ($fifo) = @_; # get args
  294. if (not sysopen $FIFO_HANDLE, $fifo, # open fifo for non-blocking
  295. O_NONBLOCK | O_RDONLY) { # reading
  296. print CLIENTERROR "could not open nagios logfile for reading";
  297. return ""; #
  298. } #
  299. Irssi::input_remove($FIFO_TAG) # disable fifo reading signal
  300. if defined $FIFO_TAG; # if there is one
  301. $FIFO_TAG = Irssi::input_add # set up signal called when
  302. fileno($FIFO_HANDLE), INPUT_READ, # there's input in the pipe
  303. \&read_fifo, ''; #
  304. return 1; #
  305. }
  306. # read from fifo
  307. # (called by fifo input signal)
  308. sub read_fifo() { # [2004-08-14]
  309. # Read logfile ###########################################################
  310. foreach (<$FIFO_HANDLE>) { # for each input line
  311. chomp; # strip trailing newline
  312. parse_nagios_log($_); #if (/ALERT/ && /HARD;/);
  313. #Irssi::active_win->print( # show incoming commands (debug)
  314. # "\u$IRSSI{name} received command: \"$_\"", #
  315. # MSGLEVEL_CLIENTNOTICE); #
  316. # Irssi::active_win->command($_); # run incoming commands
  317. } #
  318. open_fifo($FIFO); # re-open fifo
  319. # TODO: Is the above re-opening of fifo really necessary? -- If not
  320. # invoked here `read_fifo' is called repeatedly, even though no input
  321. # is to be found on the fifo. (This seems a waste of resources to me.)
  322. }
  323. # create named fifo and open it for input
  324. # (called on script load and fifo name changes)
  325. sub create_fifo($) { # [2004-08-14]
  326. my ($new_fifo) = @_; # get args
  327. if (not -p $new_fifo) { # create fifo if non-existant
  328. if (system "mkfifo '$new_fifo' &>/dev/null" and
  329. system "chmod 777 '$new_fifo' &>/dev/null" and
  330. system "mknod '$new_fifo' &>/dev/null"){
  331. print CLIENTERROR "`mkfifo' failed -- could not create named pipe";
  332. # TODO: capture `mkfifo's stderr and show that here
  333. return ""; #
  334. } #
  335. } #
  336. $FIFO = $new_fifo; # remember fifo name
  337. open_fifo($new_fifo); # open fifo for reading
  338. } #
  339. # Query nagios ###########################################################
  340. sub nagios_query_status($){
  341. # 1412478149;hyppocampe;apt;2;CRITICAL : unknown: qemu-utils, qemu-kvm, qemu-keymaps
  342. my ($option)=@_;
  343. my @unixcat;
  344. my $unixline;
  345. $nagioscmd = Irssi::settings_get_str("nagios_command");
  346. foreach $unixline ( `echo "GET services
  347. Columns: last_state_change host_name display_name state plugin_output
  348. Filter: state > 0
  349. Filter: acknowledged = 0
  350. And: 2" | unixcat $nagioscmd` ) {
  351. ($d,$host,$service,$state,$output)=split /;/,$unixline;
  352. handle_alert($option,$d,'SERVICE',$host,$NagStates[$state+10],$output,$service);
  353. chomp $unixline;
  354. Irssi::print("%B>>%n $unixline", MSGLEVEL_CLIENTCRAP);
  355. }
  356. return;
  357. foreach $unixline ( `echo "GET hosts
  358. Columns: last_state_change host_name state plugin_output
  359. Filter: state > 0
  360. Filter: acknowledged = 0
  361. And: 2" | unixcat $nagioscmd` ) {
  362. ($d,$host,$state,$output)=split /;/,$unixline;
  363. handle_alert($option,$d,'HOST',$host,$NagStates[$state],$output,"");
  364. chomp $unixline;
  365. Irssi::print("%B>>%n $unixline", MSGLEVEL_CLIENTCRAP);
  366. }
  367. }
  368. ##########################################################################
  369. # Part D : Alert processing ##############################################
  370. # Search for services/host informations, and post them to IRC ############
  371. # Delimiter : ; for logs; @ for direct nagios custom notification command
  372. sub parse_nagios_log($){
  373. my $option="";
  374. $d=0; $status_line=""; $host="";$output="";$service="";
  375. $status_line=shift;
  376. ### log :
  377. # [1412330770] SERVICE ALERT: ella;IMAPs_LOGIN;OK;SOFT;2;OK - CO1N OK LOGIN Ok.
  378. # /\[\d+\] (\w+) ALERT: (\w+);(\w+);(\w+);HARD;(\d+);(.+)/
  379. # [1410969598] HOST ALERT: filou;DOWN;SOFT;1;PING CRITICAL - Paquets perdus = 100%
  380. # [TIMESTAMP] PROCESS type ALERT: host;service;STATE1;HARD;num;commentaire
  381. if (@match=$status_line =~ /\[?(\d+)\]? HOST ALERT: ([^@;]+)[@;]([^;@]+)[@;]HARD[@;][^;@]*[@;](.+)/) {
  382. # HOST ########################
  383. ($d,$host,$state,$output)=@match;
  384. handle_alert($option,$d,"HOST",$host,$state,$output,$service);
  385. # SERVICE #####################
  386. } elsif (@match=$status_line =~ /\[?(\d+)\]? (\w+) ALERT: ([^;@]+)[@;]([^;@]+)[@;]([^;@]+)[@;]HARD[@;][^;@]*[@;](.+)/) {
  387. ($d,$type,$host,$service,$state,$output)=@match;
  388. $service=~s/[^\w\d_-]/_/g;
  389. handle_alert($option,$d,$type,$host,$state,$output,$service);
  390. # OTHER #######################
  391. } elsif (@match=$status_line =~ /\[\d+\] (\w+) ALERT: (.*)/) {
  392. ($type,$data)=@match;
  393. Irssi::print( "%B>>%n $IRSSI{name} $type - $data", MSGLEVEL_CLIENTCRAP) unless ($status_line =~ /[;@]SOFT[@;]/);
  394. # FALLBACK ####################
  395. } else {
  396. Irssi::print( #
  397. "%B>>%n $IRSSI{name} received message: \"$_\"",
  398. MSGLEVEL_CLIENTCRAP); #
  399. next
  400. }
  401. }
  402. # Use alert fields to print and insert it
  403. sub handle_alert($$$$$$$) { # $d,$type,$host,$state,$output,$service
  404. my $option;
  405. # Temporisation
  406. ($option,$d,$type,$host,$state,$output,$service)=@_;
  407. next if exists $renot{"$host:$service"} && $renot{"$host:$service"} >= time() - 5;
  408. $renot{"$host:$service"} = time();
  409. # HOST or SERVICE ?
  410. $id = insert_alert($host,$service,$state,$output,$d);
  411. $msg = "$id".$state_to_color->{$state} . "$host:$service".$C{Z}." is $state : $output";
  412. #Irssi::print( "%B>>%n $IRSSI{name} $msg", MSGLEVEL_CLIENTCRAP);
  413. $last_alert="NAGIOS - $host/$service is $state";
  414. $d=time2date($d);
  415. $message = " $d - $last_alert - $msg";
  416. # Silently ignore previously sent alerts
  417. return if $id =~ '\[!' ;
  418. $nagios_ack_channel= Irssi::settings_get_str("nagios_ack_channel");
  419. Irssi::active_server->command('MSG ' . $nagios_ack_channel .
  420. " ".$message) unless ($option eq "silent");
  421. # Irssi::print( "%B>>%n $IRSSI{name} $msg", MSGLEVEL_CLIENTCRAP);
  422. }
  423. ##########################################################################
  424. # IRSSI Events ###########################################################
  425. # create new fifo (erase any old) and get command prefix
  426. # (called on script loading and on user /set)
  427. sub setup() { # [2004-08-13]
  428. my $new_fifo = Irssi::settings_get_str # setting from Irssi
  429. 'fifo_remote_file'; # (and add path to it)
  430. return if $new_fifo eq $FIFO and -p $FIFO; # do nada if already exists
  431. destroy_fifo($FIFO) if -p $FIFO; # destroy old fifo
  432. create_fifo($new_fifo) # create new fifo
  433. and $FIFO = $new_fifo; # and remember that fifo
  434. # To ADD :
  435. # request to livestatus to fetch stored alerts
  436. }
  437. # Interact with IRC chan users
  438. sub event_privmsg($$$$) {
  439. # Commamd channel
  440. my $K;
  441. my ($server, $data, $nick, $mask) =@_;
  442. my ($target, $text, $arg) = $data =~ /^(\S*)\s:(.*)/;
  443. #print ( "C:$target X:$text A:$admin D:$warndate L:$last W:$warn N:$nick D:$data" );
  444. if ( $text =~ /^!nagios ?(.*)/i ) {
  445. $arg=$1;
  446. # If we are not in command channel : NoOP
  447. $nagios_ack_channel=Irssi::settings_get_str("nagios_ack_channel");
  448. Irssi::print(">> $arg - $nagios_ack_channel - $target");
  449. return if $target ne $nagios_ack_channel ;
  450. # !nagios [*] :
  451. # !nagios list [*] :
  452. if ($arg =~ /^refresh ?(.*)/i) {
  453. $arg=$1;
  454. Irssi::print(">> $arg");
  455. if ($arg =~ /^silent/) {
  456. nagios_query_status("silent");
  457. } else {
  458. nagios_query_status("");
  459. $server->command ( "msg ".$nagios_ack_channel.
  460. 'Refresh nagios. Use "silent" keyword to disable display.');
  461. }
  462. $server->command ( "msg ".$nagios_ack_channel.
  463. " ".scalar @ACKS." alertes");
  464. }
  465. elsif ($arg =~ /^list ?(.*)/i) {
  466. $server->command ( "msg ".$nagios_ack_channel.
  467. " ".scalar @ACKS." alertes");
  468. my $i=0;
  469. my $grepto;
  470. my $togrep="$1";
  471. Irssi::print("> ".$togrep);
  472. foreach $K (@ACKS) {
  473. my $grepto="@$K";
  474. Irssi::print("> ".$grepto);
  475. $server->command ( "msg ".$nagios_ack_channel.
  476. " ". format_alert_id(' ',$i,$K->[2]) ." ".$K->[0]." / $K->[1] / $K->[2] / $K->[3] / ".time2date $K->[4]) if ($grepto =~ /$togrep/);
  477. $i++;
  478. }
  479. }
  480. elsif ($arg =~ /^help/i) {
  481. $server->command ( "msg ".$nagios_ack_channel.
  482. " !nagios list [pattern] :liste des alertes nagios reçues ici");
  483. $server->command ( "msg ".$nagios_ack_channel.
  484. " !nagios help : l'aide");
  485. $server->command ( "msg ".$nagios_ack_channel.
  486. " !nagios ack <#ALERTE> <message> : aquitte l'alerte");
  487. $server->command ( "msg ".$nagios_ack_channel.
  488. " !nagios check <#ALERTE> : recheck une alerte donnée (service/host)");
  489. $server->command ( "msg ".$nagios_ack_channel.
  490. " !nagios refresh [silent] : interroge le nagios pour avoir la liste de toutes les alertes");
  491. } elsif ( $arg =~ /^check ?(.*)/i ){
  492. nagios_check($1,$server,undef);
  493. } elsif ( $arg =~ /^ack ?(.*)/i ){
  494. nagios_ack($1,$server,undef);
  495. } else {
  496. $server->command ( "msg ".$nagios_ack_channel.
  497. " ".scalar @ACKS." alertes");
  498. }
  499. }
  500. return 1;
  501. }
  502. ##########################################################################
  503. # Main ###################################################################
  504. ##########################################################################
  505. print "starting...\n";
  506. # clean up fifo on unload
  507. # (called on /script unload)
  508. Irssi::signal_add_first #
  509. 'command script unload', sub { # [2004-08-13]
  510. my ($script) = @_; # get args
  511. return unless $script =~ # only do cleanup when
  512. /(?:^|\s) $IRSSI{name} # unloading *this* script
  513. (?:\.[^. ]*)? (?:\s|$) /x; #
  514. destroy_fifo($FIFO) if -p $FIFO; # destroy old fifo
  515. Irssi::print("%B>>%n $IRSSI{name} $VERSION unloaded", MSGLEVEL_CLIENTCRAP);
  516. }; #
  517. setup(); # initialize setup values
  518. Irssi::signal_add('event privmsg', 'event_privmsg');
  519. Irssi::signal_add("message public", "event_privmsg");
  520. Irssi::signal_add('setup changed', \&setup); # re-read setup when it changes
  521. print CLIENTCRAP "%B>>%n $IRSSI{name} $VERSION (by $IRSSI{authors}) loaded";
  522. 1;
  523. ##########################################################################
  524. # References #############################################################
  525. ##########################################################################
  526. # REFERENCES
  527. # ----------
  528. #
  529. # This script is mainly adapted from 3 other scripts related to nagios :
  530. #
  531. # https://github.com/zorkian/nagios-irc-bot/blob/master/nagiosirc.pl
  532. # http://www.update.uu.se/~zrajm/programs/irssi-scripts/fifo_remote.pl-0.5
  533. # https://github.com/mikegrb/irssi-scripts/blob/master/nagios-ack.pl