在部署完k8s及使用prometheus监控后,发现一个问题啊,那就是能不能像zabbix一样,自己定义key啊,监控自己的服务,毕竟官方提供的key也不能完全覆盖。。

在查阅官网资料发现,prometheus是可以使用自定义key的。
官方git地址:https://github.com/prometheus/node_exporter

背景

官方描述:

1
2
There is varying support for collectors on each operating system. The tables below list all existing collectors and the supported systems.
Collectors are enabled by providing a --collector.<name> flag. Collectors that are enabled by default can be disabled by providing a --no-collector.<name> flag.

即通过官方提供的collector来实现自定义key功能。
具体的collector这里就不一一列出了,可参考这里

环境说明

环境:
   Prometheus version 2.2.0
   node_exporter version 0.15.2
   Centos 7.4

这里就以监控nginx页面的状态码为例了,使用的是textfile这个collector

监控脚本

编写和zabbix类似的监控脚本并赋予其返回值

1
2
3
4
5
6
7
8
9
10
11
[root@localhost ~]# cat node_nginx_testpage_status
#!/bin/bash
#Monitor nginx test page.
NGINX_PATH='/etc/nginx'
if [ -d "${NGINX_PATH}" ];then
NGINX_PORT='80'
#NGINX_PORT=`grep -v "#" /etc/nginx/nginx.conf | grep listen |awk -F ";" '{print $1}' |awk '{print $2}'`
/usr/bin/curl -o /dev/null --retry 1 --max-time 3 -w %{http_code} -s "http://127.0.0.1:${NGINX_PORT}"| grep -c '200'
fi
[root@localhost ~]#

1
2
3
[root@localhost ~]# sh node_nginx_testpage_status
1
[root@localhost ~]#
1
2
3
4
5
[root@localhost ~]# cat /etc/profile.d/node_exporter.sh
#!/bin/bash
node_exporter_HOME='/usr/local/node_exporter'
PATH=$node_exporter_HOME:$PATH
[root@localhost ~]#

编写脚本生成key

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
[root@localhost ~]# cat zabbix_runner
#!/bin/bash
# Runs a textfile collector.
textfile_dir=$(dirname $0)
source /etc/profile.d/node_exporter.sh
metric="$1"
shift
script="$textfile_dir/$metric"
prom_file="$textfile_dir/$metric".prom
if [[ ! -x "$script" || -d "$script" ]]; then
echo "ERROR: Can't find script for '$metric'. Aborting."
exit 1
fi
VALUE=`"$script" "$@"`
if [[ ! -n $VALUE ]]; then
exit 0
# echo "ERROR: Can't get value for '$metric'. Aborting."
# exit 1
else
echo "# TYPE ${metric} gauge"> "$prom_file".$$
echo "${metric} ${VALUE}" >> "$prom_file".$$ && mv "$prom_file".$$ "$prom_file"
fi
[root@localhost ~]#

1
2
[root@localhost ~]# chmod +x node_nginx_testpage_status
[root@localhost ~]# sh zabbix_runner node_nginx_testpage_status

zabbix_runner脚本会生成node_nginx_testpage_status.prom文件,该文件记录当前监控指标的状态

1
2
3
4
[root@localhost ~]# cat node_nginx_testpage_status.prom
# TYPE node_nginx_testpage_status gauge
node_nginx_testpage_status 1
[root@localhost ~]#

启动node_exporter

1
[root@localhost ~]# /usr/local/node_exporter/node_exporter --web.listen-address=:9100 --collector.textfile.directory=/root/

  • --collector.textfile.directory指定textfile收集器读取文件的目录。根据官网说明,textfile收集器会读取以.prom结尾的文件

访问prometheus Dashboard
http://ip:9090

首先先确认prometheus与node_exporter建立连接
node_exporter_key

输入在node_nginx_testpage_status.prom文件中生成的key
node_exporter_key
这样就完成了node_exporter自定义key

接下来把其放到定时任务中即可

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@localhost ~]# cat node_exporter_key.sh
#!/bin/bash
#这里存储所有node_exporter自定义key
#监控脚本存储目录
scripts='/root'
#zabbix_runner存储目录
zabbix_runner_dir='/root'
#注:需要进入监控脚本目录后执行命令,否则会报"ERROR: Can't get value for '$metric'. Aborting."。即脚本文件找不到,是脚本目录不对
#监控nginx testpage
cd $scripts
/bin/bash $zabbix_runner_dir/zabbix_runner node_nginx_testpage_status
[root@localhost ~]#

1
2
3
4
[root@localhost ~]# crontab -l
#获取node_exporter自定义key
*/1 * * * * /bin/bash /root/node_exporter_key.sh
[root@localhost ~]#

效果图

node_exporter_key

附件:
zabbix_runner
node_nginx_testpage_status
node_exporter_key.sh


本文出自”Jack Wang Blog”:http://www.yfshare.vip/2018/09/29/node-exporter自定义key/