AWS Athena 分析日志

AWS里面可以用Athena來分析S3里面保存的日志,他把日志轉(zhuǎn)換成數(shù)據(jù)庫表的格式,這樣就可以通過sql語句進(jìn)行查詢了。這個(gè)功能和在windows服務(wù)器上用logparser來分析Exchange或者IIS的日志很相似。?
下面做個(gè)演示,通過Cloudtrail記錄管理日志,然后通過Athena來查詢?nèi)罩緝?nèi)容。?
首先選擇CloudTrail, CloudTrail?是一個(gè)日志記錄的服務(wù),他和cloudwatch的區(qū)別在于這個(gè)服務(wù)更多是側(cè)重于審計(jì),他的內(nèi)容都是關(guān)于什么時(shí)候,什么賬號(hào),從什么IP上進(jìn)行了什么操作。?
點(diǎn)擊?Create Trail?
取個(gè)名字,?創(chuàng)建一個(gè)新的S3 bucket來保存日志?
創(chuàng)建好之后可以看見他自動(dòng)已經(jīng)在記錄最新的日志了?
然后選擇?Athena?
跳過向?qū)?,直接進(jìn)入查詢器的編輯器,這里是編輯SQL語句的地方。這里我直接創(chuàng)建一個(gè)的數(shù)據(jù)庫?
下面來創(chuàng)建一個(gè)表,從指定的S3 Bucket里面獲取數(shù)據(jù)。?
我們可以通過向?qū)?chuàng)建,但是比較繁瑣?
比較容易的是通過腳本創(chuàng)建,注意最后一行S3存儲(chǔ)桶的地址?
CREATE EXTERNAL TABLE cloudtrail_logs (eventversion STRING,useridentity STRUCT< ??????????????type:STRING, ??????????????principalid:STRING, ??????????????arn:STRING, ??????????????accountid:STRING, ??????????????invokedby:STRING, ??????????????accesskeyid:STRING, ??????????????userName:STRING,sessioncontext:STRUCT<attributes:STRUCT< ??????????????mfaauthenticated:STRING, ??????????????creationdate:STRING>,sessionissuer:STRUCT< ????????????????type:STRING, ??????????????principalId:STRING, ??????????????arn:STRING, ???????????????accountId:STRING, ??????????????userName:STRING>>>,eventtime STRING,eventsource STRING,eventname STRING,awsregion STRING,sourceipaddress STRING,useragent STRING,errorcode STRING,errormessage STRING,requestparameters STRING,responseelements STRING,additionaleventdata STRING,requestid STRING,eventid STRING,resources ARRAY<STRUCT< ??????????????ARN:STRING, ??????????????accountId:STRING, ??????????????type:STRING>>,eventtype STRING,apiversion STRING,readonly STRING,recipientaccountid STRING,serviceeventdetails STRING,sharedeventid STRING,vpcendpointid STRING)ROW FORMAT SERDE 'com.amazon.emr.hive.serde.CloudTrailSerde'STORED AS INPUTFORMAT 'com.amazon.emr.cloudtrail.CloudTrailInputFormat'OUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'LOCATION 's3://mycloudtrailbucket-faye/AWSLogs/757250003982/';?
創(chuàng)建表成功的樣子?
下面我們可以進(jìn)行一個(gè)簡單的查詢,結(jié)果如下。
了解更多網(wǎng)絡(luò)知識(shí)關(guān)注:http://www.vecloud.com/