杨梅冲
每天在想什么呢?

一、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添加图形

 

 

posted on 2024-04-25 01:10  杨梅冲  阅读(146)  评论(0编辑  收藏  举报