-
Notifications
You must be signed in to change notification settings - Fork 0
/
gas_price_de_
executable file
·203 lines (146 loc) · 5.16 KB
/
gas_price_de_
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#!/bin/sh
# -*- sh -*-
# vim: ft=sh
: <<=cut
=head1 NAME
gas_price_de_
=head1 DESCRIPTION
Graph prices of certain fuel types at gas stations within an area (defined by center
and radius) in germany using with munin. This uses the http://tanken.t-online.de API.
=head1 PREREQUISITES
This plugin uses the "xmllint" program provided by
libxml2 (http://www.xmlsoft.org) and the "wget" utility
(http://www.gnu.org/software/wget/)
=head1 CONFIGURATION
The filename must be in this format: gas_price_de_ID with
ID beeing a freely choosable string to allow creating
multiple graphs and to set the title of the graph. ID may be empty.
The following environment variables are used by this plugin
latitude - center latitude of area to query [52.527567]
longitude - center longitude of area to query [13.416131]
radius - size of area (in kilometer) [1]
fuel_type - The type of fuel [super]
Supported values are:
diesel
super
super%20e10
superplus
autogas
biodiesel
lkw-diesel
dieselplus
=head2 CONFIGURATION EXAMPLES
[gas_price_de_*]
env.latitude 52.527567
env.longitude 13.416131
env.radius 5
env.fuel_type autogas
=head1 AUTHOR
Copyright (C) 2013 D.Hiepler
=head1 LICENSE
GPLv2
=head1 MAGICK MARKERS
#%# family=manual
=cut
. $MUNIN_LIBDIR/plugins/plugin.sh
# API url
URL="http://tanken.t-online.de/api/v1/search.xml"
# maximum age of returned price (be sure to set this large enough
# to avoid empty queries) Currently the API will return a single
# most recent price per gas station/fuel type within this range
PRICE_AGE="48"
# parse ID
id=${0##*gas_price_de_}
# default values
latitude=${latitude:-52.527567}
longitude=${longitude:-13.416131}
radius=${radius:-5}
fuel_type=${fuel_type:-super}
# build query
QUERY="${URL}?lat=${latitude}&lng=${longitude}&radius=${radius}&price_age=${PRICE_AGE}&fuels=${fuel_type}"
# array of gas station IDs
GAS_STATIONS=()
# temporary XML file
XML=$(tempfile -p gas)
# install trap
trap cleanup EXIT
# clean up handler
function cleanup()
{
if [ -e ${XML} ] ; then rm "${XML}" ; fi
}
# fatal error handler
function die()
{
echo "$1"
exit -1
}
# fetch XML from API
function query()
{
# fetch file to tempfile
wget --quiet "${QUERY}" -O "${XML}" || die "wget \"${QUERY}\" failed"
}
# build array of GAS_STATION IDs from XML
function parseGasStations()
{
# query
query || die "query failed"
# parse IDs from XML
for ident in $(xmllint --xpath "/search/gasStations/*/@ident" ${XML}) ; do
sid=${ident##*ident=\"}
sid=${sid%%\"*}
GAS_STATIONS[${#GAS_STATIONS[@]}]=${sid}
done
if [ ${#GAS_STATIONS[@]} -eq 0 ] ; then die "no gas stations found in query" ; fi
}
# output configuration
function config()
{
# build array of gas station IDs in ${GAS_STATIONS}
parseGasStations || die "parsing gas stations failed"
cat <<EOS
graph_title ${id:-"Fuel prices"}
graph_category fuelprices
graph_vlabel Euro/Liter
graph_info Prices for ${fuel_type}, ${radius}km around ${latitude},${longitude}
EOS
# process every gas station found
for sid in ${GAS_STATIONS[@]} ; do
# get name
name=$(xmllint --xpath "string(/search/gasStations/gasStation[@ident=\"${sid}\"]/@name)" ${XML})
# get address
street=$(xmllint --xpath "string(/search/gasStations/gasStation[@ident=\"${sid}\"]/@street)" ${XML})
zip=$(xmllint --xpath "string(/search/gasStations/gasStation[@ident=\"${sid}\"]/@zip)" ${XML})
city=$(xmllint --xpath "string(/search/gasStations/gasStation[@ident=\"${sid}\"]/@city)" ${XML})
# handle errors
if [ -z "${name}" ] ; then die "gas station has empty name" ; fi
echo "${sid}.label ${name}"
echo "${sid}.info ${street} - ${zip} ${city}"
done
}
# output the last known price
function output()
{
# build array of gas station IDs in ${GAS_STATIONS}
parseGasStations || die "parsing gas stations failed"
# process every gas station found
for sid in ${GAS_STATIONS[@]} ; do
# get current price
price=$(xmllint --xpath "string(/search/gasStations/gasStation[@ident=\"${sid}\"]/fuels/fuel/@price)" ${XML})
# handle errors
if [ -z "${price}" ] ; then die "no price found" ; fi
echo "${sid}.value ${price}"
done
}
# ----------------------------------------------------------------------------
case $1 in
"config")
config
exit
;;
*)
output
exit
;;
esac