SSH检查以agent无代理监控方式执行。SSH检查不需要Zabbix agent。
执行SSH检查时,Zabbix server必须预先 4-配置源代码支持SSH2 (libssh或libssh2)。另请参阅: serverproxy。
从RHEL 8开始,仅支持libssh。对于其他发行版,建议使用libssh而非libssh2。
SSH检查提供两种认证方式 - 用户名/密码组合和基于file的认证
若不打算使用密钥认证 除在源码编译时将libssh或libssh2链接至Zabbix外 无需额外配置
要为SSH 监控项启用基于密钥的认证,需要对服务器配置进行特定修改。
打开Zabbix server的file配置文件 (zabbix_server.conf),即root
找到以下行:
取消注释并设置公钥/私钥存储目录的完整路径:
保存file后重启Zabbix server服务。
示例中的/home/zabbix是zabbix用户的主目录, .ssh是默认通过ssh-keygen命令 在主目录下生成公私钥的目录。
不同操作系统发行版的Zabbix server安装包 create通常将zabbix用户的主目录设为其他路径, 例如系统账户常用的/var/lib/zabbix。
生成密钥前,建议将主目录调整为/home/zabbix以匹配 上文提到的SSHKeyLocation
Zabbix server 配置参数。
若已按installation section手动添加zabbix账户, 可跳过后续步骤。此类情况下用户主目录通常已是/home/zabbix。
修改zabbix用户主目录前需停止相关进程:
执行以下命令迁移现有主目录(若存在):
若原位置无主目录,则在新位置创建:
为确保安全,可执行以下权限设置命令:
重启之前停止的进程:
现在可通过以下命令generate公私钥(为便于阅读,已注释命令提示符):
sudo -u zabbix ssh-keygen -t rsa
# Generating public/private rsa key pair.
# Enter file in which to save the key (/home/zabbix/.ssh/id_rsa):
/home/zabbix/.ssh/id_rsa
# Enter passphrase (empty for no passphrase):
<Leave empty>
# Enter same passphrase again:
<Leave empty>
# Your identification has been saved in /home/zabbix/.ssh/id_rsa.
# Your public key has been saved in /home/zabbix/.ssh/id_rsa.pub.
# The key fingerprint is:
# 90:af:e4:c7:e3:f0:2e:5a:8d:ab:48:a2:0c:92:30:b9 zabbix@it0
# The key's randomart image is:
# +--[ RSA 2048]----+
# | |
# | . |
# | o |
# | . o |
# |+ . S |
# |.+ o = |
# |E . * = |
# |=o . ..* . |
# |... oo.o+ |
# +-----------------+
默认生成的公钥(id_rsa.pub)和私钥(id_rsa) 存储在/home/zabbix/.ssh目录, 与Zabbix server的SSHKeyLocation
配置参数相符。
虽然ssh-keygen工具和SSH服务器可能支持"rsa"以外的密钥类型, 但Zabbix使用的libssh2库可能不支持其他类型。
此步骤仅需对每个要通过SSH检查监控的主机执行一次。
通过以下命令,可将公钥file安装到远程主机 10.10.10.10上,从而使用root账户执行SSH检查(为提高可读性,命令提示符已被注释):
sudo -u zabbix ssh-copy-id [email protected]
# The authenticity of host '10.10.10.10 (10.10.10.10)' can't be established.
# RSA key fingerprint is 38:ba:f2:a4:b5:d9:8f:52:00:09:f7:1f:75:cc:0b:46.
# Are you sure you want to continue connecting (yes/no)?
yes
# Warning: Permanently added '10.10.10.10' (RSA) to the list of known hosts.
# [email protected]'s password:
<Enter root password>
# Now try logging into the machine, with "ssh '[email protected]'",
# and check to make sure that only the key(s) you wanted were added.
现在可以使用zabbix用户账户的默认私钥(/home/zabbix/.ssh/id_rsa)来测试SSHlogin:
sudo -u zabbix ssh [email protected]
若login成功,则表明shell中的配置部分已完成,可关闭远程SSH会话。
实际执行的命令必须放置在监控项配置的Executed script字段中。 通过将多个命令放置在新行上可以依次执行多个命令。这种情况下返回值也将以多行格式呈现。
所有必填字段均以红色星号标记。
SSH 监控项需要特定信息的字段包括:
参数 | 描述 | 备注 |
---|---|---|
Type | 在此选择SSH agent。 | |
Key | 格式为ssh.run[唯一简短描述,<ip>,<端口>,<编码>,<ssh选项>]的唯一(每个主机)监控项键 | 唯一简短描述是必填项,且每个主机的SSH 监控项应保持唯一。 默认端口为22,而非此监控项所分配接口中指定的端口。 ssh选项(自version 6.0.25起支持;要求version的libssh版本0.9.0及以上或libssh2)允许以key1=value1;key2=value2,value3格式传递额外SSH选项。单个键的多个值可用逗号分隔传递(此时参数必须参数引用字符串);多个选项键可用分号分隔。 支持的选项键包括: KexAlgorithms 、HostkeyAlgorithms 、Ciphers 、MACs 。选项键和值的支持取决于SSH库;若选项不受支持将返回错误,且监控项将变为不支持状态。注意:不支持用于追加密码设置的"+"符号和禁用特定密码设置的"!"符号(如GnuTLS和OpenSSL中的用法)。 示例: => ssh.run[KexAlgorithms,127.0.0.1,,,Ciphers=aes128-ctr] => ssh.run[KexAlgorithms,,,,"KexAlgorithms=diffie-hellman-group1-sha1;HostkeyAlgorithms=ssh-rsa,ssh-dss,ecdh-sha2-nistp256"] |
Authentication method | 选择"Password"或"Public key"之一。 | |
User name | 用于远程主机认证的用户名。必填。 | |
Public key file | 当Authentication method为"Public key"时的公钥文件名。必填。 | 示例:id_rsa.pub - 由ssh-keygen命令生成的默认公钥file名称。 |
Private key file | 当Authentication method为"Public key"时的私钥文件名。必填。 | 示例:id_rsa - 默认私钥file名称。 |
Password or Key passphrase |
用于认证的密码或 若私钥使用时的密钥口令。 |
若未使用口令,请保持Key passphrase字段为空。 另见ssh检查关于口令使用的说明。 |
Executed script | 通过SSH远程会话执行的shell命令。 | 执行的shell命令返回值限制为16MB(包括被截断的尾部空格);文本数据限制同样适用。 注意:libssh2库可能将可执行脚本截断至约32kB。 示例: date +%s systemctl status mysql-server *ps auxww \ |