Build both Windows and Linux binaries for telegraf within Linux (Ubuntu)

Hello,

I am currently using Ubuntu. I could not find any documentation to build a Windows binary within Linux.

I successfully built a library with Linux, however, I want to compile 2 binaries (Windows and Linux).

Is there a way to accomplish building a Windows binary in Linux?

We build all our binaries in Docker using our makefile, so at the very least you can run:

GOOS=windows make build

If you share more about what you want to do I can see if there are other options you should use.

Where are you applying this line too (what file/directory)?

I am running:

/tools/custom_builder/custom_builder --config …/telegraf.conf

It provides a Linux binary. I am trying also produce a Windows binary (.exe)

This is all while on a Ubuntu Distro Machine.

Ah in that case, you set GOOS for that command:

GOOS=windows /tools/custom_builder/custom_builder --config …/telegraf.conf

For example:

❯ GOOS=windows ./tools/custom_builder/custom_builder --config config.toml
2024-07-30T22:48:56Z I! Importing configuration file(s)...
2024-07-30T22:48:56Z I! Found 1 configuration files...
-------------------------------------------------------------------------------
Enabled plugins:
-------------------------------------------------------------------------------
aggregators (0):
-------------------------------------------------------------------------------
inputs (2):
  exec                            plugins/inputs/exec
  http_listener_v2                plugins/inputs/http_listener_v2
-------------------------------------------------------------------------------
outputs (1):
  file                            plugins/outputs/file
-------------------------------------------------------------------------------
parsers (2):
  influx                          plugins/parsers/influx
  json_v2                         plugins/parsers/json_v2
-------------------------------------------------------------------------------
processors (0):
-------------------------------------------------------------------------------
secretstores (0):
-------------------------------------------------------------------------------
serializers (1):
  influx                          plugins/serializers/influx
-------------------------------------------------------------------------------
2024-07-30T22:48:56Z I! Running build...
CGO_ENABLED=0 go build -tags "custom,inputs.exec,inputs.http_listener_v2,outputs.file,parsers.influx,parsers.json_v2,serializers.influx" -ldflags " -X github.com/influxdata/telegraf/internal.Commit=f9f029e7 -X github.com/influxdata/telegraf/internal.Branch=master -X github.com/influxdata/telegraf/internal.Version=1.32.0-f9f029e7" ./cmd/telegraf

❯ file telegraf.exe 
telegraf.exe: PE32+ executable (console) x86-64, for MS Windows, 16 sections

Thank you very much jpowers. This absolutely worked and a .exe is produced with GOOS=windows.

The next issue is that when running the custom builder, the config file is not included in the binary although I have a display the same as yours where it says importing configuration file, found 1 configuration file.

When trying --test arguments in Windows, this is displayed:

2024-07-31T17:30:50Z E! unable to load default config paths: no config file specified, and could not find one in $TELEGRAF_CONFIG_PATH, /.telegraf/telegraf.conf, C:\Program Files\Telegraf\telegraf.conf, or C:\Program Files\Telegraf\telegraf.d/*.conf

This is what is displayed within Ubuntu

GOOS=windows GOARCH=amd64 ./tools/custom_builder/custom_builder --config …/telegraf.conf

2024-07-31T17:26:31Z I! Importing configuration file(s)...
2024-07-31T17:26:31Z I! Found 1 configuration files...
-------------------------------------------------------------------------------
Enabled plugins:
-------------------------------------------------------------------------------
aggregators (0):
-------------------------------------------------------------------------------
inputs (13):
  cpu                             plugins/inputs/cpu
  disk                            plugins/inputs/disk
  diskio                          plugins/inputs/diskio
  docker                          plugins/inputs/docker
  kernel                          plugins/inputs/kernel
  kernel                          plugins/inputs/kernel
  mem                             plugins/inputs/mem
  net                             plugins/inputs/net
  netstat                         plugins/inputs/netstat
  processes                       plugins/inputs/processes
  processes                       plugins/inputs/processes
  swap                            plugins/inputs/swap
  system                          plugins/inputs/system
-------------------------------------------------------------------------------
outputs (1):
  influxdb_v2                     plugins/outputs/influxdb_v2
-------------------------------------------------------------------------------
parsers (0):
-------------------------------------------------------------------------------
processors (0):
-------------------------------------------------------------------------------
secretstores (0):
-------------------------------------------------------------------------------
serializers (0):
-------------------------------------------------------------------------------
2024-07-31T17:26:31Z I! Running build...
CGO_ENABLED=0 go build -tags "custom,inputs.cpu,inputs.disk,inputs.diskio,inputs.docker,inputs.kernel,inputs.kernel,inputs.mem,inputs.net,inputs.netstat,inputs.processes,inputs.processes,inputs.swap,inputs.system,outputs.influxdb_v2" -ldflags " -X github.com/influxdata/telegraf/internal.Commit=f9f029e7 -X github.com/influxdata/telegraf/internal.Branch=master -X github.com/influxdata/telegraf/internal.Version=1.32.0-f9f029e7" ./cmd/telegraf

The custom builder does not embed the config in the binary. The resulting binary still needs to get passed a configuration file to run.

In this case, I have 2 questions:

  1. I wonder what the reasoning would be to compile a custom binary rather than using the pre-built telegraf compiled binary which is all inclusive? It seems like this would be a backwards step (especially considering 30mb vs 100mb is not much of a difference).

    In other words, what would be the benefit?

  2. Is there possibly a repo fork or someway to accomplish an embedded config within the binary? This being the goal.

For most users this is not a lot, but for some users it is a huge difference. Think embedded systems. Also consider use cases for users who want to strip out anything that is not actually used for security reasons.

  1. Is there possibly a repo fork or someway to accomplish an embedded config within the binary? This being the goal.

You could certainly fork the project and try this. Essentially you could embed the config in the code and load it rather than one of the default locations.

Is there possibly a repo fork or someway to accomplish an embedded config within the binary? This being the goal.

@QuickWick currently that’s not possible but it might be possible to add this feature. Please open a feature request and outline why you would need this feature!

heh I think security would have some concerns with users embedding their configs with creds in the binary :wink:

Yeah I mean we allow users to build a custom binary with their config embedded. I could see a clear use-case for this… :wink: That is, the file is embedded at compile time…