k8s全方位监控 -prometheus实现短信告警接口编写(python)

1、prometheus短信告警接口实现(python)源码如下:

import subprocess
from flask import Flask
from flask import request
import json
import logging
import requests

logging.basicConfig(filename='/log/phone-hook-log.log',
                    format='%(asctime)s -%(name)s-%(levelname)s-%(module)s:%(message)s',
                    datefmt='%Y-%m-%d %H:%M:%S %p',
                    level=logging.DEBUG)

app = Flask(__name__)

@app.route('/', methods=['GET', 'POST'])
def home():
    msg = ""
    msg1 = ""
    msg2 = ""
    data = json.loads(request.data)
    alerts = data['alerts']
    for itm in alerts:
        logging.info('------------------------itm info------------------\n' + str(itm))
        alert_status = itm['status']
        alert_level = itm['labels']['severity']
        alert_name = itm['labels']['alertname']
        alert_summary = itm['annotations']['summary']
        alert_value = itm['annotations']['value']
        alert_start = itm['startsAt'].split('.')[0].replace('T', ' ')
        alert_end = itm['endsAt'].split('.')[0].replace('T', ' ')

        # 告警触发
        if alert_status == "firing":
            msg1 = msg1 + "【告警触发】\n" + "告警级别: " + alert_level + "\n告警类型: " + alert_name + "\n监控项目: " + alert_summary + "\n触发时间: " + alert_start + "\n当前数值: " + alert_value + "\n\n"
        # 告警恢复
        elif alert_status == "resolved":
            msg2 = msg2 + "【告警恢复】\n" + "告警级别: " + alert_level + "\n告警类型: " + alert_name + "\n监控项目: " + alert_summary + "\n触发时间: " + alert_start + "\n恢复时间: " + alert_end + "\n当前数值: " + alert_value+ "\n\n"


# 手机短信告警配置
    if msg1 == "":
        msg = msg2
    elif msg2 == "":
        msg = msg1
    else:
        msg = msg1 + "\n" + msg2

    logging.info('------------------------alert msg-------------------\n' + msg)
    smsuser = '111111'
    smspass = '1HsbJ3YHe'
    extno = '10000'
    phones_f = open("/app/phones.txt", 'r')
    phones = phones_f.read()
    phones_f.close()
    logging.info("---------------------phones------------------------\n" + phones)
    values = {'account': smsuser,
              'password': smspass,
              'mobile': phones,
              'content': msg,
              'extno': extno}

    '''
    post_url = 'http://127.0.0.1:8080/sms?action=send'
    try:
        req = requests.get(post_url, values)
        print (req.text)
    except Exception as e:
        print (e)
    '''
    return '<h1>Home</h1>'

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000)

  

2、构建dockerfile镜像

FROM python:3.6.4

RUN mkdir -p /app /log

WORKDIR /app

ADD . /app 

# install requirements
RUN pip install -r requirements.txt

# run server
CMD python sendPhone.py
cat  requirements.txt
Flask==1.0.2
requests==2.20.1

3、推送到镜像仓库

 docker  push 172.19.0.14/prom/py-hook:v0.0.1 

4、部署

[root@VM_0_48_centos prometheus_hook]# cat phone-hook.yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: phone-hook
  namespace: monitoring
spec:
  template:
    metadata:
      labels:
        app: phone-hook
    spec:
      containers:
      - name: phone-hook
        image: 172.19.0.14/prom/py-hook:v0.0.1
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 5000
          name: http
    
        volumeMounts:
          - name: phone-hook-config-volume
            mountPath: /app
          - name: phone-hook-volume
            mountPath: /log
          - name: localtime
            mountPath: /etc/localtime
  
        resources:
          requests:
            cpu: 100m
            memory: 100Mi
          limits:
            cpu: 200m
            memory: 200Mi
      
      volumes:
        - name: phone-hook-config-volume
          configMap:
            name: phone-hook-config
        - name: phone-hook-volume
          persistentVolumeClaim:  ##此处使用了永久存储券
            claimName: web-hook
        - name: localtime
          hostPath:
            path: /etc/localtime
      imagePullSecrets:
      - name: registrysecret
  
            
---
apiVersion: v1
kind: Service
metadata:
  name: phone-hook
  namespace: monitoring
spec:
  selector:
    app: phone-hook
  ports:
  - name: hook
    port: 5000
    targetPort: http      

5、补充

kubectl create cm phone-hook-config --from-file=./conf -n monitoring
[root@VM_0_48_centos prometheus_hook]# cat conf/phones.txt 
18125012766

 

6、配置告警规则

 

posted @ 2019-08-14 19:04  马里亚纳仰望星空  Views(2713)  Comments(0Edit  收藏  举报