周末在家搭建好Prometheus环境,本篇进行实例-监控Linux服务器,这里记录下来,方便查看。
需要安装 插件 node_exporter
node_exporter 用于 NIX系统监控,使用Go语言编写收集器
在虚拟机【IP:192.169.184.144】插件安装的过程。
下载安装
// 安装 wget
yum -y install wget
// 下载 node_exporter 二进制文件
wget https://github.com/prometheus/node_exporter/releases/download/v1.2.0/node_exporter-1.2.0.linux-amd64.tar.gz
// 解压
tar -zxvf node_exporter-1.2.0.linux-amd64.tar.gz
// 创建文件夹
mkdir /opt/node_exporter
// 移动文件
mv node_exporter-1.2.0.linux-amd64/* /opt/node_exporter
若要想后台启动 Prometheus,则需要修改配置文件。具体操作可参考以下步骤。
第一步、添加系统配置文件
路径 /usr/lib/systemd/system
添加 node_exporter.service 配置文件
[Unit]
Description=https://prometheus.io
[Service]
Restart=on-failure
ExecStart=/opt/node_exporter/node_exporter
[Install]
WantedBy=multi-user.target
第二步、重新刷新配置
systemctl daemon-reload
// 启动
systemctl start node_exporter
// 查看
ps -ef | grep node_exporter
运行成功,如下图所示。
// 查看端口
netstat -antp | grep 9100
在浏览器输入 ip + 9100/metrics,这里是 192.168.184.144:9100
经过以上操作,说明插件安装成功。
那如何新增任务进行监控呢?
在上篇的 Prometheus 虚拟机【IP:192.169.184.143】的配置文件添加新的job任务(node),看看是否能监控到。
- job_name: 'node'
file_sd_configs:
- files: ['/opt/prometheus/sd_config/*.yml']
refresh_interval: 5s
同时新建 sd_config文件夹,添加 node.yml 配置文件,添加以下内容,来监控144服务器。
- targets:
- 192.168.184.144:9100
以上是添加 名为 node 的job,修改完成后,执行以下命令。
// 检查yml 配置文件是否正确,返回 success 则表示正确
./promtool check config prometheus.yml
// 更改配置,需要重新加载 系统配置
systemctl daemon-reload
// 重启 prometheus
systemctl start prometheus
// 查看启动的任务
ps -ef | grep prometheus
第三步、浏览器访问
在浏览器中输入 http://192.168.184.143:9090/targets 查看 监控情况,如下图所示。
mode=“idle” 表示空闲模式下
查看参数
在 graph 下 如查询 node任务的 cpu 相关信息,输入 node_cpu_seconds_total 查询的内容如下所示。
错误处理
1)启动报错
错误:Failed to start firewald.service: Unit not found
处理方案:重新添加 node_exporter.service 配置文件
2) yml 配置报错
错误:found character that cannot start any token
处理方案:存在制表符,就是Tab,yml只能识别空格
错误:field file not found in type file.plain
处理方案:yml 中需要 用 file报错,需要用files
参考博客
【1】https://www.cnblogs.com/chenqionghe/p/10494868.html