中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

基于k8s Prometheus+Grafana+Altermanager釘釘報警

發布時間:2020-05-31 02:12:09 來源:網絡 閱讀:12418 作者:李永峰Billy 欄目:云計算

相關推薦

1. 使用Prometheus Operator監控kubetnetes集群

2. 使用Prometheus Operator實現應用自定義監控

一、概述

Alertmanager與Prometheus是相互分離的兩個組件。Prometheus服務器根據報警規則將警報發送給Alertmanager,然后Alertmanager將silencing、inhibition、aggregation等消息通過電子郵件、dingtalk和HipChat發送通知。

Alertmanager處理由例如Prometheus服務器等客戶端發來的警報。它負責刪除重復數據、分組,并將警報通過路由發送到正確的接收器,比如電子郵件、Slack、dingtalk等。Alertmanager還支持groups,silencing和警報抑制的機制。

釘釘作為內部通訊工具,基本上大家在電腦和手機上都能用,消息可以第一時間查看,報警消息的即時性要求比較高,所以適合用釘釘通知。

二、添加釘釘機器人

請參考官方文檔:自定義機器人

添加機器人后獲取機器人的hook(機器人好像只能在釘釘群里面添加),在后面部署會用到。

機器人hook:https://oapi.dingtalk.com/robot/send?access_token=xxxxxx

三、配置Alertmanager

Alertmanager官方文檔:https://github.com/prometheus/docs/blob/db2a09a8a7e193d6e474f37055908a6d432b88b5/content/docs/alerting/configuration.md#webhook_config

修改Alertmanager報警配置,因上面的官方文檔已經給出來每個參數的詳細信息,就不再一一解釋了。

[root@node-01 prometheus]# vim prometheus-operator/values.yaml 

  config:
    global:
      resolve_timeout: 2m
    route:
      group_by: ['job']
      group_wait: 30s
      group_interval: 2m
      repeat_interval: 12h
      receiver: 'webhook'
      routes:
      - match:
          alertname: DeadMansSwitch
        receiver: 'webhook'
    receivers:
    - name: 'webhook'
      webhook_configs:
      - url: http://webhook-dingtalk/dingtalk/send/
        send_resolved: true

更新prometheus-operator

[root@node-01 prometheus]# helm upgrade p ./prometheus-operator

修改成功后可以在alertmanager的status頁面看到相關配置
基于k8s Prometheus+Grafana+Altermanager釘釘報警

四、轉換alertmanager數據格式

Alertmanager會以下列JSON格式的數據通過HTTP POST請求發送到端點:

{
  "version": "4",
  "groupKey": <string>,    // key identifying the group of alerts (e.g. to deduplicate)
  "status": "<resolved|firing>",
  "receiver": <string>,
  "groupLabels": <object>,
  "commonLabels": <object>,
  "commonAnnotations": <object>,
  "externalURL": <string>,  // backlink to the Alertmanager.
  "alerts": [
    {
      "labels": <object>,
      "annotations": <object>,
      "startsAt": "<rfc3339>",
      "endsAt": "<rfc3339>"
    },
    ...
  ]
}

這是測試報警數據的示例:

b'{
"receiver":"webhook",
"status":"firing",
"alerts":[{
    "status":"firing",
    "labels":{
        "alertname":"DeadMansSwitch",
        "prometheus":"monitoring/p-prometheus",
        "severity":"none"

    },
    "annotations":{
        "message":"This is a DeadMansSwitch meant to ensure that the entire alerting pipeline is functional."

    },
    "startsAt":"2019-03-08T10:02:28.680317737Z",
    "endsAt":"0001-01-01T00:00:00Z",
    "generatorURL":"http://prom.cnlinux.club/graph?g0.expr=vector%281%29\\u0026g0.tab=1"

}],
"groupLabels":{},
"commonLabels":{
    "alertname":"DeadMansSwitch",
    "prometheus":"monitoring/p-prometheus",
    "severity":"none"

},
"commonAnnotations":{
"message":"This is a DeadMansSwitch meant to ensure that the entire alerting pipeline is functional."

},
"externalURL":"http://alert.cnlinux.club","version":"4",
"groupKey":"{}/{alertname=\\"DeadMansSwitch\\"}:{}"}\n' 

釘釘對數據的格式是有要求的(具體要求在上面釘釘官方文檔),所以需要將Alertmanager傳過來的數據進行格式轉化。

以下我們用自己寫的python腳本來轉換。

腳本說明:

  • alertmanager傳過來的數據中,重要的是labels{}的數據,但是里面數據太多,很多信息在報警的信息中是不需要的,所以在腳本中添加了一個EXCLUDE_LIST列表,用于排除不需要的數據。
[root@node-01 prometheus]# cat app.py
#!/usr/bin/env python
import io, sys

sys.stdout = io.TextIOWrapper(sys.stdout.detach(), encoding='utf-8')
sys.stderr = io.TextIOWrapper(sys.stderr.detach(), encoding='utf-8')

from flask import Flask, Response
from flask import request
import requests
import logging
import json
import locale
#locale.setlocale(locale.LC_ALL,"en_US.UTF-8")

app = Flask(__name__)

console = logging.StreamHandler()
fmt = '%(asctime)s - %(filename)s:%(lineno)s - %(name)s - %(message)s'
formatter = logging.Formatter(fmt)
console.setFormatter(formatter)
log = logging.getLogger("flask_webhook_dingtalk")
log.addHandler(console)
log.setLevel(logging.DEBUG)

EXCLUDE_LIST = ['prometheus', 'endpoint']

