一、Pushgateway

1.Pushgateway简介

Pushgateway 是 Prometheus 生态中一个重要工具,使用它的原因主要是:

● Prometheus 采用 pull 模式,可能由于不在一个子网或者防火墙原因,导致 Prometheus 无法直接拉取各个 target 数据。
● 在监控业务数据的时候,需要将不同数据汇总, 由 Prometheus 统一收集。
● 当exporter不能满足需要时,也可以通过自定义(python、shell、java)监控我们想要的数据。 由于以上原因,不得不使用 pushgateway,但在使用之前,有必要了解一下它的一些弊端: ● 将多个节点数据汇总到 pushgateway, 如果 pushgateway 挂了,受影响比多个 target 大。
● Prometheus 拉取状态 up 只针对 pushgateway, 无法做到对每个节点有效。
● Pushgateway 可以持久化推送给它的所有监控数据。 因此,即使你的监控已经下线,prometheus 还会拉取到旧的监控数据,需要手动清理 pushgateway 不要的数据。

2. 二进制安装

官网下载地址https://prometheus.io/download/

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

tar xf pushgateway-1.5.1.linux-amd64.tar.gz
ls -l
mv pushgateway-1.5.1.linux-amd64 /opt/pushgateway
ls -l /opt/pushgateway # 更改pushgateway文件夹权限:
chown prometheus:prometheus -R /opt/pushgateway # 创建 systemd 服务
cat >/etc/systemd/system/pushgateway.service << "EOF" [Unit]
Description=Prometheus Push Gateway
After=network.target [Service]
Type=simple
User=prometheus
Group=prometheus
ExecStart=/opt/pushgateway/pushgateway
[Install]
WantedBy=multi-user.target
EOF systemctl daemon-reload
systemctl start pushgateway.service
systemctl enable pushgateway.service

3. docker 安装

docker run  -d -p 9091:9091 --restart=always --name pushgateway prom/pushgateway

cat >docker-compose.yaml <<EOF
version: '3.3'
services:
pushgateway:
image: prom/pushgateway
container_name: pushgateway
restart: always
expose:
- 9091
ports:
- "9091:9091"
EOF

docker-compose up -d

4.Prometheus配置

去pull拉取pushgateway收集到的数据。

#进入到prometheus安装目录
cd /data/docker-prometheus

通过cat在prometheus.yml文件末尾添加

cat >> prometheus/prometheus.yml <<"EOF"
- job_name: pushgateway
honor_labels: true #加上此配置,exporter节点上传数据中的一些标签将不会被pushgateway节点的相同标签覆盖
static_configs:
- targets: ['192.168.10.100:9091']
labels:
instance: pushgateway
EOF

重新加载配置:

curl -X POST http://localhost:9090/-/reload

查看:http://192.168.10.14:9090/targets?search=

5.向pushgateway推送监控数据

使用curl

正常情况我们会使用 Client SDK 推送数据到 pushgateway, 但是我们还可以curl调用 API 来管理, 例如:

● 向 {job="some_job"} 添加单条数据:
echo "some_metric 3.14" | curl --data-binary @- http://192.168.10.100:9091/metrics/job/some_job 添加更多更复杂数据,通常数据会带上 instance(some_instance为instance名), 表示来源位置:
cat <<EOF | curl --data-binary @- http://192.168.10.100:9091/metrics/job/some_job/instance/some_instance
# TYPE some_metric counter
some_metric{label="val1"} 42
# TYPE another_metric gauge
# HELP another_metric Just an example.
another_metric 2398.283
EOF 删除某个组下的某实例的所有数据:
curl -X DELETE http://192.168.10.100:9091/metrics/job/some_job/instance/some_instance 删除某个组下的所有数据:
curl -X DELETE http://192.168.10.100:9091/metrics/job/some_job 检查
http://192.168.10.14:9090/graph
http://192.168.10.100:9091/metrics

5.1 使用python

