StarRocks社區版當前會將所有的審計日志保存在本地fe/log/fe.audit.log里,并沒有保存在資料庫中,社區不時有同學反饋希望將審計日志也保存在StarRocks本地庫表中一份,以方便后面的管理和維護,
研究這塊時發現Apache Doris提供的有審計日志插件,StarRocks從Doris fork出后重寫了七成多的代碼,對比研究了下兩個產品審計日志這塊的邏輯,發現還好是變化不大的,于是參考Apache Doris的審計插件代碼,簡單改造了一版適用于StarRocks的審計日志插件,本質上還是抽取fe.audit.log中的內容,攢批后基于Stream Load的方式匯入至StarRocks表中,審計插件下載地址見評論區度盤,大家可以參考下面的步驟安裝使用,
1、創建本地表
我們首先在本地創建一個動態磁區表,來保存審計日志中的資料,為了規范使用,建議為其單獨創建一個資料庫,
創建存放審計日志的資料庫starrocks_audit_db__:
mysql> create database starrocks_audit_db__;
在starrocks_audit_db__庫創建starrocks_audit_tbl__表:
mysql> create table starrocks_audit_db__.starrocks_audit_tbl__
(
query_id varchar(48) comment "查詢唯一ID",
time datetime not null comment "查詢開始時間",
client_ip varchar(32) comment "客戶端IP",
user varchar(64) comment "查詢用戶名",
db varchar(96) comment "查詢所在資料庫",
state varchar(8) comment "查詢狀態:EOF,ERR,OK",
query_time bigint comment "查詢執行時間(毫秒)",
scan_bytes bigint comment "查詢掃描的位元組數",
scan_rows bigint comment "查詢掃描的記錄行數",
return_rows bigint comment "查詢回傳的結果行數",
stmt_id int comment "SQL陳述句的增量ID",
is_query tinyint comment "SQL是否為查詢. 1或0",
frontend_ip varchar(32) comment "執行該陳述句的FE IP",
stmt string comment "SQL原始陳述句"
) engine=OLAP
duplicate key(query_id, time, client_ip)
partition by range(time) ()
distributed by hash(query_id) buckets 1
properties(
"dynamic_partition.time_unit" = "DAY",
"dynamic_partition.start" = "-30",
"dynamic_partition.end" = "3",
"dynamic_partition.prefix" = "p",
"dynamic_partition.buckets" = "1",
"dynamic_partition.enable" = "true",
"replication_num" = "3"
);
starrocks_audit_tbl__是動態磁區表,我們在建表時沒有直接創建磁區,所以建表后需要等待后臺動態磁區調度行程調度后才會生成當天及后三天的磁區,動態磁區調度行程默認10分鐘調度一次,我們可以先觀察磁區是否已經被創建,再進行后續操作,磁區查看陳述句為:
mysql> show partitions from starrocks_audit_db__. starrocks_audit_tbl__;
2、修改組態檔
度盤中的審計插件名稱為auditloader.zip,解壓插件:
[root@node01 ~]# unzip auditloader.zip
Archive: auditloader.zip
inflating: auditloader.jar
inflating: plugin.conf
inflating: plugin.properties
這句命令會把zip里面的檔案直接解壓到當前目錄,解壓后可以得到插件中的三個檔案:
auditloader.jar:插件代碼打包的核心jar包
plugin.conf:插件組態檔,需根據集群資訊修改
plugin.properties:插件屬性檔案,無需修改
根據我們實際的集群資訊,修改組態檔plugin.conf:
### plugin configuration
# The max size of a batch, default is 50MB
max_batch_size=52428800
# The max interval of batch loaded, default is 60 seconds
max_batch_interval_sec=60
# the max stmt length to be loaded in audit table, default is 4096
max_stmt_length=4096
# StarRocks FE host for loading the audit, default is 127.0.0.1:8030.
# this should be the host port for stream load
frontend_host_port=127.0.0.1:8030
# Database of the audit table
database=starrocks_audit_db__
# Audit table name, to save the audit data.
table=starrocks_audit_tbl__
# StarRocks user. This user must have LOAD_PRIV to the audit table.
user=root
# StarRocks user's password
password=root
修改完成后,再將上面的三個檔案重新打包為zip包備用:
[root@node01 ~]# zip -q -m -r auditloader.zip auditloader.jar plugin.conf plugin.properties
注意:這句命令會將需要打包的檔案移動到auditloader.zip中,并覆寫該目錄下原有的auditloader.zip檔案,也即執行完打包命令后,該目錄下只會保留一個最新的auditloader.zip插件包檔案,
3、分發插件
將auditloader.zip分發至集群所有FE節點,各節點分發路徑需要一致,例如我們都分發至StarRocks部署目錄/opt/module/starrocks/下,也即auditloader.zip檔案在集群所有FE節點的路徑都為:
/opt/module/starrocks/auditloader.zip
4、安裝插件
StarRocks安裝本地插件的語法為:
INSTALL PLUGIN FROM "/location/plugindemo.zip";
根據我們分發檔案的路徑修改命令后執行:
mysql> INSTALL PLUGIN FROM "/opt/module/starrocks/auditloader.zip";
安裝完成后,查看當前已安裝插件資訊:
mysql> SHOW PLUGINS\G
*************************** 1. row ***************************
Name: __builtin_AuditLogBuilder
Type: AUDIT
Description: builtin audit logger
Version: 0.12.0
JavaVersion: 1.8.31
ClassName: com.starrocks.qe.AuditLogBuilder
SoName: NULL
Sources: Builtin
Status: INSTALLED
Properties: {}
*************************** 2. row ***************************
Name: AuditLoader
Type: AUDIT
Description: load audit log to olap load, and user can view the statistic of queries
Version: 1.0.0
JavaVersion: 1.8.0
ClassName: com.starrocks.plugin.audit.AuditLoaderPlugin
SoName: NULL
Sources: /opt/module/starrocks/auditloader.zip
Status: INSTALLED
Properties: {}
我們可以看到當前有兩個插件,其中Name屬性為AuditLoader的插件即為我們剛才安裝的審計日志插件,其狀態為INSTALLED,表示已安裝成功,
Name為__builtin_AuditLogBuilder的插件為StarRocks自己的日志插件,是用來列印審計日志到本地日志目錄生成fe.audit.log的,
StarRocks自帶的__builtin_AuditLogBuilder插件我們千萬不要動它,咱們自己安裝的插件則可以視需求隨意卸載,卸載命令語法為:
UNINSTALL PLUGIN plugin_name;
--plugin_name即SHOW PLUGINS命令查到的插件Name資訊,
AuditLoader日志審計插件安裝完成后,我們可以觀察到fe/plugins/目錄下生成了一個AuditLoader檔案夾,這個檔案夾相當于是StarRocks加載auditloader.zip插件后存放的臨時目錄,每次重啟FE,StarRcoks都會重新從我們安裝插件時指定的路徑中重新加載auditloader.zip檔案,也是因此,前面分發至各FE節點的auditloader.zip檔案我們千萬不要移動或洗掉,
5、查看資料
AuditLoader插件安裝完成后,審計日志檔案中的資料并不是實時的入庫,StarRocks后臺會按照我們組態檔plugin.conf中配置的引數,攢批60秒或50M執行一次Stream Load匯入,等待期間,我們可以簡單執行幾條SQL陳述句,看對應的審計資料是否能夠正常入庫,例如執行:
mysql> CREATE TABLE starrocks.audit_test(c1 int,c2 int,c3 int,c4 date,c5 bigint) Distributed BY hash(c1) properties("replication_num" = "1");
mysql> insert into starrocks.audit_test values(211014001,10001,13,'2021-10-14',1999),(211014002,10002,13,'2021-10-14',6999),(211015098,16573,19,'2021-10-15',3999);
等待1分鐘左右,查看審計表資料:
mysql> select * from starrocks_audit_db__. starrocks_audit_tbl__;
正常來說資料都可以成功入庫,如果表內始終沒有資料,可以檢查組態檔plugin.conf中資訊配置是否正確,如果有誤,可以參考前面的步驟修改組態檔,卸載插件后重新安裝,或者我們也可以查看fe.log,用排查Stream Load任務的方式來定位問題,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/438666.html
標籤:其他
上一篇:hive深入知識(二)
下一篇:Elasticsearch 集群
