[root@BJVM-2-181bin]# cat get_cdn_log.py
#!/usr/bin/env python
# coding=utf-8
importhashlib
importrequests
importhmac
importrandom
importtime
importbase64
importjson
importgzip
importos
importsys
fromdatetimeimportdatetime, timedelta
classSign(object):
def__init__(self, secretId, secretKey):
self.secretId = secretId
self.secretKey = secretKey
# 生成签名串
defmake(self, requestHost, requestUri, params, method='GET'):
srcStr = method.upper + requestHost + requestUri +'?'+'&'.join(k.replace('_''.') +'='+ str(params[k])forkinsorted(params.keys))
hashed = hmac.new(self.secretKey, srcStr, hashlib.sha1)
returnbase64.b64encode(hashed.digest)
classCdnHelper(object):
SecretId='AKIDLsldjflsdjflsdjflsdjfpGSO5XoGiY9'
SecretKey='SeaHjSDFLJSLDFJQIuFJ7rMiz0lGV'
requestHost='cdn.api.qcloud.com'
requestUri='/v2/index.php'
def__init__(self, host, startDate, endDate):
self.host = host
self.startDate = startDate
self.endDate = endDate
self.params = {
'Timestamp': int(time.time),
'Action':'GetCdnLogList'
'SecretId':CdnHelper.SecretId
'Nonce': random.randint(1000000099999999),
'host': self.host,
'startDate': self.startDate,
'endDate': self.endDate
}
self.params['Signature'] = Sign(CdnHelper.SecretIdCdnHelper.SecretKey).make(CdnHelper.requestHost,CdnHelper.requestUri, self.params)
self.url ='https://%s%s'% (CdnHelper.requestHost,CdnHelper.requestUri)
defGetCdnLogList(self):
ret = requests.get(self.url, params=self.params)
returnret.json
classGZipTool(object):
'''
压缩与解压gzip
'''
def__init__(self, bufSize =1024*8):
self.bufSize = bufSize
self.fin =None
self.fout =None
defcompress(self, src, dst):
self.fin = open(src,'rb')
self.fout = gzip.open(dst,'wb')
self.__in2out
defdecompress(self, gzFile, dst):
self.fin = gzip.open(gzFile,'rb')
self.fout = open(dst,'wb')
def__in2out(self,):
whileTrue:
buf = self.fin.read(self.bufSize)
iflen(buf) 1:
break
self.fout.write(buf)
self.fin.close
self.fout.close
defdownload(link, name):
try:
r = requests.get(link)
withopen(name,'wb')asf:
f.write(r.content)
returnTrue
except:
returnFalse
defwritelog(src, dst):
# 保存为以天命名日志
dst = dst.split('-')[0][:-2] +'-'+ dst.split('-')[1]
withopen(src,'r')asf1:
withopen(dst,'a+')asf2:
forlineinf1:
f2.write(line)
if__name__ =='__main__':
#startDate = '2017-02-23 12:00:00'
#endDate = '2017-02-23 12:00:00'
# 前一小时
# startDate = endDate = time.strftime('%Y-%m-%d ', time.localtime) + str(time.localtime.tm_hour-1) + ':00:00'
tm = datetime.now + timedelta(hours=-2)
startDate = endDate = tm.strftime('%Y-%m-%d %H:00:00')
#hosts = ['userface.51img1.com']
hosts = [
'pfcdn.xxx.com'
'pecdn.xxx.com'
'pdcdn.xxx.com'
'pccdn.xxx.com'
'pbcdn.xxx.com'
'pacdn.xxx.com'
'p9cdn.xxx.com'
'p8cdn.xxx.com'
'p7cdn.xxx.com'
]
forhostinhosts:
try:
obj =CdnHelper(host, startDate,endDate)
ret = obj.GetCdnLogList
link = ret['data']['list'][0]['link']
name = ret['data']['list'][0]['name']
# 下载链接保存的文件名
gzip_name ='/data/logs/cdn/cdn_log_temp/'+ name +'.gz'
# 解压后的文件名
local_name =+ name +'.log'
# 追加的文件名
real_path ='/data/logs/cdn/'+ name +'.log'
printlocal_name, real_path
status = download(link, gzip_name)
ifstatus:
try:
GZipTool.decompress(gzip_name, local_name)
writelog(local_name, real_path)
# os.remove(gzip_name)
os.remove(local_name)
except:
continue
exceptException,e:
printe
continue