-
-
Save superdaigo/3754055 to your computer and use it in GitHub Desktop.
#!/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 = '[email protected]' | |
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 | |
""" |
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 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 protected] title body
and the test succeeded and I received test message
but it can not suuceeded automatically using zabbix
what wrong ?
please help
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
People, don't forget to uncomment the alertscripts path on the server config once you change it.
Anyone have tried this on Zabbix appliance?
Regards,
Carlos
After a while the script fails returning this message:
smtplib.SMTPServerDisconnected: Connection unexpectedly closed
Help anyone?
This script works well!!
I follow this steps on my ubuntu server
- check the value: AlertScript=blabla path into /etc/zabbiz/zabbix_server.conf
- put the script into blabla path
- chmod +x zabbix-alert-smtp.py
- chown zabbix:zabbix zabbix-alert-smtp.py
- go on zabbix
- Administration->Create Media type -> Script Name -> zabbix-alert-smtp.py remember to enable and save
- Configuration->Action->Report problems... -> Tab Operation check values remember to enable and save
THIS STEP IS VERY IMPORTANT!!! - 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 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
Fantastic script, thank you.
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 [email protected] Test-Subject Zabbix-Body
I checked my account and it was there instantly.
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.
can you post script for zabbix 3.0.2...
because it is working only till version only...
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 [email protected] 'Subiect: Test Mail' 'Message: Ahoy!'"
Please someone can help me?
I have the same issue as costelolteanu on Zabbix version 3.0.3. Any help is appreciated.
@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
That is what i need.
Thank @bennetraja.
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'
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.
what is suitable version for both zabbix and centos to this script
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
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"
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.
Works fine with Ubnt + Zabbbix 3.2.1 ))
Thanks to @bennetraja and others!
Don't forget to add "Script parameters" !!!
@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.
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.
It works!!! =D
Don't forget to add the script parameters as pointed about above!!! (follow sgjava's comment)
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 [email protected] test testbody
Great job. This script works fine with Zabbix 4.0.5 running on Centos 7.6.1810
Any solution form this problem:
https://www.zabbix.com/forum/zabbix-troubleshooting-and-problems/415786-not-being-aboe-to-use-smtp-gmail-com-within-zabbix ?
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...