-
-
Save maddluxx/c6346052bc3a461aefa5 to your computer and use it in GitHub Desktop.
Zabbix SMTP Alert script for Yandex
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# -*- coding: utf-8 -*- | |
""" | |
Zabbix SMTP Alert script for Yandex. | |
""" | |
import sys | |
import smtplib | |
import datetime | |
from email.MIMEText import MIMEText | |
from email.Header import Header | |
from email.Utils import formatdate | |
# Mail Account | |
MAIL_ACCOUNT = '[email protected]' | |
MAIL_PASSWORD = 'your mail password' | |
# Sender Name | |
SENDER_NAME = u'Zabbix Alert <[email protected]>' | |
# Mail Server | |
SMTP_SERVER = 'smtp.yandex.ru' | |
SMTP_PORT = 25 | |
# 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['To'] = recipient | |
msg['Date'] = formatdate() | |
try: | |
session = smtplib.SMTP(SMTP_SERVER, SMTP_PORT) | |
if SMTP_TLS: | |
session.ehlo() | |
session.starttls() | |
session.ehlo() | |
session.login(MAIL_ACCOUNT, MAIL_PASSWORD) | |
session.sendmail(MAIL_ACCOUNT, 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]) | |
with open('/var/log/zabbix/mail-' + str(datetime.date.today()) + '.log', 'a') as inf: | |
inf.write(str(datetime.datetime.now()) + ': recipient=' + str(sys.argv[1]) + ', subject=' + str(sys.argv[2]) + ', SEND=OK' + '\n') | |
else: | |
print u"""requires 3 parameters (recipient, subject, body) | |
\t$ zabbix-gmail.sh recipient subject body | |
""" | |
with open('/var/log/zabbix/mail-' + str(datetime.date.today()) + '.log', 'a') as inf: | |
inf.write(str(datetime.datetime.now()) + ': SEND=ERROR' + '\n') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment