blackbox_exporter监控web 并实现钉钉报警

2023-10-17 23:59

本文主要是介绍blackbox_exporter监控web 并实现钉钉报警,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1、介绍

Blackbox Exporter是Prometheus社区提供的官方黑盒监控解决方案,其允许用户通过:HTTP、HTTPS、DNS、TCP以及ICMP的方式对网络进行探测。
在prometheus中创建相关的alert rule,再由alert manager发送告警

2、在web服务器10.0.0.6安装blackbox_exporter

# 下载源码包并解压
wget https://github.com/prometheus/blackbox_exporter/releases/download/v0.23.0/blackbox_exporter-0.23.0.linux-amd64.tar.gz
tar zxvf  blackbox_exporter-0.23.0.linux-amd64.tar.gz  -C /usr/local/
ln -sv   blackbox_exporter-0.23.0.linux-amd64.tar.gz   blackbox_exporter
创建service文件设置开机启动
vim /usr/lib/systemd/system/black_exporter.service
[Unit]
Description=blackbox_exporter
After=network.target[Service]
User=root
Type=simple
ExecStart=/usr/local/blackbox_exporter/blackbox_exporter --config.file=/usr/local/blackbox_exporter/blackbox.yml
Restart=on-failure[Install]
WantedBy=multi-user.targetsystemctl enable --now blackbox_exporter.service
编辑配置文件blackbox.yml
modules:http_2xx:prober: httptimeout: 5shttp:valid_http_versions:- "HTTP/1.1"- "HTTP/2"valid_status_codes: []  # Defaults to 2xxenable_http2: falsemethod: GETno_follow_redirects: false# fail_if_ssl为true时,表示如果站点启用了SSL则探针失败,反之成功; # fail_if_not_ssl刚好相反;fail_if_ssl: falsefail_if_not_ssl: false#  fail_if_body_matches_regexp, fail_if_body_not_matches_regexp, fail_if_header_matches, fail_if_header_not_matches#  可以定义一组正则表达式,用于验证HTTP返回内容是否符合或者不符合正则表达式的内容fail_if_body_matches_regexp:- "Could not connect to database"tls_config:insecure_skip_verify: falsepreferred_ip_protocol: "ip4" # defaults to "ip6"http_post_2xx:prober: httphttp:method: POSTtcp_connect:prober: tcppop3s_banner:prober: tcptcp:query_response:- expect: "^+OK"tls: truetls_config:insecure_skip_verify: falsegrpc:prober: grpcgrpc:tls: truepreferred_ip_protocol: "ip4"grpc_plain:prober: grpcgrpc:tls: falseservice: "service1"ssh_banner:prober: tcptcp:query_response:- expect: "^SSH-2.0-"- send: "SSH-2.0-blackbox-ssh-check"irc_banner:prober: tcptcp:query_response:- send: "NICK prober"- send: "USER prober prober prober :prober"- expect: "PING :([^ ]+)"send: "PONG ${1}"- expect: "^:[^ ]+ 001"icmp:prober: icmpicmp_ttl5:prober: icmptimeout: 5sicmp:ttl: 5
编辑prometheus配置文件
vim /usr/local/prometheus/prometheus.yml
#添加- job_name: 'blackbox'metrics_path: /probeparams:module: [http_2xx]  # Look for a HTTP 200 response.static_configs:- targets:- 10.0.0.6- www.google.com- www.baidu.comrelabel_configs:- source_labels: [__address__]target_label: __param_target- source_labels: [__param_target]target_label: instance- target_label: __address__replacement: "10.0.0.6:9115"  # Blackbox exporter.- target_label: regionreplacement: "remote"
重启服务

在这里插入图片描述
在这里插入图片描述

3 创建告警规则

编辑rule文件
#修改prometheus配置启用告警规则
vim /usr/local/prometheus/prometheus.yml
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.0.0.5:9093# - alertmanager:9093# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:- rules/alert-rules-*.yml# - "first_rules.yml"# - "second_rules.yml"vim /usr/local/prometheus/rules/alert-rules-blackbox-exporter.ymlgroups:
- name: blackboxrules:# Blackbox probe failed- alert: BlackboxProbeFailedexpr: probe_success == 0for: 0mlabels:severity: criticalannotations:summary: Blackbox probe failed (instance {{ $labels.instance }})description: "Probe failed\n  VALUE = {{ $value }}\n  LABELS = {{ $labels }}"# Blackbox slow probe- alert: BlackboxSlowProbeexpr: avg_over_time(probe_duration_seconds[1m]) > 1for: 1mlabels:severity: warningannotations:summary: Blackbox slow probe (instance {{ $labels.instance }})description: "Blackbox probe took more than 1s to complete\n  VALUE = {{ $value }}\n  LABELS = {{ $labels }}"# Blackbox probe HTTP failure- alert: BlackboxProbeHttpFailureexpr: probe_http_status_code <= 199 OR probe_http_status_code >= 400for: 0mlabels:severity: criticalannotations:summary: Blackbox probe HTTP failure (instance {{ $labels.instance }})description: "HTTP status code is not 200-399\n  VALUE = {{ $value }}\n  LABELS = {{ $labels }}"# Blackbox probe slow HTTP- alert: BlackboxProbeSlowHttpexpr: avg_over_time(probe_http_duration_seconds[1m]) > 1for: 1mlabels:severity: warningannotations:summary: Blackbox probe slow HTTP (instance {{ $labels.instance }})description: "HTTP request took more than 1s\n  VALUE = {{ $value }}\n  LABELS = {{ $labels }}"# Blackbox probe slow ping- alert: BlackboxProbeSlowPingexpr: avg_over_time(probe_icmp_duration_seconds[1m]) > 1for: 1mlabels:severity: warningannotations:summary: Blackbox probe slow ping (instance {{ $labels.instance }})description: "Blackbox ping took more than 1s\n  VALUE = {{ $value }}\n  LABELS = {{ $labels }}"
重启服务验证

在这里插入图片描述
在这里插入图片描述

4、安装alert manager

###二进制安装alert manager

wget https://github.com/prometheus/alertmanager/releases/download/v0.25.0/alertmanager-0.25.0.linux-amd64.tar.gz
tar -zxvf alertmanager-0.25.0.linux-amd64.tar.gz -C /usr/local/
ln -sv alertmanager-0.25.0.linux-amd64.tar.gz   alertmanager 
###编辑service文件设置开机启动
[Unit]
Description=alertmanager
After=network.target[Service]
User=root
Type=simple
ExecStart=/usr/local/alertmanager --config.file=/usr/local/alertmanager/alertmanager.yml
Restart=on-failure[Install]
WantedBy=multi-user.targetsystemctl enable --now alertmanager.service

下载钉钉插件

wget https://github.com/timonwong/prometheus-webhook-dingtalk/releases/download/v2.1.0/prometheus-webhook-dingtalk-2.1.0.linux-amd64.tar.gz
tar -zxvf  prometheus-webhook-dingtalk-2.1.0.linux-amd64.tar.gz  -C /usr/local
ln -sv  prometheus-webhook-dingtalk-2.1.0.linux-amd64.tar.gz    dingtalk
钉钉设置webhook获取地址

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

