prometheus/procfs
GitHub: prometheus/procfs
该项目提供 Go 函数库,用于从 Linux 伪文件系统 /proc 和 /sys 中结构化地检索系统、内核和进程指标。
Stars: 875 | Forks: 378
# procfs
本包提供了从伪文件系统 /proc 和 /sys 中检索系统、内核和进程指标的函数。
*警告*:本包目前正在开发中。其 API 可能仍会以向后不兼容的方式发生更改,且不另行警告。使用风险自负。
[](https://pkg.go.dev/github.com/prometheus/procfs)
[](https://github.com/prometheus/procfs/actions/workflows/ci.yml)
[](https://goreportcard.com/report/github.com/prometheus/procfs)
## 用法
procfs 库根据收集的数据是来自 /proc、/sys 还是两者兼有,按包进行了组织。每个包都包含一个 `FS` 类型,用于表示 /proc、/sys 或两者的路径。例如,CPU 统计信息是从 `/proc/stat` 收集的,可以通过根 procfs 包获取。首先,初始化 proc 文件系统挂载点,然后读取统计信息。
```
fs, err := procfs.NewFS("/proc")
stats, err := fs.Stat()
```
一些子包(例如 `blockdevice`)需要同时访问 proc 和 sys 文件系统。
```
fs, err := blockdevice.NewFS("/proc", "/sys")
stats, err := fs.ProcDiskstats()
```
## 包的组织结构
本项目中的包根据 (1) 数据是来自 `/proc` 还是 `/sys` 文件系统,以及 (2) 要检索的信息类型进行组织。例如,大多数进程信息可以通过根 `procfs` 包中的函数收集。有关块设备(如磁盘驱动器)的信息可在 `blockdevices` 子包中找到。
## 构建与测试
procfs 库旨在作为其他应用程序的一部分进行构建,因此没有可分发的二进制文件。不过,大部分 API 都包含单元测试,可以使用 `make test` 运行。
### 更新测试夹具
procfs 库包含一组测试夹具,其中包含了来自 `/proc` 和 `/sys` 文件系统的许多示例文件。这些夹具以 [ttar](https://github.com/ideaship/ttar) 文件的形式提供,并在测试期间自动解压。要添加/更新测试夹具,首先删除现有目录,然后使用 `make testdata/fixtures/.unpacked` 或直接使用 `make test` 解压 ttar 文件,从而确保 `testdata/fixtures` 目录是最新的。
```
rm -rf testdata/fixtures
make test
```
接下来,对 `testdata/fixtures` 目录中解压出的文件进行所需的更改。更改完成后,运行 `make update_fixtures`,基于更新后的 `fixtures` 目录创建新的 `fixtures.ttar` 文件。最后,使用 `git diff testdata/fixtures.ttar` 验证更改。
标签:EVTX分析, 日志审计