In this article we will configure a server so that Zabbix can execute remote commands when certain conditions are met.
Enable remote command execution
On the server where we want Zabbix to be able to execute remote commands, it is necessary to edit the agent configuration file.
Open the file:
# vi /etc/zabbix/zabbix_agentd.conf
Uncomment or add the following line:
AllowKey=system.run[*]
Save the changes and restart the agent:
# systemctl restart zabbix-agent
Grant permissions to the zabbix user
The Zabbix agent runs under the system user zabbix. For this user to execute remote commands, we must grant sudo permissions.
Run:
# visudo
And add the following line at the end:
zabbix ALL=NOPASSWD: ALL
With this, the zabbix user will be able to execute remote commands without requiring a password.
Add templates to the host
Before creating an automatic action, we need Triggers that are activated when certain conditions are met. Custom triggers can be created or we can use existing templates. For this example, we will use the template Apache by Zabbix agent to restart the Apache service when it goes down.
In the Zabbix interface, go to Monitoring → Hosts and select the agent host. In the drop-down menu, click Host:
In the new window, select Apache by Zabbix agent in the Templates field and update the Host:
This way, Zabbix will be able to check if the service is down and trigger the corresponding alert.
Create a script in Zabbix
Now we will define the script that will restart Apache when the condition is met.
Go to Alerts → Scripts and click on the blue button in the top right Create script.
In the New script window, assign a descriptive name, for example: Restart Apache:
In Type, select Script and in Commands, enter the command to restart Apache:
sudo /etc/init.d/apache2 restart
Create an action in Zabbix
Finally, we configure the action that will execute the command when the Apache service is down.
Go to Alerts → Actions → Trigger actions and click on the blue button Create action.
In the New action window, give it a name, for example: Apache is down.
In Conditions, click Add and add the condition by selecting in Triggers Apache: Service is down:
In the Operations tab, select from the drop-down the command Restart Apache created in the previous step.
Check the Current host box and add it.
With this configuration, Zabbix will automatically restart Apache on your server in case of failure, ensuring service continuity without manual intervention:
If you have any questions about your cloud server configuration or about Zabbix, write to us at support@clouding.io. Our Support team will be happy to help you.