1#! /bin/sh
2# Inspired by https://github.com/cmur2/munin-openweather
3# Steve Schnepp
4
5# This part is taken from the original plugin
6# Example usage:
7#  Do
8#    ln -s /path/to/openweather_ openweather_<query_string>
9#  where <query_string> is either a search query "q_Paris" or
10#  or an id search "id_2988507"
11#
12#  These parameters translate directly into a URL formed like this:
13#    http://api.openweathermap.org/data/<api>/weather?<query_string>
14#
15
16## From Oct 9 2015 OpenWeather needs you to register and get an APIKEY
17# include this key by setting 'env.apikey' in munin plugin config, i.e.:
18# [openweather_*]
19# env.apikey XYZ
20
21location=$(printf '%s' "${0#*_}" | tr '_' '=')
22query_string="${location}&appid=${apikey}"
23plugin_name=$( basename $0 )
24
25OWAPI=$( curl -s "http://api.openweathermap.org/data/2.5/weather?mode=xml&${query_string}")
26KELVIN_BIAS=273
27
28CITY=$( expr "$OWAPI" : '.*\<city.*name=\"\(.*\)\"><coord.*' )
29
30if [ "$1" = "config" ];
31then
32	cat <<- EOF
33		multigraph $plugin_name
34		graph_title Temperature in ${CITY}
35		graph_vlabel Celsius
36		graph_category sensors
37		graph_info This graph show the temperature in ${CITY}
38		temp_avg.label avg
39		temp_avg.cdef temp_avg,$KELVIN_BIAS,-
40
41		multigraph $plugin_name.temp
42		graph_title Temperature in ${CITY}
43		graph_vlabel Celsius
44		graph_category sensors
45		graph_info This graph show the temperature in ${CITY}
46		temp_avg.label avg
47		temp_avg.cdef temp_avg,$KELVIN_BIAS,-
48		temp_min.label min
49		temp_min.cdef temp_min,$KELVIN_BIAS,-
50		temp_max.label max
51		temp_max.cdef temp_max,$KELVIN_BIAS,-
52
53		multigraph $plugin_name.humidity
54		graph_title Humidity in ${CITY}
55		graph_vlabel %
56		graph_category sensors
57		graph_args --upper-limit 100 --lower-limit 0
58		graph_info This graph show the humidity in ${CITY}
59		field_humidity.label humidity
60
61		multigraph $plugin_name.pressure
62		graph_title Pressure in ${CITY}
63		graph_vlabel hPa
64		graph_category sensors
65		graph_info This graph show the pressure in ${CITY}
66		field_pressure.label pressure
67
68		multigraph $plugin_name.wind_speed
69		graph_title Wind Speed in ${CITY}
70		graph_vlabel m/s
71		graph_category sensors
72		graph_info This graph show the wind speed in ${CITY}
73		speed.label wind speed
74
75		multigraph $plugin_name.wind_direction
76		graph_title Wind direction in ${CITY}
77		graph_vlabel direction
78		graph_category sensors
79		graph_args --upper-limit 360 --lower-limit 0
80		graph_info This graph show the wind direction in ${CITY}
81		direction.label wind direction
82
83	EOF
84
85	# Continue if dirty config is enabled
86	[ "${MUNIN_CAP_DIRTYCONFIG:-0}" = 1 ] || exit 0
87fi
88
89TEMP_AVG=$( expr "$OWAPI" : '.*\<temperature value=\"\(.*\)\" min.*/temperature.*' )
90TEMP_MIN=$( expr "$OWAPI" : '.*\<temperature .*min=\"\(.*\)\" max.*/temperature.*' )
91TEMP_MAX=$( expr "$OWAPI" : '.*\<temperature .*max=\"\(.*\)\" unit.*/temperature.*' )
92
93HUMIDITY=$( expr "$OWAPI" : '.*\<humidity .*value=\"\(.*\)\" unit.*/humidity.*' )
94PRESSURE=$( expr "$OWAPI" : '.*\<pressure .*value=\"\(.*\)\" unit.*/pressure.*' )
95
96WD_SPEED=$( expr "$OWAPI" : '.*\<speed .*value=\"\(.*\)\" name.*/speed.*' )
97WD_DIREC=$( expr "$OWAPI" : '.*\<direction .*value=\"\(.*\)\" code.*/direction.*' )
98
99cat <<- EOF
100	multigraph $plugin_name
101	temp_avg.value $TEMP_AVG
102
103	multigraph $plugin_name.temp
104	temp_avg.value $TEMP_AVG
105	temp_min.value $TEMP_MIN
106	temp_max.value $TEMP_MAX
107
108	multigraph $plugin_name.humidity
109	field_humidity.value $HUMIDITY
110
111	multigraph $plugin_name.pressure
112	field_pressure.value $PRESSURE
113
114	multigraph $plugin_name.wind_speed
115	speed.value $WD_SPEED
116
117	multigraph $plugin_name.wind_direction
118	direction.value $WD_DIREC
119EOF
120
121
122exit 0
123
124: <<EOF
125<?xml version="1.0" encoding="utf-8"?>
126<current>
127  <city id="2988507" name="Paris">
128    <coord lon="2.35" lat="48.85"/>
129    <country>FR</country>
130    <sun rise="2015-01-01T07:43:52" set="2015-01-01T16:04:40"/>
131  </city>
132  <temperature value="275.099" min="275.099" max="275.099" unit="kelvin"/>
133  <humidity value="100" unit="%"/>
134  <pressure value="1038.33" unit="hPa"/>
135  <wind>
136    <speed value="2.46" name="Light breeze"/>
137    <direction value="190.509" code="S" name="South"/>
138  </wind>
139  <clouds value="0" name="clear sky"/>
140  <visibility/>
141  <precipitation mode="no"/>
142  <weather number="800" value="Sky is Clear" icon="01d"/>
143  <lastupdate value="2015-01-01T11:42:50"/>
144</current>
145EOF
146