Created
February 18, 2016 20:24
-
-
Save notz/e15327df7d7a52bea0d3 to your computer and use it in GitHub Desktop.
Opsview websms.com notification plugin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl -w | |
# | |
# | |
# SYNTAX: | |
my $usage = qq{ | |
submit_sms_websms | |
Copyright (C) 2003-2015 Opsview Limited. All rights reserved | |
Usage: submit_sms_websms -a access_token [-s Opsview] [-t alphanumeric] [-m num] [-n] | |
Options: | |
-a websms's access token | |
-s optional sender address | |
-t optional sender address type (national, international, shortcode, alphanumeric) | |
-m num Maximum number of texts of 160 chars to send. Will truncate to (160*num). Defaults to 1 | |
-n Do not send acknowledgements | |
}; | |
# | |
# DESCRIPTION: | |
# Sends text message via websms.com | |
# | |
# LICENCE: | |
# Copyright (C) 2003-2015 Opsview Limited. All rights reserved | |
# | |
# This file is part of Opsview | |
# | |
# | |
sub usage { | |
if ( $_ = shift ) { print "Error: $_\n" } | |
print $usage; | |
exit 1; | |
} | |
use strict; | |
use lib "/usr/local/nagios/lib", "/usr/local/nagios/perl/lib"; | |
use LWP::UserAgent; | |
use URI; | |
use Opsview::Getopt::Std; | |
use Opsview::Config::Notifications; | |
sub ldie { $_ = shift; print $_. $/; exit 1 } | |
my $opts = {}; | |
getopts( "ha:m:s:t:n", $opts ); | |
if ( $opts->{h} ) { | |
usage(); | |
exit(0); | |
} | |
if ( $opts->{n} && $ENV{NAGIOS_NOTIFICATIONTYPE} eq 'ACKNOWLEDGEMENT' ) { | |
print 'Not sending acknowledgement', $/; | |
exit 0; | |
} | |
my $config = Opsview::Config::Notifications->notification_variables( | |
"com.opsview.notificationmethods.websms" | |
); | |
my $access_token = | |
$opts->{a} | |
|| $config->{WEBSMS_ACCESS_TOKEN} | |
|| usage "Must specify websms auth token"; | |
my $sender = exists $opts->{s} ? $opts->{s} : $config->{WEBSMS_SENDER} | |
|| ''; | |
my $sender_type = exists $opts->{t} ? $opts->{t} : $config->{WEBSMS_SENDER_TYPE} | |
|| ''; | |
my ( $numbers, $text ); | |
if ( $ENV{OPSVIEW_NAME} ) { | |
$numbers = $ENV{OPSVIEW_CONTACTPAGER}; | |
$text = $ENV{OPSVIEW_OUTPUT}; | |
} | |
elsif ( $ENV{NAGIOS_HOSTNAME} ) { | |
$numbers = $ENV{NAGIOS_CONTACTPAGER}; | |
if ( $ENV{NAGIOS_SERVICEDESC} ) { | |
$text = | |
"$ENV{NAGIOS_NOTIFICATIONTYPE}: $ENV{NAGIOS_SERVICEDESC} on $ENV{NAGIOS_HOSTNAME} is $ENV{NAGIOS_SERVICESTATE}: $ENV{NAGIOS_SERVICEOUTPUT} ($ENV{NAGIOS_SHORTDATETIME})"; | |
} | |
else { | |
$text = | |
"$ENV{NAGIOS_NOTIFICATIONTYPE}: $ENV{NAGIOS_HOSTNAME} is $ENV{NAGIOS_HOSTSTATE}: $ENV{NAGIOS_HOSTOUTPUT} ($ENV{NAGIOS_SHORTDATETIME})"; | |
} | |
} | |
else { | |
die "No Opsview or Nagios environment variables set"; | |
} | |
unless ($numbers) { | |
ldie "Must have CONTACTPAGER for this notification script"; | |
} | |
my @numbers = split( ",", $numbers ); | |
# Validate in right format, eg, starts with a + | |
foreach my $n (@numbers) { | |
ldie "Number must start with a +" unless $n =~ /^\+/; | |
} | |
# Cleanup numbers: remove spaces | |
@numbers = map { s/\+//; s/ //g; $_ } @numbers; | |
# Cleanup text | |
$text =~ s/\n/ /g; | |
my $maxchars = ( $opts->{m} || 1 ) * 160; | |
$text = substr( $text, 0, $maxchars ); | |
$numbers = join(",", @numbers); | |
my $ua = LWP::UserAgent->new; | |
$ua->timeout(10); | |
$ua->env_proxy; | |
my $uri = URI->new('https://@api.websms.com/rest/smsmessaging/simple'); | |
$uri->query_form( | |
'access_token' => $access_token, | |
'recipientAddressList' => $numbers, | |
'messageContent' => $text, | |
'senderAddress' => $sender, | |
'senderAddressType' => $sender_type, | |
); | |
my $response = $ua->get($uri); | |
if (!$response->is_success) { | |
print "Error: ".$response->status_line."\n"; | |
} | |
print "Submitted to websms: ".$response->decoded_content, $/; | |
exit; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment