воскресенье, 16 июля 2017 г.

Zabbix email alerts via AWS SES or Gmail

#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Zabbix SMTP Alert script for gmail and Amazon SES.
"""

import sys
import smtplib
from email.MIMEText import MIMEText
from email.Header import Header
from email.Utils import formatdate

## Mail Account
#MAIL_ACCOUNT = 'your.account@gmail.com'
#MAIL_PASSWORD = 'your mail password'

## AWS SES Account
MAIL_FROM = 'zabbix@yourdomain.com'
MAIL_ACCOUNT = 'AKXxXxTBKKH5EMXXXXXX'
MAIL_PASSWORD = 'AiRXxXxdjBTRFlBkbPw+s2tr6d3X9GEQXxXxXxXxXxXx'

## Sender Name
SENDER_NAME = u'Zabbix Alert'

## Mail Server
# SMTP_SERVER = 'smtp.gmail.com'
## SES Mail Server
SMTP_SERVER = 'email-smtp.us-east-1.amazonaws.com'
SMTP_PORT = 587
## TLS
SMTP_TLS = True

def send_mail(recipient, subject, body, encoding='utf-8'):
    session = None
    msg = MIMEText(body, 'plain', encoding)
    #msg['Subject'] = Header(subject, encoding)
    #msg['From'] = Header(SENDER_NAME, encoding)
    msg['Subject'] = subject
    msg['From'] = MAIL_FROM
    msg['To'] = recipient
    msg['Date'] = formatdate()
    try:
        session = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
        session.set_debuglevel(10)
        if SMTP_TLS:
            session.ehlo()
            session.starttls()
            session.ehlo()
            session.login(MAIL_ACCOUNT, MAIL_PASSWORD)
        #session.sendmail(MAIL_ACCOUNT, recipient, msg.as_string())
        session.sendmail(MAIL_FROM, recipient, msg.as_string())
    except Exception as e:
        raise e
    finally:
        # close session
        if session:
            session.quit()

if __name__ == '__main__':
    """
    recipient = sys.argv[1]
    subject = sys.argv[2]
    body = sys.argv[3]
    """
    if len(sys.argv) == 4:
        send_mail(
            recipient=sys.argv[1],
            subject=sys.argv[2],
            body=sys.argv[3])
    else:
        print u"""requires 3 parameters (recipient, subject, body)
\t$ zabbix-gmail.sh recipient subject body
"""


#!/bin/sh
set -e
set -x
#{
#date
export smtpemailfrom=support@blablabla.com
export zabbixemailto=$1
export zabbixsubject=$2
export zabbixbody=$3
aws ses send-email --from $smtpemailfrom --to "$zabbixemailto" --text "$zabbixbody" --subject "$zabbixsubject" --region us-west-2
#} 2>&1 | tee /var/log/zabbix_sendmail.log

Комментариев нет:

Отправить комментарий

Bash: MySql backup (file per db), restore+ users and privileges

Backup Mysql DB (file per db) #!/bin/bash USER="root" databases=`mysql -u $USER -e "SHOW DATABASES;" | tr -d "|...