指定端口监控流量的输入及输出(简化版,复制粘贴即用)

指定端口监控服务流量走向

1. 安装iftop

yum -y install iftop

2. 脚本

while true
do

case "$(uname)" in
Linux)
  bin_absolute_path=$(readlink -f $(dirname $0))
  ;;
*)
  bin_absolute_path=$(cd $(dirname $0) pwd)
  ;;
esac
echo "$bin_absolute_path"
outdir=$bin_absolute_path
mkdir -p $outdir


# 配置目录
IFTOP_DIR=${outdir}/temporary_file
BAKCUP_DIR=${outdir}/backup
IFTOP_LOG=${outdir}/log


# 临时生成文件
Iftop_file=${IFTOP_DIR}/test-iftop
Change_file=${IFTOP_DIR}/change_iftop
INPUT_File=${IFTOP_DIR}/input_iftop
OUTPUT_File=${IFTOP_DIR}/output_iftop
IFTOP_OUT=${IFTOP_DIR}/iftop_out
TIME_LOG=${IFTOP_DIR}/iftop_TIME



# 日志文件
Iftop_Log_kafka=${IFTOP_LOG}/iftop_kafka.log
Iftop_Log_redis=${IFTOP_LOG}/iftop_redis.log
Iftop_Log_hbase=${IFTOP_LOG}/iftop_hbase.log
Iftop_Log_spack_4518=${IFTOP_LOG}/spack_4518.log


# 指定要监控的端口
PORT1=9092
PORT2=6379
PORT3=16010
PORT4=4518

if [ ! -d  ${IFTOP_DIR} ];then
	mkdir -p ${IFTOP_DIR}
elif [ ! -d  ${BAKCUP_DIR} ];then
	mkdir -p ${BAKCUP_DIR}
elif [ ! -d ${IFTOP_LOG} ];then
	mkdir -p ${IFTOP_LOG}
fi

iftop -nNP -i any -t -s 1 |egrep   '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}:[0-9]{1,5}|[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}:[0-9]{1,5}' >${Iftop_file}

cat $Iftop_file|awk 'NF==7{$1=a;print;next}1' |column -t >${Change_file}

# MB转换KB
for i in `egrep -o  '[0-9]{1,4}\.[0-9]{1,4}MB|[0-9]{1,4}MB' ${Change_file}`
do
	num=`awk "BEGIN{print ${i%MB}*1024}"`
	sed -i "s/$i/$num/g"  ${Change_file}
done

# Mb转换KB
for i in `egrep -o  '[0-9]{1,4}\.[0-9]{1,4}MB|[0-9]{1,4}Mb' ${Change_file}`
do
        num=`awk "BEGIN{print ${i%Mb}*1024}"`
        sed -i "s/$i/$num/g"  ${Change_file}
done


# B转换KB
for i in `egrep -o  '[0-9]{1,4}\.[0-9]{1,4}b|[0-9]{1,4}B' ${Change_file}`
do
	num=`awk "BEGIN{print ${i%B}/1024}"`
	sed -i "s/$i/${num}/g"  ${Change_file}
done

# b转换KB
for i in `egrep -o  '[0-9]{1,4}\.[0-9]{1,4}B|[0-9]{1,4}b' ${Change_file}`
do
	num=`awk "BEGIN{print ${i%b}/1024}"`
	sed -i "s/$i/${num}/g"  ${Change_file}
done

# GB转换KB
for i in `egrep -o  '[0-9]{1,4}\.[0-9]{1,4}gb|[0-9]{1,4}GB' ${Change_file}`
do
	num=`awk "BEGIN{print ${i%GB}*1024*1024}"`
	sed -i "s/$i/${num}/g"  ${Change_file}
done

	sed -e  "s/:/ /g" ${Change_file} -i
	sed -i 's/KB//g' ${Change_file}
	sed -i 's/Kb//g' ${Change_file}
	sed -i  "s/K//g" ${Change_file}

for i in `egrep -o  '[0-9]{1,4}\.[0-9]{1,4}MB|[0-9]{1,4}GB' ${Change_file}`
do
	num=`awk "BEGIN{print ${i%MB}*1024*1024}"`
	sed -i "s/$i/$num/g"  ${Change_file}
done

	sed -n '1~2p' ${Change_file} >${INPUT_File}
	sed -n '2~2p' ${Change_file} >${OUTPUT_File}
	paste -d '\t' ${INPUT_File}  ${OUTPUT_File} > ${IFTOP_OUT}
	cat ${IFTOP_OUT}| awk '{print $0" " strftime("%Y-%m-%d-%H:%M:%S",systime())}' >${TIME_LOG}
# 要监控的端口 和 日志存放的位置
	grep -w "${PORT1}" ${TIME_LOG} >> ${Iftop_Log_kafka}
	grep -w "${PORT2}" ${TIME_LOG} >> ${Iftop_Log_redis}
	grep -w "${PORT3}" ${TIME_LOG} >> ${Iftop_Log_hbase}
	grep -w "${PORT4}" ${TIME_LOG} >> ${Iftop_Log_spack_4518}
done

3. 实现功能及脚本使用

  1. 粘贴即用,只需修改你想要监控的端口和日志名称即可如图所示(可以指定单个端口或多个端口)
    在这里插入图片描述
  2. 主要实现的功能是监控服务的流量走向和那些服务有交互,监控其输入以及输出的流量的大小

如果想要实现图形展示可以参考以下文档
效果图展示

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值