向node_exporter中添加新监控信息

在使用promethues进行服务器指标监控的时候,有时候需要添加一下自己的监控信息, 下面是本人的做法,记录一下:

Steps:

  1. 被监控服务器上启动 node-exporter
    •   如: docker run -d --restart=always --net="host" --pid="host" --name=node-exporter_qa -v "/:/host:ro,rslave" quay.io/prometheus/node-exporter --path.rootfs=/host --collector.textfile.directory="/run/prometheus"
    • 注意上面的/run/prometheus目录,我们只需要向此目录中添加 *.prom 文件即可实现添加自定义监控数据
  2. 自定义一个自己的Docker 镜像,将需要的监控指标收集起来,实现如下:
    •   生成收集脚本需要的环境
FROM centos:latest
USER root
ADD ./get_frame.sh /get_frame.sh
RUN yum -y install wget
RUN wget http://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm

RUN rpm -ivh epel-release-latest-7.noarch.rpm
RUN yum repolist
RUN yum -y install jq

RUN mkdir -p /run/prometheus/
RUN chmod 777 /get_frame.sh
ENTRYPOINT /get_frame.sh
    •  编写收集脚本 

 

#!/bin/bash

# Adjust as needed.
TEXTFILE_COLLECTOR_DIR=/run/prometheus/
while true;do
# Your code goes here.
info_a=`curl -s -X GET http://localhost/xxxxxx`
info_b=`curl -s -X GET http://localhost/xxxxxx`
cat << EOF > "$TEXTFILE_COLLECTOR_DIR/frame.prom.$$"
$info_a
$info_b
EOF

# Rename the temporary file atomically.
# This avoids the node exporter seeing half a file.
mv "$TEXTFILE_COLLECTOR_DIR/frame.prom.$$" \
  "$TEXTFILE_COLLECTOR_DIR/frame.prom"
sleep 5
done
    • 注意收集脚本生成的文本消息格式需要满足一定格式, 如下述示例:

      indicator_a{label_a_a="value_a_a",label_a_b="value_a_b"} 1182

      indicator_b{label_b_a="value_b_a",label_b_b="value_b_b"} 10

    • 注意最后的值必须为 数值,不能是字符串

  3. 接下来就可以在Grafana中展示自定义的数据了

 

posted @ 2020-07-15 15:28  ZackZhou  阅读(1297)  评论(0编辑  收藏  举报