Python飞书自定义机器人告警模块,告警分级

# common function: feishu report infomation
# file_name is  feishu_report.py
import json
import datetime
import requests


class get_feishu_oj:
    def __init__(self, url):
        self.url = url

    def _report(self, content, report_level, url=None,):
        url = self.url
        date = datetime.datetime.now().strftime("%H:%M:%S")
        #title show the report date_str
        title = date + "--[" + report_level + "]"
        data_json = json.dumps({
            "msg_type": "post",
            "content":
                {"post":
                     {"zh_cn":
                          {"title": title,
                           "content": [[
                               {"tag": "text",
                                "text": content
                                },
                           ]]}}}})
        r_json = requests.post(url, data_json)

    def info(self, content):
        self._report(content=content, report_level="INFO")

    def debug(self, content):
        self._report(content=content, report_level="DEBUG")

    def warn(self, content):
        self._report(content=content, report_level="WARIN")

    def error(self, content):
        self._report(content=content, report_level="ERROR")

# 需要多几个的话
def get_feishu_test():
    feishu = get_feishu_oj(feishu_report_url['test'])
    return feishu
def get_feishu_product():
    feishu = get_feishu_oj(feishu_report_url['product'])
    return feishu
# url填入自己的机器人webhook
feishu_report_url = {
    "test": "https://open.feishu.cn/open-apis/bot/v2/hook/xxxxxx2"
    "product" : "https://open.feishu.cn/open-apis/bot/v2/hook/xxxxxx1"
}

添加机器人

调用测试

import feishu_report

feishu=feishu_report.get_feishu_test()
feishu.info("Info test\ntestinfo")
feishu.warn("Hello Mother Fuck")

效果
在这里插入图片描述

这么写的调用的方式类似于
logging模块
生成一个log实例对象后,
也是用
log.error(“str_a”),
log.info(“str_b”)可以打印对应的日志,
生成一个feishu对象,
不同级别的告警调用feishu对象的info,error,warn,debug函数
也可以生成不同的机器人,告警到不同的群里
只需要输入需要告警的字符串内容就行,时间自动生成,告警等级调整也很方便

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值