公司使用腾讯cdn为网站静态内容加速,由于业务需求,需要每天下载昨天的日志(因为腾讯方面给出回复,访问日志会有2个小时或以上时间的延迟,所以不建议下载当天日志,所以每天统计前一天的日志以做分析)。因为cdn是由运维来管理,但是这个需求是业务的,如果每天都由运维进行下载,再通过邮件或其他工具发送,可能就显得麻烦。所幸腾讯CDN提供了API接口,因此采用shell脚本调用API进行下载的方式,定期下载日志,这样只要业务人员运行这个脚本就能自行下载日志,解放了运维的工作。
#!/bin/bash
#########################################################
#This script is to download yesterday‘s log #files in Telent CDN
#writed on 2016-08-11
#########################################################
Timestamp=`date +%s`
Nonce=$RANDOM
LastDay=`date -d "yesterday" +%Y%m%d`
Domain=xxxxxxxxxxxxxxxxx #此处为你需要下载日志的域名
ID=xxxxxxxxxxxxxxxxx #此处为你在腾讯cdn上的secretId
key=xxxxxxxxxxxxxxxxx #此处为你在腾讯cdn上的secretKey,可以在腾讯cdn后台生成
SURL="GETcdn.api.qcloud.com/v2/index.php?Action=GenerateLogList&Nonce=${Nonce}&SecretId=${ID}&Timestamp=${Timestamp}&hostId=xxxxxx" #hostId为你的域名的id,可通过腾
#讯提供的php脚本查看,当然也可以使shell或pythonsigure=`echo -n $SURL | openssl sha1 -binary -hmac "${key}" | base64|sed ‘s/=/%3D/g;s/+/%2B/g;s/\//%2F/g‘`
HTTP_SURL=`echo -n $SURL | sed s#^GET#HTTPS://#`
Cur_URL=`echo "${HTTP_SURL}&Signature=${sigure}"`
Log_URL=`curl -s -l --url $Cur_URL|sed ‘s/},{/\n/g‘|grep "$LastDay"|awk -F‘"‘ ‘{print $14}‘|sed -e ‘s#\\\/#\/#g‘`
#echo $Log_URL
cd $HOME
if [ -d ./cdnlogs/$LastDay ]
then
cd ./cdnlogs/$LastDay
else
mkdir -p ./cdnlogs/$LastDay && cd ./cdnlogs/$LastDay
fi
if [ -f $LastDay-$Domain.gz ]
then
echo "The file was exist now."
exit 127
else
wget -q -O $LastDay-$Domain.gz $Log_URL >>/dev/null 2>&1
if [ $? -eq 0 ]
then
echo -e "\033[32mDownload log file successful.\033[0m"
else
echo -e "\033[31mDownload log file failed.\033[0m"
fi
fi资料参考:
https://www.qcloud.com/doc/api/231/3950
补充:在SURL中的接口参数是区分大小写的,因为在调试的时候hostId写成了hostid,大概调试了快一个小时都是错误4000,invalid parameter,这点需要注意。
https://github.com/QcloudApi/qcloudapi-sdk-php 如果会php的话,这里有现成的,可惜我不会,唉~
本文出自 “IT小二郎” 博客,请务必保留此出处http://jerry12356.blog.51cto.com/4308715/1836944
原文:http://jerry12356.blog.51cto.com/4308715/1836944