Skip to content

Commit 9bf502f

Browse files
author
Dylan Myers
committed
Fix logic errors, syntax errors, and typos
1 parent b133d60 commit 9bf502f

File tree

5 files changed

+51
-45
lines changed

5 files changed

+51
-45
lines changed

cpu/cpu_aix_nocgo.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func TimesWithContext(ctx context.Context, percpu bool) ([]TimesStat, error) {
4848
if t, err := strconv.ParseFloat(v[pos], 64); err == nil {
4949
switch header {
5050
case `cpu`:
51-
ct.CPU = t
51+
ct.CPU = strconv.FormatFloat(t, 'f', -1, 64)
5252
case `%usr`:
5353
ct.User = t
5454
case `%sys`:

disk/disk_aix_nocgo.go

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -83,22 +83,22 @@ func getFsType(stat unix.Statfs_t) string {
8383
}
8484

8585
func UsageWithContext(ctx context.Context, path string) (*UsageStat, error) {
86-
var ret []UsageStat
8786
out, err := invoke.CommandWithContext(ctx, "df", "-v")
8887
if err != nil {
8988
return nil, err
9089
}
9190

91+
ret := &UsageStat{}
92+
9293
blocksize := uint64(512)
9394
lines := strings.Split(string(out), "\n")
9495
if len(lines) < 2 {
95-
return []UsageStat{}, common.ErrNotImplementedError
96+
return &UsageStat{}, common.ErrNotImplementedError
9697
}
9798

9899
hf := strings.Fields(strings.Replace(lines[0], "Mounted on", "Path", -1)) // headers
99100
for line := 1; line < len(lines); line++ {
100101
fs := strings.Fields(lines[line]) // values
101-
us := &UsageStat{}
102102
for i, header := range hf {
103103
// We're done in any of these use cases
104104
if i >= len(fs) {
@@ -108,65 +108,68 @@ func UsageWithContext(ctx context.Context, path string) (*UsageStat, error) {
108108
switch header {
109109
case `Filesystem`:
110110
// This is not a valid fs for us to parse
111-
if fs[i] == "/proc" || fs[i] == "/ahafs" {
112-
continue
111+
if fs[i] == "/proc" || fs[i] == "/ahafs" || fs[i] != path {
112+
break
113113
}
114114

115-
us.Fstype, err = GetMountFSType(fs[i])
115+
ret.Fstype, err = GetMountFSTypeWithContext(ctx, fs[i])
116116
if err != nil {
117117
return nil, err
118118
}
119119
case `Path`:
120-
us.Path = fs[i]
120+
ret.Path = fs[i]
121121
case `512-blocks`:
122-
us.Total, err = strconv.Atoi(fs[i])
122+
total, err := strconv.ParseUint(fs[i], 10, 64)
123+
ret.Total = total * blocksize
123124
if err != nil {
124125
return nil, err
125126
}
126127
case `Used`:
127-
us.Used, err = strconv.Atoi(fs[i])
128+
ret.Used, err = strconv.ParseUint(fs[i], 10, 64)
128129
if err != nil {
129130
return nil, err
130131
}
131132
case `Free`:
132-
us.Free, err = strconv.Atoi(fs[i])
133+
ret.Free, err = strconv.ParseUint(fs[i], 10, 64)
133134
if err != nil {
134135
return nil, err
135136
}
136137
case `%Used`:
137-
us.UsedPercent, err = strconv.ParseFloat(fs[i])
138+
val, err := strconv.Atoi(strings.Replace(fs[i], "%", "", -1))
138139
if err != nil {
139140
return nil, err
140141
}
142+
ret.UsedPercent = float64(val) / float64(100)
141143
case `Ifree`:
142-
us.InodesFree, err = strconv.Atoi(fs[i])
144+
ret.InodesFree, err = strconv.ParseUint(fs[i], 10, 64)
143145
if err != nil {
144146
return nil, err
145147
}
146148
case `Iused`:
147-
us.InodesUsed, err = strconv.Atoi(fs[i])
149+
ret.InodesUsed, err = strconv.ParseUint(fs[i], 10, 64)
148150
if err != nil {
149151
return nil, err
150152
}
151153
case `%Iused`:
152-
us.InodesUsedPercent, err = strconv.ParseFloat(fs[i])
154+
val, err := strconv.Atoi(strings.Replace(fs[i], "%", "", -1))
153155
if err != nil {
154156
return nil, err
155157
}
158+
ret.InodesUsedPercent = float64(val) / float64(100)
156159
}
157160
}
158161

159162
// Calculated value, since it isn't returned by the command
160-
us.InodesTotal = us.InodesUsed + us.InodesFree
163+
ret.InodesTotal = ret.InodesUsed + ret.InodesFree
161164

162165
// Valid Usage data, so append it
163-
ret = append(ret, *us)
166+
return ret, nil
164167
}
165168

166-
return *ret, nil
169+
return ret, nil
167170
}
168171

169-
func GetMountFSType(mp string) (string, error) {
172+
func GetMountFSTypeWithContext(ctx context.Context, mp string) (string, error) {
170173
out, err := invoke.CommandWithContext(ctx, "mount")
171174
if err != nil {
172175
return "", err
@@ -212,11 +215,11 @@ func SerialNumberWithContext(ctx context.Context, name string) (string, error) {
212215
if strings.HasPrefix(v, "Serial Number...............") {
213216
ret = strings.TrimPrefix(v, "Serial Number...............")
214217
if ret == "" {
215-
return "", errors.New("empty eerial for disk")
218+
return "", errors.New("empty serial for disk")
216219
}
217220
return ret, nil
218221
}
219222
}
220223

221-
sreturn ret, errors.New("serial entry not found for disk")
224+
return ret, errors.New("serial entry not found for disk")
222225
}

disk/disk_unix.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
//go:build freebsd || linux || darwin || (aix && !cgo)
2-
// +build freebsd linux darwin aix,!cgo
1+
//go:build freebsd || linux || darwin
2+
// +build freebsd linux darwin
33

44
package disk
55

host/host_aix.go

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ func HostIDWithContext(ctx context.Context) (string, error) {
2626
}
2727

2828
// The command always returns an extra newline, so we make use of Split() to get only the first line
29-
return strings.Split(string(out[:]), "\n")[0]
29+
return strings.Split(string(out[:]), "\n")[0], nil
3030
}
3131

3232
func numProcs(ctx context.Context) (uint64, error) {
33-
return common.NumProcsWithContext(ctx)
33+
return 0, common.ErrNotImplementedError
3434
}
3535

3636
func BootTimeWithContext(ctx context.Context) (btime uint64, err error) {
@@ -52,7 +52,7 @@ func BootTimeWithContext(ctx context.Context) (btime uint64, err error) {
5252
//07:43PM up 5 hrs, 1 user, load average: 3.27, 2.91, 2.72
5353
//11:18:23 up 83 days, 18:29, 4 users, load average: 0.16, 0.03, 0.01
5454
func UptimeWithContext(ctx context.Context) (uint64, error) {
55-
out, err := invoke.CommandWithContext(ctx, "uptime").Output()
55+
out, err := invoke.CommandWithContext(ctx, "uptime")
5656
if err != nil {
5757
return 0, err
5858
}
@@ -112,7 +112,7 @@ func UptimeWithContext(ctx context.Context) (uint64, error) {
112112
// This is a weak implementation due to the limitations on retrieving this data in AIX
113113
func UsersWithContext(ctx context.Context) ([]UserStat, error) {
114114
var ret []UserStat
115-
out, err := invoke.CommandWithContext(ctx, "w").Output()
115+
out, err := invoke.CommandWithContext(ctx, "w")
116116
if err != nil {
117117
return nil, err
118118
}
@@ -134,9 +134,9 @@ func UsersWithContext(ctx context.Context) ([]UserStat, error) {
134134
if t, err := strconv.ParseFloat(v[i], 64); err == nil {
135135
switch header {
136136
case `User`:
137-
us.User = t
137+
us.User = strconv.FormatFloat(t, 'f', 1, 64)
138138
case `tty`:
139-
us.Terminal = t
139+
us.Terminal = strconv.FormatFloat(t, 'f', 1, 64)
140140
}
141141
}
142142
}
@@ -151,46 +151,49 @@ func UsersWithContext(ctx context.Context) ([]UserStat, error) {
151151
// 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.
152152
func PlatformInformationWithContext(ctx context.Context) (platform string, family string, version string, err error) {
153153
// Set the platform (which should always, and only be, "AIX") from `uname -s`
154-
out, err := invoke.CommandWithContext(ctx, "uname", "-s").Output()
154+
out, err := invoke.CommandWithContext(ctx, "uname", "-s")
155155
if err != nil {
156156
return "", "", "", err
157157
}
158-
platform = string(out[:])
158+
platform = strings.TrimRight(string(out[:]), "\n")
159159

160160
// Set the family
161-
out, err = invoke.CommandWithContext(ctx, "bootinfo", "-p").Output()
162-
if err != nil {
163-
return "", "", "", err
164-
}
165-
// Family seems to always be the second field from this uname, so pull that out
166-
family = string(out[:])
161+
family = strings.TrimRight(string(out[:]), "\n")
167162

168163
// Set the version
169-
out, err = invoke.CommandWithContext(ctx, "oslevel").Output()
164+
out, err = invoke.CommandWithContext(ctx, "oslevel")
170165
if err != nil {
171166
return "", "", "", err
172167
}
173-
version = string(out[:])
168+
version = strings.TrimRight(string(out[:]), "\n")
174169

175170
return platform, family, version, nil
176171
}
177172

178173
func KernelVersionWithContext(ctx context.Context) (version string, err error) {
179-
out, err := invoke.CommandWithContext(ctx, "oslevel", "-s").Output()
174+
out, err := invoke.CommandWithContext(ctx, "oslevel", "-s")
180175
if err != nil {
181176
return "", err
182177
}
183-
version = string(out[:])
178+
version = strings.TrimRight(string(out[:]), "\n")
184179

185180
return version, nil
186181
}
187182

188183
func KernelArch() (arch string, err error) {
189-
out, err := invoke.CommandWithContext(ctx, "bootinfo", "-y").Output()
184+
out, err := invoke.Command("bootinfo", "-y")
190185
if err != nil {
191186
return "", err
192187
}
193-
arch = string(out[:])
188+
arch = strings.TrimRight(string(out[:]), "\n")
194189

195190
return arch, nil
196191
}
192+
193+
func VirtualizationWithContext(ctx context.Context) (string, string, error) {
194+
return "", "", common.ErrNotImplementedError
195+
}
196+
197+
func SensorsTemperaturesWithContext(ctx context.Context) ([]TemperatureStat, error) {
198+
return nil, common.ErrNotImplementedError
199+
}

host/host_fallback.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
//go:build !darwin && !linux && !freebsd && !openbsd && !netbsd && !solaris && !windows
2-
// +build !darwin,!linux,!freebsd,!openbsd,!netbsd,!solaris,!windows
1+
//go:build !darwin && !linux && !freebsd && !openbsd && !netbsd && !solaris && !windows && !aix
2+
// +build !darwin,!linux,!freebsd,!openbsd,!netbsd,!solaris,!windows,!aix
33

44
package host
55

0 commit comments

Comments
 (0)