zabbix_sender是一个命令行工具,可以用来发送Zabbix服务器处理性能数据。该工具通常用于长时间运行的用户脚本,用于定期发送可用性和性能数据。
Bash
[root@ubuntu1804 ~]#apt -y install zabbix-sender
[root@centos8 ~]#yum -y install zabbix-sender
[root@ubuntu1804 ~]#zabbix_sender -V
zabbix_sender (Zabbix) 5.0.14
Revision 892ce506db 16 July 2021, compilation time: Jul 16 2021 14:45:07
Copyright (C) 2021 Zabbix SIA
License GPLv2+: GNU GPL version 2 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it according to
the license. There is NO WARRANTY, to the extent permitted by law.
This product includes software developed by the OpenSSL Project
for use in the OpenSSL Toolkit (http://www.openssl.org/).
Compiled with OpenSSL 1.1.1 11 Sep 2018
Running with OpenSSL 1.1.1 11 Sep 2018
范例: 查看 zabbix_sender 使用
Bash
[root@ubuntu1804 ~]#zabbix_sender -h
zabbix_sender [-v] -z server [-p port] [-I IP-address] -s host -k key -o value
-c --config < file > #配置文件绝对路径
-z --zabbix-server <server> #zabbix server的IP地址
-p --port <server port> #zabbix server端口.默认10051
-s --host < hostname > #主机名,zabbix agent里面配置的主机名(不是服务器
的 hostname ),不能使用ip地址
-I -- source -address <IP address> #源IP
-k --key <key> #监控项的key
-o --value <key value> #key值
-i --input- file <input file > #从文件里面读取 hostname 、key、value,其中一行为
一条数据,使用空格作为分隔符,如果主机名带空格,那么请使用双引号包起来
-T --with-timestamps #一行一条数据,空格作为分隔符: < hostname > <key>
<timestamp> <value>,配合 --input- file option,timestamp为unix时间戳
-r --real-time #将数据实时提交给服务器
-v --verbose #详细模式, -vv 更详细
#示例
zabbix_sender -z 127.0.0.1 -s "appserver" -k db.connections -o 100
案例 使用zabbix_sender发送信息
在非zabbix agent的客户端执行命令
Bash
[root@ubuntu1804 ~]#apt -y install zabbix-sender
[root@ubuntu1804 ~]#cat traper.txt
"Zabbix server" trapper_test "how are you?"
"Zabbix server" trapper_test "I am fine"
"Zabbix server" trapper_test "thank you"
"Zabbix server" trapper_test "how are you?"
#批量提交
[root@ubuntu1804 ~]#zabbix_sender -z 10.0.0.100 -i traper.txt
Response from "10.0.0.100:10051": "processed: 4; failed: 0; total: 4; seconds
spent: 0.000175"
sent: 4; skipped: 0; total: 4 #单个提交
[root@ubuntu1804 ~]#zabbix_sender -z 10.0.0.100 -s "Zabbix server" -k
trapper_test -o "I am wangxiaochun"
Response from "10.0.0.100:10051": "processed: 1; failed: 0; total: 1; seconds
spent: 0.000089"
sent: 1; skipped: 0; total: 1
案例配置非 Zabbix agent 实现内存监控
在被监控主机创建采集监控项数据的脚本
Bash
[root@ubuntu1804 ~]#cat memory_monitor.sh
#!/bin/bash
hostname=$1
zabbix_server_ip=10.0.0.100
TotalMemory() {
TotalMemory=`cat /proc/meminfo | awk '/^MemTotal:/{print $2}' `
echo -e "\"$hostname\" monitor[TotalMemory] $TotalMemory" >>
/tmp/memory_monitor.txt
}
FreeMemory() {
FreeMemory=`cat /proc/meminfo | awk '/^MemFree:/{print $2}' `
echo "\"$hostname\" monitor[FreeMemory] $FreeMemory" >>
/tmp/memory_monitor.txt
}
TotalMemory
FreeMemory
zabbix_sender -z $zabbix_server_ip -i /tmp/memory_monitor.txt &> /dev/null
if [ $? -eq 0 ]; then
echo 0
else
echo 1
fi
rm -f /tmp/memory_monitor.txt
创建计划任务
Bash
[root@ubuntu1804 ~]#crontab -e
* * * * * /root/memory_monitor.sh "Zabbix server"