-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatusd_weather.lua
54 lines (43 loc) · 1.6 KB
/
statusd_weather.lua
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
-- statusd_weather.lua : Mark Tran <[email protected]>
-- display weather information from a METAR station
if not statusd_weather then
statusd_weather = {
-- update every 30 minutes
interval = 30*(60*1000),
-- METAR station - http://adds.aviationweather.noaa.gov/metars/stations.txt
station = 'KMSP',
-- template variables:
-- %city, %conditions, %celsius, %fahrenheit, %humidity, %precipitation,
-- %weather
template = '%fahrenheit, %celsius'
}
end
local function fetch_metar()
local info = {}
local url = 'http://weather.noaa.gov/pub/data/observations/metar/decoded/'
local f =
io.popen('curl '..url..statusd_weather.station..'.TXT 2> /dev/null', 'r')
local data = f:read('*all')
f:close()
_, _, info.city =
string.find(data, '^([%a%s?]+),.*%c')
_, _, info.conditions =
string.find(data, 'Sky%sconditions:%s([%a%s?]+)%c')
_, _, info.fahrenheit, info.celsius =
string.find(data, 'Temperature:%s([%-?%d%.]+)%sF%s%(([%-?%d%.]+)%sC%)%c')
_, _, info.humidity =
string.find(data, 'Relative%sHumidity:%s([%d]+%%)%c')
_, _, info.precipitation =
string.find(data, 'Precipitation%slast%shour:%s([%w%s?]+)%c')
_, _, info.weather =
string.find(data, 'Weather:%s([%a%s?]+)%c')
return string.gsub(statusd_weather.template, '%%([%l]+)',
function(x) return(info[x] or "") end)
end
local weather_timer
local function update_weather()
statusd.inform('weather', fetch_metar())
weather_timer:set(statusd_weather.interval, update_weather)
end
weather_timer = statusd.create_timer()
update_weather()