Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
556
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.85 KB | None | 0 0
  1. #!/usr/bin/perl -w
  2. #
  3. # script for nagios notify via Jabber / Google Talk Instant Messaging
  4. #   using XMPP protocol and SASL PLAIN authentication.
  5. #
  6. # author: Andrew Elwell <A.Elwell@physics.gla.ac.uk>
  7. # based on work by Thus0 <Thus0@free.fr> and  David Cox
  8. #
  9. # released under the terms of the GNU General Public License v2
  10. # Copyright 2007 Andrew Elwell.
  11.  
  12. use strict;
  13. use Net::XMPP;
  14.  
  15. ## Configuration
  16. my $username = "";
  17. my $password = "";
  18. my $resource = "nagios";
  19. ## End of configuration
  20.  
  21.  
  22. my $len = scalar @ARGV;
  23. if ($len ne 2) {
  24.    die "Usage...\n $0 [jabberid] [message]\n";
  25. }
  26. my @field=split(/,/,$ARGV[0]);
  27. #------------------------------------
  28.  
  29. # Google Talk & Jabber parameters :
  30.  
  31. my $hostname = 'talk.google.com';
  32. my $port = 443;
  33. my $componentname = 'gmail.com';
  34. my $connectiontype = 'tcpip';
  35. my $ssl = 1;
  36.  
  37. #------------------------------------
  38.  
  39. my $Connection = new Net::XMPP::Client();
  40.  
  41. # Connect to talk.google.com
  42. my $status = $Connection->Connect(
  43.        hostname => $hostname, port => $port,
  44.        componentname => $componentname,
  45.        connectiontype => $connectiontype, ssl => $ssl);
  46.  
  47. if (!(defined($status))) {
  48.    print "ERROR:  XMPP connection failed.\n";
  49.    print "        ($!)\n";
  50.    exit(0);
  51. }
  52.  
  53. # Change hostname
  54. my $sid = $Connection->{SESSION}->{id};
  55. $Connection->{STREAM}->{SIDS}->{$sid}->{hostname} = $componentname;
  56.  
  57. # Authenticate
  58. my @result = $Connection->AuthSend(
  59.        username => $username, password => $password,
  60.        resource => $resource);
  61.  
  62. if ($result[0] ne "ok") {
  63.    print "ERROR: Authorization failed: $result[0] - $result[1]\n";
  64.    exit(0);
  65. }
  66.  
  67. # Send messages
  68. foreach ( @field ) {
  69. $Connection->MessageSend(
  70.         to       => "$_\@$componentname",
  71.         resource => $resource,
  72.         subject  => "Notification",
  73.         type     => "chat",
  74.         body     => $ARGV[1]);
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement