├── README.md └── notify-by-mqtt.py /README.md: -------------------------------------------------------------------------------- 1 | # notify-by-mqtt 2 | 3 | a Nagios/Icinga notification module which wraps data into JSON and fires it off to an MQTT broker. 4 | 5 | ## `icinga.cfg` 6 | 7 | ``` 8 | enable_environment_macros=1 9 | ``` 10 | 11 | ### `commands.cfg` 12 | 13 | ``` 14 | define command{ 15 | command_name notify-by-mqtt 16 | command_line /home/jpm/icinga/notify-mqtt.py 17 | } 18 | ``` 19 | 20 | ### `contacts_icinga.cfg` 21 | 22 | ``` 23 | define contact{ 24 | contact_name root 25 | alias Root 26 | service_notification_period 24x7 27 | host_notification_period 24x7 28 | service_notification_options w,u,c,r 29 | host_notification_options d,r 30 | # service_notification_commands notify-service-by-email 31 | service_notification_commands notify-by-mqtt 32 | # host_notification_commands notify-host-by-email 33 | host_notification_commands notify-by-mqtt 34 | email root@localhost 35 | } 36 | ``` 37 | 38 | 39 | As an example payload for a host notification: 40 | 41 | ```json 42 | { 43 | "_type": "icinga", 44 | "date": "2014-04-04", 45 | "eventstarttime": "1396621975", 46 | 47 | "hostdisplayname": "localhost", 48 | "hostname": "localhost", 49 | "hostoutput": "PING OK - Packet loss = 0%, RTA = 0.34 ms", 50 | "hoststate": "UP", 51 | "hoststateid": "0", 52 | "hoststatetype": "HARD", 53 | 54 | "shortdatetime": "2014-04-04 16:38:25", 55 | "time": "16:38:25", 56 | "timet": "1396622305" 57 | } 58 | 59 | ``` 60 | 61 | As an example payload for a service notification: 62 | 63 | ```json 64 | { 65 | "_type": "icinga", 66 | "date": "2014-04-04", 67 | "eventstarttime": "1396621975", 68 | 69 | "hostdisplayname": "localhost", 70 | "hostname": "localhost", 71 | "hoststatetype": "HARD", 72 | 73 | "servicedesc": "JPtest", 74 | "servicedisplayname": "JPtest", 75 | "serviceoutput": "file /tmp/f1: ENOENT", 76 | "servicestate": "CRITICAL", 77 | "servicestateid": "2", 78 | 79 | "shortdatetime": "2014-04-04 16:38:25", 80 | "time": "16:38:25", 81 | "timet": "1396622305" 82 | } 83 | ``` 84 | -------------------------------------------------------------------------------- /notify-by-mqtt.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | __author__ = 'Jan-Piet Mens ' 4 | __copyright__ = 'Copyright 2014 Jan-Piet Mens' 5 | __license__ = """Eclipse Public License - v 1.0 (http://www.eclipse.org/legal/epl-v10.html)""" 6 | 7 | import paho.mqtt.publish as mqtt # pip install --upgrade paho-mqtt 8 | try: 9 | import json 10 | except ImportError: 11 | import simplejson as json 12 | import os 13 | 14 | hostname = '127.0.0.1' 15 | port = 1883 16 | qos=0 17 | retain=False 18 | auth=None 19 | prefix = 'ICINGA_' 20 | 21 | data = { 22 | "_type" : "icinga" 23 | } 24 | 25 | env_keys = [ 26 | 'DATE', # 2014-04-04 27 | 'EVENTSTARTTIME', # 1396620153 28 | 'SHORTDATETIME', # 2014-04-04 16:02:53 29 | 'TIME', # 16:02:53 30 | 'TIMET', # 1396620173 31 | ] 32 | 33 | for key in env_keys: 34 | val = os.getenv(prefix + key, None) 35 | if val is not None: 36 | data[key.lower()] = val 37 | 38 | 39 | state = os.getenv(prefix + 'SERVICESTATE', '').lower() 40 | if state is None or state == '': 41 | state = os.getenv(prefix + 'HOSTSTATE', '').lower() 42 | if state is None or state == '': 43 | state = 'unknown' 44 | else: 45 | env_keys = [ 46 | 'HOSTDISPLAYNAME', # localhost 47 | 'HOSTNAME', # localhost 48 | 'HOSTSTATE', # UP 49 | 'HOSTSTATETYPE', # HARD 50 | 'HOSTSTATEID', # 2 51 | 'HOSTOUTPUT', # Ping OK 52 | ] 53 | 54 | for key in env_keys: 55 | val = os.getenv(prefix + key, None) 56 | if val is not None: 57 | data[key.lower()] = val 58 | 59 | else: 60 | env_keys = [ 61 | 'HOSTDISPLAYNAME', # localhost 62 | 'HOSTNAME', # localhost 63 | 'HOSTSTATE', # UP 64 | 'HOSTSTATETYPE', # HARD 65 | 'SERVICEDESC', # JPtest 66 | 'SERVICEDISPLAYNAME', # JPtest 67 | 'SERVICEOUTPUT', # File /tmp/f1 is missing 68 | 'SERVICESTATE', # CRITICAL 69 | 'SERVICESTATEID', # 2 70 | ] 71 | 72 | for key in env_keys: 73 | val = os.getenv(prefix + key, None) 74 | if val is not None: 75 | data[key.lower()] = val 76 | 77 | 78 | topic = 'monitoring/' + state 79 | 80 | payload = None 81 | try: 82 | payload = json.dumps(data) 83 | except: 84 | pass 85 | 86 | rc = mqtt.single(topic, payload, 87 | qos=qos, retain=retain, 88 | hostname=hostname, port=port, 89 | auth=auth) 90 | --------------------------------------------------------------------------------