《Prometheus Exporter 与 Pushgateway》
Node Exporter 采集 Linux 服务器硬件与内核相关指标;Pushgateway 用于接收短生命周期任务的 Push 上报。
1 Node Exporter 部署
(1)下载:
wget https://github.com/prometheus/node_exporter/releases/download/v1.0.1/node_exporter-1.0.1.linux-amd64.tar.gz
tar zxf node_exporter-1.0.1.linux-amd64.tar.gz
cd node_exporter-1.0.1.linux-amd64
(2)启动:
./node_exporter --web.listen-address=":8503"
(3)检查指标:
# 查看全部 metric
curl 127.0.0.1:8503/metrics
# 只看 node_ 前缀的系统指标
curl http://localhost:8503/metrics | grep "node_"
2 主要指标族
Node Exporter 输出的指标按家族分类(metric 名称以 # HELP 注释说明语义):
node_cpu_seconds_total CPU 使用时间(含 mode 标签:idle/user/system/iowait...)
node_memory_MemTotal_bytes 总内存
node_memory_MemFree_bytes 空闲内存
node_memory_MemAvailable_bytes 可用内存
node_filesystem_size_bytes 文件系统容量
node_filesystem_avail_bytes 文件系统可用容量
node_filesystem_free_bytes 文件系统剩余空间
node_disk_read_bytes_total 磁盘累计读字节
node_disk_written_bytes_total 磁盘累计写字节
node_disk_reads_completed_total 磁盘累计完成读次数
node_disk_writes_completed_total 磁盘累计完成写次数
node_disk_io_time_seconds_total 磁盘 IO 时间
node_network_receive_bytes_total 网卡累计接收字节
node_network_transmit_bytes_total 网卡累计发送字节
node_network_up 网卡是否在线
node_load1 / node_load5 / node_load15 系统负载
node_boot_time_seconds 启动时间
node_procs_running 运行中的进程数
node_procs_blocked 阻塞的进程数
node_filefd_allocated 已分配文件描述符
process_open_fds 进程已打开文件描述符
3 常用 PromQL 查询
# CPU 使用率(排除 idle)
100 - (avg(rate(node_cpu_seconds_total{mode="idle"}[5m])) * 100)
# 内存使用率
(1 - node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes) * 100
# 磁盘使用率
(node_filesystem_size_bytes - node_filesystem_avail_bytes) / node_filesystem_size_bytes * 100
# 网卡接收速率
rate(node_network_receive_bytes_total[5m])
4 Pushgateway 部署
Docker 方式:
docker run --rm -itd -p 9091:9091 --name pushgateway --privileged=true --hostname=pushgateway prom/pushgateway
直接部署:
(1)下载:
wget https://github.com/prometheus/pushgateway/releases/download/v1.2.0/pushgateway-1.2.0.linux-amd64.tar.gz
tar zxf pushgateway-1.2.0.linux-amd64.tar.gz
cd pushgateway-1.2.0.linux-amd64
(2)启动:
./pushgateway \
--web.listen-address=0.0.0.0:8507 \
--web.enable-admin-api \
--web.telemetry-path="/metrics" \
--log.format=json \
--log.level=debug \
--persistence.file=./data.txt \
--persistence.interval=1m
(3)在 Prometheus 的 prometheus.yml 中增加 job:
scrape_configs:
- job_name: 'pushgateway'
static_configs:
- targets: ['<pushgateway-ip>:8507']
5 往 Pushgateway Push 数据
ip=127.0.0.1
port=8507
# 单条指标,注意不能带时间戳,否则报错
metric='http_requests_total{method="post",code="200"} 10'
metric=$(cat << EOF
# HELP http_requests_total The total number of HTTP requests.
# TYPE http_requests_total counter
http_requests_total{method="post",code="200"} 10
http_requests_total{method="post",code="400"} $port
EOF
)
echo "$metric" | curl --data-binary @- http://$ip:$port/metrics/job/my_job
cat <<EOF | curl --data-binary @- http://$ip:$port/metrics/job/some_job/instance/some_instance
# TYPE some_metric counter
some_metric{label="val1"} 42
# TYPE another_metric gauge
# HELP another_metric Just an example.
another_metric 2398.283
EOF
注意:pushgateway 不能添加自定义时间戳,否则会报:
pushed metrics are invalid or inconsistent with existing metrics: pushed metrics must not have timestamps
6 删除已推送的数据
# 删除某个实例的所有数据
curl -X DELETE http://<pushgateway>:9091/metrics/job/some_job/instance/some_instance
# 删除指定 job 的数据
curl -X DELETE http://<pushgateway>:9091/metrics/job/some_job
7 常见 exporter 列表
- MySQL:https://github.com/prometheus/mysqld_exporter
- Memcached:https://github.com/prometheus/memcached_exporter
- Redis:https://github.com/oliver006/redis_exporter
- 系统:https://github.com/prometheus/node_exporter
- HAProxy:https://github.com/prometheus/haproxy_exporter
- InfluxDB:https://github.com/prometheus/influxdb_exporter
- JMX:https://github.com/prometheus/jmx_exporter
参考:
- Prometheus 使用 PushGateway 数据上报采集:https://cloud.tencent.com/developer/article/1531821
阅读 —
·
全站 —