prometheus + alertmanager + PrometheusAlert实现告警

prometheus 收集监控数据
alertmanager 制定告警路由
PrometheusAlert 连通告警webhook

一、prometheus配置

https://prometheus.io/download/

1.1、prometheus安装

包的下载直接wget就行,放在data目录下,解压后在prometheus目录下创建config和rule目录
配置了热重启,可按需配置数据保存天数

cat >/etc/systemd/system/prometheus.service<<EOF
[Unit]
Description=Prometheus Service
Wants=network-online.target
After=network-online.target[Service]
User=root
Group=root
Type=simple
ExecStart=/data/prometheus/prometheus --config.file=/data/prometheus/prometheus.yml --web.enable-lifecycle  --storage.tsdb.path=/data/prometheus/data
Restart=on-failure[Install]
WantedBy=multi-user.target

1.2、prometheus配置文件

相同job使用了锚点省略一些重复配置,blackbox监控业务url

root@prometheus-monitor-c210:/data/prometheus# cat prometheus.yml 
# my global config
global:scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.# scrape_timeout is set to the global default (10s).# Alertmanager configuration
alerting:alertmanagers:- static_configs:- targets:- 10.198.199.210:9093
rule_files:- "/data/prometheus/rules/*.yaml"
# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.# - "first_rules.yml"# - "second_rules.yml"# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.- job_name: "prometheus"# metrics_path defaults to '/metrics'# scheme defaults to 'http'.static_configs:- targets: ["localhost:9090"]- job_name: 'node-exporter-ops'file_sd_configs:- files:- ./config/node-ops.ymlrelabel_configs: &node_relabel_configs- source_labels: [__address__]regex: (.*)target_label: instancereplacement: $1- source_labels: [__address__]regex: (.*)target_label: __address__replacement: $1:9100- job_name: 'node-exporter-rd'file_sd_configs:- files:- ./config/node-rd.ymlrelabel_configs: *node_relabel_configs- job_name: 'blackbox'metrics_path: /probeparams:module: [http_2xx]  # 使用的探测模块static_configs:- targets:- https://test-1254308391.cos.ap-beijing.myqcloud.com- https://www.baidu.comrelabel_configs:- source_labels: [__address__]target_label: __param_target- source_labels: [__param_target]target_label: service_url- target_label: __address__replacement: 10.198.199.28:9115  # Blackbox Exporter 的地址和端口

1.3、主机配置

主机配置文件放在config目录下,上面prometheus文件读取这个目录下的文件,而不是直接将target写在prometheus文件中
这些主机名配置在hosts文件中,aws可以使用route53私有托管

$ cat config/node-ops.yml 
- labels:service: node-opstargets:- sin-ops-logging-monitor-c210 - sin-ops-cicd-archery-c10

1.4、role配置

role都配置在统一目录

$ cat blackbox.yaml 
groups:- name: blackbox-tracerules:- alert: web服务超时了expr: probe_http_status_code{job="blackbox"} == 0for: 2mlabels:severity: criticalservice: probe-serviceannotations:title: 'Service timeout ({{ $labels.service_url }}'description: "Service timeout ({{ $labels.service_url }})"

node-baseic role

