Loading

Prometheus Pushgateway配置笔记

前言

pushgateway的安装不再赘述,通用的操作最后以daemonlized方式运行。
在Prometheus中给pushgateway上报的数据单独定义一个job:
image

需要注意的点:
pushgateway本身没有任何抓取监控数据的功能,只能被动等待推送。

推送

一个简单的推送脚本案例

以采集TCP wait connections为例

#!/bin/bash
instance_name=`hostname -f | cut -d'.' -f1`

if [ $instance_name == 'localhost' ];then 
echo "must FQDN hostname"
exit 1
fi

$ For waitting connections

label="count_netstat_wait_connections" #定义一个新的key
count_netstat_wait_connections=`netstat -an |grep -i wait |wc -l` #定义一个新的数值netstat中wait的数量

echo " $label: $count_netstat_wait_connections"
echo "$label $count_netstat_wait_connections" | curl --data-binary @~ http://prometheus.server.com:9091/metrics/job/pushgateway1/instance/$instance_name

脚本写完之后扔到crontab中定期运行即可。

url解释:

http://prometheus.server.com:9091/metrics/job/pushgateway1/instance/$instance_name

http://prometheus.server.com:9091/metrics/
URL的主location

job/pushgateway
推送到Prometheus的对应job中

instance/$instance_name
定义instance_name等其他label。

高级推送示例

以更复杂的形式传入数据

  cat <<EOF | curl --data-binary @- http://pushgateway.example.org:9091/metrics/job/some_job/instance/some_instance
  # TYPE some_metric counter
  some_metric{label="val1"} 42
  # TYPE another_metric gauge
  # HELP another_metric Just an example.
  another_metric 2398.283
  EOF

base64 push

在value存在特殊字符(例如路径中存在换行符job="directory_cleaner",path="/var/tmp")的时候,上报路径会变成/metrics/job/directory_cleaner/path//var/tmp
导致上报路径错误,此时需要做base64转换。
格式:key@base64/value
/metrics/job/directory_cleaner/path@base64/L3Zhci90bXA

如果不是使用高级程序语言直接包含base64转换的库,而是简单使用的shell命令,可以使用命令行base64url工具(Debian中的包是basez),拼入curl命令如下

echo 'some_metric{foo="bar"} 3.14' | curl --data-binary @- http://pushgateway.example.org:9091/metrics/job/directory_cleaner/path@base64/$(echo -n '/var/tmp' | base64url)

二级label

当有时候label需要有多级的时候,可以认为一级label不存在value,二级label存在value。
例如:job="example",first_label="",second_label="foobar"
会被配置成:
/metrics/job/example/first_label//second_label/foobar
这样无疑是不行的,正确的配置方式应该是用=作为占用符:

/metrics/job/example/first_label@base64/=/second_label/foobar

删除数据

可以通过http请求进行数据参数,参考:

curl -X DELETE http://pushgateway.example.org:9091/metrics/job/some_job/instance/some_instance
curl -X DELETE http://pushgateway.example.org:9091/metrics/job/some_job
curl -X PUT http://pushgateway.example.org:9091/api/v1/admin/wipe
posted @ 2023-02-02 16:22  motorao  阅读(493)  评论(0编辑  收藏  举报