summaryrefslogtreecommitdiffstats
path: root/src/go/collectors/go.d.plugin/agent/hostinfo/hostinfo.go
blob: b395b61c6f9e550eb8eb07194f41eb12406a568d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// SPDX-License-Identifier: GPL-3.0-or-later

package hostinfo

import (
	"bytes"
	"context"
	"os/exec"
	"time"
)

var Hostname = getHostname()

func getHostname() string {
	path, err := exec.LookPath("hostname")
	if err != nil {
		return ""
	}

	ctx, cancel := context.WithTimeout(context.Background(), time.Second*2)
	defer cancel()

	bs, err := exec.CommandContext(ctx, path).Output()
	if err != nil {
		return ""
	}

	return string(bytes.TrimSpace(bs))
}