From 8d11aa9ec8c4fc62fdbebb4b3dfba6d2cf1a1cf5 Mon Sep 17 00:00:00 2001 From: gitea Date: Fri, 25 Aug 2023 21:50:46 +0800 Subject: [PATCH] [modify] change alert method to mail --- alert/docker_alarm.sh | 7 +++-- alert/todo_alert.py | 62 ++++++++++++++++++++++++------------------- 2 files changed, 39 insertions(+), 30 deletions(-) diff --git a/alert/docker_alarm.sh b/alert/docker_alarm.sh index d3966c4..08c8b5d 100644 --- a/alert/docker_alarm.sh +++ b/alert/docker_alarm.sh @@ -1,12 +1,15 @@ #!/bin/bash +> /tmp/docker_alarm.log docker ps -a --format "table {{.Names}}\t{{.Status}}" > /opt/logs/docker_status.log while read line; do echo $line | grep -q 'Exited' if [[ $? -eq 0 ]]; then name=`echo $line | awk '{print $1}'` - alarm="Docker Alarm - $name:\nContainer $name has been off line, please check ASAP." - bash /opt/scripts/alert/sendmsg.sh "$alarm" + echo "Docker Alarm - $name: Container $name has been off line, please check ASAP." >> /tmp/docker_alarm.log fi done < /opt/logs/docker_status.log +alarm=`cat /tmp/docker_alarm.log` +python3 /opt/scripts/alert/sendmail.py "Docker Alarms" "$alarm" + diff --git a/alert/todo_alert.py b/alert/todo_alert.py index 7d94810..4290b27 100644 --- a/alert/todo_alert.py +++ b/alert/todo_alert.py @@ -4,33 +4,33 @@ import time import os -def send_alert_msg(alert_msg): - """ - send warning messages to phone via Enterprise WeChat Bot API - :param alert_msg: messages needed to be sent - :return: None - """ - - # get the datetime, which is using at a failed situation - alert_datetime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())) - # # Enterprise Wechat Bot API and the format of body to send - hook_url = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=ddea3f5f-fbfc-4c21-994a-71e9fc50e4ef' - body = { - "msgtype": "text", - "text": { - "content": alert_msg - } - } - - # get the result of API call - res = requests.post(hook_url, data=json.dumps(body, ensure_ascii=False).encode('utf-8')) - - # when failed, log it in /opt/logs/alert.log file - if res.status_code != 200: - with open('/opt/logs/alert.log', 'a', encoding='utf-8') as alert_log: - alert_log.write(alert_datetime + ' >>>> ') - alert_log.write('Failed sending message: ') - alert_log.write(alert_msg + '\n') +# def send_alert_msg(alert_msg): +# """ +# send warning messages to phone via Enterprise WeChat Bot API +# :param alert_msg: messages needed to be sent +# :return: None +# """ +# +# # get the datetime, which is using at a failed situation +# alert_datetime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())) +# # # Enterprise Wechat Bot API and the format of body to send +# hook_url = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=ddea3f5f-fbfc-4c21-994a-71e9fc50e4ef' +# body = { +# "msgtype": "text", +# "text": { +# "content": alert_msg +# } +# } +# +# # get the result of API call +# res = requests.post(hook_url, data=json.dumps(body, ensure_ascii=False).encode('utf-8')) +# +# # when failed, log it in /opt/logs/alert.log file +# if res.status_code != 200: +# with open('/opt/logs/alert.log', 'a', encoding='utf-8') as alert_log: +# alert_log.write(alert_datetime + ' >>>> ') +# alert_log.write('Failed sending message: ') +# alert_log.write(alert_msg + '\n') def main(): @@ -87,7 +87,13 @@ def main(): alert_msg += '\n' alert_msg += 'So, hurry up!! Go get things done!!' - send_alert_msg(alert_msg) + os.environ['subject'] = 'TODO TIPS' + os.environ['txt'] = alert_msg + # os.system('/usr/bin/echo subject = $subject') + # os.system('/usr/bin/echo txt = $txt') + # exit(9) + os.system('/usr/bin/python3 /opt/scripts/alert/sendmail.py "$subject" "$txt"') + # send_alert_msg(alert_msg) if __name__ == '__main__':