文档

安装prometheus_client模块

安装pip

apt install python3-pip

pip install prometheus_client

from prometheus_client import CollectorRegistry, Gauge, push_to_gateway

registry = CollectorRegistry()
g = Gauge('job_last_success_unixtime', 'Last time a batch job successfully finished', registry=registry)
g.set_to_current_time()
push_to_gateway('192.168.10.100:9091', job='batchA', registry=registry)

6.监控data数据目录下的文件数量(需求)

cat >>/opt/file_num.sh<<"EOF"
#!/bin/sh
FILENUM=`ls -l /data |sed 1d| wc -l`
echo "data_file_num ${FILENUM}" | curl --data-binary @- http://192.168.10.100:9091/metrics/job/test_job/instance/test
EOF # 定时任务
*/1 * * * * /bin/sh /opt/file_num.sh >/dev/null 2>&1 # python脚本
cat >>/opt/file_num.py<<"EOF"
from prometheus_client import CollectorRegistry, Gauge, push_to_gateway
import os path = '/data' # 输入文件夹地址
files = os.listdir(path) # 读入文件夹
num_png = len(files) # 统计文件夹中的文件个数 registry = CollectorRegistry()
g = Gauge('python_data_file_num', 'data file num', ['instance'], registry=registry)
g.labels('test').set(num_png)
push_to_gateway('192.168.10.100:9091', job='test_job', registry=registry)
EOF # 定时任务
*/1 * * * * /usr/bin/python3 /opt/file_num.py >/dev/null 2>&1

7.配置告警规则

例如:当data目录下的文件数量超过5,报警出来

cat >> prometheus/rules/pushgateway.yml <<"EOF"
groups:
- name: pushgateway
rules:
- alert: DataFileNum
expr: data_file_num > 5
for: 0m
labels:
severity: warning
annotations:
summary: 'data数据目录文件数过多'
description: "data数据目录文件数>5,当前数量:{{ $value }}"
EOF # 重载配置
curl -X POST http://localhost:9090/-/reload

8.grafana添加图形

15.prometheus之pushgateway自定义监控的更多相关文章

  1. prometheus自定义监控指标——实战

    上一节介绍了pushgateway的作用.优劣以及部署使用,本机通过几个实例来重温一下自定义监控指标是如何使用的. 一.监控容器启动时间(shell) 使用prometheus已经两个月了,但从未找到 ...

  2. prometheus自定义监控指标——入门

    grafana结合prometheus提供了大量的模板,虽然这些模板几乎监控到了常见的监控指标,但是有些特殊的指标还是没能提供(也可能是我没找到指标名称).受zabbix的影响,自然而然想到了自定义监 ...

  3. Prometheus Operator自定义监控项

    Prometheus Operator默认的监控指标并不能完全满足实际的监控需求,这时候就需要我们自己根据业务添加自定义监控.添加一个自定义监控的步骤如下: 1.创建一个ServiceMonitor对 ...

  4. 简单4步,利用Prometheus Operator实现自定义指标监控

    本文来自Rancher Labs 在过去的文章中,我们花了相当大的篇幅来聊关于监控的话题.这是因为当你正在管理Kubernetes集群时,一切都会以极快的速度发生变化.因此有一个工具来监控集群的健康状 ...

  5. Prometheus自定义监控内容

    Prometheus自定义监控内容 一.io.micrometer的使用 1.1 Counter 1.2 Gauge 1.3 Timer 1.4 Summary 二.扩展 相关内容原文地址: 博客园: ...

  6. Golang 基于Prometheus Node_Exporter 开发自定义脚本监控

    Golang 基于Prometheus Node_Exporter 开发自定义脚本监控 公司是今年决定将一些传统应用从虚拟机上迁移到Kubernetes上的,项目多而乱,所以迁移工作进展缓慢,为了建立 ...

  7. 基于Prometheus的Pushgateway实战

    一.Pushgateway 简介 Pushgateway 是 Prometheus 生态中一个重要工具,使用它的原因主要是: Prometheus 采用 pull 模式,可能由于不在一个子网或者防火墙 ...

  8. Grafana+Prometheus 搭建 JuiceFS 可视化监控系统

    作为承载海量数据存储的分布式文件系统,用户通常需要直观地了解整个系统的容量.文件数量.CPU 负载.磁盘 IO.缓存等指标的变化. JuiceFS 没有重复造轮子,而是通过 Prometheus 兼容 ...

  9. JVM基础系列第15讲:JDK性能监控命令

    查看虚拟机进程:jps 命令 jps 命令可以列出所有的 Java 进程.如果 jps 不加任何参数,可以列出 Java 程序的进程 ID 以及 Main 函数短名称,如下所示. $ jps 6540 ...

  10. 理解OpenShift(7):基于 Prometheus 的集群监控

    理解OpenShift(1):网络之 Router 和 Route 理解OpenShift(2):网络之 DNS(域名服务) 理解OpenShift(3):网络之 SDN 理解OpenShift(4) ...

