龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > 数据库类 > MySQL 技术 >

利用mysql的audit审计功能记录用户操作信息

时间:2011-04-12 23:18来源:未知 作者:admin 点击:
分享到:
mysql 数据库中我们如果想记录用户的操作信息,可以通过 audit 审计功能来来实现。该功能是被自动触发的,在文件plugin_audit.h中可以看到比较详细的定义。在audit插件中,可控制的变量

mysql数据库中我们如果想记录用户的操作信息,可以通过audit审计功能来来实现。该功能是被自动触发的,在文件plugin_audit.h中可以看到比较详细的定义。在audit插件中,可控制的变量包括THD以及事件。

其中事件分为两种结构体,可以进行强制转换:

第一种:

  1. struct mysql_event_general   
  2.  
  3. {  
  4.  
  5. unsigned int event_subclass;   
  6.  
  7. int general_error_code;   
  8.  
  9. unsigned long general_thread_id;   
  10.  
  11. const char *general_user;   
  12.  
  13. unsigned int general_user_length;   
  14.  
  15. const char *general_command;   
  16.  
  17. unsigned int general_command_length;   
  18.  
  19. const char *general_query;   
  20.  
  21. unsigned int general_query_length;   
  22.  
  23. struct charset_info_st *general_charset;   
  24.  
  25. unsigned long long general_time;   
  26.  
  27. unsigned long long general_rows;   
  28.  
  29. }; 

触发条件:

#define MYSQL_AUDIT_GENERAL_LOG 0 :在提交给general query log之前被触发。

#define MYSQL_AUDIT_GENERAL_ERROR 1 :在发送给用户错误之前触发。

#define MYSQL_AUDIT_GENERAL_RESULT 2 : 当将结果集合发送给用户后触发。

#define MYSQL_AUDIT_GENERAL_STATUS 3  :  当发送一个结果集或发生错误时被触发。

第二种:

  1. struct mysql_event_connection   
  2.  
  3. {  
  4.  
  5. unsigned int event_subclass;   
  6.  
  7. int status;   
  8.  
  9. unsigned long thread_id;   
  10.  
  11. const char *user;   
  12.  
  13. unsigned int user_length;   
  14.  
  15. const char *priv_user;   
  16.  
  17. unsigned int priv_user_length;   
  18.  
  19. const char *external_user;   
  20.  
  21. unsigned int external_user_length;   
  22.  
  23. const char *proxy_user;   
  24.  
  25. unsigned int proxy_user_length;   
  26.  
  27. const char *host;   
  28.  
  29. unsigned int host_length;   
  30.  
  31. const char *ip;   
  32.  
  33. unsigned int ip_length;   
  34.  
  35. const char *database;   
  36.  
  37. unsigned int database_length;   
  38.  
  39. }; 

触发条件:

#define MYSQL_AUDIT_CONNECTION_CONNECT 0 :  完成认证后触发。

#define MYSQL_AUDIT_CONNECTION_DISCONNECT 1 : 连接被中断时触发。

#define MYSQL_AUDIT_CONNECTION_CHANGE_USER 2 : 在执行COM_CHANGE_USER命令后触发。

从上面的分析,我们可以看出,在event中存储了相当丰富的信息,将notify函数进行了如下简单的修改:

  1. static void audit_null_notify(MYSQL_THD thd __attribute__((unused)),   
  2.  
  3. unsigned int event_class,  
  4.  
  5. const void *event)  
  6.  
  7. {   
  8.  
  9. const struct mysql_event_general *pEvent;  
  10.  
  11. if (log_fp == NULL)  
  12.  
  13. log_fp = fopen("/tmp/rec.log", "a");  
  14.  
  15. number_of_calls++;  
  16.  
  17. if (event_class == MYSQL_AUDIT_GENERAL_CLASS && log_fp != NULL){  
  18.  
  19. pEvent = (const struct mysql_event_general *) event;  
  20.  
  21. if ( pEvent->event_subclass == MYSQL_AUDIT_GENERAL_RESULT &&  
  22.  
  23. pEvent->general_query != NULL  
  24.  
  25. && *(pEvent->general_query) != '\0') {   
  26.  
  27. // fprintf(log_fp, "user:%s,host:%s,command:%sn",&thd->security_ctx->priv_user[0],   
  28.  
  29. // (char *) thd->security_ctx->host_or_ip ,  
  30.  
  31. // pEvent->general_query);  
  32.  
  33. time_t cur = time(NULL);  
  34.  
  35. fprintf(log_fp, "%s %sn%sn", ctime(&cur) , pEvent->general_user , pEvent->general_query);  
  36.  
  37. fflush(log_fp);  
  38.  
  39. }  
  40.  
  41. }  
  42.  

这样,我们实现了记录用户名、用户主机信息以及sql操作等相关信息。


精彩图集

赞助商链接