install with docker
influxdb: image: influxdb container_name: influxdb restart: always volumes: - ./influxdb/data:/var/lib/influxdb2 - ./influxdb/config:/etc/influxdb2 labels: - traefik.enable=true - traefik.http.routers.influxdb.rule=Host(`influxdb.${DOMAIN}`) - traefik.http.routers.influxdb.tls.certresolver=le - traefik.http.routers.influxdb.entrypoints=websecure - traefik.http.routers.influxdb.middlewares=securityheaders
create an organisation
docker exec -it influxdb influx setup
to create another bucket
docker exec -ti influxdb influx bucket create --name mydb --org x --retention 360d docker exec -ti influxdb influx bucket list docker exec -ti influxdb influx bucket delete --name mydb --org x
to get a powerful token
docker exec -ti influxdb influx auth create -o x --read-buckets --read-checks --read-dashboards --read-dbrps --read-notificationEndpoints --read-notificationRules --read-orgs --read-tasks --read-telegrafs --read-user --write-buckets --write-checks --write-dashboards --write-dbrps --write-notificationEndpoints --write-notificationRules --write-orgs --write-tasks --write-telegrafs --write-user
insert data :
curl -X POST "http://localhost:8086/api/v2/write?org=x&bucket=bucket&precision=s" --header "Authorization: Token xxxxxxxxxx" --data-raw "mem,host=host1 used_percent=23.43234543 1556896326"
explore data
goto https://influxdb.${DOMAIN}
delete data
docker exec -ti influxdb influx delete --org x --bucket bucket --start 1970-01-01T00:00:00Z --stop $(date +"%Y-%m-%dT%H:%M:%SZ") --predicate '_measurement="dockerupdate"'
curl --request POST https://influxdb.${DOMAIN}/api/v2/query?org=x \ --header 'Authorization: Token TOKEN' \ --header 'Accept: application/csv' \ --header 'Content-type: application/vnd.flux' \ --data 'from(bucket:"bucket") |> range(start: -12h) |> aggregateWindow(every: 1h, fn: mean)' from(bucket: "bucket") |> range(start: v.timeRangeStart, stop: v.timeRangeStop) |> filter(fn: (r) => r["_measurement"] == "dockerevents") |> filter(fn: (r) => r["_field"] == "nb") |> drop(columns: ["hostname", "containerName", "action", "type"]) |> aggregateWindow(every: 1h, fn: sum) |> yield(name: "mean")
dockerbackup,backuphost=host,hostname=host,driver=driver name=all,db=- error=0,ms=123,size=234,sizeTransfert=456
Cette requete renvoie une ligne par field :
backuphost=host,hostname=host,driver=driver name=all,db=- error=0
backuphost=host,hostname=host,driver=driver name=all,db=- ms=123
...
from(bucket: "bucket") |> range(start: -1d) |> filter(fn: (r) => r["_measurement"] == "dockerbackup") |> filter(fn: (r) => r["_field"] == "error" and r["_value"] == 1) |> filter(fn: (r) => r["driver"] == "rsync") |> sort(columns:["_time"], desc: true)
Pour avoir sur chaque ligne une colone qui porte le nom du field (donc error, ms, size, sizeTransfert) il faut utiliser pivot
from(bucket: "bucket") |> range(start: -1d) |> filter(fn: (r) => r["_measurement"] == "dockerbackup") |> filter(fn: (r) => r["_field"] == "error" and r["_value"] == 1) |> filter(fn: (r) => r["driver"] == "rsync") |> pivot(rowKey: ["_time"], columnKey: ["_field"], valueColumn: "_value") |> filter(fn: (r) => r["ms"] > "${maxms}") |> sort(columns:["_time"], desc: true)
Si on a plusieurs tags, mais qu'on veut faire la somme sans distinguer par tag il faut
from(bucket: "bucket") |> range(start: -1d) |> group() |> sort(columns:["_time"], desc: true)
le group par defaut est suremment
|> group(columns: ["_measurement", "_field"])