forked from CWempe/rest2influxdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rest2influxdb24.sh
91 lines (71 loc) · 3.32 KB
/
rest2influxdb24.sh
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
#!/bin/bash
# This script reads the values of an item from openhab via REST and imports the data to influxdb2.4
# usage: ./rest2influxdb24.sh <itemname>
itemname="$1"
if [ -z $itemname ]
then
echo "Please define Item!"
exit 0
fi
source ./config24.cfg
# convert historical times to unix timestamps,
tenyearsago=`date +"%Y-%m-%dT%H:%M:%S" --date="10 years ago"`
oneyearago=`date +"%Y-%m-%dT%H:%M:%S" --date="-12 months 28 days ago"`
onemonthago=`date +"%Y-%m-%dT%H:%M:%S" --date="29 days ago"`
oneweekago=`date +"%Y-%m-%dT%H:%M:%S" --date="-6 days -23 hours 59 minutes ago"`
onedayago=`date +"%Y-%m-%dT%H:%M:%S" --date="-23 hours 59 minutes ago"`
eighthoursago=`date +"%Y-%m-%dT%H:%M:%S" --date="-7 hours 59 minutes ago"`
# print timestamps
echo ""
echo "### timestamps"
echo "item: $itemname"
echo "10y: $tenyearsago"
echo "1y: $oneyearago"
echo "1m: $onemonthago"
echo "1w: $oneweekago"
echo "1d: $onedayago"
echo "8h: $eighthoursago"
resturl="http://$openhabserver:$openhabport/rest/persistence/items/$itemname?serviceId=$serviceid&api_key=$itemname"
echo "resturl: $resturl"
# get values and write to different files
# curl -X GET --header "Accept: application/json" "$resturl&starttime=${tenyearsago}&endtime=${oneyearago}" > ${itemname}_10y.xml
curl -X GET --header "Accept: application/json" "$resturl&starttime=${tenyearsago}&endtime=${oneyearago}" > ${itemname}_10y.xml
curl -X GET --header "Accept: application/json" "$resturl&starttime=${oneyearago}&endtime=${onemonthago}" > ${itemname}_1y.xml
curl -X GET --header "Accept: application/json" "$resturl&starttime=${onemonthago}&endtime=${oneweekago}" > ${itemname}_1m.xml
curl -X GET --header "Accept: application/json" "$resturl&starttime=${oneweekago}&endtime=${onedayago}" > ${itemname}_1w.xml
curl -X GET --header "Accept: application/json" "$resturl&starttime=${onedayago}&endtime=${eighthoursago}" > ${itemname}_1d.xml
curl -X GET --header "Accept: application/json" "$resturl&starttime=${eighthoursago}" > ${itemname}_8h.xml
# combine files
cat ${itemname}_10y.xml ${itemname}_1y.xml ${itemname}_1m.xml ${itemname}_1w.xml ${itemname}_1d.xml ${itemname}_8h.xml > ${itemname}.xml
# convert data to line protocol file
cat ${itemname}.xml \
| sed 's/}/\n/g' \
| sed 's/data/\n/g' \
| grep -e "time.*state"\
| tr -d ',:[{"' \
| sed 's/time/ /g;s/state/ /g' \
| awk -v item="$itemname" '{print item",item=" item " value=" $2 " " $1 "000000"}' \
| sed 's/value=ON/value=1i/g;s/value=OFF/value=0i/g' \
| sed 's/value=OPEN/value=1i/g;s/value=CLOSED/value=0i/g' \
>> ${itemname}.txt
values=`wc -l ${itemname}.txt | cut -d " " -f 1`
echo ""
echo "### found values: $values"
# split file in smaller parts to make it easier for influxdb
split -l $importsize ${itemname}.txt "${itemname}-"
for i in ${itemname}-*
do
echo "File: " @$i " --- Org: " $influxorg " --- Bucket: " $influxbucket
curl --request POST \
"http://$influxserver:$influxport/api/v2/write?org=$influxorg&bucket=$influxbucket&precision=ns" \
--header "Authorization: Token $influxapitoken" \
--header "Content-Type: text/plain; charset=utf-8" \
--header "Accept: application/json" \
--data-binary @$i
echo "Sleep for $sleeptime seconds to let InfluxDB process the data..."
sleep $sleeptime
done
echo ""
echo "### delete temporary files"
rm ${itemname}*
exit 0