Skip to content

Instantly share code, notes, and snippets.

@hemanth22
Forked from stefb69/notify_by_ifttt.pl
Created January 19, 2019 16:42
Show Gist options
  • Save hemanth22/8ffed51840d57987effc4b31f5424a97 to your computer and use it in GitHub Desktop.
Save hemanth22/8ffed51840d57987effc4b31f5424a97 to your computer and use it in GitHub Desktop.
Nagios / Shinken notification script to trigger ifttt receipes via Maker Channel
#!/usr/bin/perl -w
# IFTTT Notification for nagios,
# Largely inspired from the Notify My Andoid perl code
use strict;
use LWP::UserAgent;
use Getopt::Long;
use Pod::Usage;
# Grab our options.
my %options = ();
GetOptions(\%options, 'apikey=s', 'event=s', 'notification=s',
'ctype=s', 'application=s', 'channel=s',
'priority:i', 'help|?') or pod2usage(2);
$options{'channel'} ||= "shinken_notification";
$options{'application'} ||= "IFTTTScript";
$options{'priority'} ||= 0;
$options{'ctype'} ||= "application/json";
pod2usage(-verbose => 2) if (exists($options{'help'}));
pod2usage(-message => "$0: Event name is required") if (!exists($options{'event'}));
pod2usage(-message => "$0: Notification text is required") if (!exists($options{'notification'}));
pod2usage(-priority => "$0: Priority must be in the range [-2, 2]") if ($options{'priority'} < -2 || $options{'priority'} > 2);
# Get the API key from STDIN if one isn't provided via a file or from the command line.
if (!exists($options{'apikey'})) {
print "API key: ";
$options{'apikey'} = <STDIN>;
chomp $options{'apikey'};
}
# Generate our HTTP request.
my ($userAgent, $request, $response, $requestURL, $json);
$userAgent = LWP::UserAgent->new;
$userAgent->agent("IFTTTScript/1.01");
$userAgent->env_proxy();
$requestURL = "https://maker.ifttt.com/trigger/". $options{'channel'} . "/with/key/" . $options{'apikey'};
$request = HTTP::Request->new(GET => $requestURL);
$request->header( 'Content-Type' => $options{'ctype'} );
$json = qq|{ "value1": "$options{'application'}",
"value2": "$options{'event'}",
"value3": "$options{'notification'}" }|;
$request->content( $json );
$response = $userAgent->request($request);
if ($response->is_success) {
print "Notification successfully posted. ( ". $request->content. ")\n";
} else {
print STDERR "Notification not posted: " . $response->content . "\n" . $request->content . "\n";
}
__END__
=head1 NAME
notify_by_ifttt
=head1 SYNOPSIS
notify_by_ifttt.pl [options] event_information
Options:
-help Display all help information.
-apikey=... Your Ifttt API key.
Event information:
-application=... The name of the application.
-event=... The name of the event.
-notification=... The text of the notification.
-priority=... The priority of the notification.
An integer in the range [-2, 2]. Not Used.
=head1 OPTIONS
=over 8
=item B<-apikey>
Your IFTTT Maker Channel key. Register at <https://ifttt.com/maker_webhooks>
=item B<-application>
The name of the Application part of the notification. If none is provided, IFTTTScript is used.
=item B<-channel>
The channel part of the notification. If none is provided, shinken_notification is used.
=item B<-event>
The name of the Event part of the notification. This is generally the action which occurs, such as "disk partitioning completed."
=item B<-notification>
The text of the notification, which has more details for a particular event. This is generally the description of the action which occurs, such as "The disk /dev/abc was successfully partitioned."
=item B<-priority>
The priority level of the notification. An integer value ranging [-2, 2] with meanings Very Low, Moderate, Normal, High, Emergency. Reserved for future use if more values are available in webmaker.
=back
=head1 DESCRIPTION
B<This program> sends a notification to the IFTTT Maker Channel of you choice, which is then forwarded to your IFTTT Maker Webhooks triggers.
=head1 HELP
For more assistance, visit the IFTTT website at <https://ifttt.com/maker_webhooks>.
=cut
# This is a default admin
# CHANGE ITS PASSWORD!
define contact{
use generic-contact
contact_name admin
email info@example.org
pager 555-01234567 ; contact phone number
password PASSWORD
is_admin 1
expert 1
_ifttt_apikey abcdefghijlkmnopq...
notificationways ifttt
}
define command{
command_name notify-host-by-ifttt
command_line $PLUGINSDIR$/notify_by_ifttt.pl -channel=nagios -apikey="$_CONTACTIFTTT_APIKEY$" -priority=$HOSTSTATEID$ -application="Shinken Monitoring" -event="$NOTIFICATIONTYPE$ [Host]" -notification="$HOSTNAME$[$HOSTADDRESS$] -- Status: $HOSTSTATE$ ($HOSTOUTPUT$)"
}
define command{
command_name notify-service-by-ifttt
command_line $PLUGINSDIR$/notify_by_ifttt.pl -channel=nagios -apikey="$_CONTACTIFTTT_APIKEY$" -priority=$SERVICESTATEID$ -application="Shinken Monitoring" -event="$NOTIFICATIONTYPE$ [Service]" -notification="$HOSTNAME$[$HOSTADDRESS$]/$SERVICEDESC$ -- Status: $SERVICESTATE$ ($SERVICEOUTPUT$)"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment