Detailed examples of Zabbix remote command execution

Detailed examples of Zabbix remote command execution

When a Zabbix trigger reaches a threshold, an action is executed: sending an alert message or executing a remote command.

This article mainly configures and verifies that zabbix executes remote commands.

one. environment

Server: Based on CentOS-7-x86_64-1511;

Zabbix: zabbix-3.0.1server/agent.

two. Precautions

Some important things to note: 1. Remote command execution is from the server to the agent, and does not support active agent mode; 2. Proxy mode is not supported; 3. The zabbix user must have execution permissions for the command, and can use sudo to grant root permissions (configure sudo without password);

4. The remote command is only executed, and the success or failure of the execution is not detected and confirmed. You can check the action execution in "Monitoring-->Events", or check whether the remote command is executed successfully in "Reports-->Action log" (successful execution is "Executed").

three. Example

Verify with a simple example:

The number of real-time online users on the monitoring server does not exceed 2. When the threshold is exceeded, an alarm is triggered and a command is executed to force the third logged-in user to log off so that the monitoring item is below the threshold.

1. Set the Trigger

Configuration-->Templates-->Select "Template OS Linux" template "Triggers"-->Create trigger

#trigger name customization, you can use macros

Name:User login number gt 2 on {HOST.NAME}

Expression: {Template OS Linux:system.users.num.last()}>2

#You can click "Add" after the "Expression" item and use the Item + Function provided by Zabbix to set the threshold expression as follows:

#Custom level

Severity: Warning

The rest of the settings are acceptable. After completing the settings, click "Add".

2. Remote command switch

#Agent does not support executing remote commands by default. You need to enable this parameter in the agent configuration file;
#The agent.conf file path should be modified according to the actual situation. Here is the customized path after compilation and installation. Please see http://www.cnblogs.com/netonline/p/7406598.html
#After the agent.conf file is modified, the process needs to be restarted.
[root@localhost ~]# sed -i 's|# EnableRemoteCommands=0|EnableRemoteCommands=1|g' /usr/local/zabbix/etc/zabbix_agentd.conf
[root@localhost ~]# service zabbix_agentd restart

3. Set Action

Configuration-->Actions-->Create action

Action

In the Action option, define Name: Limit the number of online users no more than 2

#You can customize the action name, and the rest of the action options can use the default values, as follows:

Conditions

Add new conditional judgments in the Conditions option to make the judgment more targeted, such as:

New condition: Trigger severity = Warning

New condition: Trigger name like User login number gt 2

#trigger name corresponds to the trigger name defined in step 1, as follows:

Operations

In the Operations option, add a new "Action operation" and click "New".

Operation type: Select "Remote Command"

Target list: Add target as "Current host" #agent on this machine

Type: Select "Custom script"

Execute on: Select "Zabbix agent" and the command is " sudo /root/test.sh "

#The account that executes the command is the zabbix account, not the root account. Not using the sudo command will cause the command to be ineffective after execution.

#In addition, I have tried to use specific commands instead of scripts. The result is that the command is executed but not effective. Because there is no specific failure log, the reason cannot be analyzed.

#Use the default values ​​for the rest of the content and click "Add", as shown below:

4. Remote command script

#Edit the command script according to the path of the command script defined in step 3; [root@localhost ~]# vim test.sh

#!/bin/bash PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin export PATH

#Here we simply assume that the third login tty is pts/2 pkill -kill -t pts/2

[root@localhost ~]# chmod 764 test.sh

5. Configure sudo

#Grant the zabbix account sudo permission to execute commands without a password. For security reasons, the last "ALL" can be defined as a specific command; #Or in step 4, change the script owner to zabbix and give it execution permission, but note that the directory where the script is placed has executable and even writable permissions for the zabbix account, so that the script in step 3 does not need sudo to grant permissions [root@localhost ~]# visudo

zabbix ALL=NOPASSWD: ALL

6. Verification

Remotely log in to the third tty of the host, do not perform any other operations in the CLI, trigger the trigger threshold, and trigger the command action to force the third logged-in user to go offline.

As can be seen from the screenshot, when the number of online users exceeds the threshold, the trigger is in the "PROBLEM" state for only about 1 minute and then returns to the "OK" state, indicating that the script command forcing the third user to go offline has been executed and executed successfully.

From the historical data of the "Number of logged in users" Item, we can also see that when the trigger is triggered, the third logged-in user is offline.

Under the third tty, you can see that the connection is disconnected by the host itself (the local time is displayed, which deviates from the host time in the above figure).

Summarize

This is the end of this article about Zabbix remote execution commands. For more related Zabbix remote execution commands, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Installation and configuration method of Zabbix Agent on Linux platform
  • Zabbix monitors the process of Linux system services
  • Use of Zabbix Api in Linux shell environment
  • How to install Zabbix monitoring in Linux
  • How to set up Zabbix to monitor Linux hosts
  • Zabbix configuration DingTalk alarm function implementation code
  • Detailed explanation of zabbix executing scripts or instructions on remote hosts
  • Detailed explanation of Zabbix installation and deployment practices
  • Zabbix monitors Linux hosts based on snmp

<<:  Examples of using MySQL pessimistic locking and optimistic locking

>>:  Vue+swiper realizes timeline effect

Recommend

VMware configuration VMnet8 network method steps

Table of contents 1. Introduction 2. Configuratio...

Summary of nginx configuration location method

location matching order 1. "=" prefix i...

Detailed explanation of MySQL Group by optimization

Table of contents Standard execution process opti...

Vue element implements table adding, deleting and modifying data

This article shares the specific code of vue elem...

Install Docker for Windows on Windows 10 Home Edition

0. Background Hardware: Xiaomi Notebook Air 13/In...

Four ways to create objects in JS

Table of contents 1. Create objects by literal va...

Summary of commonly used tags in HTML (must read)

Content Detail Tags: <h1>~<h6>Title T...

Detailed explanation of DIV+CSS naming rules can help achieve SEO optimization

1. CSS file naming conventions Suggestion: Use le...

How to use js to determine whether a file is utf-8 encoded

Conventional solution Use FileReader to read the ...

HTML table markup tutorial (18): table header

<br />The header refers to the first row of ...

Common methods of Vue componentization: component value transfer and communication

Related knowledge points Passing values ​​from pa...

Summary of Linux command methods to view used commands

There are many commands used in the system, so ho...