linux-top-parser-rs
A lightweight Rust library for parsing Linux top command output into structured data types and JSON.
Features
- Structured parsing — Converts
topoutput (single or multi-snapshot) into typed Rust structs - JSON serialization — Built-in support for converting parsed data to JSON
- Robust parsing — Uses
nomparser combinator library for reliable parsing - Ordered process data — Preserves process table column order using
indexmap - Ready-to-use examples — Includes a working example program with sample data
Installation
Add to your Cargo.toml:
[]
= "0.1.0"
Quick Start
use parse_multiple_top_info_blocks;
use fs;
Running the Example
The repository includes a complete example program:
# Parse the default sample file
# Parse a custom top output file
Example implementation: examples/parse_top_output.rs
Testing
Run the full test suite:
The tests use sample top outputs from the data/ directory and validate against expected JSON outputs like data/multi_expected.json and data/multi_all_expected.json.
Running Tests with Coverage
# Install cargo-llvm-cov
# Generate coverage report
Data Structures
All types are defined in src/parser/using_nom.rs. Key structures:
Supporting Types
TaskStates— Counts for total, running, sleeping, stopped, and zombie processesCpuStates— CPU time percentages (user, system, nice, idle, iowait, hardware interrupts, software interrupts, steal)PhysicalMemory— Physical RAM metrics (total, free, used, buffers/cache)VirtualMemory— Swap space metricsLoadAverage&UpTimeAndLoadAverage— System load and uptime from the header line