Setup Zabbix to send gmail for triggered actions


Create gmail login script

create “zabbix-alert-smtp.sh” under /var/lib/zabbix/alertscript/

#!/usr/bin/python
# -*- coding: utf-8 -*-
“””
Zabbix SMTP Alert script for gmail.
“””

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’

# Sender Name
SENDER_NAME = u’Zabbix Alert’

# Mail Server
SMTP_SERVER = ‘smtp.gmail.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[‘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])
else:
print u”””requires 3 parameters (recipient, subject, body)
\t$ zabbix-gmail.sh recipient subject body
“””

Gmail and Zabbix setup

disable “secure app” feature in Gmail.

then go to zabbix and add media under admin tab. choose script and type name zabbix-alert-smtp.sh, add 3 parameters for 3.x: {ALERT.SENDTO},{ALERT.SUBJECT},{ALERT.MESSAGE}.

then create trigger with action loading condition and operations and tell it to send notice to which user. by this step, we’ve finished defining using which gmail account under what condition to send an email notice. However, as which receiver it’s gonna be we haven’t define yet, to do that we need to setup media for user who’s supposed to be notified under user account, with receiver’s email account.

Advertisements
By Ctrl | Alt | Del Posted in IoT

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s