在使用promethues進行服務器指標監控的時候,有時候需要添加一下自己的監控信息, 下面是本人的做法,記錄一下:
Steps:
- 被監控服務器上啟動 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 文件即可實現添加自定義監控數據
- 自定義一個自己的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中展示自定義的數據了