在部署完k8s及使用prometheus监控后,发现一个问题啊,那就是能不能像zabbix一样,自己定义key啊,监控自己的服务,毕竟官方提供的key也不能完全覆盖。。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功能。参考这里   
环境说明 环境:
这里就以监控nginx页面的状态码为例了,使用的是textfile这个collector  
监控脚本 编写和zabbix类似的监控脚本并赋予其返回值1
2
3
4
5
6
7
8
9
10
11
[root@localhost ~]
#!/bin/bash 
NGINX_PATH='/etc/nginx' 
if  [ -d  "${NGINX_PATH} "  ];then  
NGINX_PORT='80' 
/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 ~]
1
[root@localhost ~]
1
2
3
4
5
[root@localhost ~]
#!/bin/bash 
node_exporter_HOME='/usr/local/node_exporter' 
PATH=$node_exporter_HOME :$PATH 
[root@localhost ~]
编写脚本生成key1
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 ~]
#!/bin/bash 
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
    
    
else 
    echo  "# TYPE ${metric}  gauge" > "$prom_file " .$$
    echo  "${metric}  ${VALUE} "  >> "$prom_file " .$$ && mv "$prom_file " .$$ "$prom_file " 
fi  
[root@localhost ~]
1
2
[root@localhost ~]
[root@localhost ~]
zabbix_runner脚本会生成node_nginx_testpage_status.prom文件,该文件记录当前监控指标的状态1
2
3
4
[root@localhost ~]
node_nginx_testpage_status 1
[root@localhost ~]
启动node_exporter
--collector.textfile.directory指定textfile收集器读取文件的目录。根据官网说明,textfile收集器会读取以.prom结尾的文件   
访问prometheus Dashboardhttp://ip:9090   
首先先确认prometheus与node_exporter建立连接
输入在node_nginx_testpage_status.prom文件中生成的key
接下来把其放到定时任务中即可1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@localhost ~]
#!/bin/bash 
scripts='/root' 
zabbix_runner_dir='/root' 
cd  $scripts 
/bin/bash $zabbix_runner_dir /zabbix_runner node_nginx_testpage_status
[root@localhost ~]
1
2
3
4
[root@localhost ~]
*/1 * * * * /bin/bash /root/node_exporter_key.sh
[root@localhost ~]
效果图 
附件:zabbix_runner node_nginx_testpage_status node_exporter_key.sh   
本文出自”Jack Wang Blog”:http://www.yfshare.vip/2018/09/29/node-exporter自定义key/