$ cat node-basic-monitor.yaml 
groups:- name: node-basic-monitorrules:- alert: 主机宕机了expr: up{job=~"node-exporter.+"} == 0for: 0mlabels:severity: criticalservice: node-basicannotations:title: 'Instance down'description: "主机: 【{{ $labels.instance }}】has been down for more than 1 minute"- alert: 服务器已重新启动expr: (node_time_seconds - node_boot_time_seconds) / 60 <3for: 0mlabels:severity: warningservice: node-basicannotations:title: '服务器已重新启动'description: "主机: 【{{ $labels.instance }}】刚刚启动,请检查服务状态"- alert: socket数量超预期expr: node_sockstat_TCP_alloc >25000for: 2mlabels:severity: criticalservice: node-basicannotations:title: 'socket数量超预期'description: "主机: 【{{ $labels.instance }}】socket(当前值:{{ $value }})"
# ops cpu内存告警- alert: CPU使用率到达%90expr: (1-((sum(increase(node_cpu_seconds_total{mode="idle"}[5m])) by (instance))/ (sum(increase(node_cpu_seconds_total[5m])) by (instance))))*100 > 90for: 5mannotations:title: "CPU使用率到达%90"description: "主机: 【{{ $labels.instance }}】 5分钟内CPU使用率超过90% (当前值: {{ printf \"%.2f\" $value }}%)"labels:severity: 'critical'service: node-basic  - alert: 内存使用率到达%90expr: (1-((node_memory_Buffers_bytes + node_memory_Cached_bytes + node_memory_MemFree_bytes)/node_memory_MemTotal_bytes))*100 >90for: 3mannotations:title: "主机内存使用率超过90%"description: "主机: 【{{ $labels.instance }}】 内存使用率超过90% (当前使用率:{{ humanize $value }}%)"labels:severity: 'critical'service: node-basic- alert: 磁盘空间剩余%15expr: (1-(node_filesystem_free_bytes{fstype=~"ext4|xfs",mountpoint!~".*tmp|.*boot"}/node_filesystem_size_bytes{fstype=~"ext4|xfs",mountpoint!~".*tmp|.*boot",service!~"benchmark" }))*100 > 90for: 1mannotations:title: "磁盘空间剩余%15"description: "主机: 【{{ $labels.instance }}】 {{ $labels.mountpoint }}分区使用率超过85%, 当前值使用率:{{ $value }}%"labels:severity: 'critical'service: node-basic- alert: 主机ESTABLISHED连接数expr:  sum(node_netstat_Tcp_CurrEstab{instance!~"benchmark.+"}) by (instance) > 1000for: 10mlabels:severity: 'warning'service: node-basicannotations:title: "主机ESTABLISHED连接数过高"description: "主机: 【{{ $labels.instance }}】 ESTABLISHED连接数超过1000, 当前ESTABLISHED连接数: {{ $value }}"- alert: 主机TIME_WAIT数量expr:  sum(node_sockstat_TCP_tw{instance!~"benchmark.+"}) by (instance) > 6500for: 1hlabels:severity: 'warning'service: node-basicannotations:title: "主机TIME_WAIT连接数过高"description: "主机: 【{{ $labels.instance }}】 TIME_WAIT连接数超过6500, 当前TIME_WAIT连接数持续一小时: {{ $value }}"- alert: 主机网卡出口流量过高于150MB/sexpr: sum by (instance, device) (rate(node_network_transmit_bytes_total{device=~"ens.*"}[2m])) / 1024 / 1024 > 150for: 20mlabels:severity: 'critical'service: node-basicannotations:title: "主机网卡出口流量高于100MB/s"description: "主机: 【{{ $labels.instance }}】, 网卡: {{ $labels.device }} 出口流量超过 (> 100 MB/s), 当前值: {{ $value }}"- alert: 主机磁盘写入速率过高expr: sum by (instance, device) (rate(node_disk_written_bytes_total{device=~"nvme.+"}[2m])) / 1024 / 1024 > 150for: 10mlabels:severity: 'critical'service: node-basicannotations:title: "主机磁盘写入速率过高"description: "主机: 【{{ $labels.instance }}】, 磁盘: {{ $labels.device }} 写入速度超过(100 MB/s), 当前值: {{ $value }}"- alert: 主机分区Inode不足expr: node_filesystem_files_free{fstype=~"ext4|xfs",mountpoint!~".*tmp|.*boot" } / node_filesystem_files{fstype=~"ext4|xfs",mountpoint!~".*tmp|.*boot" } * 100 < 10for: 2mlabels:severity: 'warning'service: node-basicannotations:title: "主机分区Inode节点不足"description: "主机: 【{{ $labels.instance }}】 {{ $labels.mountpoint }}分区inode节点不足 (可用值小于{{ $value }}%)"- alert: 主机磁盘Read延迟高expr: rate(node_disk_read_time_seconds_total{device=~"nvme.+"}[1m]) / rate(node_disk_reads_completed_total{device=~"nvme.+"}[1m]) > 0.1 and rate(node_disk_reads_completed_total{device=~"nvme.+"}[1m]) > 0for: 2mlabels:severity: 'warning'service: node-basicannotations:title: "主机磁盘Read延迟过高"description: "主机: 【{{ $labels.instance }}】, 磁盘: {{ $labels.device }} Read延迟过高 (read operations > 100ms), 当前延迟值: {{ $value }}ms"- alert: 主机磁盘Write延迟过高expr: rate(node_disk_write_time_seconds_total{device=~"nvme.+"}[1m]) / rate(node_disk_writes_completed_total{device=~"nvme.+"}[1m]) > 0.1 and rate(node_disk_writes_completed_total{device=~"nvme.+"}[1m]) > 0for: 2mlabels:severity: 'warning'service: node-basicannotations:title: "主机磁盘Write延迟过高"description: "主机: 【{{ $labels.instance }}】, 磁盘: {{ $labels.device }} Write延迟过高 (write operations > 100ms), 当前延迟值: {{ $value }}ms"- alert: 服务器负载高expr: (node_load1{instance !~ "sin-cicd-DRONE_.+"} / count without (cpu, mode) (node_cpu_seconds_total{mode="system"})) > 1for: 30mlabels:severity: warningservice: node-basicannotations:title: 服务器负载高description: "{{ $labels.instance }} 每颗cpu的平均负载到达 {{ $value }} "

