Pormetheus - Pushgateway创建方法(实例:每10秒统计TCP_WAIT状态连接数)

转载自博客:https://blog.csdn.net/shm19990131/article/details/107180195/

整体的部署架构如下

 

介绍
pushgateway 是一种采用被动推送的方式(而不是 exporter主动获取)获取监控数据的 prometheus 插件

一、Pushgateway 安装
官网地址:https://prometheus.io/download/#pushgateway

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

daemonize 后台运行

daemonize -c /root/pushgateway-1.2.0.linux-amd64 /root/pushgateway-1.2.0.linux-amd64/pushgateway

配置文件: /usr/local/prometheus/prometheus.yml

 

行数据端口:

firewall-cmd --add-port=9090/tcp
firewall-cmd --add-port=9100/tcp
firewall-cmd --add-port=9091/tcp
firewall-cmd --add-port=9092/tcp

访问 Pushgateway: 正常 Metrics 里面是没有数据的,下图是两个节点提交过 数据之后的显示结果

http://192.168.168.11:9091/

 

二、prometheus 命令行格式
1、命令行使用
选用一个 新的Key 来做讲解

count_netstat_wait_connections (TCP 等待连接数)

自行编写的脚本 + pushgateway的方法,推送到 prometheus server 采集。是一个 guage 类型的数据。

 

gauge 类型的数据只输入 key 键名,就会直接形成人性化监控图。

gauge 类型的数据,相对于counter类型的数据,使用起来相对容器的多。需要很多函数来进行修饰,如:rate()、increase() 进行计算之后,才能获取人性化监控图。

三、自定义编写脚本的方法,pushgateway 采集数据
pushgateway 本身是没有任何抓取监控数据的功能的,它只是被动的等待推送。

实例:抓取 TCP waiting_connection 等待连接数量

vim /usr/local/pushgateway/shell/waiting_connection.sh

#!/bin/bash
instance_name=`hostname -f` # instance_name 取出主机名

if [ $instance_name == "localhost" ];   #要求主机名不能是 localhost,没法区分
then
echo "Mush FQDN hostname"
exit 1
fi

#For waitting connections

label="count_netstat_wait_connections"  # 定义 prometheus 命令行标签

count_netstat_wait_connections=`netstat -anpt | grep -i wait | wc -l`   #定义一个新的数值,统计 TCP_WAIT 的连接数

echo "$label:$count_netstat_wait_connections" # 输出格式:key/value(标签:取出的数值)


echo "$label  $count_netstat_wait_connections" | curl --data-binary @-  http://192.168.168.11:9091/metrics/job/pushgateway/instance/$instance_name

#最后把 key/value 推送给 pushgateway
#curl --data-binary : 将 HTTP POST请求中的数据发送给 pushgateway 服务器,与用户提交HTML表单时浏览的行为完全一样。
#HTTP POST 请求中的数据为纯 二进制数据。

以上脚本中最重要的是:

1、count_netstat_wait_connections=`netstat -anpt | grep -i wait | wc -l`

提取通过 linux 命令行获取到的 监控 TCP_WAIT 的数据

2、curl --data-binary @- http://192.168.168.11:9091/metrics/job/pushgateway/instance/$instance_name

用 POST 方式 把 key/value 推送给 pushgateway的URL地址
curl --data-binary:将 HTTP POST请求中的数据发送给 pushgateway 服务器
http://192.168.168.11:9091/metrics/job/pushgateway:pushgateway URL的主 location
job/pushgateway:第二部分第一个标签,推送到哪一个 prometheus.yml 定义的job里
instance/$instance_name:第二个标签,推送后显示的 机器名是什么
————————————————

pushgateway的一个案例

echo "weiyuanmetricname 120" | curl --data-binary @- http://127.0.0.1:9091/metrics/job/myjobname/labels/mylabelname/region/chengdu/syscode/j037x0

在pushgateway的web页面上的展示如下

 在普罗米修斯监控大盘效果如下

 

四、结合crontab 反复执行

结合 Crontab -e ,每分钟执行一次检测,并将结果提交到 pushgateway。

crontab -e

* * * * * sh /usr/local/pushgateway/waiting_connection.sh #每分钟执行一次

#如果想要按照秒钟来执行,按照以下来写

* * * * * sh /usr/local/pushgateway/waiting_connection.sh
* * * * * sleep 10;sh /usr/local/pushgateway/waiting_connection.sh
* * * * * sleep 20;sh /usr/local/pushgateway/waiting_connection.sh
* * * * * sleep 30;sh /usr/local/pushgateway/waiting_connection.sh
* * * * * sleep 40;sh /usr/local/pushgateway/waiting_connection.sh
* * * * * sleep 50;sh /usr/local/pushgateway/waiting_connection.sh

posted on 2024-08-21 16:34  luzhouxiaoshuai  阅读(12)  评论(0编辑  收藏  举报

导航