macmon 通过 HTTP 暴露 Apple Silicon 性能指标,接入 Prometheus 与 Grafana 实现可视化监控。
macmon 负责免 sudo 采集 CPU/GPU/温度/内存指标,/metrics 端点以 Prometheus 文本格式输出,Grafana 负责可视化。
方案简介
macmon 是一款面向 Apple Silicon Mac 的免 sudo 性能监控工具,用 Rust 编写。它通过一个私有 macOS API 读取实时 CPU / GPU / ANE 功耗、温度和内存统计——与 powermetrics 暴露的是同一份数据——但不需要 root 权限。
本方案的核心组合是:macmon 以 HTTP 服务器模式运行(macmon serve),通过 /metrics 端点以 Prometheus 文本格式暴露指标,再由 Prometheus 抓取、Grafana 展示,构成一套本地化的 Mac 硬件监控仪表盘。仓库还在 examples/grafana 提供了包含 Prometheus 和 Grafana 的本地演示栈。该方案适合想在 Grafana 中长期追踪 Apple Silicon 功耗、温度、风扇、内存等指标的开发者与运维人员。
亮点与能力
- 🚫 无需 sudo 即可运行
- ⚡ 实时 CPU / GPU / ANE 功耗
- 📊 每个集群的 CPU 频率缩放比与活跃比
- 💾 RAM / Swap 使用情况
- 📈 带平均值和最大值的历史图表
- 🌡️ CPU / GPU 平均温度
- 🎨 6 种可切换配色主题
- 🪟 可以在小窗口中显示
/metrics端点输出 Prometheus 文本格式指标,含温度、功耗、风扇转速、CPU 频率等
组成与分工
- macmon:核心采集器,免 sudo 读取 Apple Silicon 的功耗、温度、内存、风扇等指标,并以 HTTP 服务形式暴露;
- Prometheus:抓取 macmon 的
/metrics端点(Prometheus 文本格式),存储时间序列数据; - Grafana:可视化 Prometheus 中的指标,
examples/grafana提供本地演示栈; - launchd:macOS 服务管理,
macmon serve --install会创建~/Library/LaunchAgents/com.macmon.plist,登录自启、崩溃自动重启; - jq(可选):配合
pipe子命令对 JSON 输出进行格式化查看。
前置要求
- Apple Silicon Mac(macOS)
- 推荐通过 brew 安装 macmon:
sh
brew install macmon
其他安装方式:
sh
sudo port install macmon
sh
cargo install macmon
sh
nix-env -i macmon
实施步骤
1. 安装 macmon
sh
brew install macmon
2. 启动 HTTP 服务
sh
macmon serve # default port 9090, interval 1000ms
macmon serve --host 127.0.0.1 # listen on localhost only
macmon serve -p 8080 # custom port
macmon serve -i 500 # sampling interval 500ms
macmon serve & # run in background
3. (可选)安装为 launchd 服务以开机自启
sh
macmon serve --install # install and start (default port 9090)
macmon serve --port 8080 --install # with custom port
macmon serve --host 127.0.0.1 --install # listen on localhost only
macmon serve --uninstall # stop and remove
4. 接入 Prometheus 与 Grafana
/metrics 端点以 Prometheus 格式暴露指标,参考 examples/grafana 中提供的含 Prometheus 和 Grafana 的本地演示栈搭建可视化面板。
注意事项与常见问题
- 默认端口 9090,默认采样间隔 1000ms,均可通过参数调整;
macmon serve --install创建的 launchd agent 位于~/Library/LaunchAgents/com.macmon.plist,登录自启并在崩溃时重启;卸载用macmon serve --uninstall;--host 127.0.0.1可让服务仅监听本机;macmon stress的默认pulse模式在一半逻辑 CPU 上以固定 50% 占空比生成可预测的周期性 CPU 负载;cpu/all模式默认使用全部逻辑 CPU,可用--workers限制;--workers在gpu模式下无效。pipe的-s/--samples默认为 0 表示无限运行,-i/--interval默认 1000ms。
优缺点
- ✓ 免 sudo 读取功耗温度指标
- ✓ 提供 launchd 自启动保活
- ✕ 仅支持 Apple Silicon Ma
- ✕ 依赖私有 macOS API
出处
本方案挖掘自开源项目 vladkens/macmon,方案内容与实施命令均来自其 README 原文。
本方案由真实开源项目挖掘整理,实施命令均来自其 README 原文,安装使用请遵循项目开源协议。