Telegraf not able to use system variables in hostname

Posted this on Github was told to post over here.

Directions
I have installed telegraf-1.5.2 agent on Windows machine and it seems to be working fine.
I want to change the hostname to take a system variable instead of a default hostname.
Giving a system variable in hostname is not working. It is still taking the default hostname.

Bug report

Relevant telegraf.conf:
#Override default hostname, if empty use os.Hostname()
hostname = “$USERNAME”
omit_hostname = false

System info:
[Include Telegraf version, operating system name, and other relevant details]
Telegraf Version: telegraf-1.5.2_windows
OS Name: Windows 7
Telegraf is sending data to InfluxDB

Expected behavior:
When I give the hostname
hostname = “$USERNAME”
It is supposed to take the username of the machine I am installing Telegraf on

Actual behavior:
But instead of taking the username of the system it is still taking the default hostname.

Additional info:
[Include gist of relevant config, logs, etc.]

Feature Request
Opening a feature request kicks off a discussion.

Proposal:
Why is the hostname not taking the variable tag

Current behavior:
updating variables in hostname is not working it is still taking the default hostname.

Desired behavior:
Updating variables in the hostname should take the variable name we are using instead of default hostname.
When I run the C:\path to \telegraf\telegraf.exe --config C:\path to\telegraf\telegraf.conf --test
I get the bellow result, Part of the result is this, This says that the host=myusername is taking the hostname i want. But in the logs And in InfluxDB too it is updating the default hostname.

diskio,project=infrastructure,os=windows,customer=mmltest,name=C:,factory=project,platform=aws,host=myusername read
s=0i,writes=0i,read_bytes=0i,read_time=0i,write_bytes=0i,write_time=0i,io_time=0i,weighted_io_time=0i,iops_in_progress=0
i 1522186143000000000

I think @daniel’s reply on GitHub seems like it is on the right track:

Assuming you are normally running Telegraf as a service, as documented here, keep in mind that the service runs as a different user, you will have to make sure that the environment variables are set correctly for this user.

Am I right that when you run C:\path to \telegraf\telegraf.exe --config C:\path to\telegraf\telegraf.conf --test you are seeing the results you expect? This seems to indicate that Telegraf is able to use a system variable.

If the variable is empty, Telegraf uses os.Hostname(), so if Telegraf is running as another user and that user does not have the appropriate variable set, you would expect to see the system defaults.

Can you confirm whether or not you are running Telegraf as a service?

Yes you are right when I run the --test command I am able to see the desired output, But when i see the telegraf logs I am seeing the default hostname being loaded to InfluxDB Which is weird.
I checked in the powershell by running the bellow command
$env:USERNAME
and I can confirm that the system variable is not empty. I can also include that I am the root user of my account on Windows.
Yes I am running Telegraf as a Service on Windows.

When you run the --test command and $env:USERNAME in Powershell, you are running these commands from your own user account.

When you run a service on Windows, it runs as a different user: Service User Accounts - Win32 apps | Microsoft Learn

Since you have Telegraf running as a service, it is running as a different user from the one you used to test Telegraf; this is most likely why you are seeing different results.

Other people have run into similar problems working with Windows Services and environment variables: c# - Accessing Environment Variables from Windows Services - Stack Overflow

If you want to access an environment variable from Telegraf running as a service, it seems like there are a few options.

One option is to make sure that the variable is a system environment variable so it is accessible by all users. I do not believe that USERNAME is a system environment variable. Even in this case, though, it seems like there are some issues with the environment variable not being updated if it is changed until the service is restarted.

Another possibility seems to be adding variables for the service directly to the registry, but I don’t know if that is such a good idea.

Thank you @noahcrowley changing the service account to admin account and restarting the agent helped me. I am able to access the system variables now.

1 Like