Prometheus-PushGateway自定义监控项

news/2024/5/19 0:03:42 标签: prometheus, pushgateway

文章目录

    • 一、前言
    • 二、PushGateway安装
    • 三、PushGateway的使用
    • 四、PushGateway脚本思路

一、前言

pushgateway相比较exporter是主动向服务器发送请求,pushgateway本身也是一个程序,可以运行在任意节点上(不是必须在被监控端),运行本身没有抓取数据的功能,它只是被动的等待推送过来,然后发现服务端。

二、PushGateway安装

1、GitHub下载解压
PushGateway下载地址:

wget https://github.com/prometheus/pushgateway/releases/download/v1.5.0/pushgateway-1.5.0.linux-amd64.tar.gz

2、运行pushgateway 默认端口是9091,可以使用--web.listen-address更改默认端口

tar zxf pushgateway-1.5.0.linux-amd64.tar.gz 
mv pushgateway-1.5.0.linux-amd64 /usr/local/pushgateway
cd /usr/local/pushgateway
./pushgateway --web.listen-address=:19091

PS:前台启动没有报错就可以 ^C 停止掉了,我们配置systemd管理!

3、配置system管理

cat > /usr/lib/systemd/system/pushgateway.service << EOF
[Unit]
Description=prometheus pushgateway
After=network.target

[Service]
Type=simple
User=root
Group=root
ExecStart=/usr/local/pushgateway/pushgateway --web.listen-address=:19091
ExecStop=/usr/bin/kill -HUP \$MAINPID

[Install]
WantedBy=multi-user.target
EOF

启动并加入开机自启动

systemctl enable pushgateway.service --now
systemctl status pushgateway.service

在这里插入图片描述

如上图,查看状态是active (running)表示已经起来了!

4、Prometheus配置文件中,添加PushGateway地址

  - job_name: "prometheus gateway"
    static_configs:
      - targets: ["localhost:19091"]
systemctl restart prometheus.service
systemctl status prometheus.service

5、验证
浏览器访问 IP:19091 验证pushgateway页面

验证Prometheus页面

三、PushGateway的使用

1、推送单个样本

echo "test_metric 1314521" | curl --data-binary @- http://localhost:19091/metrics/job/test_job

pushgateway页面可以看到推送的数据 如下图所示:

  • test_metric 1314521: 推送的键 值
  • job/test_job:标签job=test_job,多个标签直接往后添加即可
  • data-binary:发送post请求 以二进制数据

四、PushGateway脚本思路

使用shell脚本推送 netstat wait数量

#!/bin/bash
pushgateway="localhost:19091"
lable="count_netstat_wait_connections"
count_netstat_wait_connections=$(netstat -an|grep -i wait|wc -l)

if [ ${HOSTNAME} == "localhost" ];then
	echo "主机名不可使用 'localhost'" && exit 5
fi

# 这里定义了两个标签 job=pushgateway-1 hostname=${HOSTNAME}
echo "${lable} $count_netstat_wait_connections"|curl --data-binary @- http://${pushgateway}/metrics/job/pushgateway-1/hostname/${HOSTNAME}

执行脚本 检查是否能推送到pushgateway页面

使用定时任务执执行脚本,每3秒推送一次数据

crontab -e
* * * * * /usr/bin/sleep 3; /bin/bash /root/qinzt/pushgateway.sh

prometheus中查询数据


http://www.niftyadmin.cn/n/5019405.html

相关文章

【数据结构】红黑树的插入与验证

文章目录 一、基本概念1.时代背景2. 基本概念3.基本性质 二、实现原理1. 插入1.1变色1.2旋转变色①左旋②右旋③右左双旋④左右双旋 2.验证 源码总结 一、基本概念 1.时代背景 1972年鲁道夫拜尔(Rudolf Bayer)发明了一种数据结构&#xff0c;这是一种特殊的B树4阶情况。这些树…

go初识iris框架(四) -框架设置操作

前言 iris(1) iris(2) iris(3) 框架设置操作 当我们的一个路径是xxx/user/info,xxx/user/login,xxx/user/register的时候,我们发现前面都有一个user,我们如果用/{data:string}这样的话这样导致我们的路径是灵活的&#xff0c;所以我们得用其他方法 这里我们的路径是以anime为…

Go语言网络编程(并发)Goroutine

Go语言网络编程&#xff08;并发&#xff09;Goroutine 在java/c中我们要实现并发编程的时候&#xff0c;我们通常需要自己维护一个线程池&#xff0c;并且需要自己去包装一个又一个的任务&#xff0c;同时需要自己去调度线程执行任务并维护上下文切换&#xff0c;这一切通常会…

表的内连接和外连接

表的连接是SQL中的一种操作&#xff0c;用于将两个或多个表中的数据按照某个条件进行关联。 内连接 使用内连接将两个表(Table1 和 Table2)进行连接&#xff1a; select * from Table1 inner join Table2 on Table1.id Table2.id;举例&#xff1a; -- 用普通的写法 select…

【LeetCode-中等题】39. 组合总和

文章目录 题目方法一&#xff1a;递归回溯 题目 这题的nums数组里面不存在重复元素&#xff0c;所以也就无需做去重操作 但同一个元素可以被无限次取&#xff0c;说明每次递归中的for循环的开始位置就是自己 nums数组里面存在重复元素&#xff0c;去重版本&#xff1a; 方法一…

前端DOM操作精解:基础概念、方法与最佳实践

引言 本文将深入探讨前端开发中的DOM操作&#xff0c;包括基础概念、常用方法和最佳实践。通过清晰易懂的解释和实际案例分析&#xff0c;我们将一起了解如何最有效地使用DOM操作来提升前端应用的用户体验。 一、DOM操作入门 在深入探讨DOM操作之前&#xff0c;我们先要理解…

Effective C++条款22——将成员变置声明为private(设计与声明)

OK&#xff0c;下面是我的规划。首先带你看看为什么成员变量不该是public&#xff0c;然后让你看看所有反对public成员变量的论点同样适用于protected成员变量。最后导出一个结论:成员变量应该是private。获得这个结论后&#xff0c;本条款也就大功告成了。 好&#xff0c;现在…

python切分文本到Excel,以包含指定字符串 为标识符(txt)切分txt文本)

V1.0_(批量处理有待完善&#xff0c;目前只能一个一个来) 代码如下&#xff1a; import re import openpyxldef extract_and_save_dialogues_with_headers_to_excel(input_file, output_file):# 创建一个新的Excel工作簿workbook openpyxl.Workbook()sheet workbook.active…