Prometheus的Pushgateway配置

原理参照官网pushgateway或网上其它文章,这里只有配置

#pull docker 镜像

docker pull prom/pushgateway

#运行(默认没有认证,如果需要加认证可以使用nginx做反响代理)

docker run -d \
  --name=pushgateway \
  -p 9091:9091 \
  prom/pushgateway

#访问url

http://192.168.1.100:9091
http://192.168.1.100:9091/metrics

#prometheus 配置

  - job_name: pushgateway
    scrape_interval: 30s
    honor_labels: true  #加上此配置exporter节点上传数据中的一些标签将不会被pushgateway节点的相同标签覆盖
    static_configs:
      - targets: ['192.168.1.100:9091']
        labels:
          instance: pushgateway

将node_exporter的metrics传到pushgateway

curl 127.0.0.1:9100/metrics|curl --data-binary @- http://192.168.1.100:9091/metrics/job/test/instance/10.2.1.11/hostname/ip-10-2-1-11

以上命令执行后,对于传过去的监控项会添加此处定义的标签 job=test instance=10.2.1.11 hostname=ip-10-2-1-11

自定义metrics 并传到pushgateway

这里举一个例子,使用curl获取到百度的http相关指标,如dns解析时间,请求耗时,返回code等。
如下脚本每隔15秒获取一次metrics并push到pushgateway

#/bin/bash
while true; 
do 
	echo '# HELP probe_dns_lookup_time_seconds Returns the time taken for probe dns lookup in seconds' > metric_temp
	echo '# TYPE probe_dns_lookup_time_seconds gauge' >> metric_temp
	echo `curl -o /dev/null -s -w "probe_dns_lookup_time_seconds  %{time_namelookup}\n" https://www.baidu.com` >> metric_temp
	
	echo '# HELP probe_http_status_code Response HTTP status code'  >> metric_temp
	echo '# TYPE probe_http_status_code gauge' >> metric_temp
	echo "probe_http_status_code `curl -I -m 10 -o /dev/null -s -w %{http_code} https://www.baidu.com`" >> metric_temp
	
	echo '# HELP probe_duration_seconds Returns how long the probe took to complete in seconds' >> metric_temp
	echo '# TYPE probe_duration_seconds gauge' >> metric_temp
	echo `curl -o /dev/null -s -w "probe_duration_seconds  %{time_total}\n" "https://www.baidu.com"` >> metric_temp
	
	#push metric to prometheus
	cat metric_temp| curl -s --data-binary @- http://192.168.1.100:9091/metrics/job/some_job/instance/ip-192.168.200.200; 
	date;
	sleep 15;
done

然后在Prometheus里查对应的metric值即可。

删除某个组下的某实例的所有数据:

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

你可能感兴趣的:(Prometheus)