Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

新增Qmsg酱报警 #261

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions docs/source_code/报警及监控.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,20 @@ EMAIL_RECEIVER = "" # 收件人 支持列表,可指定多个

4. 将本邮箱账号添加到白名单中

## Qmsg酱报警

Qmsg酱是一个QQ消息推送机器人,用来通知自己消息的免费服务。

可以参考文档:https://qmsg.zendee.cn/docs/api/

```python
# QMSG报警
QMSG_WARNING_URL = "" # qmsg机器人api
QMSG_WARNING_QQ = "" # 指定要接收消息的QQ号或者QQ群。多个以英文逗号分割,例如:12345,12346,支持列表,可指定多人
QMSG_WARNING_BOT = "" # 机器人的QQ号
```


## 报警间隔及报警级别

框架会对相同的报警进行过滤,防止刷屏,默认的报警时间间隔为1小时,可通过以下配置修改:
Expand Down
4 changes: 4 additions & 0 deletions feapder/setting.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,10 @@
WECHAT_WARNING_URL = "" # 企业微信机器人api
WECHAT_WARNING_PHONE = "" # 报警人 将会在群内@此人, 支持列表,可指定多人
WECHAT_WARNING_ALL = False # 是否提示所有人, 默认为False
# QMSG报警
QMSG_WARNING_URL = "" # qmsg机器人api
QMSG_WARNING_QQ = "" # 指定要接收消息的QQ号或者QQ群。多个以英文逗号分割,例如:12345,12346,支持列表,可指定多人
QMSG_WARNING_BOT = "" # 机器人的QQ号
# 时间间隔
WARNING_INTERVAL = 3600 # 相同报警的报警时间间隔,防止刷屏; 0表示不去重
WARNING_LEVEL = "DEBUG" # 报警级别, DEBUG / INFO / ERROR
Expand Down
4 changes: 4 additions & 0 deletions feapder/templates/project_template/setting.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,10 @@
# WECHAT_WARNING_URL = "" # 企业微信机器人api
# WECHAT_WARNING_PHONE = "" # 报警人 将会在群内@此人, 支持列表,可指定多人
# WECHAT_WARNING_ALL = False # 是否提示所有人, 默认为False
# # QMSG报警
# QMSG_WARNING_URL = "" # qmsg机器人api
# QMSG_WARNING_QQ = "" # 指定要接收消息的QQ号或者QQ群。多个以英文逗号分割,例如:12345,12346,支持列表,可指定多人
# QMSG_WARNING_BOT = "" # 机器人的QQ号
# # 时间间隔
# WARNING_INTERVAL = 3600 # 相同报警的报警时间间隔,防止刷屏; 0表示不去重
# WARNING_LEVEL = "DEBUG" # 报警级别, DEBUG / INFO / ERROR
Expand Down
57 changes: 53 additions & 4 deletions feapder/utils/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -2684,13 +2684,61 @@ def feishu_warning(message, message_prefix=None, rate_limit=None, url=None, user
return False


def send_msg(msg, level="DEBUG", message_prefix=""):
def qmsg_warning(
message,
message_prefix=None,
rate_limit=None,
url=None,
user_qq=None,
bot_qq=None
):
"""qmsg报警"""

# 为了加载最新的配置
rate_limit = rate_limit if rate_limit is not None else setting.WARNING_INTERVAL
url = url or setting.QMSG_WARNING_URL
user_qq = user_qq or setting.QMSG_WARNING_QQ
bot_qq = bot_qq or setting.QMSG_WARNING_BOT

if isinstance(user_qq, list):
user_qq = ','.join(map(str, user_qq))

if not all([url, message]):
return

if reach_freq_limit(rate_limit, url, user_qq, message_prefix or message):
log.info("报警时间间隔过短,此次报警忽略。 内容 {}".format(message))
return

data = {
"msg": message,
"qq": user_qq,
"bot": bot_qq,
}

headers = {"Content-Type": "application/json"}

try:
response = requests.post(
url, headers=headers, data=json.dumps(data).encode("utf8")
)
result = response.json()
response.close()
if result.get("code") == 0:
return True
else:
raise Exception(result.get("reason"))
except Exception as e:
log.error("报警发送失败。 报警内容 {}, error: {}".format(message, e))
return False


def send_msg(msg, level="DEBUG", message_prefix="", keyword="feapder报警系统\n"):
if setting.WARNING_LEVEL == "ERROR":
if level.upper() != "ERROR":
return

if setting.DINGDING_WARNING_URL:
keyword = "feapder报警系统\n"
dingding_warning(keyword + msg, message_prefix=message_prefix)

if setting.EMAIL_RECEIVER:
Expand All @@ -2700,13 +2748,14 @@ def send_msg(msg, level="DEBUG", message_prefix=""):
email_warning(msg, message_prefix=message_prefix, title=title)

if setting.WECHAT_WARNING_URL:
keyword = "feapder报警系统\n"
wechat_warning(keyword + msg, message_prefix=message_prefix)

if setting.FEISHU_WARNING_URL:
keyword = "feapder报警系统\n"
feishu_warning(keyword + msg, message_prefix=message_prefix)

if setting.QMSG_WARNING_URL:
qmsg_warning(keyword + msg, message_prefix=message_prefix)


###################

Expand Down