github.com/Ilhicas/nomad@v1.0.4-0.20210304152020-e86851182bc3/command/agent/host/unix.go (about)

     1  // +build !windows
     2  
     3  package host
     4  
     5  import (
     6  	"strings"
     7  	"syscall"
     8  
     9  	"golang.org/x/sys/unix"
    10  )
    11  
    12  // uname returns the syscall like `uname -a`
    13  func uname() string {
    14  	u := &unix.Utsname{}
    15  	err := unix.Uname(u)
    16  	if err != nil {
    17  		return err.Error()
    18  	}
    19  
    20  	uname := strings.Join([]string{
    21  		nullStr(u.Machine[:]),
    22  		nullStr(u.Nodename[:]),
    23  		nullStr(u.Release[:]),
    24  		nullStr(u.Sysname[:]),
    25  		nullStr(u.Version[:]),
    26  	}, " ")
    27  
    28  	return uname
    29  }
    30  
    31  func etcHosts() string {
    32  	return slurp("/etc/hosts")
    33  }
    34  
    35  func resolvConf() string {
    36  	return slurp("/etc/resolv.conf")
    37  }
    38  
    39  func nullStr(bs []byte) string {
    40  	// find the null byte
    41  	var i int
    42  	var b byte
    43  	for i, b = range bs {
    44  		if b == 0 {
    45  			break
    46  		}
    47  	}
    48  
    49  	return string(bs[:i])
    50  }
    51  
    52  type df struct {
    53  	s *syscall.Statfs_t
    54  }
    55  
    56  func makeDf(path string) (*df, error) {
    57  	var s syscall.Statfs_t
    58  	err := syscall.Statfs(path, &s)
    59  	return &df{s: &s}, err
    60  }
    61  
    62  func (d *df) total() uint64 {
    63  	return d.s.Blocks * uint64(d.s.Bsize)
    64  }
    65  
    66  func (d *df) available() uint64 {
    67  	return d.s.Bavail * uint64(d.s.Bsize)
    68  }