-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat: Add AIX platform support to common package with uptime and boot time functions #1979
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| // SPDX-License-Identifier: BSD-3-Clause | ||
| //go:build aix | ||
|
|
||
| package common | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "strconv" | ||
| "strings" | ||
| ) | ||
|
|
||
| func BootTimeWithContext(ctx context.Context, invoke Invoker) (btime uint64, err error) { | ||
| ut, err := UptimeWithContext(ctx, invoke) | ||
| if err != nil { | ||
| return 0, err | ||
| } | ||
|
|
||
| if ut <= 0 { | ||
| return 0, errors.New("uptime was not set, so cannot calculate boot time from it") | ||
| } | ||
|
|
||
| return timeSince(ut), nil | ||
| } | ||
|
|
||
| // Uses ps to get the elapsed time for PID 1 in DAYS-HOURS:MINUTES:SECONDS format. | ||
| // Examples of ps -o etimes -p 1 output: | ||
| // 124-01:40:39 (with days) | ||
| // 15:03:02 (without days, hours only) | ||
| // 01:02 (just-rebooted systems, minutes and seconds) | ||
| func UptimeWithContext(ctx context.Context, invoke Invoker) (uint64, error) { | ||
| out, err := invoke.CommandWithContext(ctx, "ps", "-o", "etimes", "-p", "1") | ||
| if err != nil { | ||
| return 0, err | ||
| } | ||
|
|
||
| lines := strings.Split(strings.TrimSpace(string(out)), "\n") | ||
| if len(lines) < 2 { | ||
| return 0, errors.New("ps output has fewer than 2 rows") | ||
| } | ||
|
|
||
| // Extract the etimes value from the second row, trimming whitespace | ||
| etimes := strings.TrimSpace(lines[1]) | ||
| return ParseUptime(etimes), nil | ||
| } | ||
|
|
||
| // Parses etimes output from ps command into total seconds. | ||
| // Handles formats like: | ||
| // - "124-01:40:39" (DAYS-HOURS:MINUTES:SECONDS) | ||
| // - "15:03:02" (HOURS:MINUTES:SECONDS) | ||
| // - "01:02" (MINUTES:SECONDS, from just-rebooted systems) | ||
| func ParseUptime(etimes string) uint64 { | ||
| var days, hours, mins, secs uint64 | ||
|
|
||
| // Check if days component is present (contains a dash) | ||
| if strings.Contains(etimes, "-") { | ||
| parts := strings.Split(etimes, "-") | ||
| if len(parts) != 2 { | ||
| return 0 | ||
| } | ||
|
|
||
| var err error | ||
| days, err = strconv.ParseUint(parts[0], 10, 64) | ||
| if err != nil { | ||
| return 0 | ||
| } | ||
|
|
||
| // Parse the HH:MM:SS portion (after days, must have 3 parts) | ||
| etimes = parts[1] | ||
| timeParts := strings.Split(etimes, ":") | ||
| if len(timeParts) != 3 { | ||
| return 0 | ||
| } | ||
|
|
||
| var err2 error | ||
| hours, err2 = strconv.ParseUint(timeParts[0], 10, 64) | ||
| if err2 != nil { | ||
| return 0 | ||
| } | ||
|
|
||
| mins, err2 = strconv.ParseUint(timeParts[1], 10, 64) | ||
| if err2 != nil { | ||
| return 0 | ||
| } | ||
|
|
||
| secs, err2 = strconv.ParseUint(timeParts[2], 10, 64) | ||
| if err2 != nil { | ||
| return 0 | ||
| } | ||
| } else { | ||
| // Parse time portions (either HH:MM:SS or MM:SS) when no days present | ||
| timeParts := strings.Split(etimes, ":") | ||
| switch len(timeParts) { | ||
| case 3: | ||
| // HH:MM:SS format | ||
| var err error | ||
| hours, err = strconv.ParseUint(timeParts[0], 10, 64) | ||
| if err != nil { | ||
| return 0 | ||
| } | ||
|
|
||
| mins, err = strconv.ParseUint(timeParts[1], 10, 64) | ||
| if err != nil { | ||
| return 0 | ||
| } | ||
|
|
||
| secs, err = strconv.ParseUint(timeParts[2], 10, 64) | ||
| if err != nil { | ||
| return 0 | ||
| } | ||
| case 2: | ||
| // MM:SS format (just-rebooted systems) | ||
| var err error | ||
| mins, err = strconv.ParseUint(timeParts[0], 10, 64) | ||
| if err != nil { | ||
| return 0 | ||
| } | ||
|
|
||
| secs, err = strconv.ParseUint(timeParts[1], 10, 64) | ||
| if err != nil { | ||
| return 0 | ||
| } | ||
| default: | ||
| return 0 | ||
| } | ||
| } | ||
|
|
||
| // Convert to total seconds | ||
| totalSeconds := (days * 24 * 60 * 60) + (hours * 60 * 60) + (mins * 60) + secs | ||
| return totalSeconds | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.