Connecting telegraf with InfluxDB

I want to check http status with telegraf, push that in InfluxDBv2 and use the data base in grafana.
I use these with with a local cluster. I configure the clust with:

version: "3.9"
services:
 
...
  grafana:
    image: grafana/grafana-oss
    restart: unless-stopped
    depends_on:
      - influxdb
    ports:
      - 3000:3000
    environment:
      - GF_LOG_MODE=console file
    volumes:
      - ./grafana/grafana-storage:/var/lib/grafana

      
  influxdb:
    image: influxdb
    restart: unless-stopped
    environment:
      - DOCKER_INFLUXDB_INIT_BUCKET=influx
      - DOCKER_INFLUXDB_INIT_ORG=org
      - DOCKER_INFLUXDB_INIT_USERNAME=user
      - DOCKER_INFLUXDB_INIT_PASSWORD=pw
      - DOCKER_INFLUXDB_INIT_ADMIN_TOKEN=mytoken
    ports: 
      - 8086:8086

  telegraf:
    image: telegraf
    restart: unless-stopped
    ports: 
      - 8092:8092
      - 8094:8094
      - 8125:8125
    depends_on: 
      - influxdb
    links:
      - influxdb
    volumes:
      - ./telegraf/etc/telegraf.conf:/etc/telegraf/telegraf.conf:ro
  
...

I set the configuration of influxdb with:

[global_tags]
  user = "admin"
  
[agent]
  ## Default data collection interval for all inputs
  interval = "5s"
  round_interval = true
  metric_buffer_limit = 10000
  flush_buffer_when_full = true

# HTTP/HTTPS request given an address a method and a timeout
[[inputs.http_response]]
  ## List of urls to query.
  urls = ["http://xxx.xxx.xxx.xxx"]
  ## Set response_timeout (default 5 seconds)
  response_timeout = "30s"
  ## HTTP Request Method
  method = "GET"

[[outputs.influxdb_v2]]
  # urls = ["http://influxdb2:8086"]
  urls = ["http://localhost:8086"]
  token = "mytoken"
  organization = "org"
  bucket = "influx"

The token and org, are not exactly those I used. I the problem is I currently neither can connect telegraf, nor grafana to Influxb.

Telegraf throws:

babel-telegraf-1  | 2022-04-14T15:29:43Z E! [outputs.influxdb_v2] When writing to [http://localhost:8086]: Post "http://localhost:8086/api/v2/write?bucket=influx&org=org": dial tcp 127.0.0.1:8086: connect: connection refused
babel-telegraf-1  | 2022-04-14T15:29:43Z E! [agent] Error writing to outputs.influxdb_v2: Post "http://localhost:8086/api/v2/write?bucket=influx&org=org": dial tcp 127.0.0.1:8086: connect: connection refused

Grafana when trying to connect throws:

babel-grafana-1   | logger=tsdb.influx_flux t=2022-04-14T15:31:14.49+0000 lvl=warn msg="Flux query failed" err="Post \"localhost:///api/v2/query?org=org\": unsupported protocol scheme \"localhost\"" query=buckets()
babel-grafana-1   | logger=context t=2022-04-14T15:31:14.49+0000 lvl=info msg="Request Completed" method=POST path=/api/ds/query status=400 remote_addr=172.20.0.1 time_ms=18 size=237 referer=http://localhost:3000/datasources/edit/Nb4txesnz/

But what I can is: curl http://localhost:8086/ping
It returns:

StatusCode        : 204
StatusDescription : No Content
Content           : {}
RawContent        : HTTP/1.1 204 No Content
                    X-Influxdb-Build: OSS
                    X-Influxdb-Version: v2.2.0
                    Date: Thu, 14 Apr 2022 15:29:04 GMT


Headers           : {[X-Influxdb-Build, OSS], [X-Influxdb-Version, v2.2.0], [Date, Thu, 14 Apr 2022 15:29:04 GMT]}
RawContentLength  : 0

I only had a quick glimpse, but I think the problem is that localhost in a Docker container resolves to the container itself, not the host.

okay, I looked up the name of the container in the docker-compose.yml and exchanged localhost in the telegraf config with this name:

[[outputs.influxdb_v2]]
  urls = ["http://influxdb:8086"]
  token = "..."
  organization = "..."
  bucket = "..."

Now I get the new error:

babel-telegraf-1  | 2022-04-26T08:20:15Z E! [outputs.influxdb_v2] When writing to [http://babel-influxdb-1:8086]: failed to write metric to influx (401 Unauthorized): unauthorized: unauthorized access
babel-telegraf-1  | 2022-04-26T08:20:15Z E! [agent] Error writing to outputs.influxdb_v2: failed to write metric to influx (401 Unauthorized): unauthorized: unauthorized access

and:

babel-influxdb-1  | ts=2022-04-26T08:20:25.209148Z lvl=info msg=Unauthorized log_id=0a5EqjO0000 error="authorization not found"

Did I mix something up?

Okay, I just checked http://localhost:8086/signin

looks like that the user I wanted to created with the docker-compose environment, does not exist / was not created on startup.

I was able to establish a connection:

after I added
- DOCKER_INFLUXDB_INIT_MODE=setup
to the environment, downed the docker-cluster removed the old images and restarted.

I am still trying to connect the influx db to grafana 8.4.4 though.
I used the steps from the website, but

  • the setting in Grafana looks a bit different,
  • the connection can not be estabished

Grafana Warning from the Console: logger=tsdb.influx_flux t=2022-04-26T13:54:07.37+0000 lvl=warn msg="Flux query failed" err="Post \"http://localhost:8086/api/v2/query?org=...\": dial tcp 127.0.0.1:8086: connect: connection refused" query=buckets()

(I removed the org name from the error message).

When manual post http://localhost:8086/api/v2/query?org=...\ in the webbrowser I get:

{"code":"method not allowed","message":"allow: OPTIONS, POST"}

When slightly diviate from the method described on the website and adept the url to the name in the docker-compose.yaml every thing works. (although I thought I tried out the very same setting on the day before.)