使用shell测试cdn状态
公司使用多家cdn,测试cdn在各个地方的响应情况,以便于提高视频访问的用户体验。分别在阿里云华南1,华东1,华东2,华北2等不同地区节点测试。该随笔为自己所用。
1.该脚本会测试某一cdn的url,如果302,继续测试,如果是503,则测试cdn源站是否也为503,并记http_code,录测试时间以及测试的url等信息。
#!/bin/bash #set -e start_time=$(date +%s) today=$(date +%F) mkdir -pv /cdn/logs &> /dev/null dl200="/cdn/logs/dl200-$today.log" dlno200="/cdn/logs/dlno200-$today.log" > $dl200 > $dlno200 #echo $dl200 $dlno200 #exit cat /cdn/shell/dltxt/dl-$today |while read line do #echo $line HTTP_CODE=$(curl -XGET -I -m 20 -o /dev/null -s -w "%{http_code}""\n" $line) if [ "$HTTP_CODE" -eq 200 ];then CURRENT_TIME=$(date '+%Y%m%d %H:%M:%S') echo $HTTP_CODE $CURRENT_TIME $line >> $dl200 elif [ "$HTTP_CODE" -eq 302 ];then HTTP_NEW_CODE=302 NEW_URL=$(curl -XGET -I -m 20 -s $line |grep ^Location|awk -F" " '{print $2}'|tr -d '\r'|tr -d '$') while [ "$HTTP_NEW_CODE" -eq 302 ] do #echo $NEW_URL HTTP_NEW_CODE=$(curl -XGET -I -m 20 -o /dev/null -s -w "%{http_code}""\n" $NEW_URL) #echo $HTTP_NEW_CODE #if [ "$HTTP_NEW_CODE" -eq 200 ];then # echo $HTTP_NEW_CODE $NEW_RUL >> /xiaoke/shell/logs/gs200.log if [ "$HTTP_NEW_CODE" -eq 302 ];then NEW_URL=$(curl -XGET -I -m 20 -s $NEW_URL |grep ^Location|awk -F" " '{print $2}'|tr -d '\r'|tr -d '$') elif [ "$HTTP_NEW_CODE" -eq 200 ];then CURRENT_TIME=$(date '+%Y%m%d %H:%M:%S') echo $HTTP_NEW_CODE $CURRENT_TIME $NEW_URL >> $dl200 elif [ "$HTTP_NEW_CODE" -eq 503 ];then CURRENT_TIME=$(date '+%Y%m%d %H:%M:%S') echo $HTTP_NEW_CODE $CURRENT_TIME $NEW_URL >> $dlno200 SOURCE_URL=$(echo $line |sed 's;gxbvideo-dl;gxbvideo;g') SOURCE_CODE=$(curl -XGET -I -m 20 -o /dev/null -s -w "%{http_code}""\n" $SOURCE_URL) if [ "$SOURCE_CODE" -eq 503 ];then echo $SOURCE_CODE $CURRENT_TIME $SOURCE_URL >> $dlno200 else echo $SOURCE_CODE $CURRENT_TIME $SOURCE_URL >> $dlno200 fi else CURRENT_TIME=$(date '+%Y%m%d %H:%M:%S') echo $HTTP_NEW_CODE $CURRENT_TIME $NEW_URL >> $dlno200 fi done elif [ "$HTTP_CODE" -eq 503 ];then CURRENT_TIME=$(date '+%Y%m%d %H:%M:%S') echo $HTTP_CODE $CURRENT_TIME $line >> $dlno200 SOURCE_URL=$(echo $line |sed 's;gxbvideo-dl;gxbvideo;g') SOURCE_CODE=$(curl -XGET -I -m 20 -o /dev/null -s -w "%{http_code}""\n" $SOURCE_URL) if [ "$SOURCE_CODE" -eq 503 ];then echo $SOURCE_CODE $CURRENT_TIME $SOURCE_URL >> $dlno200 else echo $SOURCE_CODE $CURRENT_TIME $SOURCE_URL >> $dlno200 fi else #current_time=$(date '+%Y%m%d %H:%M:%S') CURRENT_TIME=$(date '+%Y%m%d %H:%M:%S') echo $HTTP_CODE $CURRENT_TIME $line >> $dlno200 fi sleep 25 done end_time=$(date +%s) #echo $end_time delta_time=`expr $end_time - $start_time` echo "总耗时:$delta_time秒" >> $dl200
2.查看测试信息日志即可。