#! /bin/sh
# Inspired by https://github.com/cmur2/munin-openweather
# Steve Schnepp

# This part is taken from the original plugin
# Example usage:
#  Do
#    ln -s /path/to/openweather_ openweather_<query_string>
#  where <query_string> is either a search query "q_Paris" or
#  or an id search "id_2988507"
#
#  These parameters translate directly into a URL formed like this:
#    http://api.openweathermap.org/data/<api>/weather?<query_string>
#

## From Oct 9 2015 OpenWeather needs you to register and get an APIKEY
# include this key by setting 'env.apikey' in munin plugin config, i.e.:
# [openweather_*]
# env.apikey XYZ

location=$(printf '%s' "${0#*_}" | tr '_' '=')
query_string="${location}&appid=${apikey}"
plugin_name=$( basename $0 )

OWAPI=$( curl -s "http://api.openweathermap.org/data/2.5/weather?mode=xml&${query_string}")
KELVIN_BIAS=273

CITY=$( expr "$OWAPI" : '.*\<city.*name=\"\(.*\)\"><coord.*' )

if [ "$1" = "config" ];
then
	cat <<- EOF
		multigraph $plugin_name
		graph_title Temperature in ${CITY}
		graph_vlabel Celsius
		graph_category sensors
		graph_info This graph show the temperature in ${CITY}
		temp_avg.label avg
		temp_avg.cdef temp_avg,$KELVIN_BIAS,-

		multigraph $plugin_name.temp
		graph_title Temperature in ${CITY}
		graph_vlabel Celsius
		graph_category sensors
		graph_info This graph show the temperature in ${CITY}
		temp_avg.label avg
		temp_avg.cdef temp_avg,$KELVIN_BIAS,-
		temp_min.label min
		temp_min.cdef temp_min,$KELVIN_BIAS,-
		temp_max.label max
		temp_max.cdef temp_max,$KELVIN_BIAS,-

		multigraph $plugin_name.humidity
		graph_title Humidity in ${CITY}
		graph_vlabel %
		graph_category sensors
		graph_args --upper-limit 100 --lower-limit 0
		graph_info This graph show the humidity in ${CITY}
		field_humidity.label humidity

		multigraph $plugin_name.pressure
		graph_title Pressure in ${CITY}
		graph_vlabel hPa
		graph_category sensors
		graph_info This graph show the pressure in ${CITY}
		field_pressure.label pressure

		multigraph $plugin_name.wind_speed
		graph_title Wind Speed in ${CITY}
		graph_vlabel m/s
		graph_category sensors
		graph_info This graph show the wind speed in ${CITY}
		speed.label wind speed

		multigraph $plugin_name.wind_direction
		graph_title Wind direction in ${CITY}
		graph_vlabel direction
		graph_category sensors
		graph_args --upper-limit 360 --lower-limit 0
		graph_info This graph show the wind direction in ${CITY}
		direction.label wind direction

	EOF

	# Continue if dirty config is enabled
	[ "${MUNIN_CAP_DIRTYCONFIG:-0}" = 1 ] || exit 0
fi

TEMP_AVG=$( expr "$OWAPI" : '.*\<temperature value=\"\(.*\)\" min.*/temperature.*' )
TEMP_MIN=$( expr "$OWAPI" : '.*\<temperature .*min=\"\(.*\)\" max.*/temperature.*' )
TEMP_MAX=$( expr "$OWAPI" : '.*\<temperature .*max=\"\(.*\)\" unit.*/temperature.*' )

HUMIDITY=$( expr "$OWAPI" : '.*\<humidity .*value=\"\(.*\)\" unit.*/humidity.*' )
PRESSURE=$( expr "$OWAPI" : '.*\<pressure .*value=\"\(.*\)\" unit.*/pressure.*' )

WD_SPEED=$( expr "$OWAPI" : '.*\<speed .*value=\"\(.*\)\" name.*/speed.*' )
WD_DIREC=$( expr "$OWAPI" : '.*\<direction .*value=\"\(.*\)\" code.*/direction.*' )

cat <<- EOF
	multigraph $plugin_name
	temp_avg.value $TEMP_AVG

	multigraph $plugin_name.temp
	temp_avg.value $TEMP_AVG
	temp_min.value $TEMP_MIN
	temp_max.value $TEMP_MAX

	multigraph $plugin_name.humidity
	field_humidity.value $HUMIDITY

	multigraph $plugin_name.pressure
	field_pressure.value $PRESSURE

	multigraph $plugin_name.wind_speed
	speed.value $WD_SPEED

	multigraph $plugin_name.wind_direction
	direction.value $WD_DIREC
EOF


exit 0

: <<EOF
<?xml version="1.0" encoding="utf-8"?>
<current>
  <city id="2988507" name="Paris">
    <coord lon="2.35" lat="48.85"/>
    <country>FR</country>
    <sun rise="2015-01-01T07:43:52" set="2015-01-01T16:04:40"/>
  </city>
  <temperature value="275.099" min="275.099" max="275.099" unit="kelvin"/>
  <humidity value="100" unit="%"/>
  <pressure value="1038.33" unit="hPa"/>
  <wind>
    <speed value="2.46" name="Light breeze"/>
    <direction value="190.509" code="S" name="South"/>
  </wind>
  <clouds value="0" name="clear sky"/>
  <visibility/>
  <precipitation mode="no"/>
  <weather number="800" value="Sky is Clear" icon="01d"/>
  <lastupdate value="2015-01-01T11:42:50"/>
</current>
EOF
