Skip to content

Commit 8df2696

Browse files
author
Dylan Myers
committed
Account for other possible formats output by AIX uptime command
1 parent a0bbcca commit 8df2696

File tree

1 file changed

+45
-15
lines changed

1 file changed

+45
-15
lines changed

host/host_aix.go

Lines changed: 45 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ func BootTimeWithContext(ctx context.Context) (btime uint64, err error) {
4747
return timeSince(ut), nil
4848
}
4949

50+
//11:54AM up 13 mins, 1 user, load average: 2.78, 2.62, 1.79
51+
//12:41PM up 1 hr, 1 user, load average: 2.47, 2.85, 2.83
52+
//07:43PM up 5 hrs, 1 user, load average: 3.27, 2.91, 2.72
53+
//11:18:23 up 83 days, 18:29, 4 users, load average: 0.16, 0.03, 0.01
5054
func UptimeWithContext(ctx context.Context) (uint64, error) {
5155
out, err := invoke.CommandWithContext(ctx, "uptime").Output()
5256
if err != nil {
@@ -56,27 +60,53 @@ func UptimeWithContext(ctx context.Context) (uint64, error) {
5660
// Convert our uptime to a series of fields we can extract
5761
ut := strings.Fields(string(out[:]))
5862

59-
// Convert the second field "Days" value to integer and roll it to minutes
60-
days, err := strconv.Atoi(ut[2])
61-
if err != nil {
62-
return 0, err
63-
}
63+
// Convert the second field value to integer
64+
var days uint64 = 0
65+
var hours uint64 = 0
66+
var minutes uint64 = 0
67+
if ut[3] == "days," {
68+
days, err = strconv.ParseUint(ut[2], 10, 64)
69+
if err != nil {
70+
return 0, err
71+
}
6472

65-
// Split field 4 into hours and minutes
66-
hm := strings.Split(ut[4], ":")
67-
hours, err := strconv.Atoi(hm[0])
68-
if err != nil {
69-
return 0, err
70-
}
71-
minutes, err := strconv.Atoi(strings.Replace(hm[1], ",", "", -1))
72-
if err != nil {
73-
return 0, err
73+
// Split field 4 into hours and minutes
74+
hm := strings.Split(ut[4], ":")
75+
hours, err = strconv.ParseUint(hm[0], 10, 64)
76+
if err != nil {
77+
return 0, err
78+
}
79+
minutes, err = strconv.ParseUint(strings.Replace(hm[1], ",", "", -1), 10, 64)
80+
if err != nil {
81+
return 0, err
82+
}
83+
} else if ut[3] == "hr," || ut[3] == "hrs," {
84+
hours, err = strconv.ParseUint(ut[2], 10, 64)
85+
if err != nil {
86+
return 0, err
87+
}
88+
} else if ut[3] == "mins," {
89+
minutes, err = strconv.ParseUint(ut[2], 10, 64)
90+
if err != nil {
91+
return 0, err
92+
}
93+
} else if _, err := strconv.ParseInt(ut[3], 10, 64); err == nil && strings.Contains(ut[2], ":") {
94+
// Split field 2 into hours and minutes
95+
hm := strings.Split(ut[2], ":")
96+
hours, err = strconv.ParseUint(hm[0], 10, 64)
97+
if err != nil {
98+
return 0, err
99+
}
100+
minutes, err = strconv.ParseUint(strings.Replace(hm[1], ",", "", -1), 10, 64)
101+
if err != nil {
102+
return 0, err
103+
}
74104
}
75105

76106
// Stack them all together as minutes
77107
total_time := (days * 24 * 60) + (hours * 60) + minutes
78108

79-
return uint64(total_time), nil
109+
return total_time, nil
80110
}
81111

82112
// This is a weak implementation due to the limitations on retrieving this data in AIX

0 commit comments

Comments
 (0)