github.com/netdata/go.d.plugin@v0.58.1/agent/hostinfo/hostinfo.go (about)

     1  // SPDX-License-Identifier: GPL-3.0-or-later
     2  
     3  package hostinfo
     4  
     5  import (
     6  	"bytes"
     7  	"context"
     8  	"os/exec"
     9  	"time"
    10  )
    11  
    12  var Hostname = getHostname()
    13  
    14  func getHostname() string {
    15  	path, err := exec.LookPath("hostname")
    16  	if err != nil {
    17  		return ""
    18  	}
    19  
    20  	ctx, cancel := context.WithTimeout(context.Background(), time.Second*2)
    21  	defer cancel()
    22  
    23  	bs, err := exec.CommandContext(ctx, path).Output()
    24  	if err != nil {
    25  		return ""
    26  	}
    27  
    28  	return string(bytes.TrimSpace(bs))
    29  }