Home Directory Plugins Notifications Notification for Microsoft Teams

Search Exchange

Search All Sites

Nagios Live Webinars

Let our experts show you how Nagios can help your organization.

Contact Us

Phone: 1-888-NAGIOS-1
Email: sales@nagios.com

Login

Remember Me

Notification for Microsoft Teams

Rating
2 votes
Favoured:
0
Last Release Date
2019-03-01
Compatible With
  • Nagios 3.x
  • Nagios 4.x
Owner
License
Apache
Hits
2244
Files:
FileDescription
Screen Shot 2019-03-01 at 10.40.39 PM.pngScreen Shot 2019-03-01 at 10.40.39 PM.png

Help Support Ethan Galstad's New Project:

Help Support Ethan

A Message From The Founder...

As the founder of Nagios, I'm asking for your help in a cause that's dear to my heart.

I'm launching a new project to help better the world by providing the information, ideas, and inspiration that I believe can improve the lives of people everywhere.

I know you're busy managing networks, but I would appreciate it if you would consider liking my Facebook page and showing your support for the content and messages I produce by sharing them with your friends and family.

You can learn more about my project by visiting ethangalstad.me/nagios. Thank you for your time. I wish you all the best in your endeavors, whatever they may be.

- Ethan

Nagios plugin to send notifications to MS Teams (MessageCard)
Reviews (2)
No more empty Teams postings, and UTF-8 support:
#!/usr/bin/perl

#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

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;
use Encode;

my %opt_fields;
my %event;
my %nagios;
my @sections;
my @actions;
my @targets;
my $webhook = "https://outlook.office365.com/webhook/MISSING";
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";
$nagios{'HOSTNOTES'} = "not defined";


#
# Get command-line options
#
GetOptions ("webhook=s" => \$webhook, "nagios_url=s" => \$nagios_url, "field=s%" => \%opt_fields)
or die("Error in command line arguments\n");

#
# Read Nagios events
#
while ((my $k, my $v) = each %ENV) {
next unless $k =~ /^(?:NAGIOS|ICINGA)_(.*)$/;
$nagios{$1} = $v;
print STDERR "$1 is $nagios{$1}\n";
}

# Merge in passed-in variables
%nagios = (%nagios, %opt_fields);

#
# Format message card
#
if (not length($nagios{'SERVICESTATE'})) {
# if service state env is not defnined or empty, event is host notification.
$nagios{'SERVICEDESC'} = "host status";
$nagios{'SERVICESTATE'} = $nagios{'HOSTSTATE'};
$nagios{'SERVICEOUTPUT'} = $nagios{'HOSTOUTPUT'};
}
$event{'themeColor'} = $color{"$nagios{'SERVICESTATE'}"};
$event{'title'} = "$nagios{'HOSTALIAS'}/$nagios{'SERVICEDESC'} is $nagios{'SERVICESTATE'}";
$event{'summary'} = $event{'title'};
my @facts = ({
'name' => "Host:",
'value' => "$nagios{'HOSTALIAS'}"
},{
'name' => "Details:",
'value' => "$nagios{'SERVICEOUTPUT'}"
});
my %section = ( 'facts' => \@facts, );
push(@sections, \%section);
$event{'sections'} = \@sections;

if ($nagios_url ne '') {
my %target = (
'os' => 'default',
'uri' => "$nagios_url/cgi-bin/status.cgi?host=$nagios{'HOSTNAME'}"
);
push(@targets, \%target);
my %link = (
'@type' => 'OpenUri',
'name' => 'View in Nagios',
'targets' => \@targets
);
push(@actions, \%link);
$event{'potentialAction'} = \@actions;
}
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;charset=utf-8');
$req->content(Encode::decode_utf8($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";
byjeyminee, June 26, 2019
1 of 1 people found this review helpful
This is exactly what i needed but... the notification sent to teams is empty as it seems the environment variables are not set.

What could be the problem ?