scripts/monitoring/collectweather.sh
2023-10-19 17:53:44 +02:00

16 lines
813 B
Bash
Executable file

#!/bin/sh
HOSTNAME="${COLLECTD_HOSTNAME:-$(hostname -f)}"
INTERVAL="${COLLECTD_INTERVAL:-300}"
FILE="/home/weather/tmp/openweathermap.json"
# get $FILE using a cronjob (no more than 1M requests per month/1 request per second for free - one every 5 minutes is fine) like
# curl "https://api.openweathermap.org/data/2.5/weather?lat=<latitude>&lon=<longitude>&units=metric&lang=<language>&appid=<your api key>" -o $FILE -s
while sleep "$INTERVAL"; do
for field in main.temp main.feels_like main.pressure main.humidity wind.speed wind.gust clouds.all rain.\"1h\" rain.\"3h\" snow.\"1h\" snow.\"3h\" sys.sunrise sys.sunset visibility;do
key=$(echo "$field" | sed 's/\./-/g' | sed 's/"//g')
echo "PUTVAL $HOSTNAME/weather/gauge-${key} interval=$INTERVAL N:$(jq ."$field" $FILE | sed s/null/0/g)" ;
done
done