操作審計(ActionTrail)幫助您監控阿里云賬號的活動并記錄最近90天的事件。當您需要分析更長時間的事件時,可以通過操作審計創建跟蹤,將事件投遞到日志服務SLS,使用SQL語句對事件進行查詢和分析。本文為您介紹在如何在SLS設置SQL語句。
SQL語句語法
SQL(Structured Query Language)語句格式為:<查詢語句> | <分析語句>
。
操作審計支持通過多種方式查詢事件。不同查詢方式對應的查詢語句和分析語句如下表所示:
查詢方式 | 查詢語句 | 分析語句 |
事件查詢 |
|
|
事件聚合查詢 |
|
|
Insight事件查詢 |
|
|
SQL語句示例
示例一:查詢管控事件中的所有寫事件
* AND "event.eventCategory": Management AND "event.eventRW": Write | select "event.acsRegion" as acsRegion, "event.apiVersion" as apiVersion, "event.eventId" as eventId, "event.eventName" as eventName, "event.eventRW" as eventRW, "event.eventSource" as eventSource, from_unixtime(__time__) as eventTime, "event.eventType" as eventType, "event.eventVersion" as eventVersion, "event.errorCode" as errorCode, "event.errorMessage" as errorMessage, "event.requestId" as requestId, "event.requestParameterJson" as requestParameterJson, "event.resourceName" as resourceName, "event.resourceType" as resourceType, "event.serviceName" as serviceName, "event.sourceIpAddress" as sourceIpAddress, "event.userAgent" as userAgent, "event.userIdentity.accessKeyId" as accessKeyId, "event.userIdentity.accountId" as accontId, "event.userIdentity.principalId" as principalId, "event.userIdentity.type" as type, "event.userIdentity.userName" as userName
示例二:查詢管控事件中所有寫事件的聚合情況
說明如果設置的查詢時間段較長,建議設置
LIMIT N
,返回N
條事件。例如:設置LIMIT 20
,返回20條事件。* AND "event.eventCategory": Management AND "event.eventRW": Write | SELECT"event.serviceName"AS servieName,"event.eventName"AS eventName,"event.eventRw"AS eventRw,"event.sourceIpAddress"AS sourceIpAddress,"event.resourceName"AS resourceName,"event.resourceType"AS resourceType,"event.userIdentity.userName"AS userName,"event.userIdentity.type"AS userType,"event.userIdentity.accessKeyId"AS accessKeyId,"event.acsRegion"AS eventRegion,COUNT("event.eventId")AS n, date_trunc('hour', __time__) AS time GROUP BY time, servieName, eventName, eventRw, sourceIpAddress, resourceType, resourceName, accessKeyId, userType, userName, eventRegion ORDER BY time DESC LIMIT 20
示例三:查詢Insight事件中所有IP異常事件
* AND "event.eventCategory": Insight AND event.insightDetails.insightType: IpInsight | select from_unixtime(__time__) as eventTime, "event.acsRegion" as eventRegion, "event.insightDetails.sourceIpAddress" as sourceIpAddress, "event.insightDetails.insightContext.statistics.insightCount" as count