PageRenderTime 1376ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/notify_eagle_sms.pl

https://bitbucket.org/proximus/smseagle-nagios
Perl | 69 lines | 29 code | 9 blank | 31 comment | 9 complexity | 984d35500c11578c505c430e6cee171e MD5 | raw file
  1. #!/usr/bin/perl
  2. #
  3. # ============================== SUMMARY =====================================
  4. #
  5. # Summary : This plugin sends SMS alerts with SMSEagle hardware sms gateway
  6. # Program : notify_eagle_sms.pl
  7. # Version : 2.0
  8. # Date : 30.12.2020
  9. # Author : RJ / SMSEAGLE.EU
  10. # Forked from: Nagios-SMS-WT (https://github.com/m-r-h/Nagios-SMS-WT)
  11. # License : BSD
  12. # Copyright (c) 2020, SMSEagle www.smseagle.eu
  13. #
  14. # ============================= MORE INFO ======================================
  15. #
  16. # Visit: https://www.smseagle.eu
  17. #
  18. # README file and the latest version of this plugin can be found on:
  19. # https://bitbucket.org/proximus/smseagle-nagios
  20. #
  21. # ============================= SCRIPT ==========================================
  22. # Script params description:
  23. #
  24. # smseagleurl = URL of your SMSEagle device (eg.: http://192.168.1.150)
  25. # apitoken = SMSEagle API token
  26. # dstaddr = Destination mobile number (the number to send SMS to)
  27. # txt = the text message body
  28. use strict;
  29. use LWP::Simple;
  30. use LWP::UserAgent;
  31. use URI::Escape;
  32. use Getopt::Long;
  33. use HTTP::Request::Common;
  34. my %args;
  35. GetOptions(
  36. 'help' => \$args{help},
  37. 'smseagleurl=s' => \$args{smseagleurl},
  38. 'apitoken=s' => \$args{apitoken},
  39. 'dstaddr=s' => \$args{dstaddr},
  40. 'txt=s' => \$args{txt}
  41. );
  42. if(defined($args{help}) || !defined($args{smseagleurl}) || !defined($args{apitoken}) || !defined($args{dstaddr}) || !defined($args{txt}) ) {
  43. print "Script usage: notify_eagle_sms.pl --smseagleurl <URL of your SMSEagle> --apitoken <API token for your SMSEagle> --dstaddr <phone number> --txt <message>
  44. Example: notify_eagle_sms.pl --smseagleurl http://192.168.50.150 --apitoken jCOlMH8F2q --dstaddr 123456789 --txt \"My Message\"\n";
  45. exit(0);
  46. }
  47. ## URL Encode the message text
  48. my $text = uri_escape($args{txt});
  49. ## Build the URL
  50. my $baseurl = $args{smseagleurl}.'/http_api/';
  51. my $req = GET $baseurl."send_sms?access_token=$args{apitoken}&to=$args{dstaddr}&message=$text";
  52. ## Create the user agent and send the request
  53. my $ua = LWP::UserAgent->new();
  54. my $rsp = $ua->request($req);
  55. ## Process the response
  56. if (index($rsp->content, "OK;") != -1) {
  57. print "Message sent succesfully to $args{dstaddr}\n";
  58. } else {
  59. print "Message sending error: " . $rsp->content . "\n";
  60. }