朝阳区网站建设公司,施工企业降本增效的方法和措施,wordpress密码加密,wordpress demoprometheus-pushgateway安装
一. Pushgateway简介
Pushgateway为Prometheus整体监控方案的功能组件之一#xff0c;并做于一个独立的工具存在。它主要用于Prometheus无法直接拿到监控指标的场景#xff0c;如监控源位于防火墙之后#xff0c;Prometheus无法穿透防火墙并做于一个独立的工具存在。它主要用于Prometheus无法直接拿到监控指标的场景如监控源位于防火墙之后Prometheus无法穿透防火墙目标服务没有可抓取监控数据的端点等多种情况。
在类似场景中可通过部署Pushgateway的方式解决问题。当部署该组件后监控源通过主动发送监控数据到Pushgateway再由Prometheus定时获取信息实现资源的状态监控。
简单图
工作流程
a. 监控源通过Post方式发送数据到Pushgateway路径为/metrics。
b. Prometheus服务端设置任务定时获取Pushgateway上面的监控指标。
c. Prometheus拿到监控指标后根据配置的告警规则如果匹配将触发告警到Alertmanager同时Grafana可配置数据源调用Prometheus数据做为数据展示。
d. Alertmanager收到告警后根据规则转发到对应接收人及接收介质Grafana方面用户可登录并根据数据源的监控指标配置相关的图表展示 。
二. 安装部署
二进制安装
下载安装包
cd /usr/local
wget https://github.com/prometheus/pushgateway/releases/download/v1.4.3/pushgateway-1.4.3.linux-amd64.tar.gz
tar -xf pushgateway-1.4.3.linux-amd64.tar.gzsystem管理
启动服务默认端口为9091,可通过–web.listen-address更改监听端口
rootbj-1:/usr/local# cat /usr/lib/systemd/system/pushgateway.service
[Unit]
DescriptionPrometheus pushgateway
Requiresnetwork.target remote-fs.target
Afternetwork.target remote-fs.target
?
[Service]
Typesimple
Userroot
Grouproot
ExecStart/usr/local/pushgateway/pushgateway --persistence.file/usr/local/pushgateway/data/ --persistence.interval5m #保存时间5分钟
ExecReload/bin/kill -HUP $MAINPID
KillModeprocess
Restarton-failure
RestartSec5s
?
[Install]
WantedBymulti-user.target
三.prometheus添加配置
新增job pushgateway
vim /usr/local/prometheus/prometheus.yml- job_name: pushgatewayscrape_interval: 30shonor_labels: true #加上此配置exporter节点上传数据中的一些标签将不会被pushgateway节点的相同标签覆盖static_configs:- targets: [10.3.1.11:9091]labels:instance: pushgateway‘’查看target状态
四. 数据推送Pushgateway
pushgateway的数据推送支持两种方式Prometheus Client SDK推送和API推送。
1、Client SDK推送
Prometheus本身提供了支持多种语言的SDK可通过SDK的方式生成相关的数据并推送到pushgateway这也是官方推荐的方案。目前的SDK覆盖语言有官方的
Go
Java or Scala
Python
Ruby
也有许多第三方的详情可参见此链接https://prometheus.io/docs/instrumenting/clientlibs/
示例:
本示例以python为例讲解SDK的使用
from prometheus_client import Counter,Gauge,push_to_gateway
from prometheus_client.core import CollectorRegistryregistry CollectorRegistry()
data1 Gauge(gauge_test_metric,This is a gauge-test-metric,[method,path,instance],registryregistry)
data1.labels(methodget,path/aaa,instanceinstance1).inc(3)push_to_gateway(10.12.61.3:9091, jobalex-job,registryregistry)注解
第一、二行代码引入相关的Prometheus SDK
第五行代码创建相关的指标类型为Gauge。其中“gauge_test_metric”为指标名称This is a gauge-test-metric’为指标注释[‘method’,‘path’,‘instance’] 为指标相关的label。
第六行代码添加相关的label信息和指标value 值。
第六行代码push数据到pushgateway10.12.61.3:9091’为发送地址job指定该任务名称。
以上代码产生的指标数据等同如下
# HELP gauge_test_metric This is a gauge-test-metric
# TYPE gauge_test_metric gauge
gauge_test_metric{instanceinstance1,methodget,path/aaa} 3.02、Post推送Node-expoerter组件数据
安装好node_exporter,此处不多介绍 传送监控数据到pushgateway节点 对于传过去的监控项会添加此处定义的标签 jobtest instance10.2.1.11 hostnameip-10-2-1-11
curl 127.0.0.1:9100/metrics|curl --data-binary - http://10.3.1.11:9091/metrics/job/test/instance/10.2.1.11/hostname/ip-10-2-1-11
编写脚本
node_date.sh
#!/bin/bash
job_nameBj
hostname$(hostname)
HOST_IP$(hostname --all-ip-addresses | awk {print $1})/usr/bin/curl 127.0.0.1:9100/metrics|/usr/bin/curl --data-binary - http://sanming.f3322.net:9091/metrics/job/$job_name/instance/$HOST_IP/hostname/$hostname
crontab定时任务
#Ansible: node_date
* * * * * /bin/bash /usr/local/node_exporter/node_date.sh批量给node-exporter添加定时任务
Ansible剧本
rootbj-1:/opt/node_date# cat playbook.yml
- hosts: allremote_user: rootgather_facts: notasks:- name: 推送磁盘脚本copy: srcnode_date.sh dest/usr/local/node_exporter modeux- name: 设置定时任务cron: namenode_date job/bin/bash /usr/local/node_exporter/node_date.sh statepresent- name: 执行脚本shell: /bin/bash /usr/local/node_exporter/node_date.sh
删除某个实例的数据
curl -X DELETE http://10.3.1.11:9091/metrics/job/test/instance/10.2.1.11/hostname/ip-10-2-1-11
3、pushgateway脚本示例
(1)TCP连接
pushgateway本身没有任何抓取监控数据的功能它只能被动地等待数据被推送过来故需要用户自行编写数据采集脚本。
例采集TCP waiting_connection瞬时数量
mkdir -p /app/scripts/pushgatewaycat EOF /app/scripts/pushgateway/tcp_waiting_connection.sh
#!/bin/bash# 获取hostname且host不能为localhost
instance_namehostname -f | cut -d . -f 1
if [ $instance_name localhost ];thenecho Must FQDN hostnameexit 1
fi# For waiting connections
labelcount_netstat_wait_connetions
count_netstat_wait_connetionsnetstat -an | grep -i wait | wc -l
echo $label:$count_netstat_wait_connetions
echo $label $count_netstat_wait_connetions | curl --data-binary - http://localhost:9091/metrics/job/pushgateway/instance/$instance_nameEOFchmod x /app/scripts/pushgateway/tcp_waiting_connection.sh1)netstat -an | grep -i wait | wc -l该自定义监控的取值方法
2)实际上就是将K/V键值对通过POST方式推送给pushgateway格式如下
http://localhost:9091/metricspushgateway url job/pushgateway数据推送过去的第一个label即exported_job“pushgateway”类似prometheus.yml中定义的job instance/$instance_name数据推送过去的第一个label即exported_instance“deepin-PC”
2.定时执行脚本
crontab -e * * * * * /app/scripts/pushgateway/tcp_waiting_connection.sh /dev/null 21prometheus默认每15秒从pushgateway获取一次数据而cron定时任务最小精度是每分钟执行一次若想没15秒执行一次则
方法1sleep定义多条定时任务
* * * * * /app/scripts/pushgateway/tcp_waiting_connection.sh /dev/null 21
* * * * * * sleep 15; /app/scripts/pushgateway/tcp_waiting_connection.sh /dev/null 21
* * * * * * sleep 30; /app/scripts/pushgateway/tcp_waiting_connection.sh /dev/null 21
* * * * * * sleep 45; /app/scripts/pushgateway/tcp_waiting_connection.sh /dev/null 21方法2for循环
cat EOF /app/scripts/pushgateway/tcp_waiting_connection.sh
#!/bin/bash
time15
for (( i0; i60; iitime )); doinstance_namehostname -f | cut -d . -f 1if [ $instance_name localhost ];thenecho Must FQDN hostnameexit 1filabelcount_netstat_wait_connetionscount_netstat_wait_connetionsnetstat -an | grep -i wait | wc -lecho $label:$count_netstat_wait_connetionsecho $label $count_netstat_wait_connetions | curl --data-binary - http://localhost:9091/metrics/job/pushgateway/instance/$instance_namesleep $time
done
exit 0EOF
此时cron定时任务只需要定义一条
crontab -e * * * * * /app/scripts/pushgateway/tcp_waiting_connection.sh /dev/null 21注若解释器使用#!/bin/bash则调试时使用全路径或相对路径或者bash /app/scripts/pushgateway/tcp_waiting_connection.sh执行脚本若解释器使用#!/bin/sh则调试时使用sh /app/scripts/pushgateway/tcp_waiting_connection.sh执行脚本否则出现错误Syntax error: Bad for loop variable
3.promethues查看监控值count_netstat_wait_connetions
4.TCP等待连接数count_netstat_wait_connetions通过自定义脚本实现通过node_exporter也可实现
处于各种wait状态的TCP连接close_waittime_wait等也是日常排查负载网络负载服务器负载数据库负载等的一个重要指标一般wait类型的TCP过大时一定说明系统网络负载流量负载出现了问题原因多样网络问题访问请求量DDOS流量数据库CPU等都有可能
vi count_netstat_wait_connections.sh
#!/bin/bash
instance_namehostname -f | cut -d. -f1 #获取本机名用于后面的的标签
labelcount_netstat_wait_connections #定义key名
count_netstat_wait_connectionsnetstat -an | grep -i wait | wc -l #获取数据的命令
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则是指定机器名使用的就是脚本里获取的那个变量值
参考文档
Prometheus分布式监控
prometheus-pushgateway安装
Prometheus监控运维实战十一Pushgateway