Python&Djangok8s&docker

django实现webhook服务端接收alertmanager

2020-07-07  本文已影响0人  阿乐_822e

在kube-promethues中默认安装了alertmanger,alertmanger可以配置通过email或webhook向第三方平台发送告警信息。如果是内网,可以使用django创建一个server来接收告警信息。
kube-promethues的安装不赘述。

[root@k8s-master0 alertmanager]# cat alertmanager-secret.yaml
apiVersion: v1
data: {}
kind: Secret
metadata:
  name: alertmanager-main
  namespace: monitoring
stringData:
  alertmanager.yaml: |-

    "global":
      "resolve_timeout": "1m"

    "route":
      "group_by": ["alertname","container_name","namespace","severity"]
      "group_wait": "10s"
      "group_interval": "10s"
      "repeat_interval": "5m"
      "receiver": "web.hook"
    "receivers":
    - "name": "web.hook"
      "webhook_configs":
      - "url": "http://192.168.53.151:8000/webhook/"
    "inhibit_rules":
      - "source_match":
          "severity": "critical"
        "target_match":
          "severity": "warning"
        "equal": ["alertname", "dev", "instance"]

[root@k8s-master0 alertmanager]# kubectl apply -f alertmanager-secret.yaml
# alertmanager会自动重新拉取此配置文件
$ mkdir -p /root/django/django-www
$ docker run -it -v /root/django/django-www:/srv:rw  -p 8000:8000 django  /bin/bash
root@1143eae8d584:/# cd /srv
 # 先创建工程
root@1143eae8d584:/srv# django-admin startproject webhk
 # 修改时区和汉字编码(如果容器不包括vi程序,可直接在宿主机的 /root/django/django-www目录下修改)
root@1143eae8d584:/srv# cat  webhk/webhk/settings.py 
.......
LANGUAGE_CODE = 'zh-hans'
TIME_ZONE = 'Asia/Shanghai'
# 注释掉防跨域,否则无法接收外站post数据
# 'django.middleware.csrf.CsrfViewMiddleware',
# 允许站外访问
ALLOWED_HOSTS = ['*',]

# 再创建应用
root@1143eae8d584:/srv# cd webhk/
root@1143eae8d584:/srv/webhk# python manage.py startapp app
# 将应用加入启动组
root@1143eae8d584:/srv# cat webhk/webhk/settings.py 
# 在INSTALLED_APPS组中加入app
INSTALLED_APPS = [
    'django.contrib.admin',
    ......
    'app',
]
root@1143eae8d584:/srv# cat  webhk/app/models.py 
from django.db import models 

class alerts(models.Model):
        startsAt=models.DateTimeField(verbose_name='告警产生时间')
        instance=models.CharField(max_length=50,verbose_name='实例',blank=True)
        alertname=models.CharField(max_length=100,verbose_name='告警名称')
        status=models.CharField(max_length=20,verbose_name='状态', blank=True)
        severity=models.CharField(max_length=20,verbose_name='告警级别', blank=True)
        message=models.CharField(max_length=1000,verbose_name='告警信息', blank=True)
        known=models.BooleanField(default=False,verbose_name='知悉')
memo=models.CharField(max_length=50,verbose_name='知悉备注', blank=True)
        def __str__(self):
                return self.message
        class Meta:
                db_table = 'alerts'
                ordering = ['-startsAt']    # 按故障时间倒排

# 应用到数据库
root@1143eae8d584:/srv# python manage.py makemigrations 
Migrations for 'app':
  app/migrations/0001_initial.py
    - Create model alerts

root@1143eae8d584:/srv# python manage.py migrate
Operations to perform:
  Apply all migrations: admin, app, auth, contenttypes, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying admin.0003_logentry_add_action_flag_choices... OK
  Applying app.0001_initial... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying auth.0009_alter_user_last_name_max_length... OK
  Applying sessions.0001_initial... OK
root@1143eae8d584:/srv/webh# cat app/views.py 
from django.http import HttpResponse
import json
import time
from app.models import alerts as alerts_t

def webhook(request):
        if request.method == 'POST':
                try:
                        request_data=request.body
                        request_dict=json.loads(request_data.decode('utf-8'))
                        alerts = request_dict['alerts']
                        for i in alerts:
                                msg=i['annotations']['message'] if 'message' in i['annotations'] else 'null'
                                if msg=='null' and 'summary' in i['annotations'] :
                                        msg=i['annotations']['summary'] 
                                print(i['startsAt'][0:19] + i['status']+ " :" + msg)
                                if msg=='null': print(i)
                                ints=i['labels']['instance'] if 'instance' in i['labels'] else 'unknown'

                                print(ints + " --- " + i['labels']['alertname'] + " ---- " + i['labels']['severity'])
                                a = alerts_t()
                                a.startsAt=i['startsAt'][0:19]
                                a.instance=ints
                                a.alertname=i['labels']['alertname'] 
                                a.status=i['status']
                                a.severity=i['labels']['severity']
                                a.message=msg
                                a.save()
                except (ValueError,TypeError,KeyError,AttributeError,'Internal Server Error') :
                        pass
                finally:
                        return HttpResponse(1)
root@1143eae8d584:/srv/webh# cat app/urls.py 
from django.conf.urls import url 
from . import views
 
urlpatterns = [
    url(r'^webhook/',views.webhook,name='webhook'),
]

root@1143eae8d584:/srv/webh# cat webh/urls.py 
# 修改首行
from django.conf.urls import url, include
# 加入下列这行
url(r'', include('app.urls')),

到此就已完成全部工作。下面测试下

root@1143eae8d584:/srv/webhk# python manage.py runserver 0.0.0.0:8000

贴一张工作的图:


ddd.png

PS:以上只是个半成品,实现了告警信息的接收与存储,还可以再扩展一下,写个前端页面,做告警信息的展示,也可以把数据库换为MySQL/部署到k8s中。

上一篇下一篇

猜你喜欢

热点阅读