Compare commits
	
		
			4 Commits
		
	
	
		
			d05c443552
			...
			e0cdc133a2
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| e0cdc133a2 | |||
| 8d11aa9ec8 | |||
| 5efa59be75 | |||
| b32ea0978d | 
@@ -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"
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										58
									
								
								alert/sendmail.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										58
									
								
								alert/sendmail.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,58 @@
 | 
			
		||||
#!/usr/bin/python3
 | 
			
		||||
 | 
			
		||||
from email.mime.text import MIMEText
 | 
			
		||||
from email.mime.image import MIMEImage
 | 
			
		||||
from email.mime.multipart import MIMEMultipart
 | 
			
		||||
import smtplib
 | 
			
		||||
import sys
 | 
			
		||||
 | 
			
		||||
# Mail basic information
 | 
			
		||||
mail_host = "smtp.163.com"
 | 
			
		||||
from_mail = 'xgdfmf@163.com'
 | 
			
		||||
from_mail_password = 'SATTGHVFIJEGZMFP'
 | 
			
		||||
receiver_to = ['mffan0922@163.com']
 | 
			
		||||
receiver_cc = []
 | 
			
		||||
to_mail = receiver_to + receiver_cc
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
# MIMEMultipart: mixed/alternative/related
 | 
			
		||||
# (1)mixed: default option, especially situation with mail attachments must use this one
 | 
			
		||||
# (2)alternative: with both plain text and hyper text in mail content, using this one
 | 
			
		||||
# (3)related:sending content of html format, probably using picture as the background of mail
 | 
			
		||||
# content, then html text will be stored in alternative segment, whereas the background picture
 | 
			
		||||
# will be stored in multipart/related segment
 | 
			
		||||
 | 
			
		||||
# Mail content
 | 
			
		||||
msg = MIMEMultipart()
 | 
			
		||||
msg['From'] = from_mail
 | 
			
		||||
msg['To'] = ";".join(to_mail)
 | 
			
		||||
msg['Subject'] = sys.argv[1]
 | 
			
		||||
txt = sys.argv[2]
 | 
			
		||||
body = MIMEText(txt, 'plain', 'utf-8')
 | 
			
		||||
msg.attach(body)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
# Mail attachment
 | 
			
		||||
if len(sys.argv) > 3:
 | 
			
		||||
    for attachment in sys.argv[3:]:
 | 
			
		||||
        filename = attachment.split('/')[-1]
 | 
			
		||||
        attach_file = open(attachment, 'r').read()
 | 
			
		||||
        attach = MIMEText(str(attach_file), 'base64', 'utf-8')
 | 
			
		||||
        attach["Content-Type"] = 'application/octet-stream'
 | 
			
		||||
        attach.add_header('Content-Disposition', 'attachment', filename=filename)
 | 
			
		||||
        msg.attach(attach)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
try:
 | 
			
		||||
    server = smtplib.SMTP(mail_host)
 | 
			
		||||
    server.docmd('helo', from_mail)
 | 
			
		||||
    server.starttls()
 | 
			
		||||
    server.login(from_mail, from_mail_password)
 | 
			
		||||
 | 
			
		||||
    server.sendmail(from_mail, to_mail, msg.as_string())
 | 
			
		||||
    server.quit()
 | 
			
		||||
    print('sendemail successful!')
 | 
			
		||||
except Exception as err:
 | 
			
		||||
    print('Sending email failed, the reason is as below:')
 | 
			
		||||
    print(err)
 | 
			
		||||
 | 
			
		||||
@@ -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__':
 | 
			
		||||
 
 | 
			
		||||
@@ -18,6 +18,10 @@ cp -rf /root/.ssh/config /opt/configs/conf/
 | 
			
		||||
cp -rf /etc/samba/smb.conf /opt/configs/conf/
 | 
			
		||||
cp -rf /root/.acme.sh/*ecc /opt/configs/acme/
 | 
			
		||||
cp -rf /opt/scripts/update/restore.sh /opt/apps/syncthing/data/common/F-Backup/Linux/
 | 
			
		||||
scp -r /opt/configs/certs/ arm1:/opt/ > /dev/null &
 | 
			
		||||
scp -r /opt/configs/certs/ arm2:/opt/ > /dev/null &
 | 
			
		||||
scp -r /opt/configs/certs/ amd:/opt/ > /dev/null &
 | 
			
		||||
wait
 | 
			
		||||
 | 
			
		||||
cd /opt && t=`date +%Y%m%dT%H%M%S`
 | 
			
		||||
rsync --delete-after -avz apps configs logs scripts websites wd/72-Backups/VPS/ > /opt/logs/rsync/rsync_${t}.log
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user