Skip to content

Instantly share code, notes, and snippets.

@fredrike
Last active March 19, 2018 13:22
Show Gist options
  • Save fredrike/98e60950883196e881aa8e067567af1e to your computer and use it in GitHub Desktop.
Save fredrike/98e60950883196e881aa8e067567af1e to your computer and use it in GitHub Desktop.
Scratchpad for editing influxdb series.

Initial config :)

influxdb="influxdb"
db="homeassistant"

Show measurement tags:

echo 'show series on homeassistant from "°C";' |  docker run --rm --link=$influxdb -i influxdb influx -host $influxdb

Dump data:

docker exec -it  $influxdb influx_inspect export -database homeassistant -datadir /var/lib/influxdb/data -waldir /var/lib/influxdb/wal -out /export
docker cp $influxdb:export ./

Edit data:

sed -i -e "s/old_measurement/new_measurement/;s/old_field_key/new_field_key/;s/old_tag_key/new_tag_key/;s/new_tag_key/additional_tag_key=additional_tag,new_tag_key/;s/i,/,/g;s/i / /" export

 sed -e 's/entity_id=ute_temperature_2/entity_id=ute_temperature/;s/entity_id=ute_temperature_2/entity_id=ute_temperature/;s/entity_id=ute_temperature_2/entity_id=vit_inne_temperature/;s/entity_id=vitinne_temperature/entity_id=vit_inne_temperature/;s/entity_id=vitinne_temperature/entity_id=vit_inne_temperature/;s/entity_id=wms_temperature/entity_id=ute_temperature/'

#split
split -l 100000 -a 3 export.modified export.modified-split.

Add

for f in export.modified-split.???; do
  echo $f
  grep -v ^CREATE $f | curl -i -POST "http://192.168.1.2:8086/write?db=home_assistant" --data-binary @-
done

Automation attempt :P (will read from $DB) to find measures and add friendly_name to the entity_id's that have a friendly name in the hass db (but not in the export file).

DB="hass"
for measure in `echo "show measurements on $DB;"|docker run --rm --link=$influxdb -i influxdb influx -host $influxdb|tail -n +4`; do
  echo $measure;
  
  SED=$(echo "show series on $DB from \"$measure\";" |  docker run --rm --link=$influxdb -i influxdb influx -host $influxdb|grep ",friendly_name"|sed -e 's/\\/\\\\/g;s/\//\\\//g' |awk -F, '{ print "s/"$3" /"$3","$4" /;"}')
  
  grep ^$measure export |sed -e "$SED" | split -l 100000 -a 2 - "$measure-split."
done


for f in *-split.??; do
  echo $f
  curl -i -POST "http://192.168.1.2:8086/write?db=home_assistant  --data-binary @$f
done

gzip -v *-split.??
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment