From 8f3d0d0c57ffadec3a2fd2c54dcc7578e641a8c8 Mon Sep 17 00:00:00 2001 From: Aidan Date: Thu, 6 Jun 2024 14:36:40 +1200 Subject: [PATCH] Correctly parse new uptime case at zero minutes, has been tested against all test cases --- host/host_aix.go | 54 ++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 34 insertions(+), 20 deletions(-) diff --git a/host/host_aix.go b/host/host_aix.go index 6c3c085..d665609 100644 --- a/host/host_aix.go +++ b/host/host_aix.go @@ -63,7 +63,7 @@ func UptimeWithContext(ctx context.Context) (uint64, error) { ut := strings.Fields(string(out[:])) // Initialise variables - var days, hours, minutes uint64 + var days, hours, mins uint64 // Check if days are specified and parse them if ut[3] == "day," || ut[3] == "days," { @@ -73,39 +73,53 @@ func UptimeWithContext(ctx context.Context) (uint64, error) { } } - // Identify and parse hours and minutes based on the format present switch { - case ut[4] == "hrs," || ut[4] == "hr,": - // Only hours are given, and they are placed in ut[4] - hours, err = strconv.ParseUint(ut[2], 10, 64) + case ut[3] == "day," || ut[3] == "days,": + days, err = strconv.ParseUint(ut[2], 10, 64) if err != nil { return 0, err } - case strings.Contains(ut[4], ":"): - // Hours and minutes are specified in the format "hours:minutes" - hm := strings.Split(ut[4], ":") - hours, err = strconv.ParseUint(hm[0], 10, 64) + + // day provided along with a single hour or hours + // ie: up 2 days, 20 hrs + if ut[5] == "hr," || ut[5] == "hrs," { + hours, err = strconv.ParseUint(ut[4], 10, 64) + if err != nil { + return 0, err + } + } + + // alternatively day provided with hh:mm + // ie: up 83 days, 18:29 + if strings.Contains(ut[4], ":") { + hm := strings.Split(ut[4], ":") + hours, err = strconv.ParseUint(hm[0], 10, 64) + if err != nil { + return 0, err + } + mins, err = strconv.ParseUint(strings.Trim(hm[1], ","), 10, 64) + if err != nil { + return 0, err + } + } + case ut[3] == "hr," || ut[3] == "hrs,": + hours, err = strconv.ParseUint(ut[2], 10, 64) if err != nil { return 0, err } - minutes, err = strconv.ParseUint(strings.Trim(hm[1], ","), 10, 64) + case ut[3] == "mins," || ut[3] == "mins,": + mins, err = strconv.ParseUint(ut[2], 10, 64) if err != nil { return 0, err } - default: - // No explicit hours or "hours:minutes" format after days, check if minutes are given directly - if ut[3] == "mins," { - minutes, err = strconv.ParseUint(ut[2], 10, 64) - if err != nil { - return 0, err - } - } } + total_time := (days * 24 * 60) + (hours * 60) + mins + // Calculate total time in minutes - totalTime := (days * 24 * 60) + (hours * 60) + minutes + log.Println("days / hrs / mins ", days, hours, mins, total_time) - return totalTime, nil + return total_time, nil } // This is a weak implementation due to the limitations on retrieving this data in AIX