随机推荐

  1. 【已失效】Xcode GUI 添加 SPM 依赖的时候访问不了 github,无视 git config proxy 配置解决方案

    此 openradar 中提出者指出了原因:Xcode 调用 libgit2 时传入了 GIT_PROXY_NONE,无视了 git config 中的 proxy 配置.作者说用了自己打的 libg ...

  2. KingbaseES V8R3集群运维案例之---备库状态‘down’修复

    ​ 案例说明: Kingbase V8R3集群,集群启动正常,备库数据库服务正常,流复制状态正常.但是备库在show pool_nodes下查看是'down'状态,通过pcp_attach_node重 ...

  3. debootstrap 命令行安装 debian12(stable) btrfs文件系统 uefi引导 (像arch一样)

    1,制作debian12 live 启动盘 2.联网,可以手机usb共享,可以用wpasupplicant连wifi 3.修改镜像列表 sudo nano /etc/apt/source.list 修 ...

  4. Scala 递归和尾递归

    1 package com.atguigu.function 2 3 object Recursion { 4 def main(args: Array[String]): Unit = { 5 // ...

  5. C++ 简单实现shared_ptr

    共享指针 管理指针的存储,提供有限的垃圾回收工具,并可能与其他对象共享该管理. shared_ptr类型的对象都能够获得指针的所有权并共享该所有权:一旦它们获得所有权,当最后一个所有者释放该所有权时, ...

  6. Zookeeper解决了什么问题?

    在公司中用到了zookeeper协调分布式系统,在这里记录下. (一). 首先是什么? 是一种适用于分布式应用程序的高性能协调服务.它在一个简单的界面中公开常见服务(如命名.配置管理.同步和组服务), ...

  7. OpenHarmony创新赛人气投票活动,最佳人气作品由你来定!

      12月1日至12月15日 十大入围作品线上投票激战正酣 最佳人气作品,由你来定!   投票链接:https://forums.openharmony.cn/forum.php?mod=viewth ...

  8. 【直播回顾】参与文档贡献,开启OpenHarmony社区贡献

      5月25日晚上19点,战"码"先锋第二期直播 <参与文档贡献,开启OpenHarmony社区贡献> ,在OpenHarmony社群内成功举行.   本期课程,由华为 ...

  9. WPF/MVVM模式入门教程(一):简介与规范

    引用:https://www.cnblogs.com/flh1/p/12421652.html 什么是MVVM模式? MVVM的全称是--Model.View.ViewModel,翻译过来就是:模型. ...

  10. MFC程序隐藏托盘+右键关闭菜单

    背景介绍: 我的程序是启动后,默认就隐藏到托盘中,等待http请求后,显示界面.所以最小化到托盘的代码,我是写在初始化里面.     正文: 一.自定义消息 WM_SHOWTASK #define W ...