nagios_sendim.pl 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #!/usr/bin/perl -w
  2. #
  3. # SENDIM 1.0 by Sergio Freire (sergio-s-freire@ptinovacao.pt)
  4. # Nagios plugin to send notifications using instant messages through a jabber server
  5. #
  6. # Note: a) you can send messages to several different IM systems like ICQ,AIM,MSN,etc...
  7. # b) to test this plugin you can execute it with the arguments needed and write the message followed by a CTRL+D
  8. #
  9. # Please check http://www.jabber.org and http://www.jabberstudio.org for more information on Jabber Instant Messaging
  10. use Net::Jabber qw(Client);
  11. use Getopt::Std;
  12. my $tmp;
  13. my $mensagem="";
  14. getopts("u:p:t:");
  15. if ( (!defined($opt_u)) || (!defined($opt_p)) || (!defined($opt_t)))
  16. {
  17. print "USE: sendim -u user_JID -p password -t destination_JID\n";
  18. print 'EXAMPLE: sendim -u nagios@jabber.org -p nagios -t bitcoder@nagios.org'."\n";
  19. print " (send an instant message as user nagios\@jabber.org to bitcoder\@jabber.org)\n";
  20. exit;
  21. }
  22. my @buf=split('@',$opt_u);
  23. my $login=$buf[0];
  24. @buf=split('/',$buf[1]);
  25. my $server=$buf[0];
  26. my $resource=$buf[1] || "nagios";
  27. my $password=$opt_p;
  28. my $jid_dest=$opt_t;
  29. my $debug=0; # Set debug=1 to enable output of debug information
  30. while ($tmp=<STDIN>)
  31. {
  32. $mensagem.=$tmp;
  33. }
  34. print "LOGIN: $login\nSERVER: $server\nRESOURCE: $resource\n" if $debug;
  35. print "TO: $jid_dest\n" if $debug;
  36. $Con1 = new Net::Jabber::Client();
  37. $Con1->Connect(hostname=>$server);
  38. if ($Con1->Connected()) {
  39. print "CON1: We are connected to the server...\n" if $debug;
  40. }
  41. @result1 = $Con1->AuthSend(username=>$login,
  42. password=>$password,
  43. resource=>$resource);
  44. $Con1->PresenceSend();
  45. $Con1->Process(1);
  46. @result1=$Con1->MessageSend( to=>$jid_dest,
  47. subject=>"nagios",
  48. body=>$mensagem,
  49. type=>"chat",
  50. priority=>1);
  51. $Con1->Process(1);
  52. $Con1->Disconnect();
  53. exit;