Problem start telegraf service

Hello everyone I can’t fix this problem when I start the service sudo service telegraf start I get this error

basalisco@HASSIO:~$ sudo service telegraf status
● telegraf.service - Telegraf
     Loaded: loaded (/lib/systemd/system/telegraf.service; enabled; preset: enabled)
     Active: active (running) since Tue 2024-01-09 21:38:07 CET; 1min 6s ago
       Docs: https://github.com/influxdata/telegraf
   Main PID: 7539 (telegraf)
      Tasks: 9 (limit: 9345)
     Memory: 47.5M
     CGroup: /system.slice/telegraf.service
             └─7539 /usr/bin/telegraf -config /etc/telegraf/telegraf.conf -config-directory /etc/telegraf/telegraf.d

gen 09 21:38:29 HASSIO telegraf[7539]: 2024-01-09T20:38:29Z E! [outputs.influxdb_v2] When writing to [http://10.0.0.30:8086]: failed to write metric to Monitoring (401 Unauthorized): unauthorized: unauthorized access
gen 09 21:38:29 HASSIO telegraf[7539]: 2024-01-09T20:38:29Z E! [agent] Error writing to outputs.influxdb_v2: failed to send metrics to any configured server(s)
gen 09 21:38:39 HASSIO telegraf[7539]: 2024-01-09T20:38:39Z E! [outputs.influxdb_v2] When writing to [http://10.0.0.30:8086]: failed to write metric to Monitoring (401 Unauthorized): unauthorized: unauthorized access
gen 09 21:38:39 HASSIO telegraf[7539]: 2024-01-09T20:38:39Z E! [agent] Error writing to outputs.influxdb_v2: failed to send metrics to any configured server(s)
gen 09 21:38:49 HASSIO telegraf[7539]: 2024-01-09T20:38:49Z E! [outputs.influxdb_v2] When writing to [http://10.0.0.30:8086]: failed to write metric to Monitoring (401 Unauthorized): unauthorized: unauthorized access
gen 09 21:38:49 HASSIO telegraf[7539]: 2024-01-09T20:38:49Z E! [agent] Error writing to outputs.influxdb_v2: failed to send metrics to any configured server(s)
gen 09 21:38:59 HASSIO telegraf[7539]: 2024-01-09T20:38:59Z E! [outputs.influxdb_v2] When writing to [http://10.0.0.30:8086]: failed to write metric to Monitoring (401 Unauthorized): unauthorized: unauthorized access
gen 09 21:38:59 HASSIO telegraf[7539]: 2024-01-09T20:38:59Z E! [agent] Error writing to outputs.influxdb_v2: failed to send metrics to any configured server(s)
gen 09 21:39:09 HASSIO telegraf[7539]: 2024-01-09T20:39:09Z E! [outputs.influxdb_v2] When writing to [http://10.0.0.30:8086]: failed to write metric to Monitoring (401 Unauthorized): unauthorized: unauthorized access
gen 09 21:39:09 HASSIO telegraf[7539]: 2024-01-09T20:39:09Z E! [agent] Error writing to outputs.influxdb_v2: failed to send metrics to any configured server(s)

but when I start it as a program with the command telegraf telegraf --config /etc/telegraf/telegraf.conf I don’t have any problem, everything works. I can’t fix this problem

What does your config look like?
Are you using any environment variables?

This is my setup

  interval = "10s"
  round_interval = true
  metric_batch_size = 1000
  metric_buffer_limit = 10000
  collection_jitter = "0s"
  flush_interval = "10s"
  flush_jitter = "0s"
  precision = ""
  hostname = "test"
  omit_hostname = false
[[outputs.influxdb_v2]]
  urls = ["http://10.0.0.30:8086"]
  organization = "$INFLUX_ORG"
  token = "$INFLUX_TOKEN"
  bucket = "Monitoring"
  timeout = "5s"

[[inputs.snmp]]
  agents = ["udp://10.0.0.200:161"]
  version = 2
  path = ["/usr/share/snmp/mibs"]
  community = "public"
  agent_host_tag = "monitoring test"
   retries = 3

i have set eviroment variable for token and for organization

echo 'export INFLUX_TOKEN=xxxxxxxxxxxxxxxxxxxxxxxxxxxx' >> ~/.bashrc 

echo 'export INFLUX_ORG=xxxxxxxxxxxxxxxxxxxxxxxxxxxx' >> ~/.bashrc

I don’t know what to do anymore, when I start it from shell everything works, but when it has to work as a service it doesn’t work, it looks like there’s no token, but the token is set in the environment variable

This file is for your user and only your user, not the user the telegraf service runs as. You need to pass those env. variables to the telegraf user service as well.

One way is to add these to /etc/systemd/system/telegraf.service.d/myenv.conf for example:

[Service]
Environment="INFLUX_TOKEN=foobar"
Environment="INFLUX_ORG=foobar"

edit: there is some additional examples here: systemd.service

have “solved”

[[outputs.influxdb_v2]]
  urls = ["http://10.0.0.30:8086"]
  organization = "$INFLUX_ORG"
  token = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
  bucket = "Monitoring"
  timeout = "5s"

I entered the token by hand in the config file instead of in the environment variable and function.

But I wanted to try and added the variables in /etc/default/telegraf and it works using the old configuration with variables. Thank you.

one last question if I set the variable Environment="INFLUX_HOST=http://10.0.0.30:8086" it gives me an error error was [agent] Failed to connect to [outputs.influxdb_v2], retrying in 15s, error was "unsupported scheme [\"$INFLUX_HOST\"]: \"\""

This would indicate that the variable was not set or did not get resolved as your scheme shows up as two quotes escaped.

["$INFLUX_HOST"]

In your TOML the quotes should not need to be escaped. That should actually be invalid toml.