Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Zabbix SMTP Alert script for gmail
#!/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
"""
@dthom91

This comment has been minimized.

Show comment
Hide comment
@dthom91

dthom91 Dec 15, 2012

This is great, thanks! Saved me a chunk of time and frustration.

dthom91 commented Dec 15, 2012

This is great, thanks! Saved me a chunk of time and frustration.

@dsociative

This comment has been minimized.

Show comment
Hide comment

+1

@e4k4

This comment has been minimized.

Show comment
Hide comment
@e4k4

e4k4 Nov 26, 2013

Awesome, thank you!

e4k4 commented Nov 26, 2013

Awesome, thank you!

@darren1973

This comment has been minimized.

Show comment
Hide comment
@darren1973

darren1973 Nov 27, 2013

This looks great but I'm unsure as to where my alertscripts folder is, I've searched using find -name alertscripts but I don't get anything, please advise.

This looks great but I'm unsure as to where my alertscripts folder is, I've searched using find -name alertscripts but I don't get anything, please advise.

@wilyoj

This comment has been minimized.

Show comment
Hide comment
@wilyoj

wilyoj Jan 8, 2014

Works flawlessly, thanks.

wilyoj commented Jan 8, 2014

Works flawlessly, thanks.

@steelywing

This comment has been minimized.

Show comment
Hide comment
@steelywing

steelywing Sep 27, 2014

Install guide:

  • copy to AlertScriptsPath, you can check the path in /etc/zabbix/zabbix_server.conf, default path is /usr/lib/zabbix/alertscripts
  • cd /usr/lib/zabbix/alertscripts
  • chmod 755 zabbix-alert-smtp.sh
  • Go to Zabbix web UI, Administration > Media types > Create media type
  • Enter Script name: zabbix-alert-smtp.sh
  • Save

Install guide:

  • copy to AlertScriptsPath, you can check the path in /etc/zabbix/zabbix_server.conf, default path is /usr/lib/zabbix/alertscripts
  • cd /usr/lib/zabbix/alertscripts
  • chmod 755 zabbix-alert-smtp.sh
  • Go to Zabbix web UI, Administration > Media types > Create media type
  • Enter Script name: zabbix-alert-smtp.sh
  • Save
@Geo-Gee

This comment has been minimized.

Show comment
Hide comment
@Geo-Gee

Geo-Gee Oct 8, 2014

Fantastic script! I appreciate you sharing this.

Geo-Gee commented Oct 8, 2014

Fantastic script! I appreciate you sharing this.

@rggassner

This comment has been minimized.

Show comment
Hide comment
@rggassner

rggassner Nov 6, 2014

Flawless victory! Worked perfectly.

Flawless victory! Worked perfectly.

@anoshkin

This comment has been minimized.

Show comment
Hide comment
@anoshkin

anoshkin Nov 27, 2014

Thank you very much!

Thank you very much!

@Superkikim

This comment has been minimized.

Show comment
Hide comment
@Superkikim

Superkikim Dec 5, 2014

Hey thank you for this helpful script.

@steelywing
Why 0755 ? 0700 should be fine enough.

I also suggest to put the password in a hidden textfile with owner zabbixsrv and mod 0600. Then you can use "cat hiddenfile" in the script to get it.

Hey thank you for this helpful script.

@steelywing
Why 0755 ? 0700 should be fine enough.

I also suggest to put the password in a hidden textfile with owner zabbixsrv and mod 0600. Then you can use "cat hiddenfile" in the script to get it.

@powerdrogba

This comment has been minimized.

Show comment
Hide comment
@powerdrogba

powerdrogba Jan 12, 2015

What an incredible script.
Thank you so much!

What an incredible script.
Thank you so much!

@ramxis

This comment has been minimized.

Show comment
Hide comment
@ramxis

ramxis Feb 11, 2015

hi this script is not working for me . I placed the script in /... /zabbix/alertscripts then did the following
chmod 755 zabbix-alert-smtp.sh
Go to Zabbix web UI, Administration > Media types > Create media type
NAME:Gmail Scripts
Enter Script name: zabbix-alert-smtp.sh
Save
also created an action which sends messages Gmail Scripts when ever a trigger event fires

i didnt receive any emails on my gmail account i tried running the script directly from my terminal i get the following errors
Zabbix SMTP Alert script for gmail.
: command not found
/usr/local/share/zabbix/alertscripts/zabbix-alert-smtp.sh: line 9: from: command not found
/usr/local/share/zabbix/alertscripts/zabbix-alert-smtp.sh: line 10: from: command not found
/usr/local/share/zabbix/alertscripts/zabbix-alert-smtp.sh: line 11: from: command not found
/usr/local/share/zabbix/alertscripts/zabbix-alert-smtp.sh: line 14: MAIL_ACCOUNT: command not found
/usr/local/share/zabbix/alertscripts/zabbix-alert-smtp.sh: line 15: MAIL_PASSWORD: command not found
/usr/local/share/zabbix/alertscripts/zabbix-alert-smtp.sh: line 18: SENDER_NAME: command not found
/usr/local/share/zabbix/alertscripts/zabbix-alert-smtp.sh: line 21: SMTP_SERVER: command not found
/usr/local/share/zabbix/alertscripts/zabbix-alert-smtp.sh: line 22: SMTP_PORT: command not found
/usr/local/share/zabbix/alertscripts/zabbix-alert-smtp.sh: line 24: SMTP_TLS: command not found
/usr/local/share/zabbix/alertscripts/zabbix-alert-smtp.sh: line 26: syntax error near unexpected token (' /usr/local/share/zabbix/alertscripts/zabbix-alert-smtp.sh: line 26:def send_mail(recipient, subject, body, encoding='utf-8'):'
I have installed zabbix 2.4.3 in oracle linux 6.5
Please help me sort this out

ramxis commented Feb 11, 2015

hi this script is not working for me . I placed the script in /... /zabbix/alertscripts then did the following
chmod 755 zabbix-alert-smtp.sh
Go to Zabbix web UI, Administration > Media types > Create media type
NAME:Gmail Scripts
Enter Script name: zabbix-alert-smtp.sh
Save
also created an action which sends messages Gmail Scripts when ever a trigger event fires

i didnt receive any emails on my gmail account i tried running the script directly from my terminal i get the following errors
Zabbix SMTP Alert script for gmail.
: command not found
/usr/local/share/zabbix/alertscripts/zabbix-alert-smtp.sh: line 9: from: command not found
/usr/local/share/zabbix/alertscripts/zabbix-alert-smtp.sh: line 10: from: command not found
/usr/local/share/zabbix/alertscripts/zabbix-alert-smtp.sh: line 11: from: command not found
/usr/local/share/zabbix/alertscripts/zabbix-alert-smtp.sh: line 14: MAIL_ACCOUNT: command not found
/usr/local/share/zabbix/alertscripts/zabbix-alert-smtp.sh: line 15: MAIL_PASSWORD: command not found
/usr/local/share/zabbix/alertscripts/zabbix-alert-smtp.sh: line 18: SENDER_NAME: command not found
/usr/local/share/zabbix/alertscripts/zabbix-alert-smtp.sh: line 21: SMTP_SERVER: command not found
/usr/local/share/zabbix/alertscripts/zabbix-alert-smtp.sh: line 22: SMTP_PORT: command not found
/usr/local/share/zabbix/alertscripts/zabbix-alert-smtp.sh: line 24: SMTP_TLS: command not found
/usr/local/share/zabbix/alertscripts/zabbix-alert-smtp.sh: line 26: syntax error near unexpected token (' /usr/local/share/zabbix/alertscripts/zabbix-alert-smtp.sh: line 26:def send_mail(recipient, subject, body, encoding='utf-8'):'
I have installed zabbix 2.4.3 in oracle linux 6.5
Please help me sort this out

@ramxis

This comment has been minimized.

Show comment
Hide comment
@ramxis

ramxis Feb 13, 2015

any help will be greatly appreciated thank you

ramxis commented Feb 13, 2015

any help will be greatly appreciated thank you

@RJReed67

This comment has been minimized.

Show comment
Hide comment
@RJReed67

RJReed67 Feb 19, 2015

ramxis, do you have python installed?

ramxis, do you have python installed?

@ramxis

This comment has been minimized.

Show comment
Hide comment
@ramxis

ramxis Feb 20, 2015

@rjreed yes i have it installed . I didnt uncomment the first line of script so thats why i was getting errors but i have uncommented it now by removing the #
!/usr/bin/python

-- coding: utf-8 --

"""
Zabbix SMTP Alert script for gmail.
"""

import sys
import smtplib
from email.MIMEText import MIMEText

now the script times out in zabbix event log screen with the error "Time out while executing the shell script"

ramxis commented Feb 20, 2015

@rjreed yes i have it installed . I didnt uncomment the first line of script so thats why i was getting errors but i have uncommented it now by removing the #
!/usr/bin/python

-- coding: utf-8 --

"""
Zabbix SMTP Alert script for gmail.
"""

import sys
import smtplib
from email.MIMEText import MIMEText

now the script times out in zabbix event log screen with the error "Time out while executing the shell script"

@abandin-stratio

This comment has been minimized.

Show comment
Hide comment
@abandin-stratio

abandin-stratio Feb 22, 2015

Hi:

The script works perfectly from command line, but the action configured to use the script is not sending any email. I dont see any log even in debug mode. Is there something im not doing good?

Thanks a lot

Hi:

The script works perfectly from command line, but the action configured to use the script is not sending any email. I dont see any log even in debug mode. Is there something im not doing good?

Thanks a lot

@onurzaim

This comment has been minimized.

Show comment
Hide comment
@onurzaim

onurzaim Mar 4, 2015

This worked with zabbix 2.4 with greatest ease. Thanks a lot for your effort.
For CentOS:
Put this script in /usr/lib/zabbix/alertscripts and modify the 14,15, 21th lines
chmod +x filename.py
chmod 775 filename.py

Go to Administrator -> media and add this script as filename.py
Dont forget to enable send all alerts to administrators in config. -> actions.

onurzaim commented Mar 4, 2015

This worked with zabbix 2.4 with greatest ease. Thanks a lot for your effort.
For CentOS:
Put this script in /usr/lib/zabbix/alertscripts and modify the 14,15, 21th lines
chmod +x filename.py
chmod 775 filename.py

Go to Administrator -> media and add this script as filename.py
Dont forget to enable send all alerts to administrators in config. -> actions.

@aswinKM

This comment has been minimized.

Show comment
Hide comment
@aswinKM

aswinKM Mar 6, 2015

Clean script and working well. Please make sure that the file name is zabbix-alert-smtp.py instead of zabbix-alert-smtp.sh

aswinKM commented Mar 6, 2015

Clean script and working well. Please make sure that the file name is zabbix-alert-smtp.py instead of zabbix-alert-smtp.sh

@kuzmenko-evgeniy

This comment has been minimized.

Show comment
Hide comment
@kuzmenko-evgeniy

kuzmenko-evgeniy Mar 26, 2015

Enable this parameter, if you have error smtplib.SMTPAuthenticationError: https://www.google.com/settings/security/lesssecureapps

Enable this parameter, if you have error smtplib.SMTPAuthenticationError: https://www.google.com/settings/security/lesssecureapps

@freewood

This comment has been minimized.

Show comment
Hide comment
@freewood

freewood Apr 8, 2015

Hello, friends. Need some advice. Trying to send email via command line:
"./zabbix-alert-smtp.sh my@test.email testmail testbody"
And it's freezes until i initerrupt it with "Ctrl+C". Then i get output:
"^CTraceback (most recent call last):
File "./zabbix-alert-smtp.sh", line 61, in
body=sys.argv[3])
File "./zabbix-alert-smtp.sh", line 34, in send_mail
session = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
File "/usr/lib64/python2.7/smtplib.py", line 256, in init
(code, msg) = self.connect(host, port)
File "/usr/lib64/python2.7/smtplib.py", line 317, in connect
(code, msg) = self.getreply()
File "/usr/lib64/python2.7/smtplib.py", line 361, in getreply
line = self.file.readline(_MAXLINE + 1)
File "/usr/lib64/python2.7/socket.py", line 476, in readline
data = self._sock.recv(self._rbufsize)
KeyboardInterrupt"

freewood commented Apr 8, 2015

Hello, friends. Need some advice. Trying to send email via command line:
"./zabbix-alert-smtp.sh my@test.email testmail testbody"
And it's freezes until i initerrupt it with "Ctrl+C". Then i get output:
"^CTraceback (most recent call last):
File "./zabbix-alert-smtp.sh", line 61, in
body=sys.argv[3])
File "./zabbix-alert-smtp.sh", line 34, in send_mail
session = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
File "/usr/lib64/python2.7/smtplib.py", line 256, in init
(code, msg) = self.connect(host, port)
File "/usr/lib64/python2.7/smtplib.py", line 317, in connect
(code, msg) = self.getreply()
File "/usr/lib64/python2.7/smtplib.py", line 361, in getreply
line = self.file.readline(_MAXLINE + 1)
File "/usr/lib64/python2.7/socket.py", line 476, in readline
data = self._sock.recv(self._rbufsize)
KeyboardInterrupt"

@DarkStorm007

This comment has been minimized.

Show comment
Hide comment
@DarkStorm007

DarkStorm007 May 25, 2015

Hello, i keep having problem using the script. Its appear that Zabbix sent the email but it is not... i preconfigured a 4 different emails to my user (hotmail, gmail, corporation and personal) and when i see the audit -> action log says the four were send. Does someone happened this? is there some troubleshoot debug (and how activate it) to see what im doing wrong or an extended log?, thank you in advanced.
Heres my conf:
http://puu.sh/i1rk0/8fa595e01a.png
http://puu.sh/i1rmb/ce829d45ed.png
http://puu.sh/i0rIQ/fd59579633.png
When i execute this:
http://puu.sh/i1rpG/e7f20fe903.png the email is received

UPDATE!
I made a test creating an script with a simple code that works without Zabbix, just executing "py a.py" (a.py is the name of the script). And also change the user and group owner to zabbix.
http://puu.sh/i2JcO/17724090f2.png
http://puu.sh/i3Hn9/9d209bbcc9.png
I reconfigure media type in order to call the a.py, chmod 777 and something i see is that the action log is registering by "sent" but it not creating the file as expected.

Hello, i keep having problem using the script. Its appear that Zabbix sent the email but it is not... i preconfigured a 4 different emails to my user (hotmail, gmail, corporation and personal) and when i see the audit -> action log says the four were send. Does someone happened this? is there some troubleshoot debug (and how activate it) to see what im doing wrong or an extended log?, thank you in advanced.
Heres my conf:
http://puu.sh/i1rk0/8fa595e01a.png
http://puu.sh/i1rmb/ce829d45ed.png
http://puu.sh/i0rIQ/fd59579633.png
When i execute this:
http://puu.sh/i1rpG/e7f20fe903.png the email is received

UPDATE!
I made a test creating an script with a simple code that works without Zabbix, just executing "py a.py" (a.py is the name of the script). And also change the user and group owner to zabbix.
http://puu.sh/i2JcO/17724090f2.png
http://puu.sh/i3Hn9/9d209bbcc9.png
I reconfigure media type in order to call the a.py, chmod 777 and something i see is that the action log is registering by "sent" but it not creating the file as expected.

@proligde

This comment has been minimized.

Show comment
Hide comment
@proligde

proligde May 31, 2015

Exactly what I was searching for - thanks a lot! :-)

Exactly what I was searching for - thanks a lot! :-)

@rowlandm

This comment has been minimized.

Show comment
Hide comment
@rowlandm

rowlandm Jul 6, 2015

I was using zabbix that had the files in /var/lib/zabbix

I downloaded the file and called it zabbix-alert-smtp.py

I ran command line of
cd /var/lib/zabbix
python zabbix-alert-smtp.py info@example.com Subject Body

I had to change in zabbix-alert-smtp.py

Sender Name

SENDER_NAME = u'Zabbix Alert'
to an email address when using mandrillapp.com
SENDER_NAME = u'info@example.com'

Hope this helps someone else.

PS. I also added this under the TLS section just to get more information for debugging.
session.set_debuglevel(1)

rowlandm commented Jul 6, 2015

I was using zabbix that had the files in /var/lib/zabbix

I downloaded the file and called it zabbix-alert-smtp.py

I ran command line of
cd /var/lib/zabbix
python zabbix-alert-smtp.py info@example.com Subject Body

I had to change in zabbix-alert-smtp.py

Sender Name

SENDER_NAME = u'Zabbix Alert'
to an email address when using mandrillapp.com
SENDER_NAME = u'info@example.com'

Hope this helps someone else.

PS. I also added this under the TLS section just to get more information for debugging.
session.set_debuglevel(1)

@DarkStorm007

This comment has been minimized.

Show comment
Hide comment
@DarkStorm007

DarkStorm007 Jul 6, 2015

It cannot work... is like zabbix cannot run the script... it is validated if exist when i remove it from the folder but just cannot executed it...

Changed owner to zabbix
Changed group owner to zabbix
Chmod 777
I tried with other file like a c++ program that just create a file when executed.

If i run the script manually from CLI just run easily and there's no problem...

It cannot work... is like zabbix cannot run the script... it is validated if exist when i remove it from the folder but just cannot executed it...

Changed owner to zabbix
Changed group owner to zabbix
Chmod 777
I tried with other file like a c++ program that just create a file when executed.

If i run the script manually from CLI just run easily and there's no problem...

@hosein33

This comment has been minimized.

Show comment
Hide comment
@hosein33

hosein33 Jul 12, 2015

i have problem with this script, i get this error :( anybody can i help me?

Traceback (most recent call last):
File "zabbix-alert-smtp.py", line 58, in
body=sys.argv[3])
File "zabbix-alert-smtp.py", line 42, in send_mail
raise e
socket.gaierror: [Errno -2] Name or service not known

i have problem with this script, i get this error :( anybody can i help me?

Traceback (most recent call last):
File "zabbix-alert-smtp.py", line 58, in
body=sys.argv[3])
File "zabbix-alert-smtp.py", line 42, in send_mail
raise e
socket.gaierror: [Errno -2] Name or service not known

@engasmaasamir

This comment has been minimized.

Show comment
Hide comment
@engasmaasamir

engasmaasamir Aug 16, 2015

I have the same problem as dark storm 007
I did the following
1-create media type with name of filename.py (after adding the script in alertscripts path)
2- in configuration --> action , I add action to send message via all media
3- in media type I disabled email
4- I created user and added the script media type to it and in send to I added its email
5-I opened less secure apps on gmail https://www.google.com/settings/security/lesssecureapps
6- I tested manually using : python filename.py my email@example.com title body
and the test succeeded and I received test message
but it can not suuceeded automatically using zabbix
what wrong ?
please help

I have the same problem as dark storm 007
I did the following
1-create media type with name of filename.py (after adding the script in alertscripts path)
2- in configuration --> action , I add action to send message via all media
3- in media type I disabled email
4- I created user and added the script media type to it and in send to I added its email
5-I opened less secure apps on gmail https://www.google.com/settings/security/lesssecureapps
6- I tested manually using : python filename.py my email@example.com title body
and the test succeeded and I received test message
but it can not suuceeded automatically using zabbix
what wrong ?
please help

@araghav

This comment has been minimized.

Show comment
Hide comment
@araghav

araghav Aug 25, 2015

The script works very well in Ubuntu, but throws error "smtplib.smtpException: STARTTLS not supported by the server". OS is CentOS 6.4 and Python version 2.6

Struggling to get this working. Any help?

Regards,
Ananth

araghav commented Aug 25, 2015

The script works very well in Ubuntu, but throws error "smtplib.smtpException: STARTTLS not supported by the server". OS is CentOS 6.4 and Python version 2.6

Struggling to get this working. Any help?

Regards,
Ananth

@tukalamakana

This comment has been minimized.

Show comment
Hide comment
@tukalamakana

tukalamakana Aug 27, 2015

People, don't forget to uncomment the alertscripts path on the server config once you change it.

People, don't forget to uncomment the alertscripts path on the server config once you change it.

@chuckxyu

This comment has been minimized.

Show comment
Hide comment
@chuckxyu

chuckxyu Sep 3, 2015

Anyone have tried this on Zabbix appliance?

Regards,
Carlos

chuckxyu commented Sep 3, 2015

Anyone have tried this on Zabbix appliance?

Regards,
Carlos

@chuckxyu

This comment has been minimized.

Show comment
Hide comment
@chuckxyu

chuckxyu Sep 4, 2015

After a while the script fails returning this message:

smtplib.SMTPServerDisconnected: Connection unexpectedly closed

Help anyone?

chuckxyu commented Sep 4, 2015

After a while the script fails returning this message:

smtplib.SMTPServerDisconnected: Connection unexpectedly closed

Help anyone?

@trinkity

This comment has been minimized.

Show comment
Hide comment
@trinkity

trinkity Sep 11, 2015

This script works well!!
I follow this steps on my ubuntu server

  1. check the value: AlertScript=blabla path into /etc/zabbiz/zabbix_server.conf
  2. put the script into blabla path
  3. chmod +x zabbix-alert-smtp.py
  4. chown zabbix:zabbix zabbix-alert-smtp.py
  5. go on zabbix
  6. Administration->Create Media type -> Script Name -> zabbix-alert-smtp.py remember to enable and save
  7. Configuration->Action->Report problems... -> Tab Operation check values remember to enable and save
    THIS STEP IS VERY IMPORTANT!!!
  8. Administration->Users-> Into admin account (you can filter with listbox Users menu top right page) -> Tab Media ->Add -> Type -> listbox: zabbix-alert-smtp.py -> Send To -> your email -> Status -> Enable and save

Hope to help someone...

This script works well!!
I follow this steps on my ubuntu server

  1. check the value: AlertScript=blabla path into /etc/zabbiz/zabbix_server.conf
  2. put the script into blabla path
  3. chmod +x zabbix-alert-smtp.py
  4. chown zabbix:zabbix zabbix-alert-smtp.py
  5. go on zabbix
  6. Administration->Create Media type -> Script Name -> zabbix-alert-smtp.py remember to enable and save
  7. Configuration->Action->Report problems... -> Tab Operation check values remember to enable and save
    THIS STEP IS VERY IMPORTANT!!!
  8. Administration->Users-> Into admin account (you can filter with listbox Users menu top right page) -> Tab Media ->Add -> Type -> listbox: zabbix-alert-smtp.py -> Send To -> your email -> Status -> Enable and save

Hope to help someone...

@engasmaasamir

This comment has been minimized.

Show comment
Hide comment
@engasmaasamir

engasmaasamir Oct 6, 2015

this script donot work for me although it work from command but not automatic

I used another way where I installed ssmtp and used mail command (after install mailutils on my server) to send emails using simple script

I used the following URLs to install ssmtp and add the script on my alertscript path

http://www.havetheknowhow.com/Configure-the-server/Install-ssmtp.html
http://www.codingmerc.com/blog/zabbix-send-email-through-ssmtp/

I used this script

!/bin/sh

export zabbixemailto="$1"
export zabbixsubject="$2"
export zabbixbody="$3"
echo "$zabbixbody" > /tmp/mymailinput
mail --subject="$zabbixsubject" $zabbixemailto < /tmp/mymailinput
rm /tmp/mymailinput

then save it as zabbix-alerts.sh

then added this script as my media type

then configured the action and the emails send automatically

this script donot work for me although it work from command but not automatic

I used another way where I installed ssmtp and used mail command (after install mailutils on my server) to send emails using simple script

I used the following URLs to install ssmtp and add the script on my alertscript path

http://www.havetheknowhow.com/Configure-the-server/Install-ssmtp.html
http://www.codingmerc.com/blog/zabbix-send-email-through-ssmtp/

I used this script

!/bin/sh

export zabbixemailto="$1"
export zabbixsubject="$2"
export zabbixbody="$3"
echo "$zabbixbody" > /tmp/mymailinput
mail --subject="$zabbixsubject" $zabbixemailto < /tmp/mymailinput
rm /tmp/mymailinput

then save it as zabbix-alerts.sh

then added this script as my media type

then configured the action and the emails send automatically

@Miskude

This comment has been minimized.

Show comment
Hide comment
@Miskude

Miskude Jan 25, 2016

Fantastic script, thank you.

Miskude commented Jan 25, 2016

Fantastic script, thank you.

@Komorebi-E

This comment has been minimized.

Show comment
Hide comment
@Komorebi-E

Komorebi-E Feb 6, 2016

Thank you for a great script.

If you're using 2 factor auth for Gmail, then you will need to generate an App password.

admin@zabbix:/usr/lib/zabbix/alertscripts$ python ./gmail-auth-smtp.py
requires 3 parameters (recipient, subject, body)
$ zabbix-gmail.sh recipient subject body

admin@zabbix:/usr/lib/zabbix/alertscripts$ python ./gmail-auth-smtp.py a.user@gmail.com Test-Subject Zabbix-Body

I checked my account and it was there instantly.

Thank you for a great script.

If you're using 2 factor auth for Gmail, then you will need to generate an App password.

admin@zabbix:/usr/lib/zabbix/alertscripts$ python ./gmail-auth-smtp.py
requires 3 parameters (recipient, subject, body)
$ zabbix-gmail.sh recipient subject body

admin@zabbix:/usr/lib/zabbix/alertscripts$ python ./gmail-auth-smtp.py a.user@gmail.com Test-Subject Zabbix-Body

I checked my account and it was there instantly.

@rajiv2205

This comment has been minimized.

Show comment
Hide comment
@rajiv2205

rajiv2205 May 11, 2016

Hi,
This script is really good. I used it for my office365 account, however, I am not able to get logs for any email sent by zabbix. I checked my mailbox "sent" but no email records are present there.

I would appreciate if anybody can suggest something.

Hi,
This script is really good. I used it for my office365 account, however, I am not able to get logs for any email sent by zabbix. I checked my mailbox "sent" but no email records are present there.

I would appreciate if anybody can suggest something.

@moorthy938

This comment has been minimized.

Show comment
Hide comment
@moorthy938

moorthy938 May 23, 2016

can you post script for zabbix 3.0.2...

because it is working only till version only...

can you post script for zabbix 3.0.2...

because it is working only till version only...

@costelolteanu

This comment has been minimized.

Show comment
Hide comment
@costelolteanu

costelolteanu Jun 27, 2016

Hi,
Thanks for the script.
I have been tried this script and in Actions log status is "Sent" but I don't receive the message in the email. I run this command from terminal and I receive emails: "python zabbix-alert-smtp.py user.account@mycompany.com 'Subiect: Test Mail' 'Message: Ahoy!'"
Please someone can help me?

Hi,
Thanks for the script.
I have been tried this script and in Actions log status is "Sent" but I don't receive the message in the email. I run this command from terminal and I receive emails: "python zabbix-alert-smtp.py user.account@mycompany.com 'Subiect: Test Mail' 'Message: Ahoy!'"
Please someone can help me?

@bhboots

This comment has been minimized.

Show comment
Hide comment
@bhboots

bhboots Jul 7, 2016

I have the same issue as costelolteanu on Zabbix version 3.0.3. Any help is appreciated.

bhboots commented Jul 7, 2016

I have the same issue as costelolteanu on Zabbix version 3.0.3. Any help is appreciated.

@bennetraja

This comment has been minimized.

Show comment
Hide comment
@bennetraja

bennetraja Jul 13, 2016

@bhboots and @costelolteanu, You would need to provide the following "Script parameters" - {ALERT.SENDTO}, {ALERT.SUBJECT} and {ALERT.MESSAGE}, while configuring the Media Type as "Script". It has been clearly documented on the Zabbix website under "Zabbix Documentation 3.0". Just follow it and you'll start receiving the Zabbix Alert mails as soon as it's done.

https://www.zabbix.com/documentation/3.0/manual/config/notifications/media/script

Thanks,
Franklin Bennet Raja

bennetraja commented Jul 13, 2016

@bhboots and @costelolteanu, You would need to provide the following "Script parameters" - {ALERT.SENDTO}, {ALERT.SUBJECT} and {ALERT.MESSAGE}, while configuring the Media Type as "Script". It has been clearly documented on the Zabbix website under "Zabbix Documentation 3.0". Just follow it and you'll start receiving the Zabbix Alert mails as soon as it's done.

https://www.zabbix.com/documentation/3.0/manual/config/notifications/media/script

Thanks,
Franklin Bennet Raja

@viettd

This comment has been minimized.

Show comment
Hide comment
@viettd

viettd Aug 18, 2016

That is what i need.
Thank @bennetraja.

viettd commented Aug 18, 2016

That is what i need.
Thank @bennetraja.

@abhijith0505

This comment has been minimized.

Show comment
Hide comment
@abhijith0505

abhijith0505 Sep 3, 2016

The script is written in python, but named as .sh
Is this right?

And my python script isnt being executed after adding it into media types.
Audit says 'Sent'

The script is written in python, but named as .sh
Is this right?

And my python script isnt being executed after adding it into media types.
Audit says 'Sent'

@sdbhabal

This comment has been minimized.

Show comment
Hide comment
@sdbhabal

sdbhabal Sep 14, 2016

I am getting bellow error in my /var/log/zabbix/zabbix_server.log after adding zabbix-alert-smtp.sh script to Media Types.

22037:20160914:052835.445 watchdog: no recipients found for database down messages

Please help.

I am getting bellow error in my /var/log/zabbix/zabbix_server.log after adding zabbix-alert-smtp.sh script to Media Types.

22037:20160914:052835.445 watchdog: no recipients found for database down messages

Please help.

@ah0khali

This comment has been minimized.

Show comment
Hide comment
@ah0khali

ah0khali Nov 21, 2016

what is suitable version for both zabbix and centos to this script

what is suitable version for both zabbix and centos to this script

@brinkdogg

This comment has been minimized.

Show comment
Hide comment
@brinkdogg

brinkdogg Apr 9, 2017

Zabbix says it "done" but never get an email I've tried .sh .py - nothing. But when I test manually I get emails without issue

Zabbix says it "done" but never get an email I've tried .sh .py - nothing. But when I test manually I get emails without issue

@rmsys

This comment has been minimized.

Show comment
Hide comment
@rmsys

rmsys May 29, 2017

Hi brinkdogg,

Same problem:
"Zabbix says it "done" but never get an email I've tried .sh .py - nothing. But when I test manually I get emails without issue"

rmsys commented May 29, 2017

Hi brinkdogg,

Same problem:
"Zabbix says it "done" but never get an email I've tried .sh .py - nothing. But when I test manually I get emails without issue"

@sgjava

This comment has been minimized.

Show comment
Hide comment
@sgjava

sgjava May 30, 2017

I just set this up on Zabbix 3.2.6 built from source running on Ubuntu 16.04 using https://www.jc.me.uk/2016/06/19/installing-zabbix-3-0-on-ubuntu-16-04.

  • I downloaded the script to /usr/local/share/zabbix/alertscripts and changed extension to .py
  • chmod +x *.py
  • chown -R zabbix:zabbix /usr/local/share/zabbix
  • nano zabbix-alert-smtp.py and change MAIL_ACCOUNT, MAIL_PASSWORD and SENDER_NAME
  • In UI go to Administration, Media types, Create media type and enter Type Script, Name Gmail, Script name zabbix-alert-smtp.py, add Script parameters:
    {ALERT.SENDTO}
    {ALERT.SUBJECT}
    {ALERT.MESSAGE}
    and click Add. This is important otherwise the parameters are not passed to the script.
  • In UI add the GMail media type to your user and set up action.

If it doesn't work then change Zabbix DebugLevel=4 and restart Zabbix server and test again. Then look in log /tmp/zabbix_server.log for zabbix-alert-smtp.py and after that you should see the return code and error message.

sgjava commented May 30, 2017

I just set this up on Zabbix 3.2.6 built from source running on Ubuntu 16.04 using https://www.jc.me.uk/2016/06/19/installing-zabbix-3-0-on-ubuntu-16-04.

  • I downloaded the script to /usr/local/share/zabbix/alertscripts and changed extension to .py
  • chmod +x *.py
  • chown -R zabbix:zabbix /usr/local/share/zabbix
  • nano zabbix-alert-smtp.py and change MAIL_ACCOUNT, MAIL_PASSWORD and SENDER_NAME
  • In UI go to Administration, Media types, Create media type and enter Type Script, Name Gmail, Script name zabbix-alert-smtp.py, add Script parameters:
    {ALERT.SENDTO}
    {ALERT.SUBJECT}
    {ALERT.MESSAGE}
    and click Add. This is important otherwise the parameters are not passed to the script.
  • In UI add the GMail media type to your user and set up action.

If it doesn't work then change Zabbix DebugLevel=4 and restart Zabbix server and test again. Then look in log /tmp/zabbix_server.log for zabbix-alert-smtp.py and after that you should see the return code and error message.

@l0bz1k

This comment has been minimized.

Show comment
Hide comment
@l0bz1k

l0bz1k Aug 10, 2017

Works fine with Ubnt + Zabbbix 3.2.1 ))
Thanks to @bennetraja and others!
Don't forget to add "Script parameters" !!!

l0bz1k commented Aug 10, 2017

Works fine with Ubnt + Zabbbix 3.2.1 ))
Thanks to @bennetraja and others!
Don't forget to add "Script parameters" !!!

@afield1235

This comment has been minimized.

Show comment
Hide comment
@afield1235

afield1235 Aug 11, 2017

@sgjava
I followed those steps and it worked for Office365. Make sure that each script parameter is on its own line. I accidentally tried copying and pasting all of them into one parameter line which doesn't work.

@sgjava
I followed those steps and it worked for Office365. Make sure that each script parameter is on its own line. I accidentally tried copying and pasting all of them into one parameter line which doesn't work.

@raghusundaram

This comment has been minimized.

Show comment
Hide comment
@raghusundaram

raghusundaram Oct 6, 2017

I was also been experiencing the issue of not receive the alert mail.
I have done two things which solved the problem.

I have added the script parameters as suggested by @bennetraja
I have also set the media type name just as Script. not as the actual script name.

I have just created the account to share my findings here.
I am more than happy to help for those who still couldn't crack it after seeing this post.

I was also been experiencing the issue of not receive the alert mail.
I have done two things which solved the problem.

I have added the script parameters as suggested by @bennetraja
I have also set the media type name just as Script. not as the actual script name.

I have just created the account to share my findings here.
I am more than happy to help for those who still couldn't crack it after seeing this post.

@vitutc

This comment has been minimized.

Show comment
Hide comment
@vitutc

vitutc Jun 5, 2018

It works!!! =D
Don't forget to add the script parameters as pointed about above!!! (follow sgjava's comment)

vitutc commented Jun 5, 2018

It works!!! =D
Don't forget to add the script parameters as pointed about above!!! (follow sgjava's comment)

@sgjava

This comment has been minimized.

Show comment
Hide comment
@sgjava

sgjava Jul 10, 2018

OK folks, my Zabbix server sharted, so I had to rebuild it. A little different this time with ODROID XU4, mainline kernel, Ubuntu 18.04, Zabbix Server 3.4.11 and Python 3 (Python 2 not installed by default). Hopefully my loss is your gain :) Since the original script doesn't work on Python 3 you'll need to edit that. I was too lazy to do a PR :)

#!/usr/bin/env python3

from email.mime.text import MIMEText
from email.header import Header
from email.utils import formatdate

print(u'requires 3 parameters (recipient, subject, body) \t$ zabbix-gmail.sh recipient subject body')

The rest of the stuff in my previous post is still relevant.

Test with:
sudo -H -u zabbix python3 /usr/local/share/zabbix/alertscripts/zabbix-alert-smtp.py yourgmail@gmail.com test testbody

sgjava commented Jul 10, 2018

OK folks, my Zabbix server sharted, so I had to rebuild it. A little different this time with ODROID XU4, mainline kernel, Ubuntu 18.04, Zabbix Server 3.4.11 and Python 3 (Python 2 not installed by default). Hopefully my loss is your gain :) Since the original script doesn't work on Python 3 you'll need to edit that. I was too lazy to do a PR :)

#!/usr/bin/env python3

from email.mime.text import MIMEText
from email.header import Header
from email.utils import formatdate

print(u'requires 3 parameters (recipient, subject, body) \t$ zabbix-gmail.sh recipient subject body')

The rest of the stuff in my previous post is still relevant.

Test with:
sudo -H -u zabbix python3 /usr/local/share/zabbix/alertscripts/zabbix-alert-smtp.py yourgmail@gmail.com test testbody

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment