zoukankan      html  css  js  c++  java
  • prometheus使用四(alertmanager&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即可。选择

     有丰富的类型可以选择

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

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

     二.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,可以看到有两个注册上了

     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已发现了这些目标。

  • 相关阅读:
    Tensorflow中张量的数学运算
    TensorFlow2.0中tf.concat和tf.stack的区别
    机器学习之K-Means(聚类分析)
    机器学习之线性回归
    机器学习之随机森林
    机器学习之决策树
    python爬取FTP文件,并批量下载到本地。
    ATOM系列之-atom报错"Cannot load the system dictionary for zh-CN"
    开机出现loading (hd0)/ntldr。。。
    如何制作一张符合上传的照片
  • 原文地址:https://www.cnblogs.com/lpcyj/p/13411071.html
Copyright © 2011-2022 走看看