Saturday 6 February 2016

Zabbix and authenticated smtp email using gmail's servers


I'm using Zabbix to monitor my personal VMware server and VMs running on it. It's running as a VM on the same ESXi host. Not ideal but it works.

For alerting I wanted to use email but don't have my own mail sever, however I do have gmail account.

Gmail uses authenticated SMTP, however Zabbix doesn't support this. I did find though that Zabbix can use scripts for alerting and looking online, I found the answer on Github where someone had already solved this problem.

I've tested the script in this repository and it worked with only needing to set the username (email address) and password.

N.B. If you are using 2 factor authentication with Google, then you will need to generate an App password for the script to run.

https://gist.github.com/superdaigo/3754055

The below is based on Ubuntu 14.04 LTS:

You can find the location that alert scripts are stored in from the /etc/zabbix/zabbix_server.conf file.
This is normally: /usr/lib/zabbix/alertscripts

        admin@zabbix:/etc/zabbix$ sudo grep alert zabbix_server.conf
     # How often Zabbix will try to send unsent alerts (in seconds).
     # Full path to location of custom alert scripts.
     # AlertScriptsPath=${datadir}/zabbix/alertscripts
     AlertScriptsPath=/usr/lib/zabbix/alertscripts

The script from https://gist.github.com/superdaigo/3754055 should be saved to:  /usr/lib/zabbix/alertscripts with the extension ".py", e.g. "gmail-suth-smtp.py".

It then needs to be made executable and give zabbix permission to run it, most easily done with:

     sudo chmod +x gmail-suth-smtp.py

     sudo chown root:zabbix gmail-suth-smtp.py

You can now test is by running it - if you don't give it the right parameters it will let you know what they are.

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

Configuring Zabbix to use the script

Create a new Media Type:

On your Zabbix server, go to the Administration Tab, Media Types, in the top right, click "Create Media Type". As you can see, as I am not using the default alert types, so I have disabled them.




Configure Zabbix to use the new media type:
Click on the Configuration tab, Actions section.

The default Action is disabled, "Report problems to Zabbix administrators" and is set to use all Media Types.

Click on this action and then its Operations tab, then on the Edit button for the Action Operations section.

Half way down (highlighted) is the "Send only to" this can be changed to a specific type, in this case the gmail-auth-smtp.

N.B. do not use the [Update] button at the very bottom just yet, you need to use the smaller "Update" button within the section first. Otherwise the section settings won't get saved.



Setting the email address for the user and the media type

To set an email address for the user for the media type this is done via Administration, Users.
Edit the User and click on the Media tab.
Click on Add or Edit if one exists.
Set the "Type to" be: gmail-auth-smtp
Then set the "Send to" to the desired email address.

N.B. It is possible to bulk Add and Modify User's media using the API and say a csv file of user and email address. I say this, as I haven't found a way in the GUI to do it in bulk, e.g. CSV import. The import/export xml does not lists users as option. https://www.zabbix.com/documentation/2.4/manual/xml_export_import

API Documentation for adding and modifying users media types. 


No comments:

Post a Comment