Alertmanager配置短信告警


Prometheus是以operator方式部署
這里僅僅提供一個思路,萬變不離其宗。
使用短信告警之前需要自己購買短信服務,然后定義好短信模板,一般都有現成的sdk,自己簡單包裝一下就可以使用了。

思路:通過自定義webhook的方式進行發送。
我簡單寫了一個webhook,項目地址:https://github.com/cool-ops/prometheus-alert-sms.git

部署


1、下載代碼

git clone https://github.com/cool-ops/prometheus-alert-sms.git


2、編譯代碼

cd prometheus-alert-sms/
sh build.sh


3、打包鏡像

docker build -t registry.cn-hangzhou.aliyuncs.com/rookieops/prometheus-alert-sms:v0.0.7 .


注:鏡像地址更換成自己的倉庫地址
4、推送鏡像到鏡像倉庫

docker push registry.cn-hangzhou.aliyuncs.com/rookieops/prometheus-alert-sms:v0.0.7


5、修改項目目錄下的prometheus-alert-sms.yaml

apiVersion: v1
kind: ConfigMap
metadata:
  name: sms-conf
  namespace: monitoring
data:
  sms.yaml: |
    adapter:
      adapter_name: "RongLianYun"
    RongLianYun:
      baseUrl : "https://app.cloopen.com:8883"
      accountSid : "xxxxxx"
      appToken   : "xxxxxx"
      appId      : "xxxxx"
      templateId : "xxx"
      phones : ["11111111111","22222222222"]

    AliYun:
      aliRegion: "cn-hangzhou"
      accessKeyId: "xxxx"
      accessSecret: "xxxx"
      phoneNumbers: "11111111111,22222222222"
      signName: "xxxx"
      templateCode: "xxxx"
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: prometheus-alert-sms
  namespace: monitoring
spec:
  replicas: 1
  selector:
    matchLabels:
      app: prometheus-alert-sms
  template:
    metadata:
      labels:
        app: prometheus-alert-sms
    spec:
      containers:
        - name: prometheus-alert-sms
          image: registry.cn-hangzhou.aliyuncs.com/rookieops/prometheus-alert-sms:v0.0.7
          imagePullPolicy: IfNotPresent
          livenessProbe:
            failureThreshold: 3
            httpGet:
              path: /healthCheck
              port: tcp-9000
              scheme: HTTP
            initialDelaySeconds: 30
            periodSeconds: 10
            successThreshold: 1
            timeoutSeconds: 2
          readinessProbe:
              failureThreshold: 3
              httpGet:
                path: /healthCheck
                port: tcp-9000
                scheme: HTTP
              initialDelaySeconds: 30
              periodSeconds: 10
              successThreshold: 1
              timeoutSeconds: 2
          env:
            - name: CONFIG_PATH
              value: /app/conf/sms.yaml
          ports:
            - name: app-port
              containerPort: 9000
              protocol: TCP
          resources:
            limits:
              cpu: 500m
              memory: 1Gi
            requests:
              cpu: 500m
              memory: 1Gi
          volumeMounts:
            - name: sms-conf
              mountPath: /app/conf/sms.yaml
              subPath: sms.yaml
      volumes:
        - name: sms-conf
          configMap:
            name: sms-conf
---
apiVersion: v1
kind: Service
metadata:
  name: prometheus-alter-sms
  namespace: monitoring
spec:
  selector:
    app: prometheus-alert-sms
  ports:
    - name: app-port
      port: 9000
      targetPort: 9000
      protocol: TCP


到自己購買的短信服務獲取對應的信息。
7、部署yaml文件

kubectl apply -f prometheus-alert-sms.yaml


8、修改alertmanager的報警媒介

 ......
      - receiver: sms 
        group_wait: 10s
        match:
          filesystem: node
    receivers:
    - name: 'sms'
      webhook_configs:
      - url: "http://prometheus-alter-sms.monitoring.svc:9000"
        send_resolved: true
......


然后如果有報警就可以正常接受到報警了。
image.png

PS:因為這個是按照我自己的短信模板進行開發的,可能並不適合你,你可以做相應的修改。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM