您好,登錄后才能下訂單哦!
這篇文章給大家介紹如何用Python抓取AWS的日志數(shù)據(jù),內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。
如今是云的時(shí)代,許多公司都把自己的IT架構(gòu)部署在基礎(chǔ)架構(gòu)云(IaaS)上。著名的IaaS提供商有亞馬遜,微軟(Azure),IBM等,國(guó)內(nèi)也有諸如阿里云等。這里亞馬遜毫無(wú)疑問(wèn)是該市場(chǎng)的領(lǐng)軍者。
AWS提供了非常多的服務(wù),領(lǐng)先了競(jìng)爭(zhēng)對(duì)手一大截。并且AWS提供非常豐富的API,其API基于Rest,所以很容易被不同的語(yǔ)言的平臺(tái)來(lái)調(diào)用。
在如今的大數(shù)據(jù)時(shí)代,利用數(shù)據(jù)在做決策是大數(shù)據(jù)的核心價(jià)值,AWS提供了許多服務(wù)來(lái)獲取其運(yùn)行數(shù)據(jù)cloudtrail和cloudwatch是經(jīng)常被用到的兩個(gè)。CloudTrail是對(duì)AWS的所有API調(diào)用的日志,CloudWatch是監(jiān)控AWS服務(wù)的性能數(shù)據(jù)。(新出的Config服務(wù)可用于監(jiān)控AWS的資源變化)
今天我們來(lái)看看如何使用Python(Boto AWS的開(kāi)源Python SDK)來(lái)自動(dòng)配置ClouTrail的服務(wù)并獲取日志內(nèi)容。
我們先來(lái)看看CloudTrail的概念和相關(guān)的配置。
S3 Bucket
在打開(kāi)CloudTrail的服務(wù)時(shí),需要指定一個(gè)相關(guān)的S3的Bucket,S3是亞馬遜提供的存儲(chǔ)服務(wù),你可以把它當(dāng)作一個(gè)基于云的文件系統(tǒng)。CloudTrail的API調(diào)用日志,會(huì)以壓縮文件的形式,存儲(chǔ)在你指定的Bucket里。
SNS
SNS是亞馬遜提供的通知服務(wù),該服務(wù)使用的是訂閱/發(fā)布(Subsrcibe/Publish)的模式。在創(chuàng)建CloudTrail的時(shí)候,可以關(guān)聯(lián)一個(gè)SNS的Topic(可選),這樣做的好處是當(dāng)有API調(diào)用時(shí),可以第一時(shí)間得到通知??梢允褂貌煌目蛻舳藖?lái)訂閱SNS的通知,例如Email,Mobile的Notification Service,SQS等
SQS
SQS是亞馬遜提供的隊(duì)列服務(wù),在本文中,我們使用SQS訂閱SNS的的內(nèi)容,這樣我們的Python程序就可以從SQS的隊(duì)列中獲取相應(yīng)的通知。
首先我們需要?jiǎng)?chuàng)建SNS,并指定相應(yīng)的策略。代碼如下:
import boto.sns import json key_id='yourawskeyid' secret_key='yourawssecretkey' region_name="eu-central-1" trail_topic_name="topicABC" sns_policy_sid="snspolicy0001" sns_conn = boto.sns.connect_to_region(region_name, aws_access_key_id=key_id, aws_secret_access_key=secret_key) sns_topic = sns_conn.create_topic(trail_topic_name) # Get ARN of SNS topic sns_arn = sns_topic['CreateTopicResponse']['CreateTopicResult']['TopicArn'] # Add related policy attrs = sns_conn.get_topic_attributes(sns_arn) policy = attrs['GetTopicAttributesResponse']['GetTopicAttributesResult']['Attributes']['Policy'] policy_obj = json.loads(policy) statements = policy_obj['Statement'] default_statement = statements[0] new_statement = default_statement.copy() new_statement["Sid"] = sns_policy_sid new_statement["Action"] = "SNS:Publish" new_statement["Principal"] = { "AWS": [ "arn:aws:iam::903692715234:root", "arn:aws:iam::035351147821:root", "arn:aws:iam::859597730677:root", "arn:aws:iam::814480443879:root", "arn:aws:iam::216624486486:root", "arn:aws:iam::086441151436:root", "arn:aws:iam::388731089494:root", "arn:aws:iam::284668455005:root", "arn:aws:iam::113285607260:root" ] } new_statement.pop("Condition", None) statements.append(new_statement) new_policy = json.dumps(policy_obj) sns_conn.set_topic_attributes(sns_arn,"Policy",new_policy)
CloudTrail是和區(qū)域(Region)相關(guān)的,不同的Region有不同的CloudTrail服務(wù),所以,在創(chuàng)建對(duì)應(yīng)的SNS時(shí),需要保證使用同一個(gè)Region。
這里要注意的是我們創(chuàng)建了新的policy來(lái)使得CloudTrail擁有向我們創(chuàng)建的SNS發(fā)布消息(Action=“SNS:Publish”)的權(quán)限。我們的做法是從缺省的策略中拷貝了一份,修改了相應(yīng)的Action和Sid(隨便取一個(gè)不重復(fù)的名字),Principal部分是一個(gè)缺省的account的列表,這里是硬編碼,AWS有可能會(huì)修改該列表的值,但在當(dāng)前環(huán)境下,該值是固定的。最后移除Condition的值。把新創(chuàng)建的Policy片段添加到原來(lái)的Policy中就好了。
然后我們需要?jiǎng)?chuàng)建一個(gè)SQS的隊(duì)列,并訂閱我們創(chuàng)建的SNS的Topic。這一步相對(duì)比較簡(jiǎn)單。
import boto.sqs sqs_queue_name="sqs_queue" sqs_conn = boto.sqs.connect_to_region(region_name, aws_access_key_id=key_id, aws_secret_access_key=secret_key) sqs_queue = sqs_conn.create_queue(sqs_queue_name) sns_conn.subscribe_sqs_queue(sns_arn, sqs_queue)
然后,我們需要?jiǎng)?chuàng)建一個(gè)S3的Bucket用來(lái)存儲(chǔ)CloudTrail產(chǎn)生的日志文件。同樣的,需要指定響應(yīng)的策略以保證CloudTrail能夠有權(quán)限寫(xiě)入對(duì)應(yīng)的日志文件。
import boto bucket_name="bucket000" policy_sid="testpolicy000" s3_conn = boto.connect_s3(aws_access_key_id=key_id,aws_secret_access_key=secret_key) bucket = s3_conn.create_bucket(bucket_name) bucket_policy = '''{ "Version": "2012-10-17", "Statement": [ { "Sid": "%Sid%GetPolicy", "Effect": "Allow", "Principal": { "AWS": [ "arn:aws:iam::903692715234:root", "arn:aws:iam::035351147821:root", "arn:aws:iam::859597730677:root", "arn:aws:iam::814480443879:root", "arn:aws:iam::216624486486:root", "arn:aws:iam::086441151436:root", "arn:aws:iam::388731089494:root", "arn:aws:iam::284668455005:root", "arn:aws:iam::113285607260:root" ] }, "Action": "s3:GetBucketAcl", "Resource": "arn:aws:s3:::%bucket_name%" }, { "Sid": "%Sid%PutPolicy", "Effect": "Allow", "Principal": { "AWS": [ "arn:aws:iam::903692715234:root", "arn:aws:iam::035351147821:root", "arn:aws:iam::859597730677:root", "arn:aws:iam::814480443879:root", "arn:aws:iam::216624486486:root", "arn:aws:iam::086441151436:root", "arn:aws:iam::388731089494:root", "arn:aws:iam::284668455005:root", "arn:aws:iam::113285607260:root" ] }, "Action": "s3:PutObject", "Resource": "arn:aws:s3:::%bucket_name%/*", "Condition": { "StringEquals": { "s3:x-amz-acl": "bucket-owner-full-control" } } } ] }''' bucket_policy = bucket_policy.replace("%bucket_name%",bucket_name) bucket_policy = bucket_policy.replace("%Sid%",policy_sid) bucket.set_policy(bucket_policy)
這里我們使用一個(gè)缺省的Policy文件,替換掉響應(yīng)的字段就好了。
最后,我們創(chuàng)建CloudTrail的服務(wù):
import boto.cloudtrail trail_name="Trailabc" log_prefix="log" cloudtrail_conn=boto.cloudtrail.connect_to_region(region_name, aws_access_key_id=key_id, aws_secret_access_key=secret_key) ##cloudtrail_conn.describe_trails() cloudtrail_conn.create_trail(trail_name,bucket_name, s3_key_prefix=log_prefix,sns_topic_name=trail_topic_name) cloudtrail_conn.start_logging(trail_name)
好了,現(xiàn)在CloudTrail已經(jīng)配置好了,并且關(guān)聯(lián)的SNS也被我們創(chuàng)建的SQS隊(duì)列訂閱,下面我們就可以抓取日志了
每當(dāng)有一個(gè)API調(diào)用,CloudTrail都會(huì)把響應(yīng)的日志文件寫(xiě)入到S3我們創(chuàng)建的Bucket中,同時(shí)在我們?cè)趧?chuàng)建的SNS的topic中發(fā)布一條消息,因?yàn)槲覀兪褂肧QS的隊(duì)列訂閱了該消息,所以我們可以通過(guò)讀取SQS消息的方式來(lái)獲得日志數(shù)據(jù)。
首先連接到SQS的隊(duì)列,并從中讀取消息
import boto.sqs sqs_queue_name="sqs_queue" sqs_conn = boto.sqs.connect_to_region(region_name, aws_access_key_id=key_id, aws_secret_access_key=secret_key) sqs_queue = sqs_conn.get_queue(sqs_queue_name) notifications = sqs_queue.get_messages()
然后我們從消息中獲得響應(yīng)的日志文件在S3中的地址,并利用該地址從S3中獲得對(duì)應(yīng)的日志文件
for notification in notifications: envelope = json.loads(notification.get_body()) message = json.loads(envelope['Message']) bucket_name = message['s3Bucket'] s3_bucket = s3_conn.get_bucket(bucket_name) for key in message['s3ObjectKey']: s3_file = s3_bucket.get_key(key) with io.BytesIO(s3_file.read()) as bfile: with gzip.GzipFile(fileobj=bfile) as gz: logjson = json.loads(gz.read())
logjson就是對(duì)應(yīng)的日記內(nèi)容的JSON格式。這里有一個(gè)例子
{ "Records": [{ "eventVersion": "1.0", "userIdentity": { "type": "IAMUser", "principalId": "EX_PRINCIPAL_ID", "arn": "arn:aws:iam::123456789012:user/Alice", "accessKeyId": "EXAMPLE_KEY_ID", "accountId": "123456789012", "userName": "Alice" }, "eventTime": "2014-03-06T21:22:54Z", "eventSource": "ec2.amazonaws.com", "eventName": "StartInstances", "awsRegion": "us-west-2", "sourceIPAddress": "205.251.233.176", "userAgent": "ec2-api-tools 1.6.12.2", "requestParameters": { "instancesSet": { "items": [{ "instanceId": "i-ebeaf9e2" }] } }, "responseElements": { "instancesSet": { "items": [{ "instanceId": "i-ebeaf9e2", "currentState": { "code": 0, "name": "pending" }, "previousState": { "code": 80, "name": "stopped" } }] } } }, ... additional entries ... ] }
你可以使用以上代碼來(lái)監(jiān)控所有的cloudtrail的日志,拿到的JSON格式的日志可以放在你的數(shù)據(jù)庫(kù)(Mongo不錯(cuò))中,然后利用你的BI工具做分析。
注意你也可以不創(chuàng)建SNS和SQS,直接掃描bucket的內(nèi)容,這樣做的好處是配置更簡(jiǎn)單,缺點(diǎn)是實(shí)時(shí)性比較差,掃面Bucket需要額外的計(jì)算,并且需要在本地保存文件掃描的狀態(tài),code會(huì)更加復(fù)雜。
利用CloudTrail的日志,你可以做很多事情,比如看看有沒(méi)有非法的登陸,各個(gè)服務(wù)的使用頻率,總之,當(dāng)你有了足夠多的數(shù)據(jù),你就可以從中發(fā)現(xiàn)足夠的價(jià)值。
關(guān)于如何用Python抓取AWS的日志數(shù)據(jù)就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。