使用NodeExporter采集数据
安装NodeExporter
从官网(https://prometheus.io/download/#node_exporter
)下载最新版本(0.18.1
)的二进制的NodeExporter,解压
$ wget https://github.com/prometheus/node_exporter/releases/download/v0.18.1/node_exporter-0.18.1.linux-amd64.tar.gz
$ tar xzvf node_exporter-0.18.1.linux-amd64.tar.gz
同样,我们使用systemd
来管理这个服务。我们把解压后的整个node_exporter-0.18.1.linux-amd64
文件夹移动到/usr/local/
目录下,并重命名为node_exportor
$ mv ./node_exporter-0.18.1.linux-amd64 /usr/local/node_exportor
然后创建文件/usr/lib/systemd/system/node_exporter.service
,内容如下
[Unit]
Description=node_exporter
After=network.target
[Service]
Type=simple
ExecStart=/usr/local/node_exporter/node_exporter
Restart=on-failure
[Install]
WantedBy=multi-user.target
关于启动参数可以使用命令/usr/local/node_exporter/node_exporter --help
查看。
然后启动node_exporter
,并查看是否启动成功
$ systemctl daemon-reload && systemctl enable node_exporter
$ systemctl start node_exporter && systemctl status node_exporter
我们可以通过 IP:PORT/metrics
来查看NodeExporter的数据,如下:

配置Prometheus抓取NodeExporter的数据
当node_exporter
启动成功后,我们便可以配置prometheus
,去抓取node_exporter
的数据。node_exporter
默认的端口是9100。
一开始的时候,prometheus只有一个抓取对象,就是抓取自已的数据。即一开始时,/usr/local/prometheus/prometheus.yml
的scrape_configs:
的内容如下:
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090']
修改配置文件/usr/local/prometheus/prometheus.yml
,加入新的job
,抓取node_exporter
的数据,添加完后scrape_configs:
的完整内容如下:
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090']
- job_name: 'node_exporter'
static_configs:
- targets: ['localhost:9100']
labels:
instance: peng01
然后重启prometheus
$ systemctl restart prometheus
然后,我们去到prometheus的Status -> Targets
,就可以看到上面的两个target,均为UP
状态

接下来,我们通过Prometheus来查看一下主机的内存使用率,输入以下查询语句然后就可以看到内存使用率的曲线图(注意:其实在截这个图时,我们的node-exporter与prometheus已经运行了很长一段时间,如果你的运行时间不长,看到的曲线会比较少)
(node_memory_MemTotal_bytes - node_memory_MemAvailable_bytes) / node_memory_MemTotal_bytes * 100

Last updated
Was this helpful?