**前置文章:**[Centos7.6安装Prometheus(普罗米修斯)监控](https://www.xiaoleizhang.com/index.php/archives/10/ "Centos7.6安装Prometheus(普罗米修斯)监控") **前置文章:**[Prometheus(普罗米修斯)安装Grafana可视化图形工具](https://www.xiaoleizhang.com/index.php/archives/23/ "Prometheus(普罗米修斯)安装Grafana可视化图形工具") **环境配置** **内网地址10.x.x.x网段与192.168.x.x网段并不互通,但是外网地址是可以互通的。这次主要监控的是前端服务器与后端数据库的网络链路** |服务器|内网IP地址|外网IP地址| | :------------: | :------------: | :------------: | |Prometheus服务器|10.60.147.101|117.50.173.41| |grafana服务器|10.60.15.202| | |前端服务器|192.168.0.150| | |后端数据库|192.168.0.172| | |Pushgateway服务器|192.168.0.153|106.75.22.105| ------------ ------------ # 一、Pushgateway简介 Pushgateway是Prometheus生态中一个重要工具,使用它的原因主要是: Prometheus采用pull模式,可能由于不在一个子网或者防火墙原因,导致Prometheus无法直接拉取各个target数据。在监控业务数据的时候,需要将不同数据汇总, 由Prometheus统一收集。 - 简单的说就是当prometheus的server与target不在同一网段网络不通,无法直接拉取target数据,需要使用pushgateway作为数据中转点。 由于以上原因,不得不使用pushgateway,但在使用之前,有必要了解一下它的一些弊端: - 将多个节点数据汇总到pushgateway, 如果pushgateway挂了,受影响比多个target 大。 - Prometheus拉取状态up只针对pushgateway, 无法做到对每个节点有效。 - Pushgateway可以持久化推送给它的所有监控数据。因此,即使你的监控已经下线,prometheus还会拉取到旧的监控数据,需要手动清理pushgateway不要的数据。 # 二、Pushgateway部署 ## 1、Pushgateway安装 Pushgateway安装包下载地址:https://prometheus.io/download/#pushgateway ![pushgateway下载.png](https://www.xiaoleizhang.com/usr/uploads/2021/11/1280327775.png) 下载后,上传至Pushgateway服务器 ```shell [root@192-168-0-153 ~]# ll total 8976 -rw-r--r-- 1 root root 9189846 Nov 13 13:44 pushgateway-1.4.2.linux-amd64.tar.gz [root@192-168-0-153 ~]# ``` 然后解压重命名 ```shell tar -xvf pushgateway-1.4.2.linux-amd64.tar.gz -C /usr/local mv /usr/local/pushgateway-1.4.2.linux-amd64 /usr/local/pushgateway ``` 把Pushgateway做成Service ```shell vim /etc/systemd/system/pushgateway.service ``` 内容如下 ```shell [Unit] Description=pushgateway After=network.target [Service] ExecStart=/usr/local/pushgateway/pushgateway Restart=on-failure [Install] WantedBy=multi-user.target ``` 启动服务以及设置开机自启 ```shell systemctl daemon-reload systemctl start pushgateway.service systemctl enable pushgateway.service systemctl status pushgateway.service ``` 确认端口(9091) ```shell [root@192-168-0-153 ~]# netstat -nltup|grep 9091 tcp6 0 0 :::9091 :::* LISTEN 3343/pushgateway [root@192-168-0-153 ~]# ``` ## 2、Prometheus服务器新增pushgateway的job 登录Prometheus服务器,在Prometheus配置文件下新增一个叫pushgateway的job ```shell vim /usr/local/prometheus/prometheus.yml ``` 在配置文件的最后加上如下内容 ```shell - job_name: 'pushgateway' static_configs: - targets: ['106.75.22.105:9091'] labels: instance: pushgateway ``` 保存退出后,重启下prometheus的服务 ```shell systemctl restart prometheus.service ``` 在Prometheus的web界面中,查看Targets中Pushgateway的状态,可以看到是UP的 ![Targets中pushgateway的状态.png](https://www.xiaoleizhang.com/usr/uploads/2021/11/2981984881.png) ## 3、在前端服务器内编写脚本监控网络并将格式转化为prometheus支持的 ### (1)编写Network.sh脚本 在前端服务器内编写Network.sh脚本 ```shell vim /root/Network.sh ``` 内容如下,注意192.168.0.172是ping的目标地址,192.168.0.153是Pushgateway的地址 ```shell #!/bin/bash # ##################################### #@brief 功能:监控网路丢包率和延迟 -s 是一个ping包的大小 -W 是延迟timeout -c 是发生多少数据包 #@author xiajing #@version 1.0 #@date 2021/01/13 #@log no ##################################### #shell Env #ping发包数 c_times=200 #IP列表数组 ip_arr=(192.168.0.172) for (( i = 0; i < ${#ip_arr[@]}; ++i )) do result=`timeout 16 ping -q -A -s 200 -W 250 -c $c_times ${ip_arr[i]}|grep transmitted|awk '{print $6,$10}'` if [ -z "$result" ] then value_lostpk=101 value_rrt=1000 echo "ykt_lostpk_gt_jd ${value_lostpk}" | curl --data-binary @- http://192.168.0.153:9091/metrics/job/ykt_icmp/instance/${ip_arr[i]} echo "ykt_rrt_gt_jd ${value_rrt}" | curl --data-binary @- http://192.168.0.153:9091/metrics/job/ykt_icmp/instance/${ip_arr[i]} else lostpk=$(echo $result|awk '{print $1}') rrt=$(echo $result|awk '{print $2}') value_lostpk=$(echo $lostpk | sed 's/%//g') value_rrt=$(echo $rrt |sed 's/ms//g') #value_rrt=$(($value_rrt/$c_times)) value_rrt=$(printf "%.5f" `echo "scale=5;$value_rrt/$c_times"|bc`) echo "ykt_lostpk_gt_jd ${value_lostpk}" | curl --data-binary @- http://192.168.0.153:9091/metrics/job/ykt_icmp/instance/${ip_arr[i]} echo "ykt_rrt_gt_jd ${value_rrt}" | curl --data-binary @- http://192.168.0.153:9091/metrics/job/ykt_icmp/instance/${ip_arr[i]} fi echo ${ip_arr[i]}"==="$value_lostpk"==="$value_rrt done ``` ### (2)编写计划任务脚本并添加计划任务 在前端服务器内编写计划任务脚本并添加计划任务 ```shell vim /root/crontab.sh ``` 内容如下 ```shell #!/bin/bash step=1 #间隔的秒数,不能大于60 for (( i = 0; i < 60; i=(i+step) )); do $(sh '/root/Network.sh') sleep $step done exit 0 ``` 添加计划任务 ```shell vim /etc/crontab ``` 在配置文件最后加上如下内容 ```shell * * * * * root sh /root/crontab.sh &>/dev/null ``` # 三、添加Grafana Dashboards ## 1、Prometheus web界面查看 去Prometheus的web界面查看是否已经有数据,从下图中我们可以看到已经有数据产生了 ![pushgateway在prometheus界面中的数据.png](https://www.xiaoleizhang.com/usr/uploads/2021/11/864373432.png) ## 2、在Grafana web界面中增加图表 登录Grafana的web界面,可以选择新增dashboard也可以在已有的dashboard里增加 ![Grafana增加dashboard.png](https://www.xiaoleizhang.com/usr/uploads/2021/11/2653963154.png) 按照如下图片所示配置 ![Grafana增加dashboard(2).png](https://www.xiaoleizhang.com/usr/uploads/2021/11/1642543418.png) ![Grafana增加dashboard(3).png](https://www.xiaoleizhang.com/usr/uploads/2021/11/160411034.png) 同样的我们可以再创建一个丢包率的图形监控(ykt_lostpk_gt_jd),最后的效果展示如下图所示 ![pushgateway网络监控展示.png](https://www.xiaoleizhang.com/usr/uploads/2021/11/894018206.png) 最后修改:2022 年 01 月 09 日 © 允许规范转载 赞 0 如果觉得我的文章对你有用,请随意赞赏