一、pushgateway的作用
传统监控软件如Zabbix通常都建议使用push的方式来由客户端推送数据给服务端,这样可以减轻服务端压力。但是这样也有个弊端就是配置更为复杂,需要在每个agent上都配置server的地址才可以感知到server的存在。虽然Prometheus默认已经是采用pull的方式来主动拉取数据,但是在某些情况下还是会使用更灵活的push方式,比如需要监控的项目生命周期很短,需要主动上报数据给服务端,这个时候就可以使用Prometheus的pushgateway来实现push方式的监控。Prometheus不再需要自己去拉取数据,而是让用户通过自定义的Shell脚本把需要的数据发送给pushgateway,然后再由pushgateway推送数据给Prometheus。pushgateway是可以单独运行在任何节点上的插件,不一定要在被监控客户端上。
二、配置pushgateway方式监控
1、在Prometheus官网下载pushgateway组件。下载地址是https://prometheus.io/download/
2、将压缩包解压并运行pushgateway程序,默认会监听在9091端口
1
.
/pushgateway
3、修改prometheus的配置文件,给pushgateway定义一个job
1
2
3
-job_name:
'pushgateway_test'
#这个名称可以自定义,后面会用到
static_configs:
- targets: [
'192.168.100.110:9091'
] 
#这里是pushgateway的地址
4、自定义脚本采集数据,这里以监控timewait的数量为例
1
2
3
4
5
6
7
vi
count_netstat_wait_connections.sh
#!/bin/bash
instance_name=
-f |
-d
'.'</code>-f1<code>
</code>#获取本机名,用于后面的的标签
label=
"count_netstat_wait_connections"
#定义key名
count_netstat_wait_connections=
-an |
-i wait |
</code>-l<code>
</code>#获取数据的命令
echo
"$label: $count_netstat_wait_connections"
echo
"$label  $count_netstat_wait_connections"
| curl --data-binary @- http:
//server
.com:9091
/metrics/job/pushgateway_test/instance/
$instance_name 
#这里pushgateway_test就是prometheus主配置文件里job的名字,需要保持一致,这样数据就会推送给这个job。后面的instance则是指定机器名,使用的就是脚本里获取的那个变量值
5、将脚本加入到计划任务中,定时推送数据,如果希望监控时长小于一分钟,可以使用sleep方法
6、在Prometheus界面使用自定义的key获取数据测试
实例操作:
python;gutter:true;
python3 查询influxdb的数据</p>
<h1>!/usr/bin/env python3</h1>
<p>-<em>- encoding: utf-8 -</em>-</p>
<p>import datetime as dt
import re
import sys
import os
from influxdb import InfluxDBClient</p>
<p>def influxdb_query():
host = '10.96.66.79'
port = 8086
user = 'root'
password = 'root'
dbname = 'telegraf'
query = 'select last(value) from iostat_info;'</p>
<pre><code>client = InfluxDBClient(host, port, user, password, dbname)
</code></pre>
<p># print("Querying data: " + query)
result = client.query(query)
result_list = list(result)[0]
print(result_list[0]["last"])</p>
<p>influxdb_query()</p>
<pre><code>
shell 调用python的返回值, 并将值推送给 pushgateway
;gutter:true;
#!/bin/bash
instance_name="测试环境的iostat信息"
label="iostat_info_value"
iostat_info_value=python3 influxdb_query.py
echo "$label: ${iostat_info_value}"
echo "$label: ${iostat_info_value}" | curl --data-binary @- http://172.16.234.60:9091/metrics/job/pushgateway_test/instance/$instance_name
prometheus 配置 相关的 job_name 信息:
python;gutter:true;
- job_name: 'pushgateway_test'
scrape_interval: 5s
honor_labels: true
static_configs:
- targets: ['172.16.234.60:9091']</p>
<p>重启 prometheus: :kill -HUP $(ps aux|grep prometheus |awk 'NR==1{print $2}')</p>
<pre><code>
执行脚本 sh iostat_info_value.sh 查看promethus 信息, 数据已经产生
![Prometheus监控系统(4)pushgateway及自定义脚本](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230526/1152810-20220819175057460-185549194.png)
;gutter:true;
脚本监控gitlab的备份文件
cat gitlab_backup_monitor_to_prometheus.sh
#!/bin/bash
instance_name="gitlab备份文件监控"
label="gitlab_backup_file_monitor"
gitlabfile_nums_value=cd /data3/wjj/gitlabbak && ls |grep -c gitlab
echo "$label: ${gitlabfile_nums_value}"
echo "$label ${gitlabfile_nums_value}" | curl --data-binary @- http://47.*****.**:9091/metrics/job/pushgateway_test/instance/$instance_name
bash;gutter:true;
监控规则</p>
<p>cat pushgateway.yml
groups:
- name: gitlab-backup_file-monitor
rules:
- alert: gitlab-backup_file-failed
expr: gitlab_backup_file_monitor < 5
for: 1m
labels:
severity: critical
annotations:
summary: gitlab backup files is less than 5pcs.</p>
<pre><code> description: "gitlab backup process failed"
</code></pre>
<p>
Original: https://www.cnblogs.com/weifeng1463/p/16214867.html
Author: Oops!#
Title: Prometheus监控系统(4)pushgateway及自定义脚本
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/553866/
转载文章受原作者版权保护。转载请注明原作者出处!