prometheus使用四(alertmanager&grafana告警及服务发现)

摘要:
1、 普罗米修斯警报普罗米修斯被监控和显示后,我们可以看到各种指示器,但没有警报仍然很不方便。以上案例未通过生产测试,需要调试后使用。Grafana报警可通过添加通道触发。您可以选择各种类型并将它们添加到仪表板上的任何面板。第一个有点复杂,但报警规则灵活及时。第二种配置简单,通知类型丰富。

一.prometheus告警

prometheus有了监控和展示后,我们可以看到各种指标,但没有告警的话还是不方便。

(1)alertmanager告警

 

1.下载安装

下载地址:https://prometheus.io/download/

tar -xvf alertmanager-0.20.0.linux-amd64.tar.gz

alertnatives --version

启动
./alertmanager --config.file=simple.yml

后台启动
nohup ./alertmanager &

 

2.编辑Prometheus配置文件prometheus.yml,并添加以下内容:

1 alerting:
2 alertmanagers:
3 - static_configs:
4 targets: ['localhost:9093']
5 
6 rule_files:
7 
8  -"/usr/local/alertmanager_rules.yml"   #报警规则邮件

3.编写alertmanager.yml文件

global:
resolve_timeout: 5m
wechat_api_corp_id: "ww8b888888"
wechat_api_url: "https://qyapi.weixin.qq.com/cgi-bin"
wechat_api_secret: "FTzXYrR123123dsf"

templates:
- 'template/*.tmpl'

route:
group_by: ['alertname'] # 报警分组依据
group_wait: 10s # 最初即第一次等待多久时间发送一组警报的通知
group_interval: 10s # 在发送新警报前的等待时间
repeat_interval: 2m # 发送重复警报的周期 对于email配置中,此项不可以设置过低,否则将会由于邮件发送太多频繁,被smtp服务器拒绝
receiver: 'webhook' # 发送警报的接收者的名称,以下receivers name的名称


#两种方式,1转到特定url报警
receivers:
- name: 'webhook'
webhook_configs:
- url: 'http://ip:8080/user/alert'

#2.微信报警
receivers:
- name: 'wechat'
wechat_configs: # 企业微信报警配置
- send_resolved: true
to_party: '2' # 接收组的id
agent_id: '1000002' # (企业微信-->自定应用-->AgentId)
corp_id: 'ww8b888888' # 企业信息(我的企业-->CorpId[在底部])
api_secret: 'FTzXYrR123123dsf' # 企业微信(企业微信-->自定应用--

 

4.编写alertmanager_rules.yml文件

groups:

 - name: alertmanager_rules

   rules:

   - alert: InstanceDown # 告警名称

     expr: up == 0 # 告警的判定条件,参考Prometheus高级查询来设定

     for: 2m # 满足告警条件持续时间多久后,才会发送告警

     labels: #标签项

      team: node

     annotations: # 解析项,详细解释告警信息

      summary: "{{$labels.instance}}: has been down"

      description: "{{$labels.instance}}: job {{$labels.job}} has been down "

 

分别重启alertmanaget和prometheus完成。

以上案例未经过生产测试,需调试后使用。

(2)grafana告警

如果添加channel即可。选择

prometheus使用四(alertmanager&grafana告警及服务发现)第1张

 有丰富的类型可以选择

prometheus使用四(alertmanager&grafana告警及服务发现)第2张

然后在Dashboard上任意panel上添加即可

prometheus使用四(alertmanager&grafana告警及服务发现)第3张

两种报警方式,第一种配置稍复杂,但报警规则灵活,及时。第二种配置简单,通知类型丰富不用自己开发。各位可按需选择。

 二.prometheus服务发现

(1)基于consul的服务发现

1.安装参考这里  https://cloud.tencent.com/developer/article/1096705

consul装好后启动,修改prometheus.yml

添加个job

 -job_name: 'consul'

  consul_sd_config:

   -server: 'localhost:8500'

 在consul里添加服务即可

 可在网关里配置端口转发到安装机器的8500端口即可访问consul的ui,可以看到有两个注册上了

prometheus使用四(alertmanager&grafana告警及服务发现)第4张

 2.随着consul里服务的注册和删除,prometheus会自动监控,这样只维护consul即可,不需要修改prometheus的yml了

(2)基于文件的服务发现

 1.也是先配个job

 -job_name: 'prometheusfile'

  file_sd_configs:

  - files:['/usr/local/file/*.yml']

  refresh_interval: 5S #5s刷新一次

 2.写yml文件

  可以写多个,也可以在一个yml里写

 -targets: ['ip:1234‘]

  labels:

    job: "file1"

 #再写一个

 -targets: ['ip:2345']

  labels:

    job: "file2"

保存后发现,prometheus已发现了这些目标。

免责声明:文章转载自《prometheus使用四(alertmanager&grafana告警及服务发现)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇mysql的log_output参数OFBIZ安装下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

kubernetes 1.21部署 kubeprometheus

https://github.com/prometheus-operator/kube-prometheus 1. helm方式 helm repo add prometheus-community https://prometheus-community.github.io/helm-charts helm repo update helm search...

prometheus使用三(自定义监控指标实现)

  prometheus提供了一系列的export帮助采集各种容器和中间件的运行指标,但有时我们还需要更灵活的监控指标,介绍一下自定义监控指标        本文用来监控dubbo的服务提供者的被调用指标,包括调用次数,p99等。        首先引入jar包 <dependency> <groupId>io.prometheu...

Consul集群搭建

  近几年随着Docker容器技术、微服务等架构的兴起,人们开始意识到服务发现的必要性。微服务架构简单来说,是一种以一些微服务来替代开发单个大而全应用的方法, 每一个小服务运行在自己的进程里,并以轻量级的机制来通信, 通常是 HTTP RESTful API。微服务强调小快灵, 任何一个相对独立的功能服务不再是一个模块, 而是一个独立的服务。那么,当我们需...

prometheus 配置支持consul动态拉取

/etc/prometheus/prometheus.yml - job_name: 'consul'consul_sd_configs: - server: 'consul-dev.cmsp-dev.svc.cluster.local:8500'scrape_interval: 5s # my global config global:...

consul注册中心如何自动剔除下线服务

背景:consul注册中心,对已经下线的服务不会立刻剔除,过了很久才会剔除,这样会导致很多问题,例如a服务明明已经下线,但还在注册中心注册,这样其他服务可能就会访问到该服务 处理思路:我们可以监听spring的关闭事件,然后在该监听方法中剔除下线的服务即可:  注册中心已经有2个服务,第二个是我本地启动的服务,那么我们如何剔除它呢: @Component...

Consul--客户端访问Consul服务

根据上一篇Consul服务的注册和发现,那么客户端如何去访问我们的Consul服务?其实客户端访问Consul实际上是访问Consul的服务实例。客户端自己可以实现对Consul服务实例的轮训,每次刷新端口都会发生改变,由于客户端访问Consul采用的轮训策略,所以每次刷新Consul的服务实例都会发生改变。 下面就直接上客户端访问Consul服务实例的代...