Nagios and Forecast.io-Scripts
authorJoachim Breitner <mail@joachim-breitner.de>
Sun, 16 Mar 2014 15:09:21 +0000 (16:09 +0100)
committerJoachim Breitner <mail@joachim-breitner.de>
Sun, 16 Mar 2014 15:11:45 +0000 (16:11 +0100)
forecast.py [new file with mode: 0755]
gen-display.sh [new file with mode: 0755]
nagios-status.py [new file with mode: 0755]

diff --git a/forecast.py b/forecast.py
new file mode 100755 (executable)
index 0000000..64cb1de
--- /dev/null
@@ -0,0 +1,52 @@
+#!/usr/bin/python
+
+import forecastio
+
+from config import config
+
+api_key = config['forecast-key']
+lat = config['forecast-lat']
+lng = config['forecast-lon']
+forecast = forecastio.load_forecast(api_key, lat, lng)
+
+icons = {
+    'clear-day': 'sun',
+    'clear-night': 'sun',
+    'cloudy': 'cloudy',
+    'fog': 'fog',
+    'partly-cloudy': 'partly-cloudy',
+    'partly-cloudy-day': 'partly-cloudy',
+    'partly-cloudy-night': 'partly-cloudy',
+    'rain': 'rain',
+    'snow': 'snow',
+    }
+
+now = forecast.currently()
+later = None
+for m in forecast.minutely().data + forecast.hourly().data + forecast.daily().data:
+    if m.time < now.time: continue
+    if m.icon is not None and m.icon in icons and icons[m.icon] != icons[now.icon]:
+        if later is None or m.time < later.time:
+            later = m
+
+if now.icon not in icons:
+    print '<hscroll><text>Need "%s"</text></hscroll>' % now.icon
+elif later and later.icon not in icons:
+    print '<hscroll><text>Need "%s"</text></hscroll>' % later.icon
+elif later:
+    minutes = int((later.time -now.time).total_seconds() / 60)
+
+    if minutes < 0:
+        desc = "&lt;?"
+    elif minutes >= 100:
+        hours = int((later.time -now.time).total_seconds() / (60*69))
+        if hours < 10:
+            desc = "%dH" % hours
+        else:
+            desc = "&gt;"
+    else:
+        desc = str(minutes)
+
+    print '<spaceout><icon>%s</icon><thintext>%s</thintext><icon>%s</icon></spaceout>' % (icons[now.icon], desc, icons[later.icon])
+else:
+    print '<center><icon>%s</icon></center>' % icons[now.icon]
diff --git a/gen-display.sh b/gen-display.sh
new file mode 100755 (executable)
index 0000000..f24b43e
--- /dev/null
@@ -0,0 +1,8 @@
+#!/bin/bash
+cat <<__END__ > display.xml.new
+<alternate transition="scrollUp">
+$(./nagios-status.py)
+$(./forecast.py)
+</alternate>
+__END__
+mv display.xml.new display.xml
diff --git a/nagios-status.py b/nagios-status.py
new file mode 100755 (executable)
index 0000000..d9040d7
--- /dev/null
@@ -0,0 +1,35 @@
+#!/usr/bin/python
+
+import requests
+
+from config import config
+
+response = requests.get(
+    config['nagios-url'],
+    auth=(config['nagios-username'], config['nagios-password']),
+    verify=False) # no SNI :-(
+brokenjsontxt = str(response.content)
+
+jsontxt = brokenjsontxt.replace(u'''['service_state', 'service_description', 'svc_plugin_output', 'service_icons', 'svc_state_age', 'svc_check_age', 'perfometer'],''', '')
+
+import simplejson
+data = simplejson.loads(jsontxt)
+
+state = {'OK': 0, 'WARN': 0, 'CRIT': 0}
+
+for l in data:
+    s = l[0]
+    if s not in state:
+        state[s] = 0
+    state[s] += 1
+
+problems = state['CRIT'] + state['WARN']
+
+if problems == 0:
+    #print "<spaceout><icon>smiley</icon><thintext>%d</thintext><icon>smiley</icon></spaceout>" % state['OK']
+    print "<center><icon>smiley</icon></center>"
+else:
+    print "<spaceout><icon>skull</icon><thintext>%d</thintext><icon>skull</icon></spaceout>" % problems
+
+
+