Skip to content

Conversation

@Dylan-M
Copy link

@Dylan-M Dylan-M commented Dec 16, 2025

Description

This PR replaces the AIX host module's uptime determination method from parsing the uptime command output to a more reliable approach using ps -o etimes -p 1. This directly queries the process table for PID 1's elapsed time rather than relying on variable human-readable output formatting.

Fixes #1809 (comment)

Motivation

The previous implementation parsing uptime command output had several limitations:

  • Variable and complex output format requiring extensive pattern matching
  • Reliance on a utility command that's designed for human consumption
  • Difficulty handling edge cases and format variations reliably

Direct access to the process table via ps etimes is more robust and efficient.

Technical Changes

Implementation

  • UptimeWithContext(): Now executes ps -o etimes -p 1 instead of uptime
  • Output Validation: Added check to ensure at least 2 rows (header + data)
  • parseUptime(): Refactored to handle three possible etimes output formats:
    • DAYS-HOURS:MINUTES:SECONDS (e.g., 124-01:40:39)
    • HOURS:MINUTES:SECONDS (e.g., 15:03:02)
    • MINUTES:SECONDS (e.g., 01:02) - from just-rebooted systems where hours are omitted when zero
  • Error Handling: Gracefully returns 0 for any malformed input

Testing

  • Updated all test cases to use the new etimes format
  • Added comprehensive test coverage for all three time format variations
  • Updated invalid input test cases to validate the new parsing logic

Files Changed

  • host/host_aix.go: Updated UptimeWithContext() and parseUptime()
  • host/host_aix_test.go: Updated test cases for new format

Backward Compatibility

The function signature and behavior remain unchanged from the caller's perspective. The return value (uptime in minutes) is consistent with the previous implementation.

Previously, the AIX host module parsed the output of the `uptime` command
to determine system uptime. This approach had limitations in terms of
reliability and parsing complexity due to the variable output format of
the uptime command.

This change replaces the uptime-based approach with a more reliable method:
querying PID 1's elapsed time using `ps -o etimes -p 1`. This directly
accesses the process table rather than relying on human-readable output.

The new implementation handles three possible etimes output formats:
- DAYS-HOURS:MINUTES:SECONDS (e.g., "124-01:40:39")
- HOURS:MINUTES:SECONDS (e.g., "15:03:02")
- MINUTES:SECONDS (e.g., "01:02") - from just-rebooted systems where
  hours are omitted when zero

Changes:
- Updated UptimeWithContext() to execute `ps -o etimes -p 1`
- Added validation to ensure at least 2 rows of output (header + data)
- Modified parseUptime() to handle the new etimes format with flexible
  time component parsing
- Added graceful error handling that returns 0 for malformed input
- Updated function comments to document the new approach and formats

Tests:
- Replaced all test cases to use the new etimes format
- Added test coverage for all three time format variations
- Updated invalid input test cases to reflect new parsing logic
- Convert if-else chain to switch statement in parseUptime() to satisfy gocritic
- Format files with goimports/gofmt to resolve gci formatting issues

These changes address all remaining linter warnings reported by golangci-lint.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant