#!/bin/bash

log_path="/var/log/zabbix"
cur_date=`date +%Y%m%d_%H_%M`

qrouter=$1
trigger=$2
pid=$$

if [ "$trigger" == "ok" ]
then
    /usr/bin/zabbix_sender -s ${HOSTNAME} -c /etc/zabbix/zabbix_agentd.conf -k qrouter.if.nf.conntrack.scan[$qrouter] -o "ok" &> /dev/null
else
### nf_conntrack log
ip netns exec $qrouter conntrack -L  2> /dev/null > ${log_path}/${qrouter}.${cur_date}.log

## scan
echo "1. TCP_Flag Top" > ${log_path}/${qrouter}.report.${pid}
cat ${log_path}/${qrouter}.${cur_date}.log|grep tcp|awk '{print $4}'|sort -n|uniq -c|sort -rn|head -3|awk '{print $2,$1}' >> ${log_path}/${qrouter}.report.${pid}
echo "2. Src_IP Top" >> ${log_path}/${qrouter}.report.${pid}
cat ${log_path}/${qrouter}.${cur_date}.log|grep tcp|awk '{print $5}'|sort -n|uniq -c|sort -rn|head -3|awk '{print $2,$1}' >> ${log_path}/${qrouter}.report.${pid}
echo "3. Dst_IP Top" >> ${log_path}/${qrouter}.report.${pid}
cat ${log_path}/${qrouter}.${cur_date}.log|grep tcp|awk '{print $6}'|sort -n|uniq -c|sort -rn|head -3|awk '{print $2,$1}' >> ${log_path}/${qrouter}.report.${pid}
echo "4. Dst_Port Top" >> ${log_path}/${qrouter}.report.${pid}
cat ${log_path}/${qrouter}.${cur_date}.log|grep tcp|awk '{print $8}'|sort -n|uniq -c|sort -rn|head -3|awk '{print $2,$1}' >> ${log_path}/${qrouter}.report.${pid}

msg=`cat ${log_path}/${qrouter}.report.$$`

## report
/usr/bin/zabbix_sender -s ${HOSTNAME} -c /etc/zabbix/zabbix_agentd.conf -k qrouter.if.nf.conntrack.scan[$qrouter] -o "$msg" &>/dev/null
fi

rm ${log_path}/${qrouter}.report.${pid}
