Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
André Freitas committed Dec 10, 2015
0 parents commit 10187da
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 0 deletions.
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Nagios Mattermost Plugin
A plugin for Nagios to enable notifications to Mattermost Open Source Chat.

# Usage
Assuming you are using Nagios 4, the steps are:

1. Copy _mattermost.py_ to /usr/local/nagios/libexec.
2. Create the notification command:
define command {
command_name notify-service-by-mattermost
command_line /usr/local/nagios/libexec/mattermost.py --url [URL] --hostalias "$HOSTNAME$" --notificationtype "$NOTIFICATIONTYPE$" --servicedesc "$SERVICEDESC$" --servicestate "$SERVICESTATE$" --serviceoutput "$SERVICEOUTPUT$"
}

define command {
command_name notify-host-by-mattermost
command_line /usr/local/nagios/libexec/mattermost.py --url [URL] --hostalias "$HOSTNAME$" --notificationtype "$NOTIFICATIONTYPE$" --hoststate "$HOSTSTATE$" --hostoutput "$HOSTOUTPUT$"
}
3. Create the contact:
define contact {
contact_name mattermost
alias Mattermost
service_notification_period 24x7
host_notification_period 24x7
service_notification_options w,u,c,r
host_notification_options d,r
service_notification_commands notify-service-by-mattermost
host_notification_commands notify-host-by-mattermost
}

4. Add the contact to a contact group:
define contactgroup{
contactgroup_name network-admins
alias Network Administrators
members email, mattermost
}
68 changes: 68 additions & 0 deletions mattermost.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#!/usr/bin/python

# Copyright (c) 2015 NDrive SA
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

import argparse
import urllib2
import json

CONFIG = {
"icon_url": "https://slack.global.ssl.fastly.net/7bf4/img/services/nagios_128.png",
"username": "Nagios"
}

TEMPLATE_SERVICE = "__{notificationtype}__ {hostalias}/{servicedesc} is {servicestate}\n{serviceoutput}"
TEMPLATE_HOST = "__{notificationtype}__ {hostalias} is {hoststate}\n{hostoutput}"

def parse():
parser = argparse.ArgumentParser(description='Send mattermost webhooks')
parser.add_argument('--url', help='Integration URL', required=True)
parser.add_argument('--hostalias', help='Host Alias', required=True)
parser.add_argument('--notificationtype', help='Notification type', required=True)
parser.add_argument('--hoststate', help='Host State')
parser.add_argument('--hostoutput', help='Host Output')
parser.add_argument('--servicedesc', help='Service Description')
parser.add_argument('--servicestate', help='Service State')
parser.add_argument('--serviceoutput', help='Service Output')
args = parser.parse_args()
return args

def make_data(args, config):
template = TEMPLATE_SERVICE if args.servicestate else TEMPLATE_HOST
text = template.format(**vars(args))
payload = {
"username": config["username"],
"icon_url": config["icon_url"],
"text": text
}
data = "payload=" + json.dumps(payload)
return data

def request(url, data):
req = urllib2.Request(url, data)
response = urllib2.urlopen(req)
return response.read()

if __name__ == "__main__":
args = parse()
data = make_data(args, CONFIG)
response = request(args.url, data)
print response

0 comments on commit 10187da

Please sign in to comment.