forked from outlyerapp/plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmqtt.py
More file actions
executable file
·109 lines (84 loc) · 2.83 KB
/
mqtt.py
File metadata and controls
executable file
·109 lines (84 loc) · 2.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/env python
import mosquitto
import ssl
import time
import sys
import os
mqtt_host = 'localhost'
mqtt_port = 1883
mqtt_username = None
mqtt_password = None
check_topic = 'nagios/test'
check_payload = 'PiNG'
max_wait = 4
status = 0
message = ''
nagios_codes = [ 'OK', 'WARNING', 'CRITICAL', 'UNKNOWN' ]
def on_connect(mosq, userdata, rc):
"""
Upon successfully being connected, we subscribe to the check_topic
"""
mosq.subscribe(check_topic, 0)
def on_publish(mosq, userdata, mid):
pass
def on_subscribe(mosq, userdata, mid, granted_qos):
"""
When the subscription is confirmed, we publish our payload
on the check_topic. Since we're subscribed to this same topic,
on_message() will fire when we see that same message
"""
(res, mid) = mosq.publish(check_topic, check_payload, qos=2, retain=False)
def on_message(mosq, userdata, msg):
"""
This is invoked when we get our own message back. Verify that it
is actually our message and if so, we've completed a round-trip.
"""
global message
global status
if str(msg.payload) == check_payload:
userdata['have_response'] = True
status = 0
elapsed = (time.time() - userdata['start_time'])
message = "PUB to %s at %s responded in %.2f" % (check_topic, mqtt_host, elapsed)
def on_disconnect(mosq, userdata, rc):
exitus(1, "Unexpected disconnection. Incorrect credentials?")
def exitus(status=0, message="all is well"):
"""
Produce a Nagios-compatible single-line message and exit according
to status
"""
print "%s - %s" % (nagios_codes[status], message)
sys.exit(status)
userdata = {
'have_response' : False,
'start_time' : time.time(),
}
mqttc = mosquitto.Mosquitto('nagios-%d' % (os.getpid()), clean_session=True, userdata=userdata)
mqttc.on_message = on_message
mqttc.on_connect = on_connect
mqttc.on_disconnect = on_disconnect
mqttc.on_publish = on_publish
mqttc.on_subscribe = on_subscribe
#mqttc.tls_set('root.ca',
# cert_reqs=ssl.CERT_REQUIRED,
# tls_version=1)
#mqttc.tls_set('root.ca', certfile='c1.crt', keyfile='c1.key', cert_reqs=ssl.CERT_REQUIRED, tls_version=3, ciphers=None)
#mqttc.tls_insecure_set(True) # optional: avoid check certificate name if true
# username & password may be None
mqttc.username_pw_set(mqtt_username, mqtt_password)
# Attempt to connect to broker. If this fails, issue CRITICAL
try:
mqttc.connect(mqtt_host, mqtt_port, 60)
except Exception, e:
status = 2
message = "Connection to %s:%d failed: %s" % (mqtt_host, mqtt_port, str(e))
exitus(status, message)
rc = 0
while userdata['have_response'] == False and rc == 0:
rc = mqttc.loop()
if time.time() - userdata['start_time'] > max_wait:
message = 'timeout waiting for PUB'
status = 2
break
mqttc.disconnect()
exitus(status, message)