• Post author:
  • Post category:prometheus
  • Post comments:0评论

一、安装配置

下载地址:https://prometheus.io/download/

[root@cp-3 ~]# tar xf node_exporter-1.1.2.linux-amd64.tar.gz -C /usr/local/
[root@cp-3 ~]# mv /usr/local/node_exporter-1.1.2.linux-amd64 /usr/local/node_exporter
[root@cp-3 ~]# ln -s /usr/local/node_exporter/node_exporter /usr/local/bin/node_exporter
[root@cp-3 ~]# node_exporter --version
node_exporter, version 1.1.2 (branch: HEAD, revision: b597c1244d7bef49e6f3359c87a56dd7707f6719)
  build user:       root@f07de8ca602a
  build date:       20210305-09:29:10
  go version:       go1.15.8
  platform:         linux/amd64

  我们可以通过命令行参数对 node_exporter 进行配置,–help 就可以查看完整的参数列表。
  默认情况下。node_exporter 在端口 9100 上运行,并在路径 /metrics 上暴露指标。可以通过 –web.listen-address 和 –web.telemetry-path 参数来设置端口和路径。

node_exporter --web.listen-address=":9101" --web.telemetry-path="/metrics_test"

# 这会将 node_exporter 绑定到端口 9101 并在路径 /metrics_test 上暴露指标

  大多数参数其实都是收集器,我们可以控制启用或者禁用那些收集器,许多收集器默认都是启用的,我们可以通过 –no- 前缀来修改状态。

# 启动cpu信息收集器
node_exporter --collector.cpu.info

# 禁用
node_exporter --no-collector.cpu.info

启动:

[root@cp-3 ~]# nohup node_exporter &

# 测试
[root@cp-3 ~]# curl http://10.88.88.13:9100/metrics

二、textfile 收集器

  textfile 收集器允许我们暴露自定义指标,它通过扫描指定目录中的文件,提取所有格式为 Prometheus 指标的字符串,然后暴露它们以便抓取。
  指标一般在以 .prom 结尾文件中定义,并且需要使用 Prometheus 特定的文本格式(计数型、测量型、计时型)。

[root@cp-3 ~]# mkdir -p /var/lib/node_exporter/txtfile_collector
[root@cp-3 ~]# vim /var/lib/node_exporter/txtfile_collector/test.prom
login_num 1
host_info{host_name="pro",ip="10.88.88.13"} 1

  textfile 收集器默认就是启用,但是我们需要指定目录以便 node exporter 知道在那找到自定义指标,使用 --collector.textfile.directory 选项即可。

[root@cp-3 ~]# nohup node_exporter --collector.textfile.directory=/var/lib/node_exporter/txtfile_collector/ &

[root@cp-3 ~]# curl -s -g -X GET http://10.88.88.13:9100/metrics | grep -E "login|host_info"
# HELP host_info Metric read from /var/lib/node_exporter/txtfile_collector/test.prom
# TYPE host_info untyped
host_info{host_name="pro",ip="10.88.88.13"} 1
# HELP login_num Metric read from /var/lib/node_exporter/txtfile_collector/test.prom
# TYPE login_num untyped
login_num 1

三、抓取数据

  node_exporter 配置好后,我们需要到 Prometheus 服务器上,配置一个新作业来抓取 node_exporter 上的数据。

[root@cp-3 ~]# vim /usr/local/prometheus/prometheus.yml
......
scrape_configs:
  - job_name: 'prometheus'
    static_configs:
    - targets: ['localhost:9090']

  - job_name: 'node'
    static_configs:
    - targets: ['localhost:9100']
......

[root@cp-3 ~]# systemctl restart prometheus
[root@cp-3 ~]# curl -sg http://10.88.88.13:9090/api/v1/query?query=node_cpu_seconds_total{mode=\"user\"}
{"status":"success","data":{"resultType":"vector","result":[{"metric":{"__name__":"node_cpu_seconds_total","cpu":"0","instance":"localhost:9100","job":"node","mode":"user"},"value":[1626330644.331,"1505.36"]},{"metric":{"__name__":"node_cpu_seconds_total","cpu":"1","instance":"localhost:9100","job":"node","mode":"user"},"value":[1626330644.331,"1513.35"]}]}}

四、过滤收集器

node exporter 采集了很多指标,有些是我们不想要,这时我们就可以通过配置特定收集器列表来过滤。
例如我只要收集系统平均负载相关的指标:

[root@cp-3 ~]# vim /usr/local/prometheus/prometheus.yml
......
  - job_name: 'node'
    static_configs:
    - targets: ['localhost:9100']
      params:
        collect[]:
          - loadavg
......
[root@cp-3 ~]# promtool check config /usr/local/prometheus/prometheus.yml
Checking /usr/local/prometheus/prometheus.yml
  SUCCESS: 0 rule files found

[root@cp-3 ~]# systemctl restart prometheus
[root@cp-3 ~]# curl -sg http://10.88.88.13:9090/api/v1/query?query=node_load1
{"status":"success","data":{"resultType":"vector","result":[{"metric":{"__name__":"node_load1","instance":"localhost:9100","job":"node"},"value":[1626331890.677,"0.12"]}]}}

注意,此时在查询其它指标要么就是之前保留的历史记录(可以看到值是一直不变),要么就是没有数据。

五、grafana

docker run --restart always -d -p 3000:3000 grafana/grafana

数据源为prometheus 的 grafana 监控模板比较多,如果用来监控物理机推荐 89199276

发表评论

验证码: 6 + = 16