If you want to get the maximum daily traffic, you can view the graphs in Zabbix, pull the maximum, but we can also take the value from the ZABBIX_API or the database directly, which depends on the script implementation.
There is a need to collect a portion of the host daily import and export traffic maximum value, wrote a script to achieve this function.
Version One:
According to the host name found, after entering the date, output the maximum import and export traffic within one day
#!/bin/bash### #获取每日流量最大值的脚本, the script takes the # # #数据 from the History_uint table in the Zabbix database, based on the hostname in the Hostname.txt file. # # #wuhf # # #z_user = "xxx" z_passwd= "xxx" cur_dir=$ (pwd) function test_file {echo "------------------- ------start looking for the hostname--------------------------"if [ -e $Cur _dir/hostname.txt ]; thenecho > $Cur _dir/hostname.txtelsetouch $Cur _dir/hostname.txtfifor host_ip in $ (cat $Cur _dir/hostip.txt); doif /usr/local/zabbix/bin/zabbix_get -s $host _ip -k system.hostname &>/dev/null; thenz_hostname=$ (/usr/local/zabbix/bin/zabbix_get -s $host _ip -k system.hostname) echo -e "\033[32MIP: $host _ip --------> hostname: $z _hostname\033[0m " echo " $z _hostname " >> $Cur _dir/hostname.txtelseecho -e "\033[31MIP: $host _ip --------> host name: error\033[0m" echo -e "\033[31m hostname not found , either the network is unreachable or the Zabbix_agentd end does not start \033[0m "fidone}function input_date {read -p "Please enter a start date (e.g. 2015-01-01):" euntil date -d "$E" & >/dev/null; doecho -e "\033[31m The date format you entered is wrong, please enter it again!" \033[0m "read -p " Enter start Date: " Edoneif date -d " $E " >& /dev/null; thenfrom_time=$ (date -d "$E" +%s) firead -p "Please enter end Date:" duntil date -d "$D" &>/dev/null; doecho -e "\033[31m The date format you entered is wrong, please enter it again!" \033[0m "read -p " Please enter an end date (e.g. 2015-01-01): Ddoneif date -d $D >& /dev/null;thenend_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/nullif [ $? -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\") &Nbsp;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 &NBSP;$3} ') echo "| $host _name | Maximum import flow: $Max _in | Maximum outlet flow: $Max _out | date range: $E to $D | " else echo -e "\033[31m host name $host _name Does not exist in the Zabbix database, please modify the hostname and try again! \033[0m " fi}test_fileinput_dateuntil [ $from _time -lt $end _time ]; doecho -e "The \033[31m start date should be less than the end date, Please enter the new! \033[0m "input_datedonefor host_name in $ (cat $Cur _dir/hostname.txt); doecho "--- ---------------------------------------------------------------------------------------------------"Maindone
Version two:
Based on the IP and hostname information within a given text file, the maximum daily import and export traffic for a week is directly output.
#!/bin/bash### #需要在同级目录下, build the Hostip.txt file in the file format: "ip hostname" lang=en_us.utf8z_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/nullif [ $? -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\" &NBSP;LIMIT&NBSP;1) and key_ like \ "net.if.in%\" LIMIT&NBSP;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 | Maximum inlet flow: $Max _in | maximum outlet flow: $Max _out | date range: $D _frist to $D _second | " >> $Cur _dir/output-$ (date +%y-%m-%d-%h-%m) else echo -e \033[31m hostname $host _name does not exist in the Zabbix database, please modify the hostname and try again! \033[0m " fi}function input_date {if date -d " $D " > & /dev/null;thenz_sun=$ (date -d "$D" +%s) fiif [[ $z _sun > $z _today | | $z _sun < $z _lastday ]]; thenecho -e "\033[31m date exceeded query scope, script has exited!\033[0m" exit 1fiecho "-----------------------------------------------------------------------------------------------------" >> $Cur _dir/ output-$ (date +%y-%m-%d-%h-%m) for (i=0;i<=6;i++) dolet end_time= $z _sun-86400*$ Ilet from_time= $end _time-86400d_frist=$ (date -d "@ $end _time" +%y%m%d) D_second=$ (date -d "@ $from _time" +%y%m%d) main done}read -p "Please enter the query date (e.g. 2015-01-01), The script will look backwards for 7 days data: " duntil date -d" $D " &>/dev/null; doecho -e " \033[ 31m you entered the date format is wrong, please re-enter! \033[0m "read -p " Please enter the query date (e.g. 2015-01-01): " Ddonecat $Cur _dir/hostip.txt | while read linedohost_name=$ (echo $line | awk ' {print $2} ') Input_datedone
This article is from the "Tongluowan" blog, make sure to keep this source http://wuhf2015.blog.51cto.com/8213008/1720395
How to get the maximum daily traffic from the Zabbix database