原理參照官網pushgateway或網上其它文章,這裏只有配置
#pull docker 鏡像
docker pull prom/pushgateway
#運行(默認沒有認證,如果需要加認證可以使用nginx做反響代理)
docker run -d \
--name=pushgateway \
-p 9091:9091 \
prom/pushgateway
#訪問url
http://192.168.1.100:9091
http://192.168.1.100:9091/metrics
#prometheus 配置
- job_name: pushgateway
scrape_interval: 30s
honor_labels: true #加上此配置exporter節點上傳數據中的一些標籤將不會被pushgateway節點的相同標籤覆蓋
static_configs:
- targets: ['192.168.1.100:9091']
labels:
instance: pushgateway
將node_exporter的metrics傳到pushgateway
curl 127.0.0.1:9100/metrics|curl --data-binary @- http://192.168.1.100:9091/metrics/job/test/instance/10.2.1.11/hostname/ip-10-2-1-11
以上命令執行後,對於傳過去的監控項會添加此處定義的標籤 job=test instance=10.2.1.11 hostname=ip-10-2-1-11
自定義metrics 並傳到pushgateway
這裏舉一個例子,使用curl獲取到百度的http相關指標,如dns解析時間,請求耗時,返回code等。
如下腳本每隔15秒獲取一次metrics並push到pushgateway
#/bin/bash
while true;
do
echo '# HELP probe_dns_lookup_time_seconds Returns the time taken for probe dns lookup in seconds' > metric_temp
echo '# TYPE probe_dns_lookup_time_seconds gauge' >> metric_temp
echo `curl -o /dev/null -s -w "probe_dns_lookup_time_seconds %{time_namelookup}\n" https://www.baidu.com` >> metric_temp
echo '# HELP probe_http_status_code Response HTTP status code' >> metric_temp
echo '# TYPE probe_http_status_code gauge' >> metric_temp
echo "probe_http_status_code `curl -I -m 10 -o /dev/null -s -w %{http_code} https://www.baidu.com`" >> metric_temp
echo '# HELP probe_duration_seconds Returns how long the probe took to complete in seconds' >> metric_temp
echo '# TYPE probe_duration_seconds gauge' >> metric_temp
echo `curl -o /dev/null -s -w "probe_duration_seconds %{time_total}\n" "https://www.baidu.com"` >> metric_temp
#push metric to prometheus
cat metric_temp| curl -s --data-binary @- http://192.168.1.100:9091/metrics/job/some_job/instance/ip-192.168.200.200;
date;
sleep 15;
done
然後在Prometheus裏查對應的metric值即可。
刪除某個組下的某實例的所有數據:
curl -X DELETE http://pushgateway.example.org:9091/metrics/job/some_job/instance/some_instance
刪除某個組下的所有數據:
curl -X DELETE http://pushgateway.example.org:9091/metrics/job/some_job