ablog

不器用で落着きのない技術者のメモ

Amazon Redshift で監査ログを取得するクエリサンプル

Amazon Redshift で監査ログを取得するクエリサンプル。

取得項目

テーブル or ビュー カラム 説明
stl_query starttime 実行開始時刻
stl_query endtime 実行終了時刻
stl_connection_log remotehost リモートホストの名前または IP アドレス
stl_query servie_type 固定文字列
stl_query database データベース名
svl_qlog userid ユーザーID
stl_connection_log username ユーザー名
svl_qlog query クエリ ID
svl_qlog substring クエリテキスト
stl_return rows 結果セットの行数

結果セットサプル

f:id:yohei-a:20200421210228p:plain

クエリ

/* returned_rows */ select c.starttime
		,c.endtime
		,d.remotehost
        ,'Redshift' servie_type
		,c.database
		,a.userid
        ,d.username
        ,a.query
        ,a.substring sql
        ,b.rows
from svl_qlog a join stl_return b
                on a.query=b.query
        join stl_query c
        on c.query = a.query
        join stl_connection_log d
        on d.pid = c.pid
where
    b.slice >= 6411
    and a.userid != 1
    and c.starttime between '2020-04-21 11:30' and '2020-04-21 12:00'
    and sql not like '/* returned_rows */%'
union all
select  c.starttime
		,c.endtime
		,d.remotehost
        ,'Redshift' servie_type
		,c.database
		,a.userid
        ,d.username
        ,a.query
        ,a.substring sql
        ,b.rows
from svl_qlog a join stl_return b
        on a.source_query=b.query
        join stl_query c
        on c.query = a.query
        join stl_connection_log d
        on d.pid = c.pid
where
    sql not like '/* returned_rows */%'
    and c.starttime between '2020-04-21 11:30' and '2020-04-21 12:00'
order by
query desc