1.5、热加载

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

二、alertmanager配置

2.1、alertmanager安装

安装到/data下,配置systemd启动

cat >/etc/systemd/system/alertmanager.service<<EOF
[Unit]
Description=Alertmanager Service
Wants=network-online.target
After=network-online.target[Service]
User=root
Group=root
Type=simple
ExecStart=/data/alertmanager/alertmanager --config.file=/data/alertmanager/alertmanager.yml
Restart=on-failure[Install]
WantedBy=multi-user.target
EOF

2.2、alertmanager配置文件

这里的808是PrometheusAlert提供的webhook端口,三种不同级别的告警通知到三个机器人

cat alertmanager.yml 
global:resolve_timeout: 1m
route:receiver: 'default'group_by: ['alertname']group_wait: 10sgroup_interval: 10srepeat_interval: 30mroutes:- receiver: 'node-critical'group_wait: 10s continue: truematchers:- 'service=~"node-basic|probe-service"'- 'severity="critical"'- receiver: 'node-warning'group_wait: 10scontinue: truematchers:- 'service=~"node-basic"'- 'severity="warning"'
receivers:- name: 'default'webhook_configs:- url: 'http://127.0.0.1:8080/prometheusalert?type=fs&tpl=prometheus-fs&fsurl=https://open.feishu.cn/open-apis/bot/v2/hook/9264abcdc9c5'send_resolved: true- name: 'node-critical'webhook_configs:- url: 'http://127.0.0.1:8080/prometheusalert?type=fs&tpl=prometheus-fs&fsurl=https://open.feishu.cn/open-apis/bot/v2/hook/2fc0f6048e0'send_resolved: true- name: 'node-warning'webhook_configs:- url: 'http://127.0.0.1:8080/prometheusalert?type=fs&tpl=prometheus-fs&fsurl=https://open.feishu.cn/open-apis/bot/v2/hook/74dbb053ae33'send_resolved: true
inhibit_rules:- source_match:severity: 'critical'target_match:severity: 'warning'equal: ['alertname', 'dev', 'instance']

三、PrometheusAlert配置

拉包https://github.com/feiyu563/PrometheusAlert
启动后配置模板

3.1、模板配置

我这里是飞书通知,所以修改飞书的模板
在这里插入图片描述

{{ range $k,$v:=.alerts }}{{if eq $v.status "resolved"}}<font color="green">**{{$v.labels.service}}恢复信息**</font>
事件: **{{$v.labels.alertname}}**
告警类型:{{$v.status}}
告警级别: {{$v.labels.severity}}
开始时间: {{GetCSTtime $v.startsAt}}
结束时间: {{GetCSTtime $v.endsAt}}
主机: {{$v.labels.instance}}
服务组: {{$v.labels.service}}
<font color="green">**事件详情: {{$v.annotations.description}}**</font>
{{else}}**{{$v.labels.service}}告警**
事件: **{{$v.labels.alertname}}**
告警类型:{{$v.status}}
告警级别: {{$v.labels.severity}}
开始时间: {{GetCSTtime $v.startsAt}}
主机: {{$v.labels.instance}}
服务组: {{$v.labels.service}}
<font color="red">**事件详情: {{$v.annotations.description}}**</font>
{{end}}
{{ end }}

优化了一下告警内容,我这里使用prometheus-fsv2发送报错,只能用v1发送,暂时没考虑k8s发送告警

3.2、优化告警标题

