Created
July 11, 2015 22:51
-
-
Save inokappa/ea34a62de3364f127eaa to your computer and use it in GitHub Desktop.
OpenWeatherMap の API から気温、湿度を取得して不快指数を計算して出力する Sensu プラグインっぽいの
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import json, urllib2, sys, time, random | |
argvs = sys.argv | |
cities = argvs[1].split(",") | |
timestamp = int(time.time()) | |
def temperature_humidity_index(): | |
index = 0.81 * int(temperature) + 0.01 * int(humidity) * (0.99 * int(temperature) - 14.3) + 46.3 | |
return index | |
def check(): | |
index = temperature_humidity_index() | |
label = city + ' temperature-humidity_index.current' | |
if index >= 85 : | |
print '%s %s %s' % (label, index, 'critical') | |
sys.exit(2) | |
elif index >= 75 : | |
print '%s %s %s' % (label, index, 'warning') | |
sys.exit(1) | |
else: | |
print '%s %s %s' % (label, index, 'normal') | |
sys.exit(0) | |
def metrics(): | |
index = temperature_humidity_index() | |
label = 'stats' + '.' + city + '.' + 'temperature.current' | |
print '%s %s %d\n' % (label, temperature, timestamp) | |
label = 'stats' + '.' + city + '.' + 'humidity.current' | |
print '%s %s %d\n' % (label, humidity, timestamp) | |
label = 'stats' + '.' + city + '.' + 'temperature-humidity_index.current' | |
print '%s %s %d\n' % (label, index, timestamp) | |
for city in cities: | |
url = 'http://api.openweathermap.org/data/2.5/weather?q=' + city + ',jp&units=metric' | |
r = urllib2.urlopen(url) | |
j = json.loads(r.read()) | |
temperature = j['main']['temp'] | |
humidity = j['main']['humidity'] | |
if argvs[2] == "check": | |
check() | |
elif argvs[2] == "metrics": | |
metrics() | |
r.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment