2 远程命令
2 Remote commands
概述
Overview
使用远程命令,您可以定义在某些情况下,监视的主机上会自动执行某个预定义的命令。
With remote commands you can define that a certain pre-defined command is automatically executed on the monitored host upon some condition.
因此,远程命令是智能主动监控的强大机制。
Thus remote commands are a powerful mechanism for smart pro-active monitoring.
在功能最明显的用途中,您可以尝试:
In the most obvious uses of the feature you can try to:
- 如果没有响应,则自动重新启动某些应用程序(Web服务器,中间件,CRM)
- 如果不响应请求,请使用IPMI“reboot”命令重新启动一些远程服务器
- 如果磁盘空间不足,可自动释放磁盘空间(删除较旧的文件,清理/tmp)
- 根据CPU负载,将VM从一个物理机移植到另一个物理机
在CPU(磁盘,内存,任何资源)不足的情况下,将新节点添加到云环境中
Automatically restart some application (web server, middleware, CRM) if it does not respond
- Use IPMI ‘reboot’ command to reboot some remote server if it does not answer requests
- Automatically free disk space (removing older files, cleaning /tmp) if running out of disk space
- Migrate a VM from one physical box to another depending on the CPU load
- Add new nodes to a cloud environment upon insufficient CPU (disk, memory, whatever) resources
配置远程命令的操作类似于发送消息的操作,唯一的区别是Zabbix将执行命令而不是发送消息。
Configuring an action for remote commands is similar to that for sending a message, the only difference being that Zabbix will execute a command instead of sending a message.
远程命令可以通过Zabbix server, proxy 或 agent执行。其在Zabbix agent上可以直接通过Zabbix server 或 Zabbix proxy执行。同时,在Zabbix agent和Zabbix proxy上的远程命令默认是不开启的,通过将参数EnableRemoteCommands设置为1开启。
Remote commands can be executed by Zabbix server, proxy or agent. Remote commands on Zabbix agent can be executed directly by Zabbix server or through Zabbix proxy. Both on Zabbix agent and Zabbix proxy remote commands are disabled by default. They can be enabled by setting the EnableRemoteCommands parameter to ‘1’.
远程命令限制为255个字符。可以通过将多个命令放置在新行上来执行多个命令。远程命令可能包含宏。
Remote command limit after all macros expansion depends on the type of database and character set (non- ASCII characters require more than one byte to be stored):
- 如果没有响应,则自动重新启动某些应用程序(Web服务器,中间件,CRM)
- 如果不响应请求,请使用IPMI“reboot”命令重新启动一些远程服务器
- 如果磁盘空间不足,可自动释放磁盘空间(删除较旧的文件,清理/ tmp)
- 根据CPU负载,将VM从一个物理盒移植到另一个物理盒
- 在CPU(磁盘,内存,任何资源)不足的情况下,将新节点添加到云环境中
Database | Limit in characters | Limit in bytes |
MySQL | 65535 | 65535 |
Oracle Database | 2048 | 4000 |
PostgreSQL | 65535 | not limited |
IBM DB2 | 2048 | 2048 |
SQLite (only Zabbix proxy) | 65535 | not limited |
配置远程命令的操作类似于发送消息的操作,唯一的区别是Zabbix将执行命令而不是发送消息。
Remote commands executed by Zabbix server are run as described in Command execution including exit code checking.
即使目标主机处于维护状态,也会执行远程命令。
Remote commands are executed even if the target host is in maintenance.
以下教程提供了有关如何设置远程命令的分步说明。
The following tutorial provides step-by-step instructions on how to set up remote commands.
配置
Configuration
在Zabbix代理(自定义脚本)上执行的那些远程命令必须首先在相应的命令中启用 zabbix_agentd.conf.
Those remote commands that are executed on Zabbix agent (custom scripts) must be first enabled in the respective zabbix_agentd.conf.
确保** EnableRemoteCommands 参数设置为 1 **并取消注释。 如果更改此参数,请重新启动代理守护程序。
Make sure that the EnableRemoteCommands parameter is set to 1 and uncommented. Restart agent daemon if changing this parameter.
远程命令不适用于主动模式Zabbix代理。
Remote commands do not work with active Zabbix agents.
然后,在配置新的动作时进入配置 - >操作:
Then, when configuring a new action in Configuration → Actions:
定义适当的条件。 在此示例中,设置在Apache应用程序之一的任何灾难问题时激活该操作:
Define the appropriate conditions. In this example, set that the action is activated upon any disaster problems with one of Apache applications:
All mandatory input fields are marked with a red asterisk.
- 在操作选项卡中,选择远程命令操作类型
- 选择远程命令类型(IPMI,自定义脚本,SSH,Telnet,全局脚本)
输入远程命令
In the Operations tab, select the Remote command operation type
- Select the remote command type (IPMI, Custom script, SSH, Telnet, Global script)
- If Custom script type is selected choose the way how custom script will be executed (by Zabbix agent, Zabbix server (proxy) or Zabbix server only)
- Enter the remote command
例如:
sudo /etc/init.d/apache restart
在这种情况下,Zabbix将尝试重新启动Apache进程。 使用此命令,确保该命令在Zabbix代理上执行 (点击// Zabbix代理按钮执行//)。
For example:
sudo /etc/init.d/apache restart
In this case, Zabbix will try to restart an Apache process. With this command, make sure that the command is executed on Zabbix agent (click the Zabbix agent button against Execute on).
Note the use of sudo - 默认情况下,Zabbix用户没有权限重新启动系统服务。 有关如何配置** sudo **的提示,请参见下文。
Note the use of sudo - Zabbix user does not have permissions to restart system services by default. See below for hints on how to configure sudo.
Zabbix代理应在远程主机上运行并接受传入连接。Zabbix代理在后台执行命令。
Zabbix agent should run on the remote host and accept incoming connections. Zabbix agent executes commands in background.
Zabbix代理程序上的远程命令由系统无延迟执行。运行[,nowait]键,不检查执行结果。在Zabbix服务器上,远程命令是在zabbix_server的TrapperTimeout参数中设置的超时执行的。conf文件被 检查 以执行结果。
Remote commands on Zabbix agent are executed without timeout by the system.run[,nowait] key and are not checked for execution results. On Zabbix server remote commands are executed with timeout as set in the TrapperTimeout parameter of zabbix_server.conf file and are checked for execution results.
访问权限
确保’zabbix’用户具有已配置命令的执行权限。可能有兴趣使用** sudo **来访问特权命令。 要配置访问,请以root身份执行:
# visudo
可以在 sudoers文件中使用的行:
# allows 'zabbix' user to run all commands without password.
zabbix ALL=NOPASSWD: ALL
# allows 'zabbix' user to restart apache without password.
zabbix ALL=NOPASSWD: /etc/init.d/apache restart
Access permissions
Make sure that the ‘zabbix’ user has execute permissions for configured commands. One may be interested in using sudo to give access to privileged commands. To configure access, execute as root:
# visudo
Example lines that could be used in sudoers file:
# allows 'zabbix' user to run all commands without password.
zabbix ALL=NOPASSWD: ALL
# allows 'zabbix' user to restart apache without password.
zabbix ALL=NOPASSWD: /etc/init.d/apache restart
<note tip>在某些系统上sudoers文件将阻止非本地用户执行命令。若要修改,在/etc/sudoers中添加requiretty注释。 :::
On some systems sudoers file will prevent non-local users from executing commands. To change this, comment out requiretty option in /etc/sudoers.
具有多个接口的远程命令
Remote commands with multiple interfaces
如果目标系统具有所选类型的多个接口(Zabbix代理或IPMI),则将在默认接口上执行远程命令。
If the target system has multiple interfaces of the selected type (Zabbix agent or IPMI), remote commands will be executed on the default interface.
可以通过SSH和Telnet使用除Zabbix代理之外的其他界面执行远程命令。 可用的使用界面按以下顺序选择:
It is possible to execute remote commands via SSH and Telnet using another interface than the Zabbix agent one. The available interface to use is selected in the following order:
* Zabbix agent default interface
* SNMP default interface
* JMX default interface
* IPMI default interface
* Zabbix agent default interface
* SNMP default interface
* JMX default interface
* IPMI default interface
对于IPMI远程命令,应使用以下语法:
<command> [<value>]
where
- <command> - one of IPMI commands without spaces
- <value> - ‘on’, ‘off’ or any unsigned integer. <value> is an optional parameter.
IPMI remote commands
For IPMI remote commands the following syntax should be used:
<command> [<value>]
where
- <command> - one of IPMI commands without spaces
- <value> - ‘on’, ‘off’ or any unsigned integer. <value> is an optional parameter.
示例
Examples
示例 1
Example 1
在一定条件下重新启动Windows。
Restart of Windows on certain condition.
为了在Zabbix检测到问题时自动重新启动Windows,请定义以下操作:
In order to automatically restart Windows upon a problem detected by Zabbix, define the following actions:
参数 说 | |
---|---|
Operation type | ‘Remote command’ |
Type | ‘Custom script’ |
Command | c:\windows\system32\shutdown.exe -r -f |
PARAMETER | Description |
---|---|
Operation type | ‘Remote command’ |
Type | ‘Custom script’ |
Command | c:\windows\system32\shutdown.exe -r -f |
示例 2
Example 2
使用IPMI控制重新启动主机。
Restart the host by using IPMI control.
参数 说 | |
---|---|
Operation type | ‘Remote command’ |
Type | ‘IPMI’ |
Command | reset |
PARAMETER | Description |
---|---|
Operation type | ‘Remote command’ |
Type | ‘IPMI’ |
Command | reset |
示例 3
Example 3
使用IPMI控制关闭主机电源。
Power off the host by using IPMI control.
参数 说 | |
---|---|
Operation type | ‘Remote command’ |
Type | ‘IPMI’ |
Command | power off |
PARAMETER | Description |
---|---|
Operation type | ‘Remote command’ |
Type | ‘IPMI’ |
Command | power off |