apiVersion: v1
kind: ConfigMap
metadata:
  name: zabbix-dingding-script
  namespace: kube-system
data:
  zabbix_dingding.py: |
    #!/usr/bin/env python
    # -*- coding: utf-8 -*-

    import requests
    import json
    import sys
    import time
    import configparser

    Headers = {'Content-Type': 'application/json'}
    Time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())

    config = configparser.ConfigParser()
    config.read('/usr/lib/zabbix/externalscripts/dingding.conf')
    # config.read('/etc/zabbix/dingding.conf')

    log_file = config.get('config', 'log')
    api_url = config.get('config', 'webhook')


    def log(info):
        #注意权限,否则写不进去日志
        with open(log_file, 'a+') as infile:
            infile.write(info)

    def msg(text,user):
        json_text = {
         "msgtype": "text",
            "text": {
                "content": text
            },
            "at": {
                "atMobiles": [
                    user
                ],
                "isAtAll": False
            }
        }

        r = requests.post(api_url, data=json.dumps(json_text), headers=Headers).json()
        code = r["errcode"]
        if code == 0:
            log(Time + ":消息发送成功 返回码:" + str(code) + "\n")
        else:
            log(Time + ":消息发送失败 返回码:" + str(code) + "\n")
            exit(3)

    if __name__ == '__main__':
        text = sys.argv[3]
        user = sys.argv[1]
        msg(text, user)
