下载PushGateway

地址:https://github.com/prometheus/pushgateway

启动PushGateway

PushGateway不需要安装的被监控的机器上,只需要安装在任意机器启动即可

tar zxvf pushgateway-1.0.1.linux-amd64.tar.gz
cd pushgateway-1.0.1.linux-amd64
./pushgateway

使用PushGateway上报采集数据

首先先在prometheus.yml添加job,然后重新启动prometheus

- job_name: 'pushgateway'
static_configs:
- targets: ['localhost:9091','localhost:9092']

编写shell脚本监控机器内网延迟和丢包率

#!/bin/bash
hostname=$(hostname | awk -F '.' '{print $1}')
prometheus_url='192.168.1.113'

if [ $hostname == "localhost" ];then
echo "机器名不能是localhost"
exit 1
fi

label1="node_netstat_ping_delay"
node_netstat_ping_delay=$(ping -q -A -s 500 -W 1000 -c 100 $prometheus_url | grep 'transmitted' | awk '{print $10}' | sed 's/ms//')

label2="node_netstat_ping_loss"
node_netstat_ping_loss=$(ping -q -A -s 500 -W 1000 -c 100 $prometheus_url | grep 'transmitted' | awk '{print $6}' | sed 's/%//')

echo "$label1 $node_netstat_ping_delay" |curl --data-binary @- http://${prometheus_url}:9091/metrics/job/pushgateway/instance/${hostname}

echo "$label2 $node_netstat_ping_loss" |curl --data-binary @- http://${prometheus_url}:9091/metrics/job/pushgateway/instance/${hostname}

定义2个metrics监控项名称 node_netstat_ping_delay 和 node_netstat_ping_loss

label1="node_netstat_ping_delay"
node_netstat_ping_delay=$(ping -q -A -s 500 -W 1000 -c 100 $prometheus_url | grep 'transmitted' | awk '{print $10}' | sed 's/ms//')

label2="node_netstat_ping_loss"
node_netstat_ping_loss=$(ping -q -A -s 500 -W 1000 -c 100 $prometheus_url | grep 'transmitted' | awk '{print $6}' | sed 's/%//')

下面将 $label1 $node_netstat_ping_delay 使用curl发送到gateway

echo "$label1 $node_netstat_ping_delay" |curl --data-binary @- http://${prometheus_url}:9091/metrics/job/pushgateway/instance/${hostname}

${prometheus_url}:9091指向的是pushgateway地址,因为我的pushgateway是放在prometheus机器上的。

metrics是固定的

/job/pushgateway 指向的是我们prometheus.yml刚刚配置的job,这里job是固定的,然后pushgateway是我配置文件里面的job名称

/instance/${hostname} 指定我们命名的instance,这里我们就以脚本所在的hostname命名instance

添加crontab定时任务

重新刷新浏览器prometheus页面