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
nohup /usr/local/exporter/pushgateway/pushgateway \
--web.enable-admin-api \
--persistence.file="pushfile.txt" \
--persistence.interval=10m &
注意:为了防止 pushgateway 重启或意外挂掉,导致数据丢失,可以通过 -persistence.file 和 -persistence.interval 参数将数据持久化下来。
[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
- 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被覆盖。
推送URL :pushgateway默认采用9091端口,路径:/metrics/job/{/<LABEL_NAME>/<LABEL_VALUE>}是job标签的值,后面跟任意数量的标签对,instance标签可以有也可以没有。
echo "test_metric 2333" | curl --data-binary @- http://127.0.0.1:9091/metrics/job/test_job
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
curl -XPOST --data-binary @data.txt http://127.0.0.1:9091/metrics/job/nginx/instance/114.67.116.119
vim data.txt
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
/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
#!/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)