1、zabbix_sender是用来给zabbix_server端发送数据的命令。zabbix_server的捕获信息工具会根据捕获的信息创建对应项目,传入的数据仅仅会被指定主机item使用。
当你需要运行一些统计脚本或者在另一台机器获取数据,却想把监控统一做到一台机器,就要用到zabbix_sender。
2、用法:
[root@salt-master ~]# /usr/local/zabbix/bin/zabbix_sender -h
usage:
zabbix_sender [-v] -z server [-p port] [-I IP-address] -s host -k key
-o value
zabbix_sender [-v] -z server [-p port] [-I IP-address] [-s host] [-T] [-r]
-i input-file
zabbix_sender [-v] -c config-file [-z server] [-p port] [-I IP-address]
[-s host] -k key -o value
zabbix_sender [-v] -c config-file [-z server] [-p port] [-I IP-address]
[-s host] [-T] [-r] -i input-file
-z --zabbix_server zabbix_server的主机名或ip地址
-p --port 指定zabbix_server的trapper的端口,默认为10051
-s --host <hostname> 指定主机名,zabbix_agent配置文件中定义的hostname
-I --source-address 指定源地址
-k --key 指定item key
-o --value item key的值,可以是具体的值,也可以是执行脚本的结果
-i --input-file 从指定的文件加载value。按行获取标准的输入,空格是分隔符
格式: <host> <key> <value>
-T --with-timestamps 如果想在文件中指定key的时间,可以使用这个选项,这里的时间是unix的 时间戳。格式:<host> <key> <timestamp> <value>
-r --real-time 发送的key以真实时间为标准,不用指定值
3、一个例子
根据需求,需要每天统计一次某项目的“总人数”,“每天活跃用户”,“最大同时在线人数”,“新增玩家数”。
(1) 通过脚本获取值,是通过查询数据库获取数据的,可能不太合理,仅仅是个例子:
[root@salt-master ~]# cat /etc/zabbix/scripts/test_count.sh
#!/bin/bash
# Shell Env
USER="test"
PASSWD="test"
HOST="localhost"
DAY=$(date -d"yesterday" +"%F")
# Shell Usage
shell_usage(){
echo "Usage $0: [ total_players | daily_active | new_player | max_online ]"
}
Total_players(){
mysql -u $USER -h $HOST -p$PASSWD -e "select count(*) from test.player" | grep [0-9]
}
Daily_active_players(){
mysql -u $USER -h $HOST -p$PASSWD -e "select count(*) from test.player where LastLoginTime >= unix_timestamp('$DAY 00:00:00') and LastLoginTime <= unix_timestamp('$DAY 23:59:59')" | grep [0-9]
}
Newly_added_player(){
mysql -u $USER -h $HOST -p$PASSWD -e "select count(*) from test.Player where RegisterTime >= unix_timestamp('$DAY 00:00:00') and RegisterTime <= unix_timestamp('$DAY 23:59:59')" | grep [0-9]
}
Max_online_number(){
mysql -u $USER -h $HOST -p$PASSWD -e "select onlinecount from test.onlinelog where datetime >= unix_timestamp('$DAY 00:00:00') and datetime <= unix_timestamp('$DAY 23:59:59') order by onlinecount desc limit 1" | grep [0-9]
}
main(){
case $1 in
total_players)
Total_players;
;;
daily_active)
Daily_active_players;
;;
new_player)
Newly_added_player;
;;
max_online)
Max_online_number;
;;
*)
shell_usage;
esac
}
main $1
尝试运行一次脚本:
[root@salt-master ~]# /etc/zabbix/scripts/test_count.sh
Usage /etc/zabbix/scripts/test_count.sh: [ total_players | daily_active | new_player | max_online ]
[root@salt-master ~]# /etc/zabbix/scripts/test_count.sh total_players
296
(2) 在使用zabbix_sender向server端发送数据之前,需要在zabbix页面创建对应的items,否则会发送失败。
Name: 指定item的名字
Type:需要选择“zabbix_trapper”
Key:
Applications:将统计数据统一放到一个“applications”中
(3) 计划在每天的凌晨2时,统计一次数据。将多条zabbix_sender放在一个脚本,在凌晨2时执行,这样只需要在crontab中写一条计划就可以了:
[root@salt-master ~]# cat /etc/zabbix/test_cron.sh
#!/bin/bash
for key in total_players daily_active new_player max_online
do
/usr/local/zabbix/bin/zabbix_sender -s salt-master -z 1.1.1.1 -k "test_count[$key]" -o `/etc/zabbix/scripts/pdk_count.sh $key`
done
尝试运行一次test_cron.sh脚本:
[root@salt-master ]# sh /etc/zabbix/scripts/test_count.sh
info from server: "processed: 1; failed: 0; total: 1; seconds spent: 0.000035"
sent: 1; skipped: 0; total: 1
info from server: "processed: 1; failed: 0; total: 1; seconds spent: 0.000053"
sent: 1; skipped: 0; total: 1
info from server: "processed: 1; failed: 0; total: 1; seconds spent: 0.000037"
sent: 1; skipped: 0; total: 1
info from server: "processed: 1; failed: 0; total: 1; seconds spent: 0.000027"
[root@salt-master ~]# crontab -l
0 2 * * * sh /etc/zabbix/test_cron.sh
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。