如何收集和导出指标?介绍Prometheus的exporter
解读
面试官想确认三件事:
- 你是否理解“指标”在可观测体系里的定位(监控 vs 日志 vs 追踪)。
- 你是否能把“指标产生 → 暴露 → 抓取”这一整套流程用 Bash 落地,而不是只会背概念。
- 你是否清楚 Prometheus 生态的 exporter 角色,以及在国内常见的合规、内网、国产化场景下如何裁剪。
一句话:用最小依赖(纯 Bash+busybox)把业务或系统指标变成 Prometheus 能拉取的 /metrics 端点,并解释为什么这么干。
知识点
- Prometheus 文本协议格式:
# HELP/# TYPE行 + 样本行,UTF-8,LF 结尾,时间戳可省略。 - 指标类型:counter、gauge、histogram、summary,Bash 里一般只造 counter 和 gauge。
- 暴露方式:
- 拉模式:exporter 起监听端口,Prometheus 定时抓取。
- 推模式:pushgateway,适合短作业。
- 端口与路径:默认 9100/metrics,但国产化主机常要求 8080-9000 区间,避免特权端口。
- 安全合规:
- 内网环境关闭 TLS 也得加 basic-auth 或 iptables 白名单。
- 信创机器可能没 systemd,用 rc.local 或 crontab
@reboot保活。
- Bash 技术点:
- 使用内置
(( ))做整数运算,避免 fork。 - 用
printf而不是echo保证可移植性。 - 用
coproc或netcat -lk实现零依赖多线程 HTTP。 - 信号陷阱
trap 'rm -f $tmpfile; exit' INT TERM保证临时文件清理。
- 使用内置
答案
下面给出一个“纯 Bash 版 node_exporter 最小实现”,可直接放到国产麒麟 aarch64 服务器上跑,不需要 golang 环境。文件名为 bash_exporter.sh,启动后监听 9100,Prometheus 配一条 job 即可抓取。
#!/usr/bin/env bash
# 文件名: bash_exporter.sh
# 依赖: bash4+、nc (busybox 版即可)、awk、/proc 文件系统
set -euo pipefail
PORT=${PORT:-9100}
TEMP_DIR=$(mktemp -d /tmp/bash_exporter.XXXXXX)
METRICS="$TEMP_DIR/metrics.prom"
trap "rm -rf $TEMP_DIR; exit 0" INT TERM EXIT
update_metrics(){
# 1. CPU 使用率(counter 模式,单位秒)
read -r u n s i iow irq si st < /proc/stat
cpu_total=$((u+n+s+i+iow+irq+si+st))
printf "node_cpu_seconds_total{mode=\"user\"} %u\n" "$u" > "$METRICS"
printf "node_cpu_seconds_total{mode=\"system\"} %u\n" "$n" >> "$METRICS"
printf "node_cpu_seconds_total{mode=\"idle\"} %u\n" "$i" >> "$METRICS"
# 2. 内存信息(gauge)
awk '/^MemAvailable:/ {print "node_memory_MemAvailable_bytes " $2*1024}' /proc/meminfo >> "$METRICS"
awk '/^MemTotal:/ {print "node_memory_MemTotal_bytes " $2*1024}' /proc/meminfo >> "$METRICS"
# 3. 磁盘使用率(gauge)
df / | awk 'NR==2 {printf "node_filesystem_avail_bytes{mountpoint=\"/\"} %s\n", $4*1024}' >> "$METRICS"
# 4. 自定义业务指标:假设统计 /var/log/nginx/access.log 的 5xx
if [[ -f /var/log/nginx/access.log ]]; then
cnt=$(awk '($9>=500)' /var/log/nginx/access.log | wc -l)
printf "nginx_http_requests_5xx_total %d\n" "$cnt" >> "$METRICS"
fi
}
# 每 5 秒后台更新一次
while :; do update_metrics; sleep 5; done &
# 用 netcat 提供 HTTP/1.1 最小响应
while :; do
{ echo -ne "HTTP/1.1 200 OK\r\nContent-Type: text/plain; version=0.0.4\r\n\r\n"; cat "$METRICS"; } \
| nc -l -p "$PORT" -q 1
done
启动与验证:
chmod +x bash_exporter.sh
nohup ./bash_exporter.sh &
curl localhost:9100/metrics | grep node_cpu
Prometheus 侧 prometheus.yml 增加:
scrape_configs:
- job_name: 'bash_node'
static_configs:
- targets: ['10.0.0.31:9100']
至此,指标收集→导出→抓取全链路打通。若需 histogram 或 summary,可在脚本里用 awk 计算分位,再按 Prometheus 文本格式输出 _bucket、_sum、_count 系列即可。
拓展思考
- 性能与精度:
Bash 版 exporter 适合指标量 < 500 样本/轮的场景;若需毫秒级精度或上万时间序列,应切到官方 node_exporter 或自己写 Go/Python exporter。 - 配置化:
把“采集项”写成函数数组,通过 YAML 片段+yq动态加载,实现“零编译”新增指标。 - 安全加固:
国内等保 2.0 要求“最小化开放端口”,可把 exporter 监听在 127.0.0.1:9100,再通过 nginx 反向代理到 443,Prometheus 走 mTLS 拉取。 - 国产化适配:
统信 UOS 没有 systemd,用crontab @reboot启动;若 CPU 为龙芯,需把nc换成socat避免 busybox 版本缺陷。 - 云原生场景:
在容器里可把脚本打到 distroless 镜像,size < 2 MB,sidecar 方式注入,实现“无 root、无 shell” 限制下的指标暴露。