From 7ea7866aa1f006d45f41940823473cea94a7e26f Mon Sep 17 00:00:00 2001 From: Denis Zheleztsov Date: Fri, 10 Jul 2015 15:08:59 +0300 Subject: [PATCH] sms handler --- handlers/sms.pl | 59 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100755 handlers/sms.pl diff --git a/handlers/sms.pl b/handlers/sms.pl new file mode 100755 index 0000000..cd3f87a --- /dev/null +++ b/handlers/sms.pl @@ -0,0 +1,59 @@ +#!/usr/bin/perl + +use warnings; + +use JSON; +use LWP::UserAgent; +use HTTP::Request; + +use Getopt::Std; + +sub init() { + my $opt_string = 'n:'; + getopts("$opt_string"); + our ( $opt_n ); +} + +init(); + +# Get sensu output +my $json = <>; +my $message = decode_json($json); +my $check_name = $message->{check}->{name}; +my $client_name = $message->{client}->{name}; +my @ch = $message->{check}->{history}; +my @check_history; +foreach my $c (@ch) { + @check_history = @$c; +} + +my $crit_count = 0; +my $total = @check_history; +foreach my $stat (@check_history) { + if ( $stat eq '2' ) { + $crit_count++; + } +} + +# sms.ru API +my $id = ''; +my $num = ''; +$num = $opt_n if $opt_n; +my $sms = "$client_name\n$check_name\n$crit_count"; + +my $uri = "http://sms.ru/sms/send?api_id=".$id."&to=".$num."&text=".$sms; + +# Make request +my $ua = LWP::UserAgent->new(); +$ua->agent('sensu-sms-handler/0.1'); + +my $req = HTTP::Request->new( POST => $uri ); +my $res = $ua->request($req); + +if ( $res->is_success ) { + exit 0; +} +else { + exit 7; +} +