• 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
  • 相关阅读:
    推荐vue脚手架工具 vue-cli
    React仿大众点评外卖app
    推荐一个react脚手架工具
    eclipse 中配置php的 XDebug调试
    再谈extjs4.1中gridpanel动态表头动态列
    控制extsj4.1 gridpanel表格行或者单元格的编辑
    windows8.1 app所有默认样式
    windows8.1 app样式定义使用
    windows8.1 app入门开发学习
    Silverlight Tools 语言不匹配问题
  • 原文地址:https://www.cnblogs.com/yxh168/p/16147640.html
Copyright © 2020-2023  润新知