如果要获取每日流量最大值,可以查看zabbix中的graphs,拉取最大值,但是我们还可以从zabbix_api或者数据库中直接取值,这要靠脚本实现。
有一个需求,收集一部分主机每日进出口流量的最大值,写了个脚本实现这个功能。
版本一:
根据查找到的主机名,输入日期后,输出一日内最大进出口流量
#!/bin/bash # ###获取每日流量最大值的脚本,脚本从zabbix数据库的history_uint表中取 ###数据,根据hostname.txt文件中的主机名。 ###wuhf### z_user="XXX" z_passwd="XXX" Cur_Dir=$(pwd) function test_file { echo "-------------------------开始查找主机名--------------------------" if [ -e $Cur_Dir/hostname.txt ]; then echo > $Cur_Dir/hostname.txt else touch $Cur_Dir/hostname.txt fi for host_ip in $(cat $Cur_Dir/hostip.txt); do if /usr/local/zabbix/bin/zabbix_get -s $host_ip -k system.hostname &>/dev/null; then z_hostname=$(/usr/local/zabbix/bin/zabbix_get -s $host_ip -k system.hostname) echo -e "\033[32mIP:$host_ip --------> 主机名:$z_hostname\033[0m" echo "$z_hostname" >> $Cur_Dir/hostname.txt else echo -e "\033[31mIP:$host_ip --------> 主机名:Error\033[0m" echo -e "\033[31m主机名没有被找到,可能是网络不可达或者zabbix_agentd端没有启动\033[0m" fi done } function input_date { read -p "请输入开始日期(如2015-01-01):" E until date -d"$E" &>/dev/null; do echo -e "\033[31m你输入的日期格式错误,请从新输入!\033[0m" read -p "请输入开始日期:" E done if date -d "$E" >& /dev/null;then from_time=$(date -d"$E" +%s) fi read -p "请输入结束日期:" D until date -d"$D" &>/dev/null; do echo -e "\033[31m你输入的日期格式错误,请从新输入!\033[0m" read -p "请输入结束日期(如2015-01-01):" D done if date -d "$D" >& /dev/null;then end_time=$(date -d"$D" +%s) fi } function main() { mysql -u$z_user -h127.0.0.1 -p$z_passwd -Dzabbix -e "select host,hostid from hosts where host=\"$host_name\";" | grep "$host_name" &>/dev/null if [ $? -eq 0 ]; then Max_In=$(mysql -uz_user -h127.0.0.1 -pz_passwd -Dzabbix -e "select from_unixtime(clock) as DateTime,max(round(value/1024/1024,2)) as In_Value from history_uint where itemid = (select itemid from items where hostid = (select hostid from hosts where host=\"$host_name\") and key_ like \"net.if.in%\" limit 1) and clock >= $from_time and clock <= $end_time" |tail -1 |awk '{print $3}') Max_Out=$(mysql -uz_user -h127.0.0.1 -pz_passwd -Dzabbix -e "select from_unixtime(clock) as DateTime,max(round(value/1024/1024,2)) as In_Value from history_uint where itemid = (select itemid from items where hostid = (select hostid from hosts where host=\"$host_name\") and key_ like \"net.if.out%\" limit 1) and clock >= $from_time and clock <= $end_time" |tail -1 |awk '{print $3}') echo "| $host_name | 最大进口流量: $Max_In | 最大出口流量: $Max_Out | 日期范围:$E 至 $D |" else echo -e "\033[31m主机名 $host_name 在zabbix数据库中不存在,请修改主机名后重试!\033[0m" fi } test_file input_date until [ $from_time -lt $end_time ]; do echo -e "\033[31m开始日期应该小于结束日期,请从新输入!\033[0m" input_date done for host_name in $(cat $Cur_Dir/hostname.txt); do echo "------------------------------------------------------------------------------------------------------" main done
版本二:
根据给定的文本文件内的ip和主机名信息,直接输出一周内每日的进出口流量最大值。
#!/bin/bash # ###需要在同级目录下,建立hostip.txt文件,文件中格式为:”ip 主机名“ LANG=en_US.utf8 z_user="XXX" z_passwd="XXX" Cur_Dir=$(pwd) z_Today=$(date +%s) z_lastday=$[ $(date +%s) - 604800 ] function main() { mysql -u$z_user -h127.0.0.1 -p$z_passwd -Dzabbix -e "select host,hostid from hosts where host=\"$host_name\";" | grep "$host_name" &>/dev/null if [ $? -eq 0 ]; then Max_In=$(mysql -uz_user -h127.0.0.1 -pz_passwd -Dzabbix -e "select from_unixtime(clock) as DateTime,max(round(value/1024/1024,2)) as In_Value from history_uint where itemid = (select itemid from items where hostid = (select hostid from hosts where host=\"$host_name\" limit 1) and key_ like \"net.if.in%\" limit 1) and clock >= $from_time and clock <= $end_time" |tail -1 |awk '{print $3}') Max_Out=$(mysql -uz_user -h127.0.0.1 -pz_passwd -Dzabbix -e "select from_unixtime(clock) as DateTime,max(round(value/1024/1024,2)) as In_Value from history_uint where itemid = (select itemid from items where hostid = (select hostid from hosts where host=\"$host_name\" limit 1) and key_ like \"net.if.out%\" limit 1) and clock >= $from_time and clock <= $end_time" |tail -1 |awk '{print $3}') echo "| $host_name | 最大进口流量: $Max_In | 最大出口流量: $Max_Out | 日期范围:$D_frist 至 $D_second |" >> $Cur_Dir/Output-$(date +%Y-%m-%d-%H-%M) else echo -e "\033[31m主机名 $host_name 在zabbix数据库中不存在,请修改主机名后重试!\033[0m" fi } function input_date { if date -d "$D" >& /dev/null;then z_Sun=$(date -d"$D" +%s) fi if [[ $z_Sun > $z_Today || $z_Sun < $z_lastday ]]; then echo -e "\033[31m日期超出查询范围,脚本已经退出!\033[0m" exit 1 fi echo "-----------------------------------------------------------------------------------------------------" >> $Cur_Dir/Output-$(date +%Y-%m-%d-%H-%M) for ((i=0;i<=6;i++)); do let end_time=$z_Sun-86400*$i let from_time=$end_time-86400 D_frist=$(date -d "@$end_time" +%Y%m%d) D_second=$(date -d "@$from_time" +%Y%m%d) main done } read -p "请输入查询日期(如2015-01-01),脚本会往后查7天数据:" D until date -d"$D" &>/dev/null; do echo -e "\033[31m你输入的日期格式错误,请重新输入!\033[0m" read -p "请输入查询日期(如2015-01-01):" D done cat $Cur_Dir/hostip.txt | while read line do host_name=$(echo $line | awk '{print $2}') input_date done