Created
April 27, 2023 05:50
-
-
Save vigindian/b874710c30821a95f353b593e6d7003b to your computer and use it in GitHub Desktop.
Nagios MSTeams Notification Script
This file contains 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 | |
# | |
# Nagios MSTeams Notification Script | |
# You can auto-trigger this script for specific alerts, to get notified in the teams channel of your choice | |
# | |
# Setup: | |
# - Setup msteams webhook | |
# - Add your webhook URI to this script | |
# - Setup auto-trigger of this script for specific nagios alerts | |
use warnings; | |
use strict; | |
use Getopt::Long; | |
use HTTP::Request::Common qw(POST); | |
use HTTP::Status qw(is_client_error); | |
use LWP::UserAgent; | |
use JSON; | |
#DEBUG | |
#open my $fileHandle, ">", "/tmp/msteams_notify.debug" or die "Can't open '/tmp/msteams_notify.debug'\n"; | |
my %event; | |
#my %nagios; | |
my @sections; | |
my @actions; | |
my @targets; | |
#Teams Webhook URI | |
my $webhook = "https://yourdomain.webhook.office.com/webhookb2/a1234-5678\@gausga6-1234-5678-abcd-efgh/IncomingWebhook/abcd1234/2468-13579-abcd1234"; | |
#my $nagios_url = ""; | |
my %color = ( 'OK' => '008000', 'WARNING' => 'ffff00', 'UNKNOWN' => '808080','CRITICAL' => 'ff0000', | |
'UP' => '008000', 'DOWN' => 'ff0000', 'UNREACHABLE' => 'ff8700'); | |
$event{'title'} = "Nagios Notification"; | |
$event{'@type'} = "MessageCard"; | |
$event{'@context'} = "https://schema.org/extensions"; | |
my $HOSTALIAS; | |
my $HOSTNAME; | |
my $SERVICEDESC; | |
my $SERVICESTATE; | |
my $SERVICEOUTPUT; | |
# | |
# Get command-line options | |
# | |
GetOptions ("webhook=s" => \$webhook) | |
or die("Error in command line arguments\n"); | |
# | |
# Read Nagios events | |
# | |
($HOSTNAME,$SERVICEDESC,$SERVICESTATE,$SERVICEOUTPUT,$HOSTALIAS) = split /\|/, $ARGV[0]; | |
# | |
# Format message card | |
$event{'themeColor'} = $color{"$SERVICESTATE"}; | |
$event{'title'} = "$HOSTNAME/$SERVICEDESC is $SERVICESTATE"; | |
$event{'summary'} = $event{'title'}; | |
my @facts = ({ | |
'name' => "Host:", | |
'value' => "$HOSTNAME" | |
},{ | |
'name' => "Details:", | |
'value' => "$SERVICEOUTPUT" | |
}); | |
my %section = ( 'facts' => \@facts, 'text' => "Host Notes: $HOSTALIAS" ); | |
push(@sections, \%section); | |
$event{'sections'} = \@sections; | |
my $json = encode_json \%event; | |
# | |
# Make the request | |
# | |
my $ua = LWP::UserAgent->new; | |
$ua->timeout(15); | |
my $req = HTTP::Request->new('POST', $webhook); | |
$req->header('Content-Type' => 'application/json'); | |
$req->content($json); | |
my $s = $req->as_string; | |
print STDERR "Request:\n$s\n"; | |
my $resp = $ua->request($req); | |
$s = $resp->as_string; | |
#print STDERR "Response:\n$s\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment