您好,登錄后才能下訂單哦!
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
-c --config 指定配置文件
-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
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。