Global scripts

Looking at values and graphs on the frontend is nice and useful, but there are cases when extra information might be needed right away, or there might be a need to manually invoke an action, such as starting an upgrade process, rebooting the system, or performing some other administrative task. Zabbix allows us to execute commands directly from the frontend—this feature is called global scripts. Let's see what is available out of the box—navigate to Monitoring | Events and click on the host name in any of the entries:

Global scripts

The second part of this menu has convenience links to various sections in the frontend. The first part, labeled SCRIPTS, is what we are after. Currently, Zabbix ships with three preconfigured scripts—operating system detection, ping, and traceroute. We will discuss them in a bit more detail later, but for now just click on Ping. A pop-up window will open with the output of this script:

Global scripts

Notice the slight delay—the target host was pinged three times, and we had to wait for that to finish to get the output.

Global scripts are available by clicking on the host in several locations in the frontend from such a context menu. These locations are as follows:

  • Monitoring | Dashboard (in the Last 20 issues widget)
  • Monitoring | Overview (when hosts are located on the left-hand side)
  • Monitoring | Latest data (when showing data from more than one host)
  • Monitoring | Triggers
  • Monitoring | Events
  • Monitoring | Maps
  • Inventory | Hosts, where clicking on the Host name will open the inventory overview
  • Reports | Triggers top 100

Calling those three scripts preconfigured hinted at an ability to configure our own. Let's do just that.

Configuring global scripts

We can start by examining the existing scripts. Navigate to Administration | Scripts:

Configuring global scripts

The same three scripts we saw in the menu can be seen here. Let's see what they do:

  • Detect operating system: This script calls nmap and relies on sudo
  • Ping: Uses the ping utility, and pings the host three times
  • Traceroute: Calls the traceroute utility against the host

These three scripts are all are executed on the Zabbix server, so they should work for any host—a server with a Zabbix agent, a switch, a storage device, and so on.

Tip

Zabbix versions before 3.0 discarded stderr by default. If you see global scripts redirecting stderr to stdout by appending 2>&1 after the script command, it was a very important thing to configure in those versions, because otherwise, error messages from scripts would be silently lost. It is not required anymore since Zabbix 3.0, but it does not do any harm either.

We will discuss other options in a moment, but for now let's see whether all of these scripts work. Ping should work for most people. Traceroute will require the traceroute utility installed. As for operating system detection, it is unlikely to work for you out of the box. Let's try and make that one work.

Note

If Zabbix administrators are not supposed to gain root shell access to the Zabbix server, do not configure sudo as shown here. There's a feature in nmap that allows the execution of commands. Instead, create a wrapper script that only allows the -O parameter with a single argument.

Start by making sure nmap is installed on the Zabbix server. As the script uses sudo, edit /etc/sudoers (or use visudo) and add a line like this:

zabbix  ALL=NOPASSWD: /usr/bin/nmap

Tip

In distribution packages, Zabbix server might run as the zabbixs or zabbixsrv user instead—use that username in the sudoers configuration.

Adapt the nmap path if necessary. Similar to restarting the Apache web server, you might have to uncomment the requiretty option in /etc/sudoers. Again, all of these changes have to be done on the Zabbix server. When finished, run the operating system detection script from the menu—use one of the locations mentioned earlier:

Configuring global scripts

Tip

The SELinux security framework may prevent global scripts from working.

Hooray, that worked! The nmap command took some time to run. When running global scripts on the agent, they obey the same timeout as the remote commands discussed earlier in this chapter. This script was run on the server. In this case, there's a 60-second timeout in the frontend.

Now on to examining other script options, and also configuring some scripts of our own. When there's a problem on a system, it might be resource starvation. We might want to find out which processes on a system are stressing the CPU the most. Navigate back to Administration | Scripts and click on Create script. For our first script, fill in the following:

  • Name: Top CPU using processes
  • Commands: top -n 1 -b | grep -A 10 "^[ ]*PID"
    Configuring global scripts

Tip

Zabbix versions 3.0.0 and 3.0.1 have a bug—there's another Command field below the Commands field. Just ignore the second field. It is hoped that this bug will be fixed in later versions.

