Révision 7d1ac56e
p: updating openweather_: replaced fgrep, tr, cut with expr
Use expr to extract values and names from the OpenWeather API xml instead of the - when using a city with spaces in its name not correctly working - fgrep | tr | cut construct.
| plugins/weather/openweather_ | ||
|---|---|---|
| 19 | 19 |
# env.apikey XYZ |
| 20 | 20 |
|
| 21 | 21 |
query_string=$(printf '%s' "${0#*_}" | tr '_' '=')
|
| 22 |
TMPFILE=$(mktemp) |
|
| 23 |
trap 'rm -f $TMPFILE' EXIT |
|
| 24 | 22 |
|
| 23 |
OWAPI=$( curl -s "http://api.openweathermap.org/data/2.5/weather?mode=xml&${query_string}")
|
|
| 25 | 24 |
# API returns temp in K, we have to convert it in C |
| 25 |
# &units=metric would change that ;-) |
|
| 26 | 26 |
KELVIN_BIAS=273 |
| 27 | 27 |
|
| 28 |
curl -s "http://api.openweathermap.org/data/2.5/weather?mode=xml&${query_string}&APPID=${apikey}" > $TMPFILE
|
|
| 29 |
|
|
| 30 |
CITY=$(fgrep "<city id=" $TMPFILE | tr -sc '[:alnum:]' ' ' | cut -d " " -f 6) |
|
| 28 |
CITY=$( expr "$OWAPI" : '.*\<city.*name=\"\(.*\)\"><coord.*' ) |
|
| 31 | 29 |
|
| 32 | 30 |
if [ "$1" = "config" ]; |
| 33 | 31 |
then |
| ... | ... | |
| 85 | 83 |
[ "$MUNIN_CAP_DIRTYCONFIG" = 1 ] || exit 0 |
| 86 | 84 |
fi |
| 87 | 85 |
|
| 88 |
TEMP_AVG=$(fgrep "<temperature value=" $TMPFILE | tr -sc '[:alnum:].' ' ' | cut -d " " -f 4)
|
|
| 89 |
TEMP_MIN=$(fgrep "<temperature value=" $TMPFILE | tr -sc '[:alnum:].' ' ' | cut -d " " -f 6)
|
|
| 90 |
TEMP_MAX=$(fgrep "<temperature value=" $TMPFILE | tr -sc '[:alnum:].' ' ' | cut -d " " -f 8)
|
|
| 86 |
TEMP_AVG=$( expr "$OWAPI" : '.*\<temperature value=\"\(.*\)\" min.*/temperature.*' )
|
|
| 87 |
TEMP_MIN=$( expr "$OWAPI" : '.*\<temperature .*min=\"\(.*\)\" max.*/temperature.*' )
|
|
| 88 |
TEMP_MAX=$( expr "$OWAPI" : '.*\<temperature .*max=\"\(.*\)\" unit.*/temperature.*' )
|
|
| 91 | 89 |
|
| 92 |
HUMIDITY=$(fgrep "<humidity value=" $TMPFILE | tr -sc '[:alnum:].' ' ' | cut -d " " -f 4)
|
|
| 93 |
PRESSURE=$(fgrep "<pressure value=" $TMPFILE | tr -sc '[:alnum:].' ' ' | cut -d " " -f 4)
|
|
| 90 |
HUMIDITY=$( expr "$OWAPI" : '.*\<humidity .*value=\"\(.*\)\" unit.*/humidity.*' )
|
|
| 91 |
PRESSURE=$( expr "$OWAPI" : '.*\<pressure .*value=\"\(.*\)\" unit.*/pressure.*' )
|
|
| 94 | 92 |
|
| 95 |
WD_SPEED=$(fgrep "<speed value=" $TMPFILE | tr -sc '[:alnum:].' ' ' | cut -d " " -f 4)
|
|
| 96 |
WD_DIREC=$(fgrep "<direction value=" $TMPFILE | tr -sc '[:alnum:].' ' ' | cut -d " " -f 4)
|
|
| 93 |
WD_SPEED=$( expr "$OWAPI" : '.*\<speed .*value=\"\(.*\)\" name.*/speed.*' )
|
|
| 94 |
WD_DIREC=$( expr "$OWAPI" : '.*\<direction .*value=\"\(.*\)\" code.*/direction.*' )
|
|
| 97 | 95 |
|
| 98 | 96 |
cat <<- EOF |
| 99 | 97 |
multigraph $0 |
Formats disponibles : Unified diff