Projet

Général

Profil

Révision 2c5943e4

ID2c5943e46965560f704969a174d740d1cfd0723f
Parent c8f30b09
Enfant 3d7046d5

Ajouté par Steve Schnepp il y a presque 11 ans

p: updating openweather_ to be multigraph

Now it graph all the values coming from the API

Voir les différences:

plugins/weather/openweather_
5 5
# This part is taken from the original plugin
6 6
# Example usage:
7 7
#  Do
8
#    ln -s /path/to/openweather_ openweather_<type>_<id>
9
#  where <type> is currently ignored (formerly one of
10
#  [station, weather]) and <id> is a suitable
11
#  city id from http://openweathermap.org/
12
#  These parameters translate directly into a URL formed like this:
13
#    http://openweathermap.org/data/<api>/weather?id=<id>
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"
14 11
#
15
# Example config:
16
#  [openweather_*]
12
#  These parameters translate directly into a URL formed like this:
13
#    http://api.openweathermap.org/data/<api>/weather?<query_string>
17 14
#
18 15

  
19
entity_id=${0##*_}
16
query_string=$(printf '%s' "${0#*_}" | tr '_' '=')
20 17
TMPFILE=$(mktemp)
21 18
trap 'rm -f $TMPFILE' EXIT
22 19

  
20
# API returns temp in K, we have to convert it in C
23 21
KELVIN_BIAS=273
24 22

  
25
curl -s "http://openweathermap.org/data/2.5/weather?id=${entity_id}" | tr ':{},' '   \n' > $TMPFILE
26

  
27
while read KEY VALUE EXTRA
28
do
29
	[ "$KEY" = '"main"' ] && KEY=$VALUE && VALUE=$EXTRA
30
	[ "$KEY" = '"temp"' ] && {
31
		VALUE=${VALUE%%.*}
32
		[ "$1" != "config" ] && echo "temp.value $(( $VALUE - $KELVIN_BIAS ))"
33
	}
34

  
35
	[ "$KEY" = '"name"' ] && {
36
		[ "$1" = "config" ] && {
37
			cat <<- EOF
38
			graph_title Temperature in ${VALUE}
39
			graph_args --lower-limit 0
40
			graph_vlabel Celsius
41
			graph_category weather
42
			graph_scale no
43
			graph_info This graph show the temperature in ${VALUE}
44
			temp.label temperature
45
			temp.info Temperature in degree Celsius
46
			temp.type GAUGE
47
			EOF
48
		}
49
	}
50
done < $TMPFILE
23
curl -s "http://api.openweathermap.org/data/2.5/weather?mode=xml&${query_string}" > $TMPFILE
24

  
25
CITY=$(fgrep "<city id=" $TMPFILE | tr -sc '[:alnum:]' ' ' | cut -d " " -f 6)
26

  
27
if [ "$1" = "config" ];
28
then
29
	cat <<- EOF
30
		multigraph $0
31
		graph_title Temperature in ${CITY}
32
		graph_vlabel Celsius
33
		graph_category weather
34
		graph_info This graph show the temperature in ${CITY}
35
		temp_avg.label avg
36
		temp_avg.cdef $KELVIN_BIAS,-
37

  
38
		multigraph $0.temp
39
		graph_title Temperature in ${CITY}
40
		graph_vlabel Celsius
41
		graph_category weather
42
		graph_info This graph show the temperature in ${CITY}
43
		temp_avg.label avg
44
		temp_avg.cdef $KELVIN_BIAS,-
45
		temp_min.label min
46
		temp_min.cdef $KELVIN_BIAS,-
47
		temp_max.label max
48
		temp_max.cdef $KELVIN_BIAS,-
49

  
50
		multigraph $0.humidity
51
		graph_title Humidity in ${CITY}
52
		graph_vlabel %
53
		graph_category weather
54
		graph_info This graph show the humidity in ${CITY}
55
		humidity.label humidity
56

  
57
		multigraph $0.pressure
58
		graph_title Pressure in ${CITY}
59
		graph_vlabel hPa
60
		graph_category weather
61
		graph_info This graph show the pressure in ${CITY}
62
		pressure.label pressure
63

  
64
		multigraph $0.wind_speed
65
		graph_title Wind Speed in ${CITY}
66
		graph_vlabel m/s
67
		graph_category weather
68
		graph_info This graph show the wind speed in ${CITY}
69
		speed.label wind speed
70

  
71
		multigraph $0.wind_direction
72
		graph_title Wind direction in ${CITY}
73
		graph_vlabel m/s
74
		graph_category weather
75
		graph_info This graph show the wind direction in ${CITY}
76
		direction.label wind direction
77
	EOF
78

  
79
	# Continue if dirty config is enabled
80
	[ "$MUNIN_CAP_DIRTYCONFIG" = 1 ] || exit 0
81
fi
82

  
83
TEMP_AVG=$(fgrep "<temperature value=" $TMPFILE | tr -sc '[:alnum:].' ' ' | cut -d " " -f 4)
84
TEMP_MIN=$(fgrep "<temperature value=" $TMPFILE | tr -sc '[:alnum:].' ' ' | cut -d " " -f 6)
85
TEMP_MAX=$(fgrep "<temperature value=" $TMPFILE | tr -sc '[:alnum:].' ' ' | cut -d " " -f 8)
86

  
87
HUMIDITY=$(fgrep "<humidity value=" $TMPFILE | tr -sc '[:alnum:].' ' ' | cut -d " " -f 4)
88
PRESSURE=$(fgrep "<pressure value=" $TMPFILE | tr -sc '[:alnum:].' ' ' | cut -d " " -f 4)
89

  
90
WD_SPEED=$(fgrep "<speed value=" $TMPFILE | tr -sc '[:alnum:].' ' ' | cut -d " " -f 4)
91
WD_DIREC=$(fgrep "<direction value=" $TMPFILE | tr -sc '[:alnum:].' ' ' | cut -d " " -f 4)
92

  
93
cat <<- EOF
94
	multigraph $0
95
	temp_avg.value	$TEMP_AVG
96

  
97
	multigraph $0.temp
98
	temp_avg.value $TEMP_AVG
99
	temp_min.value $TEMP_MIN
100
	temp_max.value $TEMP_MAX
101

  
102
	multigraph $0.humidity
103
	humidity.value $HUMIDITY
104

  
105
	multigraph $0.pressure
106
	pressure.value $PRESSURE
107

  
108
	multigraph $0.wind_speed
109
	speed.value $WD_SPEED
110

  
111
	multigraph $0.wind_direction
112
	direction.label $WD_DIREC
113
EOF
114

  
51 115

  
52 116
exit 0
117

  
118
: <<EOF
119
<?xml version="1.0" encoding="utf-8"?>
120
<current>
121
  <city id="2988507" name="Paris">
122
    <coord lon="2.35" lat="48.85"/>
123
    <country>FR</country>
124
    <sun rise="2015-01-01T07:43:52" set="2015-01-01T16:04:40"/>
125
  </city>
126
  <temperature value="275.099" min="275.099" max="275.099" unit="kelvin"/>
127
  <humidity value="100" unit="%"/>
128
  <pressure value="1038.33" unit="hPa"/>
129
  <wind>
130
    <speed value="2.46" name="Light breeze"/>
131
    <direction value="190.509" code="S" name="South"/>
132
  </wind>
133
  <clouds value="0" name="clear sky"/>
134
  <visibility/>
135
  <precipitation mode="no"/>
136
  <weather number="800" value="Sky is Clear" icon="01d"/>
137
  <lastupdate value="2015-01-01T11:42:50"/>
138
</current>
139
EOF

Formats disponibles : Unified diff