When done, click on Add. For the top command, we told it to only print the process list and do so once only. Then we grabbed the header line and the next 10 lines after it—assuming the header line starts with any amount of spaces and a string PID.

Tip

We enabled remote commands on Another host earlier—if you skipped that, make sure to enable them before proceeding.

Navigate to Monitoring | Events, click on Another host in the HOST column, and choose Top CPU using processes.

You may use any other location where this context menu is available—we listed these locations earlier:

Configuring global scripts

In this specific case, the systemd process is using most of the CPU. The Zabbix agent, which is running on this system, is not even in the top 10 here. Well, to be fair, on this system nothing much is happening anyway—all of the processes are reported to use no CPU at all.

Other similar diagnostic commands might show some package details, Media Access Control (MAC) addresses, or any other information easily obtained from standard utilities. Note that getting a list of processes that use the most memory is not possible with top on most operating systems or distributions— the ps command will probably have to be used. The following code might provide a useful list of the top 10 memory-using processes:

ps auxw --sort -rss | head -n 11

We are grabbing the top 11 lines here because that also includes the header.

Now let's configure another script—one that would allow us to reboot the target system. Navigate to Administration | Scripts and click on Create script. Fill in the following:

  • Name: Management/Reboot.
  • Commands: reboot.
  • User group: This command is a bit riskier, so we will limit its use to administrative users only—choose Zabbix administrators.
  • Host group: As this would not work on SNMP devices, it would not make sense to make it show up for hosts other than Linux systems here—choose Selected and start typing Linux in the text field. Choose Linux servers in the dropdown.
  • Required host permissions: We wouldn't want users with read-only access to be able to reboot hosts, so choose Write.
  • Enable confirmation: This is a potentially destructive action, so mark this checkbox.
  • Confirmation text: With the previous checkbox marked, we may fill in this field. Type Reboot this system?

Tip

Even though the group selection field might look similar to other places where multiple groups can be selected, here only one host group may be selected.

We may also test what this confirmation message would look like—click on Test confirmation:

Configuring global scripts

While the Execute button is disabled right now, we can see that this would look fairly understandable. Click on Cancel in the confirmation dialog. The final result should look like this—if it does, click on the Add button at the bottom:

Configuring global scripts

Now let's see what this script would look like in the menu—navigate to Monitoring | Events and click on Another host in the HOST column. In the pop-up menu, move the mouse cursor over the entry Management:

Configuring global scripts

Notice how the syntax we used created a submenu—the slash is used as a separator. We could group Ping, Traceroute, and Top CPU using processes as Diagnostics, add more entries in the Management section, and create a useful toolset. Note that we can also use zabbix_get on the server here and poll individual items that we might not want to monitor constantly. Entries can be nested this way as many times as needed, but beware of creating too many levels. Such mouseover menus are hard to use beyond the first few levels, as it is too easy to make a wrong move and suddenly all submenus are closed.

Regarding the Reboot entry, if it seemed a bit risky to add, fear not—it does not work anyway. First, we had to use sudo for it in the command. Second, we had to configure sudoers to actually allow the running of that command by the zabbix user.

Reusing global scripts in actions

Some of the global scripts added this way only make sense when used interactively—most of the data gathering or diagnostic ones would probably fall under this category. But our Reboot entry might be reused in action operations, too. Instead of configuring such commands individually in global scripts and each action, we would have a single place to control how the rebooting happens. Maybe we want to change the reboot command to issue a pending reboot in 10 minutes. That way a system administrator who might be working on the system has some time to cancel the reboot and investigate the problem in more detail.

We already have the global script for rebooting created. If we had a trigger that warranted rebooting the whole system, we would create an action with appropriate conditions. In the action properties, global scripts may be reused by choosing Remote command in the Operation type dropdown when editing an operation. Then, in the Type dropdown, Global script must be selected and a specific script chosen:

Reusing global scripts in actions

As these scripts can be used both from the frontend and in actions, they're not called just frontend scripts—they are global scripts.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset