Skip to content

Commit

Permalink
linux and windows done
Browse files Browse the repository at this point in the history
  • Loading branch information
AndersonQ committed Jan 4, 2023
1 parent 5b961f1 commit b929ead
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 2 deletions.
25 changes: 24 additions & 1 deletion providers/linux/host_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@
package linux

import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
"time"

"github.com/joeshaw/multierror"
Expand Down Expand Up @@ -138,7 +141,7 @@ func (h *host) CPUTime() (types.CPUTimes, error) {
}

func newHost(fs procFS) (*host, error) {
stat, err := fs.NewStat()
stat, err := fs.Stat()
if err != nil {
return nil, fmt.Errorf("failed to read proc stat: %w", err)
}
Expand All @@ -149,11 +152,13 @@ func newHost(fs procFS) (*host, error) {
r.bootTime(h)
r.containerized(h)
r.hostname(h)
r.fqdn(h)
r.network(h)
r.kernelVersion(h)
r.os(h)
r.time(h)
r.uniqueID(h)

return h, r.Err()
}

Expand Down Expand Up @@ -210,6 +215,24 @@ func (r *reader) hostname(h *host) {
h.info.Hostname = v
}

func (r *reader) fqdn(h *host) {
const cmdPath, args = "/bin/hostname", "-f"
cmd := exec.Command(cmdPath, args)

var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr

err := cmd.Run()
if err != nil {
r.addErr(fmt.Errorf("could not get linux FQDN:'%s %s' failed to run: %q: %w",
cmdPath, args, strings.Trim(stderr.String(), "\n"), err))
return
}

h.info.FQDN = strings.Trim(stdout.String(), "\n")
}

func (r *reader) network(h *host) {
ips, macs, err := shared.Network()
if r.addErr(err) {
Expand Down
38 changes: 37 additions & 1 deletion providers/windows/host_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,18 @@ package windows

import (
"errors"
"fmt"
"os"
"syscall"
"time"

windows "github.com/elastic/go-windows"
"github.com/joeshaw/multierror"
stdwindows "golang.org/x/sys/windows"

"github.com/elastic/go-sysinfo/internal/registry"
"github.com/elastic/go-sysinfo/providers/shared"
"github.com/elastic/go-sysinfo/types"
"github.com/elastic/go-windows"
)

func init() {
Expand Down Expand Up @@ -137,6 +140,39 @@ func (r *reader) hostname(h *host) {
h.info.Hostname = v
}

func (r *reader) fqdn(h *host) {
size := uint32(64)
for {
buff := make([]uint16, size)
err := stdwindows.GetComputerNameEx(
stdwindows.ComputerNamePhysicalDnsFullyQualified, &buff[0], &size)
if err == nil {
h.info.FQDN = syscall.UTF16ToString(buff[:size])
return
}

// ERROR_MORE_DATA means buff is too small and size is set to the
// number of bytes needed to store the FQDN. For details, see
// https://learn.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-getcomputernameexw#return-value
if errors.Is(err, syscall.ERROR_MORE_DATA) {
if size <= uint32(len(buff)) {
// Safeguard to avoid an infinite loop.
r.addErr(fmt.Errorf(
"windows.GetComputerNameEx returned ERROR_MORE_DATA, " +
"but data size should fit into buffer"))
return
} else {
// Grow the buffer and try again.
// Should we limit its growth?
buff = make([]uint16, size)
continue
}
}

r.addErr(fmt.Errorf("could not get windows FQDN: could not get windows.ComputerNamePhysicalDnsFullyQualified: %w", err))
}
}

func (r *reader) network(h *host) {
ips, macs, err := shared.Network()
if r.addErr(err) {
Expand Down
21 changes: 21 additions & 0 deletions providers/windows/host_windows_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package windows

import (
"encoding/json"
"testing"

"github.com/elastic/go-sysinfo/internal/registry"
)

var _ registry.HostProvider = windowsSystem{}

func TestHost(t *testing.T) {
host, err := windowsSystem{}.Host()
if err != nil {
t.Fatal(err)
}

info := host.Info()
data, _ := json.MarshalIndent(info, "", " ")
t.Log(string(data))
}
1 change: 1 addition & 0 deletions types/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ type HostInfo struct {
BootTime time.Time `json:"boot_time"` // Host boot time.
Containerized *bool `json:"containerized,omitempty"` // Is the process containerized.
Hostname string `json:"name"` // Hostname
FQDN string `json:"fqdn"` // FQDN
IPs []string `json:"ip,omitempty"` // List of all IPs.
KernelVersion string `json:"kernel_version"` // Kernel version.
MACs []string `json:"mac"` // List of MAC addresses.
Expand Down

0 comments on commit b929ead

Please sign in to comment.