├── .gitignore ├── images ├── nagios_128.png └── screenshot.png ├── docker-compose.yml ├── Makefile ├── LICENSE ├── README.md └── rocketchat.py /.gitignore: -------------------------------------------------------------------------------- 1 | url.txt 2 | -------------------------------------------------------------------------------- /images/nagios_128.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrefreitas/rocketchat-nagios/HEAD/images/nagios_128.png -------------------------------------------------------------------------------- /images/screenshot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrefreitas/rocketchat-nagios/HEAD/images/screenshot.png -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: '2' 2 | services: 3 | mongo: 4 | image: mongo 5 | 6 | rocketchat: 7 | image: rocketchat/rocket.chat 8 | links: 9 | - mongo 10 | ports: 11 | - 3000 12 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | .PHONY: test-unknown test-ok test-critical test-warning 2 | 3 | URL=$(shell cat url.txt) 4 | 5 | test-unknown: 6 | ./rocketchat.py \ 7 | --url $(URL) \ 8 | --hostalias alfredo \ 9 | --notificationtype RECOVERY \ 10 | --servicestate UNKNOWN \ 11 | --serviceoutput "PING 100% LOSS" \ 12 | --servicedesc counter 13 | 14 | test-ok: 15 | ./rocketchat.py \ 16 | --url $(URL) \ 17 | --hostalias alfredo \ 18 | --notificationtype RECOVERY \ 19 | --servicestate OK \ 20 | --serviceoutput "PING 100% LOSS" \ 21 | --servicedesc counter 22 | 23 | test-critical: 24 | ./rocketchat.py \ 25 | --url $(URL) \ 26 | --hostalias alfredo \ 27 | --notificationtype RECOVERY \ 28 | --servicestate CRITICAL \ 29 | --serviceoutput "PING 100% LOSS" \ 30 | --servicedesc counter 31 | 32 | test-warning: 33 | ./rocketchat.py \ 34 | --url $(URL) \ 35 | --hostalias alfredo \ 36 | --notificationtype RECOVERY \ 37 | --servicestate WARNING \ 38 | --serviceoutput "PING 100% LOSS" \ 39 | --servicedesc counter 40 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 André Freitas 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Rocketchat Nagios Plugin 2 | Send notifications to a Rocketchat channel from Nagios. 3 | 4 | ![Screenshot](images/screenshot.png) 5 | 6 | # Usage 7 | Assuming you are using Nagios 4, the steps are: 8 | 9 | 1. Copy _rocketchat.py_ to /usr/local/nagios/libexec. 10 | 2. Create the notification command __(replace WEBHOOK_URL)__: 11 | 12 | define command { 13 | command_name notify-service-by-rocketchat 14 | command_line /usr/local/nagios/libexec/rocketchat.py --url WEBHOOK_URL --hostalias "$HOSTNAME$" --servicedesc "$SERVICEDESC$" --servicestate "$SERVICESTATE$" --serviceoutput "$SERVICEOUTPUT$" 15 | } 16 | 17 | define command { 18 | command_name notify-host-by-rocketchat 19 | command_line /usr/local/nagios/libexec/rocketchat.py --url WEBHOOK_URL --hostalias "$HOSTNAME$" --hoststate "$HOSTSTATE$" --hostoutput "$HOSTOUTPUT$" 20 | } 21 | 22 | You can also specify a Nagios URL parameter in order to get a valid URL to your alert in the Rocket Chat message __(replace NAGIOS_URL)__: 23 | 24 | define command { 25 | command_name notify-service-by-rocketchat 26 | command_line /usr/local/nagios/libexec/rocketchat.py --url WEBHOOK_URL --hostalias "$HOSTNAME$" --servicedesc "$SERVICEDESC$" --servicestate "$SERVICESTATE$" --serviceoutput "$SERVICEOUTPUT$" --nagiosurl "NAGIOS_URL" 27 | } 28 | 29 | 3. Create the contact: 30 | 31 | define contact { 32 | contact_name rocketchat 33 | alias Rocketchat 34 | service_notification_period 24x7 35 | host_notification_period 24x7 36 | service_notification_options w,u,c,r 37 | host_notification_options d,r 38 | service_notification_commands notify-service-by-rocketchat 39 | host_notification_commands notify-host-by-rocketchat 40 | } 41 | 42 | 4. Add the contact to a contact group: 43 | 44 | 45 | define contactgroup{ 46 | contactgroup_name network-admins 47 | alias Network Administrators 48 | members email, rocketchat 49 | } 50 | 51 | # Contributing 52 | 1. Launch a development instance of Rocket with Docker: 53 | 54 | docker-compose up -d 55 | 56 | 57 | 2. Create a new user and channel by accessing Rocket Chat in your browser. 58 | 59 | 3. Create a new channel called _nagios_. 60 | 61 | 4. Create a new incoming webhook integration for that channel. 62 | 63 | 5. Create a file url.txt with the complete url of the webhook: 64 | 65 | http://192.168.99.100:32769/hooks/jyfgPbsat6cKYxXWS/rocket.cat/O1%2B5u6L2OzvJJYyH6wcfEYifcbbUvoOVsP37Zd%2Fc3b0%3D 66 | 67 | 6. Test a notification: 68 | 69 | make test-ok test-critical test-unknown test-warning 70 | -------------------------------------------------------------------------------- /rocketchat.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | 3 | # Copyright (c) 2015 Andre Freitas 4 | # 5 | # Permission is hereby granted, free of charge, to any person obtaining a copy 6 | # of this software and associated documentation files (the "Software"), to deal 7 | # in the Software without restriction, including without limitation the rights 8 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | # copies of the Software, and to permit persons to whom the Software is 10 | # furnished to do so, subject to the following conditions: 11 | # 12 | # The above copyright notice and this permission notice shall be included in 13 | # all copies or substantial portions of the Software. 14 | # 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | # THE SOFTWARE. 22 | 23 | import argparse 24 | import urllib2 25 | import json 26 | 27 | VERSION = "0.0.1" 28 | 29 | CONFIG = { 30 | "avatar": "https://raw.githubusercontent.com/andrefreitas/rocketchat-nagios/master/images/nagios_128.png", #noqa 31 | "alias": "Nagios", 32 | "colors": { 33 | "OK": "#36a64f", 34 | "CRITICAL": "#d00000", 35 | "WARNING": "#daa038", 36 | "UNKNOWN": "#e3e4e6", 37 | "DOWN": "#d00000", 38 | "UP": "#40b207", 39 | "UNKNOWN": "#e3e4e6", 40 | "UNREACHABLE": "#e3e4e6" 41 | } 42 | } 43 | 44 | TEMPLATE_SERVICE = "{hostalias} {servicedesc} is {servicestate}:\n{serviceoutput}" #noqa 45 | TEMPLATE_HOST = "Host {hostalias} is {hoststate}:\n{hostoutput}" #noqa 46 | 47 | def parse(): 48 | parser = argparse.ArgumentParser(description='Sends Rocket.Chat webhooks') 49 | parser.add_argument('--url', help='Webhook URL', required=True) 50 | parser.add_argument('--proxy', help='http(s) proxy') 51 | parser.add_argument('--channel', help='Rocket.Chat channel') 52 | parser.add_argument('--hostalias', help='Host Alias', required=True) 53 | parser.add_argument('--nagiosurl', help='Nagios URL. Eg : https://nagios.example.com:8888') 54 | parser.add_argument('--hoststate', help='Host State') 55 | parser.add_argument('--hostoutput', help='Host Output') 56 | parser.add_argument('--servicedesc', help='Service Description') 57 | parser.add_argument('--servicestate', help='Service State') 58 | parser.add_argument('--serviceoutput', help='Service Output') 59 | parser.add_argument('--version', action='version', 60 | version='%(prog)s {version}'.format(version=VERSION)) 61 | args = parser.parse_args() 62 | return args 63 | 64 | 65 | def encode_special_characters(text): 66 | text = text.replace("%", "%25") 67 | text = text.replace("&", "%26") 68 | return text 69 | 70 | 71 | def render_template(template, args): 72 | text = template.format(**vars(args)) 73 | return encode_special_characters(text) 74 | 75 | 76 | def create_data(args, config): 77 | text = render_template( 78 | TEMPLATE_SERVICE if args.servicestate else TEMPLATE_HOST, 79 | args 80 | ) 81 | state = args.servicestate if args.servicestate else args.hoststate 82 | color = config["colors"][state] 83 | 84 | payload = { 85 | "alias": config["alias"], 86 | "avatar": config["avatar"], 87 | "text": text.split('\n')[0], 88 | "attachments": [ 89 | { 90 | "text": text.split("\n",1)[1], 91 | "color": color 92 | } 93 | ] 94 | } 95 | if args.channel: 96 | payload["channel"] = args.channel 97 | 98 | data = "payload=" + json.dumps(payload) 99 | return data 100 | 101 | 102 | def request(url, data, args): 103 | if args.proxy: 104 | proxy = urllib2.ProxyHandler({'http': args.proxy, 'https': args.proxy}) 105 | opener = urllib2.build_opener(proxy) 106 | urllib2.install_opener(opener) 107 | req = urllib2.Request(url, data) 108 | response = urllib2.urlopen(req) 109 | return response.read() 110 | 111 | 112 | if __name__ == "__main__": 113 | args = parse() 114 | if args.nagiosurl: 115 | if args.servicestate: 116 | TEMPLATE_SERVICE += "\nSee {nagiosurl}/nagios/cgi-bin/extinfo.cgi?type=2&host={hostalias}&service={servicedesc}" 117 | elif args.hoststate: 118 | TEMPLATE_HOST += "\nSee {nagiosurl}/nagios/cgi-bin/extinfo.cgi?type=1&host={hostalias}" 119 | 120 | data = create_data(args, CONFIG) 121 | response = request(args.url, data, args) 122 | print response 123 | --------------------------------------------------------------------------------