node_exporter 是 prometheus 官方提供的主机监控插件。点击 下载 进入下载地址,截止目前最新版本为 1.3.1。

下载

  node_exporter 的版本为 1.3.1。对应的 下载地址 为 https://github.com/prometheus/node_exporter/releases/download/v1.3.1/node_exporter-1.3.1.linux-amd64.tar.gz。

  在 CentOS 下会因为没有对应的 HTTPS 证书文件导致无法通过 wget 下载。此时可以通过在本地下载后上传到机器上或者通过添加证书解决该问题。

安装

  将对应的文件上传到对应的机器上之后,然后进行安装的配置。对于这种只有一个可执行文件的程序,个人喜欢将其放入/usr/bin 下。

1
2
[root@consul-01 ~]# tar -zxvf node_exporter-1.3.1.linux-amd64.tar.gz
[root@consul-01 ~]# mv ./node_exporter-1.3.1.linux-amd64/node_exporter /usr/bin/

node_exporter 注册

  使用 consul 实现 Prometheus 服务自动发现,需要将对应的 node_exporter 的信息注册成 consul 的服务。

手动注册

  在任意可访问 http://192.168.0.250:8500 服务器上执行一下命令。

1
[root@consul-01 ~]# curl -v -X PUT -d '{"Meta":{"hostname":"consul-01", "exporter_type":"node_exporter"}, "id":"consul-01","name":"consul-01", "address":"192.168.0.1",  "port":9100, "tags":["node-exporter"],"checks":[{"http":"http://192.168.0.1:9100", "interval":"5s"}]}' http://192.168.0.250:8500/v1/agent/service/register

自动注册

  这里提供一个简单的自动注册脚本。

1
2
3
4
5
6
7
8
9
10
[root@consul-01 ~]# cat /etc/init.d/autoRegisterNodeExporter.sh
#!/bin/bash
hostname=$(hostname)
ip=$(ifconfig | awk '/inet / && !($2 ~ /^127/){print $2}')
processor=$(ps -ef |grep node_exporter|grep -v grep| wc -l)
if [[ ${processor} -lt 1 ]];then
systemctl start node_exporter
else
curl -v -X PUT -d '{"Meta":{"hostname":"'${hostname}'", "exporter_type":"node_exporter"}, "id":"'${hostname}'","name":"'${hostname}'", "address":"'${ip}'", "port":9100, "tags":["node-exporter"],"checks":[{"http":"http://'${ip}':9100", "interval":"5s"}]}' http://192.168.0.250:8500/v1/agent/service/register
fi

  注意,这个脚本,在生产环境实用性不高。只能作为一个参考。

  这里提供两个生产上可行的方案

1、在主机名规范的情况下,通过主机名识别对应的信息(如产品线,应用名称及编号,归属环境)将其置放在上面脚本中的 “Meta” 中,以备后面使用(如告警规则中使用,已达到快速定位哪个产品,哪个应用的第几台服务器出现问题)
2、在有 CMDB 的企业中,可以结合 CMDB 的数据信息,来进行服务注册。

systemd 自启动管理

  使用 systemd 对程序进行自启动管理。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[root@consul-01 ~]# cat > /usr/lib/systemd/system/node_exporter.service << EOF
[Unit]
Description=node_exporter service

[Service]
User=root
ExecStart=/usr/bin/node_exporter --web.listen-address=:9119
ExecStartPost=/etc/init.d/autoRegisterNodeExporter.sh
TimeoutStopSec=10
Restart=on-failure
RestartSec=5

[Install]
WantedBy=multi-user.target
EOF
[root@consul-01 ~]# systemctl enable --now node_exporter.service

  注意,在生产中,不要使用 ExecStartPost 指定自动服务注册脚本。

  上述操作都成功执行的情况下,应该就已经正常了。这时在访问 http://192.168.0.250:8500/ui/prometheus/services 就能看到相应的服务了。同时 Prometheus 中也有对应的监控了。注意,防火墙导致的不可访问。

Grafanan 展示监控数据

  Grafana 官方 提供了很多优秀的数据展示 Dashboard。这里使用的 Node Exporter Full,id 是 1860。

06-node_exporter-01

  登录 http://192.168.0.253:3000 后,按上图 1、2 操作后会进入如图所示的界面。

06-node_exporter-02

  如果能通公网的环境下,直接在上图红色框中输入 1860 然后 load 就行。如果没有公网环境,则下载对应的 json 文件 后将其填入红色框中 load。在随后出来的页面中点击 import 就配置好了 node_exporter 的方案

  最后给出一个最终的结果展示。

06-node_exporter-03