前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Promethues与Gateway模式对接

Promethues与Gateway模式对接

作者头像
Linux运维技术之路
发布2022-06-07 09:14:22
1.2K0
发布2022-06-07 09:14:22
举报

客户端使用push的方式上报监控数据到pushgateway,prometheus会定期从pushgateway拉取数据。使用它的原因主要是:

  • 1、Prometheus 采用 pull 模式,可能由于不在一个子网或者防火墙原因,导致Prometheus 无法直接拉取各个 target数据。
  • 2、在监控业务数据的时候,需要将不同数据汇总, 由 Prometheus 统一收集。

一、部署 pushgateway

  • 安装pushgateway
代码语言:javascript
复制
wget https://github.com/prometheus/pushgateway/releases/download/v1.1.0/pushgateway-1.1.0.linux-amd64.tar.gz
tar zxvf pushgateway-1.1.0.linux-amd64.tar.gz
mv pushgateway-1.1.0.linux-amd64 /usr/local/exporter/pushgateway
  • pushgateway启动
代码语言:javascript
复制
nohup /usr/local/exporter/pushgateway/pushgateway \
--web.enable-admin-api  \
--persistence.file="pushfile.txt" \
--persistence.interval=10m  &

注意:为了防止 pushgateway 重启或意外挂掉,导致数据丢失,可以通过 -persistence.file 和 -persistence.interval 参数将数据持久化下来。

  • 访问WEB curl http://127.0.0.1:9091/metrics
代码语言:javascript
复制
[root@Prometheus pushgateway]# curl http://127.0.0.1:9091/metrics
# HELP go_gc_duration_seconds A summary of the GC invocation durations.
# TYPE go_gc_duration_seconds summary
go_gc_duration_seconds{quantile="0"} 0
go_gc_duration_seconds{quantile="0.25"} 0
go_gc_duration_seconds{quantile="0.5"} 0
go_gc_duration_seconds{quantile="0.75"} 0
go_gc_duration_seconds{quantile="1"} 0
go_gc_duration_seconds_sum 0
go_gc_duration_seconds_count 0
# HELP go_goroutines Number of goroutines that currently exist.
# TYPE go_goroutines gauge
go_goroutines 10
# HELP go_info Information about the Go environment.
# TYPE go_info gauge
go_info{version="go1.13.6"} 1
# HELP go_memstats_alloc_bytes Number of bytes allocated and still in use.
# TYPE go_memstats_alloc_bytes gauge
go_memstats_alloc_bytes 2.859168e+06
# HELP go_memstats_alloc_bytes_total Total number of bytes allocated, even if freed.
# TYPE go_memstats_alloc_bytes_total counter
go_memstats_alloc_bytes_total 2.859168e+06
# HELP go_memstats_buck_hash_sys_bytes Number of bytes used by the profiling bucket hash table.
# TYPE go_memstats_buck_hash_sys_bytes gauge
go_memstats_buck_hash_sys_bytes 3037
# HELP go_memstats_frees_total Total number of frees.
# TYPE go_memstats_frees_total counter
go_memstats_frees_total 402

二 prometheus 采集pushgateway数据

  • 修改配置文件 vim /usr/local/prometheus/prometheus.yml
代码语言:javascript
复制
- job_name: 'pushgateway'
    static_configs:
    - targets: ['114.67.116.119:9091']
    honor_labels: true

因为prometheus配置pushgateway 的时候,也会指定job和instance,但是它只表示pushgateway实例,不能真正表达收集数据的含义。所以配置pushgateway需要添加honor_labels:true,避免收集数据本身的job和instance被覆盖。

  • 重启动配置服务 kill -hup pid

三、push数据到pushgateway

推送URL :pushgateway默认采用9091端口,路径:/metrics/job/{/<LABEL_NAME>/<LABEL_VALUE>}是job标签的值,后面跟任意数量的标签对,instance标签可以有也可以没有。

  • 1、push数据类型1
代码语言:javascript
复制
echo "test_metric 2333" | curl --data-binary @- http://127.0.0.1:9091/metrics/job/test_job
  • 2、push数据类型2
代码语言:javascript
复制
vim push_memory.sh

#!/bin/bash 
# desc push memory info 

total_memory=$(free  |awk '/Mem/{print $2}')
used_memory=$(free  |awk '/Mem/{print $3}')

job_name="custom_memory"
instance_name="10.0.0.11"

cat <<EOF | curl --data-binary @- http://127.0.0.1:9091/metrics/job/$job_name/instance/$instance_name
#TYPE custom_memory_total  gauge
custom_memory_total $total_memory
#TYPE custom_memory_used  gauge
custom_memory_used $used_memory
EOF

  • 3、push数据类型3, 用文件导入数据

curl -XPOST --data-binary @data.txt http://127.0.0.1:9091/metrics/job/nginx/instance/114.67.116.119

vim data.txt

代码语言:javascript
复制
http_request_total{code="200",domain="abc.cn"} 276683
http_request_total{code="400",domain="abc.cn"} 0
http_request_total{code="408",domain="abc.cn"} 7
http_request_total{code="401",domain="abc.cn"} 0
http_request_total{schema="http",domain="abc.cn"} 277725
http_request_total{schema="https",domain="abc.cn"} 0
http_request_time{code="total",domain="abc.cn"} 76335.809000
http_request_uniqip{domain="abc.cn"} 216944
http_request_maxip{clientip="114.67.116.119",domain="abc.cn"} 81
  • 4、删除指标:curl -X DELETE http://127.0.0.1:9091/metrics/job/test_job 也可以在web界面进行删除

四、通过使用官方给的python library,使用push 方式把数据推送到pushgateway。

  • 安装prometheus_client
代码语言:javascript
复制
/usr/local/python3.6/bin/pip install --upgrade pip
/usr/local/python3.6/bin/pip  install prometheus_client
/usr/local/python3.6/bin/pip  list
  • 程序文件client.py cat client.py
代码语言:javascript
复制
#!/usr/local/python3.6/bin/python3
from prometheus_client import CollectorRegistry, Gauge, push_to_gateway
registry = CollectorRegistry()
g = Gauge('ping', '检测最大响应时间',['dst_ip','city'], registry=registry) #Guage(metric_name,HELP,labels_name,registry=registry)
g.labels('10.0.0.5','shenzhen').set(42.2) #set设定值
g.labels('10.0.0.11','shenzhen').dec(2)  #dec递减2
g.labels('10.0.0.5','shenzhen').inc()  #inc递增,默认增1
push_to_gateway('localhost:9091', job='ping_status', registry=registry)
  • 执行文件 /usr/local/python3.6/bin/python3 client.py
  • 界面
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2020-11-22,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Linux运维技术之路 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 客户端使用push的方式上报监控数据到pushgateway,prometheus会定期从pushgateway拉取数据。使用它的原因主要是:
  • 一、部署 pushgateway
  • 二 prometheus 采集pushgateway数据
  • 三、push数据到pushgateway
  • 四、通过使用官方给的python library,使用push 方式把数据推送到pushgateway。
相关产品与服务
命令行工具
腾讯云命令行工具 TCCLI 是管理腾讯云资源的统一工具。使用腾讯云命令行工具,您可以快速调用腾讯云 API 来管理您的腾讯云资源。此外,您还可以基于腾讯云的命令行工具来做自动化和脚本处理,以更多样的方式进行组合和重用。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档