diff --git a/README.md b/README.md index 92c34b5..a19174c 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,8 @@ Nagios Mattermost Plugin ======================== +[![langage](https://img.shields.io/badge/Langage-Python-green.svg)](https://www.python.org/) + A plugin for [Nagios](https://www.nagios.org/) and compatible software (e.g. [Icinga](https://www.icinga.org/)) to enable notifications to a [Mattermost](http://www.mattermost.org/) server. ## Plugin Usage @@ -23,54 +25,13 @@ The steps below are for a Nagios 4 server but should work with minimal modificat 2. Create an *Incoming Webhook* integration for the approriate team and note the provided URL. -3. Create the command definitions in your Nagios configuration: - - ``` - define command { - command_name notify-service-by-mattermost - command_line /usr/local/nagios/libexec/mattermost.py --url [MATTERMOST-WEBHOOK-URL] \ - --channel [OPTIONAL-MATTERMOST-CHANNEL] \ - --notificationtype "$NOTIFICATIONTYPE$" \ - --hostalias "$HOSTNAME$" \ - --hostaddress "$HOSTADDRESS$" \ - --servicedesc "$SERVICEDESC$" \ - --servicestate "$SERVICESTATE$" \ - --serviceoutput "$SERVICEOUTPUT$" - } - - define command { - command_name notify-host-by-mattermost - command_line /usr/local/nagios/libexec/mattermost.py --url [MATTERMOST-WEBHOOK-URL] \ - --channel [OPTIONAL-MATTERMOST-CHANNEL] \ - --notificationtype "$NOTIFICATIONTYPE$" \ - --hostalias "$HOSTNAME$" \ - --hostaddress "$HOSTADDRESS$" \ - --hoststate "$HOSTSTATE$" \ - --hostoutput "$HOSTOUTPUT$" - } -``` - -4. Create the contact definition in your Nagios configuration: - - ``` - 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 - host_notification_commands notify-host-by-mattermost - service_notification_commands notify-service-by-mattermost - } -``` - -5. Add the contact to a contact group in your Nagios configuration: +3. copy configuration `mattermost.cfg` to `/etc/nagios4/conf.d/` and upgrade parameter , and - ``` - define contactgroup{ - contactgroup_name network-admins - alias Network Administrators - members email, mattermost - } +4. Add the contact to a contact group in your Nagios configuration: ``` +define contactgroup{ + contactgroup_name network-admins + alias Network Administrators + members email, mattermost +} +``` \ No newline at end of file diff --git a/mattermost.cfg b/mattermost.cfg new file mode 100644 index 0000000..502e0de --- /dev/null +++ b/mattermost.cfg @@ -0,0 +1,38 @@ +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 + _mattermoste_url + _mattermoste_channel + _nagios_cgiurl + host_notification_commands notify-host-by-mattermost + service_notification_commands notify-service-by-mattermost +} + +define command { + command_name notify-service-by-mattermost + command_line /usr/local/nagios/libexec/mattermost.py --cgiurl "$_CONTACTNAGIOS_CGIURL$" \ + --url "$_CONTACTMATTERMOSTE_URL$" \ + --channel "$_CONTACTMATTERMOSTE_CHANNEL$" \ + --notificationtype "$NOTIFICATIONTYPE$" \ + --hostalias "$HOSTNAME$" \ + --hostaddress "$HOSTADDRESS$" \ + --servicedesc "$SERVICEDESC$" \ + --servicestate "$SERVICESTATE$" \ + --serviceoutput "$SERVICEOUTPUT$" +} + +define command { + command_name notify-host-by-mattermost + command_line /usr/local/nagios/libexec/mattermost.py --cgiurl "$_CONTACTNAGIOS_URL$" \ + --url "$_CONTACTMATTERMOSTE_URL$" \ + --channel "$_CONTACTMATTERMISTE_CHANNEL$" \ + --notificationtype "$NOTIFICATIONTYPE$" \ + --hostalias "$HOSTNAME$" \ + --hostaddress "$HOSTADDRESS$" \ + --hoststate "$HOSTSTATE$" \ + --hostoutput "$HOSTOUTPUT$" +} diff --git a/mattermost.py b/mattermost.py index 9a96302..5f8f0a6 100755 --- a/mattermost.py +++ b/mattermost.py @@ -35,8 +35,7 @@ def parse(): default='Nagios') parser.add_argument('--iconurl', help='URL of icon to use for username', default='https://slack.global.ssl.fastly.net/7bf4/img/services/nagios_128.png') # noqa - parser.add_argument('--notificationtype', help='Notification Type', - required=True) + parser.add_argument('--notificationtype', help='Notification Type',required=True) parser.add_argument('--hostalias', help='Host Alias', required=True) parser.add_argument('--hostaddress', help='Host Address', required=True) parser.add_argument('--hoststate', help='Host State') @@ -44,7 +43,7 @@ def parse(): parser.add_argument('--servicedesc', help='Service Description') parser.add_argument('--servicestate', help='Service State') parser.add_argument('--serviceoutput', help='Service Output') - parser.add_argument('--cgiurl', help='Link to extinfo.cgi on your Nagios instance') + parser.add_argument('--cgiurl', help='Link to global cgi on your Nagios instance') parser.add_argument('--version', action='version', version='% (prog)s {version}'.format(version=VERSION)) args = parser.parse_args() @@ -62,17 +61,23 @@ def emoji(notificationtype): "RECOVERY": ":white_check_mark: ", "PROBLEM": ":fire: ", "DOWNTIMESTART": ":clock10: ", - "DOWNTIMEEND": ":sunny: " + "DOWNTIMEEND": ":sunny: ", + "ACKNOWLEDGEMENT": ":ok_hand: " }.get(notificationtype, "") def text(args): - template_host = "__{notificationtype}__ {hostalias} is {hoststate}\n{hostoutput}" # noqa - template_service = "__{notificationtype}__ {hostalias} at {hostaddress}/{servicedesc} is {servicestate}\n{serviceoutput}" # noqa + template_host = "__{notificationtype}__ {hostalias} is {hoststate}\n{hostoutput}" + template_service = "__{notificationtype}__ {hostalias} at {hostaddress}/{servicedesc} is {servicestate}\n{serviceoutput}" if args.hoststate is not None: - template_cgiurl = " [View :link:]({cgiurl}?type=1&host={hostalias})" + template_cgiurl = " [:link: View]({cgiurl}/extinfo.cgi?type=1&host={hostalias})" elif args.servicestate is not None: - template_cgiurl = " [View :link:]({cgiurl}?type=2&host={hostalias}&service={servicedesc})" + template_cgiurl = " [:link: View]({cgiurl}/extinfo.cgi?type=2&host={hostalias}&service={servicedesc})" + if args.notificationtype == "PROBLEM": + if args.hoststate is not None: + template_cgiurl += " | [Ack]({cgiurl}/cmd.cgi?cmd_typ=34&host={hostalias}&service={servicedesc})" + elif args.servicestate is not None: + template_cgiurl += " | [Ack]({cgiurl}/cmd.cgi?cmd_typ=34&host={hostalias}&service={servicedesc})" template = template_service if args.servicestate else template_host text = emoji(args.notificationtype) + template.format(**vars(args))