iliaal/statgrab
Composer 安装命令:
pie install iliaal/statgrab
包简介
PHP extension wrapping libstatgrab, the cross-platform system-statistics library. CPU, memory, disk I/O, filesystems, network interfaces, processes, users.
README 文档
README
A native PHP extension wrapping libstatgrab, the cross-platform system-statistics library. Originally released to PECL in 2006 against libstatgrab 0.6; the 2.0.0 line is a full modernization for the PHP 8.0+ era against libstatgrab 0.92+.
Supports PHP 8.0 through 8.5 on glibc Linux, musl, macOS, and *BSD.
The Problem
Reading system stats from PHP usually means one of three bad options:
- Shell out to
top,vmstat,df,psand parse the output. Fragile on every OS update, andfork/execoverhead adds up if you poll on a schedule. - Read
/procby hand. Linux-only. Forces hand-rolled regex for every statistic, and the format drifts between kernel releases. - Pull in a heavy monitoring framework. Overkill if all you need is a CPU number for a health endpoint.
libstatgrab is the right primitive: cross-platform, well-tested, in the package manager of every modern Unix. But the 2006 PECL binding has not shipped a PHP 8 build, and its 2006 BC quirks (stringified counters, swapped page-stat keys, a flat name_list for users) made the old extension awkward even when you could compile it.
✨ Key Features
| Feature | Notes |
|---|---|
| Cross-platform | glibc Linux, musl, macOS, FreeBSD |
| Procedural + OO API | 2006 sg_* function names preserved; modern Statgrab class on top |
| Bundled libstatgrab option | Vendored 0.92.1 with a leak-fix patch; resulting .so has no runtime dependency on libstatgrab.so |
| Modern types | Counters returned as 64-bit int, not stringified numbers |
| Modern PHP errors | E_WARNING on library failure, ArgumentCountError for arg-count violations |
| BC-preserved 2006 names | Drop-in for callers of the original PECL extension, with the 2.0 BC notes documented below |
⚖️ Why native
The case for a native extension is the failure modes of the alternatives:
exec("top ...")and friends fork a process per call. The overhead is real if you poll every few seconds, and the output format drifts between OS releases.- Hand-parsing
/procties you to Linux and to whatever the kernel decided to print this year. Each file (/proc/meminfo,/proc/loadavg,/proc/diskstats,/proc/net/dev) has its own format and edge cases. - Calling out to a stats daemon adds a network hop and a daemon to deploy.
statgrab calls libstatgrab in-process. libstatgrab handles the per-OS path (Linux /proc, FreeBSD kvm, macOS host_* APIs) and exposes a single typed surface. The extension wraps that surface with no allocation per call beyond the result array.
🚀 Quick Start
PIE (recommended on PHP 8.x)
PIE is the PHP Foundation's PECL successor.
It installs from Packagist, builds against the active php-config, and
produces a loadable .so. Make sure libstatgrab is installed first
(see the From-source section below for the system package names), then:
pie install iliaal/statgrab
Then add extension=statgrab to your php.ini.
PECL
The package remains in the PECL channel for legacy installers:
pecl install statgrab
From source
sudo apt install libstatgrab-dev # Debian/Ubuntu # OR: brew install libstatgrab # macOS # OR: pkg install libstatgrab # FreeBSD phpize ./configure --with-statgrab make sudo make install
Then add extension=statgrab to your php.ini.
config.m4 resolves libstatgrab through pkg-config first; if that
fails it falls back to a path probe (/usr and /usr/local). Pass
--with-statgrab=<prefix> to point at a custom install.
Bundled libstatgrab (statically linked, leak-fixed)
The repo carries a vendored copy of libstatgrab 0.92.1 under
vendor/libstatgrab/ with one local patch (see
vendor/libstatgrab/LOCAL_PATCHES.md) that fixes a process-exit leak
upstream hasn't released yet. To use it:
(cd vendor/libstatgrab && ./configure --enable-static --disable-shared --without-ncurses --with-pic && make) phpize ./configure --with-statgrab=bundled make
The resulting .so has no libstatgrab.so runtime dependency.
The vendored libstatgrab tree stays LGPL 2.1+ (see LICENSE.libstatgrab);
the extension code stays PHP-3.01 (see LICENSE). Dynamic-link or
static-link, neither license infects the other.
API
Procedural
The 2006 function names are preserved.
sg_cpu_percent_usage(): array|false // user/kernel/idle/iowait/swap/nice (%) sg_cpu_totals(): array|false // cumulative jiffies + ctx switches/syscalls/IRQs sg_cpu_diff(): array|false // jiffies since last call sg_diskio_stats(): array|false // [diskname => [read, written, time_frame]] sg_diskio_stats_diff(): array|false sg_fs_stats(): array|false // mounted filesystems with size/used/inodes/... sg_general_stats(): array|false // os_name, hostname, uptime, ncpus, ... sg_load_stats(): array|false // min1, min5, min15 sg_memory_stats(): array|false // total, free, used, cache (bytes) sg_swap_stats(): array|false // total, free, used (bytes) sg_network_stats(): array|false // [ifname => sent/received/packets/...] sg_network_stats_diff(): array|false sg_page_stats(): array|false // pages_in, pages_out (cumulative) sg_page_stats_diff(): array|false sg_process_count(): array|false // total, running, sleeping, stopped, zombie sg_process_stats(?int $sort = null, int $limit = 0): array|false sg_user_stats(): array|false // [{login_name, device, pid, login_time, ...}] sg_network_iface_stats(): array|false // [ifname => {speed, duplex, active}]
Object-oriented
$sg = new Statgrab(); $sg->cpu(); $sg->host(); $sg->memory(); $sg->processes(Statgrab::SORT_CPU, 10); $sg->disks(diff: true);
Class constants:
Statgrab::DUPLEX_FULL | DUPLEX_HALF | DUPLEX_UNKNOWNStatgrab::SORT_NAME | PID | UID | GID | SIZE | RES | CPU | TIMEStatgrab::STATE_RUNNING | SLEEPING | STOPPED | ZOMBIE | UNKNOWN
The SG_* global constants from 2006 are still defined for BC.
Errors
Library-side errors emit E_WARNING with the libstatgrab error string
and code, and the function returns false. The OO surface follows the
same convention. Argument-count violations on no-arg functions throw
ArgumentCountError per modern PHP convention.
Notable 2.0 BC breaks
sg_user_stats()returns per-user records, not a flat array of usernames. The underlyingname_listfield was removed from libstatgrab 0.91+. Migrate callers to readlogin_namefrom each record.- Numeric counters (memory totals, fs sizes, jiffies) are returned as
intinstead of stringified numbers. The 2006 release stringified viasnprintf("%lld")because 32-bit PHP couldn't hold them; modern 64-bitzend_longdoes. sg_page_stats()/sg_page_stats_diff()were swapped in 2006 and are now correct.sg_process_stats()fieldsgidandegidare now distinct fromuidandeuid(2006 had a copy-paste bug returning uid/euid for both).
See CHANGELOG.md for the full list.
🔗 Native PHP extensions
Companion native PHP extensions:
- php_excel: native Excel I/O via LibXL. 7-10× faster than PhpSpreadsheet, full XLS/XLSX with formulas, formatting, and styling.
- mdparser: native CommonMark + GFM markdown parser via md4c. 15-30× faster than pure-PHP libraries.
- php_clickhouse: native ClickHouse client speaking the wire protocol directly. Picks up where SeasClick left off.
- pdo_duckdb: PDO driver for DuckDB, analytical SQL in your PHP stack.
- fastjson: drop-in faster
ext/json, backed by yyjson. 6× encode, 2.7× decode, 5× validate. - phpser: decoder-optimized binary serializer for cache workloads. Faster than igbinary on packed numerics and DTO batches.
- fast_uuid: high-throughput UUID generation (v1/v4/v7), batched CSPRNG and SIMD hex formatter, ramsey-compatible API.
- fastchart: native chart-rendering extension. 38 chart types behind one fluent OO API, SVG-canonical with PNG/JPG/WebP and optional PDF output.
- phonetic: native phonetic name matching (Double Metaphone, Beider-Morse, Daitch-Mokotoff, NYSIIS, Match Rating), the encoders PHP core lacks.
License
If this saved you from parsing /proc by hand, ⭐ star it!
iliaal/statgrab 适用场景与选型建议
iliaal/statgrab 是一款 基于 C 开发的 Composer 扩展包,目前已累计 30 次下载、GitHub Stars 达 1, 最近一次更新时间为 2026 年 04 月 28 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「monitoring」 「pecl」 「statistics」 「system」 「pie」 「php-extension」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 iliaal/statgrab 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 iliaal/statgrab 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 iliaal/statgrab 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
PECL installing script with cache for Travis CI
Native PHP drop-in replacement for the PECL OAuth extension
Helper to handle stats table with multiple formatter
The "View Counter" bundle
A Symfony bundle for chameleon-system/sanitycheck
SPSS is a php-based implementation of IBM SPSS Statistics Standard. (Read/write SPSS, PSPP .sav files)
统计信息
- 总下载量: 30
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 40
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: PHP-3.01
- 更新时间: 2026-04-28