编辑prometheus-webhook-dingtalk的配置文件
vim config.example.yml
## Request timeout
# timeout: 5s## Uncomment following line in order to write template from scratch (be careful!)
#no_builtin_template: true## Customizable templates path
templates:- ./templates/*.tmpl # 这里指向你生成的模板## You can also override default template using `default_message`
## The following example to use the 'legacy' template from v0.3.0
#default_message:
#  title: '{{ template "legacy.title" . }}'
#  text: '{{ template "legacy.content" . }}'## Targets, previously was known as "profiles"
targets:webhook1:# 钉钉机器人的webhookurl: https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx# secret for signature 加签后得到的值secret: xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
#  webhook2:
#    url: https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxxxxxx
#  webhook_legacy:
#    url: https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxxxxxx
#    # Customize template content
#    message:
#      # Use legacy template
#      title: '{{ template "legacy.title" . }}'
#      text: '{{ template "legacy.content" . }}'
#  webhook_mention_all:
#    url: https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxxxxxx
#    mention:
#      all: true
#  webhook_mention_users:
#    url: https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxxxxxx
#    mention:
#      mobiles: ['156xxxx8827', '189xxxx8325']
编辑告警模板文件

mkdir templates
vim templates/template.tmpl

{{ define "__subject" }}
[{{ .Status | toUpper }}{{ if eq .Status "firing" }}:{{ .Alerts.Firing | len }}{{ end }}]
{{ end }}{{ define "__alert_list" }}{{ range . }}
---
{{ if .Labels.owner }}@{{ .Labels.owner }}{{ end }}**告警名称**: {{ index .Annotations "title" }} **告警级别**: {{ .Labels.severity }} **告警主机**: {{ .Labels.instance }} **告警信息**: {{ index .Annotations "description" }}**告警时间**: {{ dateInZone "2006.01.02 15:04:05" (.StartsAt) "Asia/Shanghai" }}
{{ end }}{{ end }}{{ define "__resolved_list" }}{{ range . }}
---
{{ if .Labels.owner }}@{{ .Labels.owner }}{{ end }}**告警名称**: {{ index .Annotations "title" }}**告警级别**: {{ .Labels.severity }}**告警主机**: {{ .Labels.instance }}**告警信息**: {{ index .Annotations "description" }}**告警时间**: {{ dateInZone "2006.01.02 15:04:05" (.StartsAt) "Asia/Shanghai" }}**恢复时间**: {{ dateInZone "2006.01.02 15:04:05" (.EndsAt) "Asia/Shanghai" }}
{{ end }}{{ end }}{{ define "default.title" }}
{{ template "__subject" . }}
{{ end }}{{ define "default.content" }}
{{ if gt (len .Alerts.Firing) 0 }}
**====侦测到{{ .Alerts.Firing | len  }}个故障====**
{{ template "__alert_list" .Alerts.Firing }}
---
{{ end }}{{ if gt (len .Alerts.Resolved) 0 }}
**====恢复{{ .Alerts.Resolved | len  }}个故障====**
{{ template "__resolved_list" .Alerts.Resolved }}
{{ end }}
{{ end }}{{ define "ding.link.title" }}{{ template "default.title" . }}{{ end }}
{{ define "ding.link.content" }}{{ template "default.content" . }}{{ end }}
{{ template "default.title" . }}
{{ template "default.content" . }}
配置prometheus的alert manager
vim /usr/local/alertmanager/alertmanager.yml
global:resolve_timeout: 1msmtp_smarthost: 'smtp.163.com:465'smtp_from: '17358273990@163.com'smtp_auth_username: '17358273990@163.com'smtp_auth_password: 'LAJLGFXHNVWPSOAG'smtp_hello: '@163.com'smtp_require_tls: false
route:group_by: ['alertname']group_wait: 30sgroup_interval: 10srepeat_interval: 1m#  receiver: 'email'receiver: 'dingding.webhook1'
receivers:- name: 'email'email_configs:- to: '1305783815@qq.com'send_resolved: true- name: 'dingding.webhook1'webhook_configs:- url: 'http://10.0.0.5:8060/dingtalk/webhook1/send' #这里的webhook1,根据我们在钉钉告警插件配置文件>中targets中指定的值做修改send_resolved: true
inhibit_rules:- source_match:severity: 'critical'target_match:severity: 'warning'equal: ['alertname', 'dev', 'instance']
启动prometheus-webhook-dingtalk
[root@Rocky8 webhook]#./prometheus-webhook-dingtalk --config.file=config.example.yml
测试告警

#重启服务测试告警
在这里插入图片描述

5、grafana

在这里插入图片描述

这篇关于blackbox_exporter监控web 并实现钉钉报警的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/228822

相关文章

基于 HTML5 Canvas 实现图片旋转与下载功能(完整代码展示)

《基于HTML5Canvas实现图片旋转与下载功能(完整代码展示)》本文将深入剖析一段基于HTML5Canvas的代码,该代码实现了图片的旋转(90度和180度)以及旋转后图片的下载... 目录一、引言二、html 结构分析三、css 样式分析四、JavaScript 功能实现一、引言在 Web 开发中,

SpringBoot中使用Flux实现流式返回的方法小结

《SpringBoot中使用Flux实现流式返回的方法小结》文章介绍流式返回(StreamingResponse)在SpringBoot中通过Flux实现,优势包括提升用户体验、降低内存消耗、支持长连... 目录背景流式返回的核心概念与优势1. 提升用户体验2. 降低内存消耗3. 支持长连接与实时通信在Sp

Conda虚拟环境的复制和迁移的四种方法实现

《Conda虚拟环境的复制和迁移的四种方法实现》本文主要介绍了Conda虚拟环境的复制和迁移的四种方法实现,包括requirements.txt,environment.yml,conda-pack,... 目录在本机复制Conda虚拟环境相同操作系统之间复制环境方法一:requirements.txt方法

Spring Boot 实现 IP 限流的原理、实践与利弊解析

《SpringBoot实现IP限流的原理、实践与利弊解析》在SpringBoot中实现IP限流是一种简单而有效的方式来保障系统的稳定性和可用性,本文给大家介绍SpringBoot实现IP限... 目录一、引言二、IP 限流原理2.1 令牌桶算法2.2 漏桶算法三、使用场景3.1 防止恶意攻击3.2 控制资源

springboot下载接口限速功能实现

《springboot下载接口限速功能实现》通过Redis统计并发数动态调整每个用户带宽,核心逻辑为每秒读取并发送限定数据量,防止单用户占用过多资源,确保整体下载均衡且高效,本文给大家介绍spring... 目录 一、整体目标 二、涉及的主要类/方法✅ 三、核心流程图解(简化) 四、关键代码详解1️⃣ 设置

Nginx 配置跨域的实现及常见问题解决

《Nginx配置跨域的实现及常见问题解决》本文主要介绍了Nginx配置跨域的实现及常见问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来... 目录1. 跨域1.1 同源策略1.2 跨域资源共享(CORS)2. Nginx 配置跨域的场景2.1

Python中提取文件名扩展名的多种方法实现

《Python中提取文件名扩展名的多种方法实现》在Python编程中,经常会遇到需要从文件名中提取扩展名的场景,Python提供了多种方法来实现这一功能,不同方法适用于不同的场景和需求,包括os.pa... 目录技术背景实现步骤方法一:使用os.path.splitext方法二:使用pathlib模块方法三

CSS实现元素撑满剩余空间的五种方法

《CSS实现元素撑满剩余空间的五种方法》在日常开发中,我们经常需要让某个元素占据容器的剩余空间,本文将介绍5种不同的方法来实现这个需求,并分析各种方法的优缺点,感兴趣的朋友一起看看吧... css实现元素撑满剩余空间的5种方法 在日常开发中,我们经常需要让某个元素占据容器的剩余空间。这是一个常见的布局需求

HTML5 getUserMedia API网页录音实现指南示例小结

《HTML5getUserMediaAPI网页录音实现指南示例小结》本教程将指导你如何利用这一API,结合WebAudioAPI,实现网页录音功能,从获取音频流到处理和保存录音,整个过程将逐步... 目录1. html5 getUserMedia API简介1.1 API概念与历史1.2 功能与优势1.3

Java实现删除文件中的指定内容

《Java实现删除文件中的指定内容》在日常开发中,经常需要对文本文件进行批量处理,其中,删除文件中指定内容是最常见的需求之一,下面我们就来看看如何使用java实现删除文件中的指定内容吧... 目录1. 项目背景详细介绍2. 项目需求详细介绍2.1 功能需求2.2 非功能需求3. 相关技术详细介绍3.1 Ja