@app.route('/')
def index():
    return 'Webhook Dingtalk by Billy https://blog.51cto.com/billy98'

@app.route('/dingtalk/send/',methods=['POST'])

def hander_session():

    profile_url = sys.argv[1]
    post_data = request.get_data()
    post_data = json.loads(post_data.decode("utf-8"))['alerts']
    post_data = post_data[0]
    messa_list = []
    messa_list.append('### 報警類型: %s' % post_data['status'].upper())
    messa_list.append('**startsAt:** %s' % post_data['startsAt'])
    for i in post_data['labels'].keys():
        if i in EXCLUDE_LIST:
            continue
        else:
            messa_list.append("**%s:** %s" % (i, post_data['labels'][i]))
    messa_list.append('**Describe:** %s' % post_data['annotations']['message'])

    messa = (' \\n\\n > '.join(messa_list))
    status = alert_data(messa, post_data['labels']['alertname'], profile_url )
    log.info(status)
    return status

def alert_data(data,title,profile_url):
    headers = {'Content-Type':'application/json'}
    send_data = '{"msgtype": "markdown","markdown": {"title": \"%s\" ,"text": \"%s\" }}' %(title,data)  # type: str
    send_data = send_data.encode('utf-8')
    reps = requests.post(url=profile_url, data=send_data, headers=headers)
    return reps.text

if __name__ == '__main__':
    app.debug = False
    app.run(host='0.0.0.0', port='8080')

五、制作Docker鏡像

將上面的python腳本做成鏡像,然后把他們以服務的形式運行在k8s集群中,保證高可用。

大家也可以用我已經制作成功的鏡像:docker pull billy98/webhook-dingtalk:latest,直接pull即可。

[root@node-01 prometheus]# cat Dockerfile

FROM centos:7 as build
MAINTAINER billy98 5884625@qq.com

RUN curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo && yum install -y python36 python36-pip && pip3.6 install flask requests werkzeug
ADD app.py /usr/local/alert-dingtalk.py

FROM gcr.io/distroless/python3
COPY --from=build /usr/local/alert-dingtalk.py /usr/local/alert-dingtalk.py
COPY --from=build usr/local/lib64/python3.6/site-packages usr/local/lib64/python3.6/site-packages
COPY --from=build usr/local/lib/python3.6/site-packages usr/local/lib/python3.6/site-packages
ENV PYTHONPATH=usr/local/lib/python3.6/site-packages:usr/local/lib64/python3.6/site-packages
EXPOSE 8080
ENTRYPOINT ["python","/usr/local/alert-dingtalk.py"]
[root@node-01 prometheus]# docker build -t billy98/webhook-dingtalk:latest .

我這樣build出來的鏡像只有50多M,具體的使用方法參考:

distroless:https://github.com/GoogleContainerTools/distroless

六、部署webhook-dingtalk

[root@node-01 prometheus]# cat webhook-dingtalk.yaml 
apiVersion: apps/v1beta2
kind: Deployment
metadata:
  labels:
    app: webhook-dingtalk
  name: webhook-dingtalk
  namespace: monitoring
  #需要和alertmanager在同一個namespace
spec:
  replicas: 1
  selector:
    matchLabels:
      app: webhook-dingtalk
  template:
    metadata:
      labels:
        app: webhook-dingtalk
    spec:
      containers:
      - image: billy98/webhook-dingtalk:latest
        name: webhook-dingtalk
        args:
        - "https://oapi.dingtalk.com/robot/send?access_token=xxxxxx"
        #上面創建的釘釘機器人hook
        ports:
        - containerPort: 8080
          protocol: TCP
        resources:
          requests:
            cpu: 100m
            memory: 100Mi
          limits:
            cpu: 500m
            memory: 500Mi
        livenessProbe:
          failureThreshold: 3
          initialDelaySeconds: 30
          periodSeconds: 10
          successThreshold: 1
          timeoutSeconds: 1
          tcpSocket:
            port: 8080
        readinessProbe:
          failureThreshold: 3
          initialDelaySeconds: 30
          periodSeconds: 10
          successThreshold: 1
          timeoutSeconds: 1
          httpGet:
            port: 8080
            path: /
      imagePullSecrets:
        - name: IfNotPresent
---
apiVersion: v1
kind: Service
metadata:
  labels:
    app: webhook-dingtalk
  name: webhook-dingtalk
  namespace: monitoring
  #需要和alertmanager在同一個namespace
spec:
  ports:
  - name: http
    port: 80
    protocol: TCP
    targetPort: 8080
  selector:
    app: webhook-dingtalk
  type: ClusterIP 

釘釘中報警信息如下:

基于k8s Prometheus+Grafana+Altermanager釘釘報警

基于k8s Prometheus+Grafana+Altermanager釘釘報警

報警恢復的消息
基于k8s Prometheus+Grafana+Altermanager釘釘報警

至此所有的操作已完成。

如有問題歡迎在下面留言交流。希望大家多多關注和點贊,謝謝!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

门头沟区| 集安市| 盐城市| 湘潭市| 泰州市| 正宁县| 鲁山县| 翼城县| 阳朔县| 克什克腾旗| 金昌市| 秭归县| 神木县| 吉林省| 昌乐县| 彭阳县| 探索| 双城市| 枞阳县| 济源市| 普定县| 右玉县| 巴彦县| 云霄县| 娄烦县| 萝北县| 子长县| 湖南省| 乌兰浩特市| 兴化市| 雷山县| 黎川县| 和顺县| 丹棱县| 广平县| 西畴县| 庐江县| 西城区| 宁德市| 泸西县| 陵水|