Thursday, December 10, 2015

[Zabbix] How to execute a script if your host/website is down in Zabbix

Zabbix is a bit confusing for beginners so here a quick tutorial how to simply execute a script, in my case it was used for a failover actions (mysql slave to master promotion and DNS ip change).

First of all you have to understand the way of how things work in Zabbix:

(more here: http://www.zbxtutorials.org/2012/10/z20g001-terminology.html )

Let's start by setting up a web scenario:
Go Configuration -> Hosts, pick a host that will do the monitoring (I often use the zabbix server itself), click web(0) and right upper corner "create scenario" (read wiki how to, but its easy)

Next step we find out what the event is called since we need this for our trigger expression: Go to Monitoring -> Latest Data -> Show Filter (small the center of screen), pick your host and filter, under other you'll find web.test.fail[site test](name depends on your setup) and its last value 0, this will jump to 1 if the site is not reachable.

edit: IN ZABBIX 3.0 you gotta check "Show Details" to see key names

Next step we will create a trigger: Configuration -> Hosts -> pick Host -> Triggers -> create new trigger upper right corner: expression should look like this "{YOURHOSTNAME:web.test.fail[site test].last()}>0"

Next step create an Action: Configuration -> Actions -> Create Action:
You only have to set name, under conditions remove the given ones and under New condition pick "trigger" "like" and your trigger name which should be auto-suggested. Under Operations click new, Operation Type : Remote Command, Target add new Current Host, Execute on...your choice, Type Custom script and enter your commands.

That is it.

I recommend not sending sensitive information in the commands, if you have such then just put the commands in a script at the agent server and execute them with "/bin/bash /path/script.sh"