Fix logic errors, syntax errors, and typos

pull/1651/head
Dylan Myers 10 months ago
parent b133d602ce
commit 9bf502f825

@ -48,7 +48,7 @@ func TimesWithContext(ctx context.Context, percpu bool) ([]TimesStat, error) {
if t, err := strconv.ParseFloat(v[pos], 64); err == nil { if t, err := strconv.ParseFloat(v[pos], 64); err == nil {
switch header { switch header {
case `cpu`: case `cpu`:
ct.CPU = t ct.CPU = strconv.FormatFloat(t, 'f', -1, 64)
case `%usr`: case `%usr`:
ct.User = t ct.User = t
case `%sys`: case `%sys`:

@ -83,22 +83,22 @@ func getFsType(stat unix.Statfs_t) string {
} }
func UsageWithContext(ctx context.Context, path string) (*UsageStat, error) { func UsageWithContext(ctx context.Context, path string) (*UsageStat, error) {
var ret []UsageStat
out, err := invoke.CommandWithContext(ctx, "df", "-v") out, err := invoke.CommandWithContext(ctx, "df", "-v")
if err != nil { if err != nil {
return nil, err return nil, err
} }
ret := &UsageStat{}
blocksize := uint64(512) blocksize := uint64(512)
lines := strings.Split(string(out), "\n") lines := strings.Split(string(out), "\n")
if len(lines) < 2 { if len(lines) < 2 {
return []UsageStat{}, common.ErrNotImplementedError return &UsageStat{}, common.ErrNotImplementedError
} }
hf := strings.Fields(strings.Replace(lines[0], "Mounted on", "Path", -1)) // headers hf := strings.Fields(strings.Replace(lines[0], "Mounted on", "Path", -1)) // headers
for line := 1; line < len(lines); line++ { for line := 1; line < len(lines); line++ {
fs := strings.Fields(lines[line]) // values fs := strings.Fields(lines[line]) // values
us := &UsageStat{}
for i, header := range hf { for i, header := range hf {
// We're done in any of these use cases // We're done in any of these use cases
if i >= len(fs) { if i >= len(fs) {
@ -108,65 +108,68 @@ func UsageWithContext(ctx context.Context, path string) (*UsageStat, error) {
switch header { switch header {
case `Filesystem`: case `Filesystem`:
// This is not a valid fs for us to parse // This is not a valid fs for us to parse
if fs[i] == "/proc" || fs[i] == "/ahafs" { if fs[i] == "/proc" || fs[i] == "/ahafs" || fs[i] != path {
continue break
} }
us.Fstype, err = GetMountFSType(fs[i]) ret.Fstype, err = GetMountFSTypeWithContext(ctx, fs[i])
if err != nil { if err != nil {
return nil, err return nil, err
} }
case `Path`: case `Path`:
us.Path = fs[i] ret.Path = fs[i]
case `512-blocks`: case `512-blocks`:
us.Total, err = strconv.Atoi(fs[i]) total, err := strconv.ParseUint(fs[i], 10, 64)
ret.Total = total * blocksize
if err != nil { if err != nil {
return nil, err return nil, err
} }
case `Used`: case `Used`:
us.Used, err = strconv.Atoi(fs[i]) ret.Used, err = strconv.ParseUint(fs[i], 10, 64)
if err != nil { if err != nil {
return nil, err return nil, err
} }
case `Free`: case `Free`:
us.Free, err = strconv.Atoi(fs[i]) ret.Free, err = strconv.ParseUint(fs[i], 10, 64)
if err != nil { if err != nil {
return nil, err return nil, err
} }
case `%Used`: case `%Used`:
us.UsedPercent, err = strconv.ParseFloat(fs[i]) val, err := strconv.Atoi(strings.Replace(fs[i], "%", "", -1))
if err != nil { if err != nil {
return nil, err return nil, err
} }
ret.UsedPercent = float64(val) / float64(100)
case `Ifree`: case `Ifree`:
us.InodesFree, err = strconv.Atoi(fs[i]) ret.InodesFree, err = strconv.ParseUint(fs[i], 10, 64)
if err != nil { if err != nil {
return nil, err return nil, err
} }
case `Iused`: case `Iused`:
us.InodesUsed, err = strconv.Atoi(fs[i]) ret.InodesUsed, err = strconv.ParseUint(fs[i], 10, 64)
if err != nil { if err != nil {
return nil, err return nil, err
} }
case `%Iused`: case `%Iused`:
us.InodesUsedPercent, err = strconv.ParseFloat(fs[i]) val, err := strconv.Atoi(strings.Replace(fs[i], "%", "", -1))
if err != nil { if err != nil {
return nil, err return nil, err
} }
ret.InodesUsedPercent = float64(val) / float64(100)
} }
} }
// Calculated value, since it isn't returned by the command // Calculated value, since it isn't returned by the command
us.InodesTotal = us.InodesUsed + us.InodesFree ret.InodesTotal = ret.InodesUsed + ret.InodesFree
// Valid Usage data, so append it // Valid Usage data, so append it
ret = append(ret, *us) return ret, nil
} }
return *ret, nil return ret, nil
} }
func GetMountFSType(mp string) (string, error) { func GetMountFSTypeWithContext(ctx context.Context, mp string) (string, error) {
out, err := invoke.CommandWithContext(ctx, "mount") out, err := invoke.CommandWithContext(ctx, "mount")
if err != nil { if err != nil {
return "", err return "", err
@ -212,11 +215,11 @@ func SerialNumberWithContext(ctx context.Context, name string) (string, error) {
if strings.HasPrefix(v, "Serial Number...............") { if strings.HasPrefix(v, "Serial Number...............") {
ret = strings.TrimPrefix(v, "Serial Number...............") ret = strings.TrimPrefix(v, "Serial Number...............")
if ret == "" { if ret == "" {
return "", errors.New("empty eerial for disk") return "", errors.New("empty serial for disk")
} }
return ret, nil return ret, nil
} }
} }
sreturn ret, errors.New("serial entry not found for disk") return ret, errors.New("serial entry not found for disk")
} }

@ -1,5 +1,5 @@
//go:build freebsd || linux || darwin || (aix && !cgo) //go:build freebsd || linux || darwin
// +build freebsd linux darwin aix,!cgo // +build freebsd linux darwin
package disk package disk

@ -26,11 +26,11 @@ func HostIDWithContext(ctx context.Context) (string, error) {
} }
// The command always returns an extra newline, so we make use of Split() to get only the first line // The command always returns an extra newline, so we make use of Split() to get only the first line
return strings.Split(string(out[:]), "\n")[0] return strings.Split(string(out[:]), "\n")[0], nil
} }
func numProcs(ctx context.Context) (uint64, error) { func numProcs(ctx context.Context) (uint64, error) {
return common.NumProcsWithContext(ctx) return 0, common.ErrNotImplementedError
} }
func BootTimeWithContext(ctx context.Context) (btime uint64, err error) { func BootTimeWithContext(ctx context.Context) (btime uint64, err error) {
@ -52,7 +52,7 @@ func BootTimeWithContext(ctx context.Context) (btime uint64, err error) {
//07:43PM up 5 hrs, 1 user, load average: 3.27, 2.91, 2.72 //07:43PM up 5 hrs, 1 user, load average: 3.27, 2.91, 2.72
//11:18:23 up 83 days, 18:29, 4 users, load average: 0.16, 0.03, 0.01 //11:18:23 up 83 days, 18:29, 4 users, load average: 0.16, 0.03, 0.01
func UptimeWithContext(ctx context.Context) (uint64, error) { func UptimeWithContext(ctx context.Context) (uint64, error) {
out, err := invoke.CommandWithContext(ctx, "uptime").Output() out, err := invoke.CommandWithContext(ctx, "uptime")
if err != nil { if err != nil {
return 0, err return 0, err
} }
@ -112,7 +112,7 @@ func UptimeWithContext(ctx context.Context) (uint64, error) {
// This is a weak implementation due to the limitations on retrieving this data in AIX // This is a weak implementation due to the limitations on retrieving this data in AIX
func UsersWithContext(ctx context.Context) ([]UserStat, error) { func UsersWithContext(ctx context.Context) ([]UserStat, error) {
var ret []UserStat var ret []UserStat
out, err := invoke.CommandWithContext(ctx, "w").Output() out, err := invoke.CommandWithContext(ctx, "w")
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -134,9 +134,9 @@ func UsersWithContext(ctx context.Context) ([]UserStat, error) {
if t, err := strconv.ParseFloat(v[i], 64); err == nil { if t, err := strconv.ParseFloat(v[i], 64); err == nil {
switch header { switch header {
case `User`: case `User`:
us.User = t us.User = strconv.FormatFloat(t, 'f', 1, 64)
case `tty`: case `tty`:
us.Terminal = t us.Terminal = strconv.FormatFloat(t, 'f', 1, 64)
} }
} }
} }
@ -151,46 +151,49 @@ func UsersWithContext(ctx context.Context) ([]UserStat, error) {
// Much of this function could be static. However, to be future proofed, I've made it call the OS for the information in all instances. // Much of this function could be static. However, to be future proofed, I've made it call the OS for the information in all instances.
func PlatformInformationWithContext(ctx context.Context) (platform string, family string, version string, err error) { func PlatformInformationWithContext(ctx context.Context) (platform string, family string, version string, err error) {
// Set the platform (which should always, and only be, "AIX") from `uname -s` // Set the platform (which should always, and only be, "AIX") from `uname -s`
out, err := invoke.CommandWithContext(ctx, "uname", "-s").Output() out, err := invoke.CommandWithContext(ctx, "uname", "-s")
if err != nil { if err != nil {
return "", "", "", err return "", "", "", err
} }
platform = string(out[:]) platform = strings.TrimRight(string(out[:]), "\n")
// Set the family // Set the family
out, err = invoke.CommandWithContext(ctx, "bootinfo", "-p").Output() family = strings.TrimRight(string(out[:]), "\n")
if err != nil {
return "", "", "", err
}
// Family seems to always be the second field from this uname, so pull that out
family = string(out[:])
// Set the version // Set the version
out, err = invoke.CommandWithContext(ctx, "oslevel").Output() out, err = invoke.CommandWithContext(ctx, "oslevel")
if err != nil { if err != nil {
return "", "", "", err return "", "", "", err
} }
version = string(out[:]) version = strings.TrimRight(string(out[:]), "\n")
return platform, family, version, nil return platform, family, version, nil
} }
func KernelVersionWithContext(ctx context.Context) (version string, err error) { func KernelVersionWithContext(ctx context.Context) (version string, err error) {
out, err := invoke.CommandWithContext(ctx, "oslevel", "-s").Output() out, err := invoke.CommandWithContext(ctx, "oslevel", "-s")
if err != nil { if err != nil {
return "", err return "", err
} }
version = string(out[:]) version = strings.TrimRight(string(out[:]), "\n")
return version, nil return version, nil
} }
func KernelArch() (arch string, err error) { func KernelArch() (arch string, err error) {
out, err := invoke.CommandWithContext(ctx, "bootinfo", "-y").Output() out, err := invoke.Command("bootinfo", "-y")
if err != nil { if err != nil {
return "", err return "", err
} }
arch = string(out[:]) arch = strings.TrimRight(string(out[:]), "\n")
return arch, nil return arch, nil
} }
func VirtualizationWithContext(ctx context.Context) (string, string, error) {
return "", "", common.ErrNotImplementedError
}
func SensorsTemperaturesWithContext(ctx context.Context) ([]TemperatureStat, error) {
return nil, common.ErrNotImplementedError
}

@ -1,5 +1,5 @@
//go:build !darwin && !linux && !freebsd && !openbsd && !netbsd && !solaris && !windows //go:build !darwin && !linux && !freebsd && !openbsd && !netbsd && !solaris && !windows && !aix
// +build !darwin,!linux,!freebsd,!openbsd,!netbsd,!solaris,!windows // +build !darwin,!linux,!freebsd,!openbsd,!netbsd,!solaris,!windows,!aix
package host package host

Loading…
Cancel
Save