https://github.com/feiyu563/PrometheusAlert/blob/master/controllers/prometheusalert.go
重新定义告警Title的值,默认是配置文件指定的,但任何告警都是一个标题

        } else {json.Unmarshal(c.Ctx.Input.RequestBody, &p_json)//针对prometheus的消息特殊处理json.Unmarshal(c.Ctx.Input.RequestBody, &p_alertmanager_json)}
//大概113行title := p_alertmanager_json["alerts"].([]interface{})[0].(map[string]interface{})["labels"].(map[string]interface{})["alertname"].(string)// alertgroupalertgroup := c.Input().Get("alertgroup")openAg := beego.AppConfig.String("open-alertgroup")var agMap map[string]stringif openAg == "1" && len(alertgroup) != 0 {agMap = Alertgroup(alertgroup)}

更新完之后打个包启动

告警效果如下
在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.xdnf.cn/news/1551542.html

如若内容造成侵权/违法违规/事实不符,请联系一条长河网进行投诉反馈,一经查实,立即删除!

相关文章

完整网络模型训练(一)

文章目录 一、网络模型的搭建二、网络模型正确性检验三、创建网络函数 一、网络模型的搭建 以CIFAR10数据集作为训练例子 准备数据集&#xff1a; #因为CIFAR10是属于PRL的数据集&#xff0c;所以需要转化成tensor数据集 train_data torchvision.datasets.CIFAR10(root&quo…

养猪场饲料加工机械设备有哪些

养猪场饲料加工机械设备主要包括以下几类&#xff1a;1‌、粉碎机‌&#xff1a;主要用于将原料进行粉碎&#xff0c;以便与其他饲料原料混合均匀。常见的粉碎机有水滴式粉碎机和立式粉碎机两种&#xff0c;用户可以根据原料的特性选择适合的机型。2‌、搅拌机‌&#xff1a;用…

Element UI教程:如何将Radio单选框的圆框改为方框

大家好&#xff0c;今天给大家带来一篇关于Element UI的使用技巧。在项目中&#xff0c;我们经常会用到Radio单选框组件&#xff0c;默认情况下&#xff0c;Radio单选框的样式是圆框。但有时候&#xff0c;为了满足设计需求&#xff0c;我们需要将圆框改为方框&#xff0c;如下…

【新闻转载】Storm-0501:勒索软件攻击扩展到混合云环境

icrosoft发出警告&#xff0c;勒索软件团伙Storm-0501近期调整了攻击策略&#xff0c;目前正将目标瞄准混合云环境&#xff0c;旨在全面破坏受害者的资产。 该威胁行为者自2021年首次露面&#xff0c;起初作为Sabbath勒索软件行动的分支。随后&#xff0c;他们开始分发来自Hive…

华为OD机试 - 积木最远距离(Python/JS/C/C++ 2024 E卷 100分)

华为OD机试 2024E卷题库疯狂收录中&#xff0c;刷题点这里 专栏导读 本专栏收录于《华为OD机试真题&#xff08;Python/JS/C/C&#xff09;》。 刷的越多&#xff0c;抽中的概率越大&#xff0c;私信哪吒&#xff0c;备注华为OD&#xff0c;加入华为OD刷题交流群&#xff0c;…

MQTT--EMQX入门+MQTTX使用

目录 1、什么是EMQX&#xff1f;1.1 EMQX介绍1.2 EMQX特点1.3 与物联网之间的关系以及主要的产品主要的产品 2、安装启动2.1 基本命令2.2 目录结构 3、MQTTX客户端3.1 连接配置 总结PS: 1、什么是EMQX&#xff1f; 首先你得有MQTT的知识&#xff0c;不认识MQTT的小伙伴可以先看…

如何初步部署自己的服务器,达到生信分析的及格线2(待更新)

参考我的上一篇博客https://blog.csdn.net/weixin_62528784/article/details/142621762?spm1001.2014.3001.5501&#xff0c; 现在我们已经有了一个能够跑一些基础任务的、基本没有配置的服务器了&#xff0c;接下来要做的任务就是&#xff1a; &#xff08;1&#xff09;进一…

单片机毕业设计选题基于单片机的炉温度控制系统

** 文章目录 前言概要功能设计设计思路效果图 程序文章目录 前言 &#x1f497;博主介绍&#xff1a;✌全网粉丝10W,CSDN特邀作者、博客专家、CSDN新星计划导师&#xff0c;一名热衷于单片机技术探索与分享的博主、专注于 精通51/STM32/MSP430/AVR等单片机设计 主要对象是咱们…

深拷贝浅拷贝 JS代码实现

文章目录 JS数据类型深拷贝 & 浅拷贝赋值和浅拷贝的区别浅拷贝&#xff08;Shallow Copy&#xff09;代码实现 深拷贝&#xff08;Deep Copy&#xff09;代码实现 Map & WeakMap示例 WeakMap 和垃圾回收weakmap处理循环引用 typepf & instanceof JS数据类型 基本数…

满填充透明背景二维码生成

前几天项目上线的时候发现一个问题&#xff1a;通过Hutool工具包生成的二维码在内容较少时无法填满(Margin 已设置为 0)给定大小的图片。因此导致前端在显示二维码时样式异常。 从图片中我们可以看到&#xff0c;相同大小的图片&#xff0c;留白内容是不一样的。其中上半部分…

dwceqos网络驱动性能优化

文章介绍 本文会介绍优化QNX系统下io-pkt-v6-hc驱动模块cpu loading过高问题&#xff0c;经过优化可以降低约一半的cpu loading. 问题背景 激光雷达通过以太网发送数据到ADAS域控中&#xff0c;测试发现在激光功能激活的情况下&#xff0c;会出现比较明显的网络丢帧现象。 …

平安养老险深圳分公司积极开展“金融教育宣传月”活动,展现金融为民新风尚

2024年9月&#xff0c;平安养老险深圳分公司以“金融为民谱新篇&#xff0c;守护权益防风险”为主题&#xff0c;正式启动2024年“金融教育宣传月”活动&#xff0c;通过多样化开展进乡村、进商圈、进企业等宣传教育活动&#xff0c;将金融消保知识送达广大消费者身边&#xff…

光通信——PON技术

PON网络结构 PON&#xff08;Passive Optical Network&#xff0c;无源光网络&#xff09;系统的基本组成包括OLT&#xff08;Optical Line Terminal&#xff0c;光线路终端&#xff09;、ODN&#xff08;Optical Distribution Network&#xff0c;光分配单元&#xff09;和ON…

数据结构——队列的基本操作

前言 介绍 &#x1f343;数据结构专区&#xff1a;数据结构 参考 该部分知识参考于《数据结构&#xff08;C语言版 第2版&#xff09;》24~28页 &#x1f308;每一个清晨&#xff0c;都是世界对你说的最温柔的早安&#xff1a;ૢ(≧▽≦)و✨ 目录 前言 1、队列的基本概念…

Oracle 闪回版本(闪回表到指定SCN)

1.创建目录 mkdir /u01/app/oracle/flash 2.配置FRA alter system set db_recovery_file_dest_size15G; alter system set db_recovery_file_dest/u01/app/oracle/flash; 3.设置闪回参数--确保可以闪回48h内的数据库 alter system set db_flashback_retention_target2880; 4…

中关村环球时尚产业联盟 东晟时尚产业创新中心成立

2024年9月6日&#xff0c;中关村环球时尚产业联盟与东晟时尚创新科技&#xff08;北京&#xff09;有限公司于中关村科技园东城园举行了隆重的战略合作签约仪式。 中关村科技园东城园领导发表了致辞&#xff0c;并表示东城区作为首都北京的核心区域&#xff0c;拥有深厚的历史…

SW - 装配图旋转到一个想要的正视图

文章目录 SW - 装配图旋转到一个想要的正视图概述笔记将装配图旋转到自己想要的视图的方法保存当前视图选择自己保存的视图END SW - 装配图旋转到一个想要的正视图 概述 在弄装配图。 如果按照SW默认的视图&#xff0c;Y方向是反的。 原因在于我画零件图时&#xff0c;方向就…

从“抄袭”到“原创”:5个超实用的论文降重技巧!

AIPaperGPT&#xff0c;论文写作神器~ https://www.aipapergpt.com/ 每当写完一篇论文&#xff0c;松了一口气准备庆祝时&#xff0c;突然想到还有一个名叫“查重”的终极大Boss等着你&#xff0c;瞬间心情从云端跌入谷底。 是不是你&#xff1f; 很多同学在提交之前&#…

fatfs API使用手册

配置 /*---------------------------------------------------------------------------/ / Configurations of FatFs Module /---------------------------------------------------------------------------*/#define FFCONF_DEF 80286 /* Revision ID *//*---------------…

Spring IoC笔记

目录 1.什么是 IoC&#xff1f; 2.IoC类注解&#xff08;五大注解&#xff09; 2.1那为什么要这么多类注解&#xff1f; 2.2五大注解是不是可以混用&#xff1f; 2.3程序被spring管理的条件是&#xff1f; 3.bean对象 3.1Bean 命名约定 3.2获取bean对象 4.⽅法注解 B…