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()
View Code

       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
View Code

   

大屏展示数据

    

     

     

    

   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)
View Code

 

posted @ 2022-04-15 09:41  不懂123  阅读(161)  评论(0编辑  收藏  举报