]> git.michaelhowe.org Git - packages/n/nagios-plugins-local.git/commitdiff
Initial import of check_owntracks
authorMichael Howe <michael@michaelhowe.org>
Mon, 21 Dec 2015 20:16:26 +0000 (20:16 +0000)
committerMichael Howe <michael@michaelhowe.org>
Mon, 21 Dec 2015 20:16:26 +0000 (20:16 +0000)
Upstream source:
https://raw.githubusercontent.com/owntracks/recorder/master/contrib/ot-ping.py

plugins/check_owntracks [new file with mode: 0644]

diff --git a/plugins/check_owntracks b/plugins/check_owntracks
new file mode 100644 (file)
index 0000000..e5067df
--- /dev/null
@@ -0,0 +1,117 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+# ot-ping.py
+# send a "pingping" to the OwnTracks Recorder via MQTT with "now"
+# in timestamp (tst). Then verify via Recorder's REST interface
+# whether the timestamp matches. Print a Nagios/Icinga-compatible
+# string to stdout and exit() appropriately.
+
+import paho.mqtt.publish as mqtt  # pip install paho-mqtt
+import json
+import time
+import os
+import sys
+import requests
+
+__author__    = 'Jan-Piet Mens <jpmens()gmail.com>'
+__copyright__ = 'Copyright 2015 Jan-Piet Mens'
+__license__ = 'MIT'
+
+hostname    = 'localhost'
+port        = 1883
+username    = None
+password    = None
+tls         = None
+qos         = 0
+retain      = 0
+
+UNKNOWN = -1
+OK = 0
+WARNING = 1
+CRITICAL = 2
+
+codes = [ 'OK', 'WARNING', 'CRITICAL' ]
+
+def pingping(tics):
+    status = OK
+    msg = "MQTT publish"
+
+    params = {
+            'hostname'  : hostname,
+            'port'      : port,
+            'qos'       : qos,
+            'retain'    : retain,
+            'client_id' : "ot-recorder-ping-ping-%s" % os.getpid(),
+    }
+    auth = None
+
+    if username is not None:
+        auth = {
+            'username' : username,
+            'password' : password
+        }
+
+    topic = "owntracks/ping/ping"
+
+    location = {
+        "_type"     : "location",
+        "tid"       : "pp",
+        "lat"       : 51.47879,
+        "lon"       : -0.010677,
+        "tst"       : tics,
+    }
+
+    topic = "nop/ping/ping"
+    payload = json.dumps(location)
+
+    try:
+        mqtt.single(topic, payload,
+                auth=auth,
+                tls=tls,
+                **params)
+    except Exception, e:
+        status = CRITICAL
+        msg = msg + " " + str(e)
+
+    return status, msg
+
+# Connect to URL (http://host:port) and obtain the last position
+# of the ping/ping user. Note: URL must not include the API endpoint
+# Verify, that the `tst' in the returned payload is close to `tics'
+
+def check_response(url, tics):
+    status = OK
+    msg = "ot-recorder pingping at %s: " % url
+    data = None
+
+    try:
+        r = requests.post(url + "/api/0/last", params= { 'user' : 'ping', 'device' : 'ping' })
+        data = json.loads(r.text)[0]    # Return is an array
+    except Exception, e:
+        return CRITICAL, str(e)
+
+    tst = data['tst']
+    diff = tics - tst
+
+    if diff > 10:
+        status = WARNING
+    if diff > 60:
+        status = CRITICAL
+
+    msg = msg + " %d seconds difference" % diff
+    return status, msg
+
+if __name__ == '__main__':
+    tics = int(time.time())
+
+    status, msg = pingping(tics)
+    if status != OK:
+        print "%s ot-recorder pingping failed: %s" % (codes[status], msg)
+        sys.exit(status)
+
+    status, msg = check_response('http://127.0.0.1:8085', tics)
+
+    print "%s %s" % (codes[status], msg)
+    sys.exit(status)
+