1.前言 在 Prometheus 中负责数据汇报的程序统一叫做 Exporter, 而不同的 Exporter 负责不同的业务。 它们具有统一命名格式,即 xx_exporter, 例如负责主机信息收集的 node_exporter。
Exporter原理就是将收集的数据转化为文本格式,并对外暴露接口,提供 http 请求。
2.node_exporter node_exporter 主要用于 *NIX 系统监控, 用 Golang 编写。
默认开启的功能:
默认关闭的功能:
注意:我们可以使用 –collectors.enabled 运行参数指定 node_exporter 收集的功能模块, 如果不指定,将使用默认模块。
3.程序安装和启动 3.1 二进制安装 我们可以到下载页面 选择对应的二进制安装包,下面我将以 0.14.0 作为例子:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 cd /usr/local /src/https://github.com/prometheus/node_exporter/releases/download/v0.14.0/node_exporter-0.14.0.linux-amd64.tar.gz cd /usr/local /src/tar -xvzf ~/Download/node_exporter-0.14.0.linux-amd64.tar.gz cd node_exporter-0.14.0.linux-amd64./node_exporter INFO[0000] Starting node_exporter (version=0.14.0, branch=master, revision=840ba5dcc71a084a3bc63cb6063003c1f94435a6) source ="node_exporter.go:140" INFO[0000] Build context (go=go1.7.5, user=root@bb6d0678e7f3, date=20170321-12:13:32) source ="node_exporter.go:141" INFO[0000] No directory specified, see --collector.textfile.directory source ="textfile.go:57" INFO[0000] Enabled collectors: source ="node_exporter.go:160" ..... INFO[0000] Listening on :9100 source ="node_exporter.go:186"
3.2 Docker 安装 我们可以使用 docker 镜像 安装,命令为:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 docker run -d -p 9100:9100 \ -v "/proc:/host/proc:ro" \ -v "/sys:/host/sys:ro" \ -v "/:/rootfs:ro" \ --net="host" \ quay.io/prometheus/node-exporter \ -collector.procfs /host/proc \ -collector.sysfs /host/sys \ -collector.filesystem.ignored-mount-points "^/(sys|proc|dev|host|etc)($|/)" go_gc_duration_seconds{quantile="0" } 0 go_gc_duration_seconds{quantile="0.25" } 0 go_gc_duration_seconds{quantile="0.5" } 0 ......
4.数据存储 我们可以利用 Prometheus 的 static_configs 来拉取 node_exporter 的数据。
打开 prometheus.yml 文件, 在 scrape_configs 中添加如下配置:
1 2 3 - job_name: "node" static_configs: - targets: ["127.0.0.1:9100"]
重启加载配置,然后到 Prometheus Console 查询,你会看到 node_exporter 的数据。
5.Node Exporter 常用查询语句 收集到 node_exporter 的数据后,我们可以使用 PromQL 进行一些业务查询和监控,下面是一些比较常见的查询。
注意:以下查询均以单个节点作为例子,如果大家想查看所有节点,将 instance=”xxx” 去掉即可。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 100 - (avg by (instance) (irate(node_cpu{instance="xxx", mode="idle"}[5m])) * 100 ) avg by (instance, mode) (irate(node_cpu{instance="xxx"}[5m])) * 100 node_load1{instance="xxx"} // 1 分钟负载 node_load5{instance="xxx"} // 5 分钟负载 node_load15{instance="xxx"} // 15 分钟负载 100 - ((node_memory_MemFree{instance="xxx"}+node_memory_Cached{instance="xxx"}+node_memory_Buffers{instance="xxx"})/node_memory_MemTotal) * 100 100 - node_filesystem_free{instance="xxx",fstype!~"rootfs|selinuxfs|autofs|rpc_pipefs|tmpfs|udev|none|devpts|sysfs|debugfs|fuse.*"} / node_filesystem_size{instance="xxx",fstype!~"rootfs|selinuxfs|autofs|rpc_pipefs|tmpfs|udev|none|devpts|sysfs|debugfs|fuse.*"} * 100 sum by (instance) (irate(node_network_receive_bytes{instance="xxx",device!~"bond.*?|lo"}[5m])/128) sum by (instance) (irate(node_network_transmit_bytes{instance="xxx",device!~"bond.*?|lo"}[5m])/128) sum by (instance) (rate(node_network_receive_bytes{instance="xxx",device!="lo"}[5m])) sum by (instance) (rate(node_network_transmit_bytes{instance="xxx",device!="lo"}[5m]))
6.总结 除了 node_exporter 我们还会根据自己的业务选择安装其他 exporter 或者自己编写,比较常用的 exporter 有:
Memcached exporter 负责收集 Memcached 信息MySQL server exporter 负责收集 Mysql Sever 信息MongoDB exporter 负责收集 MongoDB 信息InfluxDB exporter 负责收集 InfluxDB 信息JMX exporter 负责收集 Java 虚拟机信息 更多 exporter 请参考链接 。