Serverspace Black Friday
WT
April 21, 2023
Updated June 7, 2023

Configuring remote comands and scripts in Zabbix

Monitoring Zabbix

Scripts in Zabbix can be used to perform custom monitoring checks, collect and process data from external sources, and automate tasks based on monitoring events. Zabbix supports a variety of scripting languages, including Bash, Python, Perl, and Ruby.
In this article, we'll explore Zabbix alert scripts, how to create them, and how to configure the Zabbix agent to work with them.

Configuring Zabbix Agent to Work with remote commands.

First, we’ll need to configure the Zabbix agent to work with scripts:

  1. Log in to the host running the agent.
  2. Edit the zabbix_agentd.conf:
  3. Set the "EnableRemoteCommands" parameter to 1 to let the agent execute remote commands.
  4. And set the "UnsafeUserParameters" parameter to 1 to allow the Zabbix agent to execute user scripts.
  5. Optionally you can switch “LogRemoteCommands” on if you want to keep the logs.

Zabbix agent .conf

Restart the Zabbix agent service to apply the changes.

Creating Zabbix Alert Scripts

To create an alert script in Zabbix, follow these steps:

Go to the "Administration" → "Scripts" in the left-hand side menu. Click on "Create Script" in the top right corner of the screen. Give your script a name, select the scope option you want to use, fill out the information needed, which may include things like script parameters, command arguments, or media type settings, and click “Add”.

Creating script

Now let's have a more detailed look into some of the options, you can choose the Scope and the type of the script.

Scopes of the script

Action operation scripts:

These are scripts that are executed as part of an active operation. Action operation scripts are executed automatically and can be used to perform tasks such as sending notifications, running scripts on remote hosts, or updating data in external systems. These scripts are associated with a specific action and are executed whenever that action is triggered.

Manual host action scripts:

These are scripts that can be executed manually on a specific host. They are associated with a specific host and can be used to perform tasks such as restarting a service, running a diagnostic command, or performing a backup. Manual host action scripts are executed manually by a user from the Zabbix web interface or API, and can only be executed on the host that they are associated with.

Manual event action scripts:

These are scripts that can be executed manually on a specific event. They are associated with a specific event and can be used to perform tasks such as sending a custom notification, updating a ticketing system, or triggering a workflow. Manual event action scripts are executed manually by a user from the Zabbix web interface or API, and can only be executed on the event that they are associated with.

Script types

Webhook scripts: A webhook is a way for one application to provide data to another application in real-time. Webhook scripts in Zabbix can be used to send data to an external system, such as a chat application or incident management platform. To configure a webhook script in Zabbix, you'll need to create a new media type with the appropriate settings (such as the webhook URL and any required headers or authentication), and then associate that media type with an action.

Script type: Zabbix also supports custom scripts, which can be written in various scripting languages (such as Bash, Perl, Python, or PowerShell) and executed in different scopes, depending on where the script is intended to run. There are three different execution scopes for custom scripts which defines where can they be used:

  • Zabbix agent
  • Zabbix server proxy
  • Zabbix server

To configure a custom script in Zabbix, you'll need to create a new script with the appropriate code and language settings. You'll then be able to use that script in various contexts, such as in an action or in a custom item key.

SSH scripts: SSH scripts in Zabbix can be used to execute commands or scripts on a remote host over SSH. To configure an SSH script in Zabbix, you'll need to create a new SSH key pair and add the public key to the remote host's authorized_keys file. You'll then need to create a new script in Zabbix that specifies the command to be executed on the remote host, along with the necessary SSH connection details.

Telnet scripts: Telnet is a network protocol that provides a virtual terminal connection to a remote host. To configure a Telnet script in Zabbix, you'll need to create a new Telnet item in the Zabbix configuration (on any host of your choosing, it’s an agentless check which can point to any IP, which can be useful for monitoring hosts unable to run agent), specifying the hostname or IP address of the remote host, as well as the port number and any necessary login credentials. You'll then need to create a new script in Zabbix that specifies the command to be executed on the remote host.

Creating Telnet item

IPMI scripts: IPMI (Intelligent Platform Management Interface) is a standard for remote server management that allows administrators to monitor and manage hardware components such as power supplies, fans, and temperature sensors. IPMI scripts in Zabbix can be used to execute IPMI commands on a remote server. To configure an IPMI script in Zabbix, you'll need to enable IPMI on the remote server and configure the necessary network settings. You'll then need to create a new script in Zabbix that specifies the IPMI command to be executed, along with the necessary connection details.

Examples of Zabbix Alert Scripts

Once you have created your alert script, you can configure it to execute specific actions when an alert is generated. For example, you can configure a script to restart a service, send an email notification, or execute a command on a remote server.

Here are some examples of alert scripts from the basic Zabbix configuration:

Detect operating system: This script can be used to detect the version of the operating system on the host

sudo /usr/bin/nmap -O/usr/bin/traceroute {HOST.CONN} {HOST.CONN}

Ping: speaks for itself

ping -c 3 {HOST.CONN}; case $? in [01]) true;; *) false;; esac

Service restart: restarts a service on a Windows host discovered by the autodiscovery function

net start {TRIGGER.DESCRIPTION}

Conclusion

We looked into configuring Zabbix scripts, overall, configuring these scripts requires some technical knowledge of the protocols and technologies involved, as well as an understanding of Zabbix's scripting and automation capabilities. However, with the right configuration, these scripts can provide powerful automation and integration capabilities for your Zabbix monitoring system.

You may be also interested in

Vote:
5 out of 5
Аverage rating : 5
Rated by: 4
1101 CT Amsterdam The Netherlands, Herikerbergweg 292
+31 20 262-58-98
700 300
ITGLOBAL.COM NL
700 300
We use cookies to make your experience on the Serverspace better. By continuing to browse our website, you agree to our
Use of Cookies and Privacy Policy.