promtheus的pushgateway实例
pushgateway使用场景
日志监控只能监控本地文件系统日志文件
如果要监控远程系统数据需要通过pushgateway的http接口推送远程数据
1.安装好pushgateway
pushgateway自生的数据存储在内存,一旦重启它自身的数据全部会被清零
2.自生的业务程序 采集数据端

import os url="192.168.30.99:9091" job_name = "mytestjob" metric_name = "mymetrics" metric_value = 1000 dimensions ={} dimensions["aa"]="bb" dimensions["11"]=222 def submit_wrapper(url, job_name, metric_name, metric_value, dimensions): dim = '' headers = {'X-Requested-With': 'Python requests', 'Content-type': 'text/xml'} for key, value in dimensions.items(): dim += '/%s/%s' % (key, value) requests.post('http://%s/metrics/job/%s%s' % (url, job_name, dim), data='%s %s\n' % (metric_name, metric_value), headers=headers) # submit_wrapper(url,job_name,metric_name,metric_value,dimensions) activeCount="20000" taskCount ="30000" queueSize = "100000" ptype ="1" def push_data(): for i in range(10): os.popen('./Pushgateway.sh --local-ip-address 192.168.30.99 --activeCount '+activeCount+' --taskCount '+taskCount+' --queueSize '+queueSize+' --ptype '+ptype) print 'activeCount=',activeCount, ' taskCount=',taskCount, ' queueSize=',queueSize,' ptype=',ptype push_data()
3.拼装数据上报给pushgateway

#!/bin/bash usage () { echo "USAGE: $0 [--local-ip-address xxx.xxx.xxx.xxx] [--instance-name ser_name] [--ser-res ser_res] [--pid pid] [--run-time run_time] [--value value]" echo " [-l|--local-ip-address ip_addr] local ip address." echo " [-i|--activeCount]" echo " [-r|--taskCount]" echo " [-p|--queueSize]" echo " [-t|--ptype ptype]" echo " [-h|--help] Usage message" } POSITIONAL=() while [[ $# -gt 0 ]]; do key="$1" case $key in -l|--local-ip-address) ip_addr="$2" shift # past argument shift # past value ;; -i|--activeCount) activeCount="$2" shift # past argument shift # past value ;; -r|--taskCount) taskCount="$2" shift # past argument shift # past value ;; -p|--queueSize) queueSize="$2" shift # past argument shift # past value ;; -t|--ptype) ptype="$2" shift # past argument shift # past value ;; -h|--help) help="true" shift ;; *) usage exit 1 ;; esac done if [[ $help ]]; then usage exit 0 fi if [[ -z $ip_addr ]]; then usage exit 1 fi cat <<EOF | curl --data-binary @- http://$ip_addr:9091/metrics/job/probemonitor/instance/probe activeCount{ptype="$ptype"} $activeCount taskCount{ptype="$ptype"} $taskCount queueSize{ptype="$ptype"} $queueSize EOF
大屏展示数据
pushgateway查看数据
promtheus展示历史数据
python把数据推送到pushgateway

from prometheus_client import CollectorRegistry, Gauge, push_to_gateway """ please install prometheus client through pip install prometheus_client """ registry = CollectorRegistry() g = Gauge( 'pushgateway_job_last_success_unixtime', 'Last time a batch job successfully finished', ['a', 'b'], registry=registry) g.labels(a='1', b='2').set_to_current_time() push_to_gateway('localhost:9091', job='job test', registry=registry)
本文来自博客园,作者:不懂123,转载请注明原文链接:https://www.cnblogs.com/yxh168/p/16147640.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· AI与.NET技术实操系列(六):基于图像分类模型对图像进行分类