原創審計外掛程式(過濾使用者 IP)

來源:互聯網
上載者:User

#include <stdio.h>
#include <mysql/plugin.h>
#include <mysql/plugin_audit.h>
#include <string.h> 
#include <plugin.h> 
#include<mysql_version.h> 
#include <my_global.h> 
#include <my_sys.h> 
#include<sys/resource.h> 
#include <sys/time.h> 


#if !defined(__attribute__) && (defined(__cplusplus) || !defined(__GNUC__)  || __GNUC__ == 2 && __GNUC_MINOR__ < 8)
#define __attribute__(A)
#endif


static volatile int number_of_calls; /* for SHOW STATUS, see below */
static volatile int number_of_calls_general_log;
static volatile int number_of_calls_general_error;
static volatile int number_of_calls_general_result;
static FILE *log_fp;
char time_str[20];


/*
  Initialize the plugin at server start or plugin installation.


  SYNOPSIS
    audit_null_plugin_init()


  DESCRIPTION
    Does nothing.


  RETURN VALUE
    0                    success
    1                    failure (cannot happen)
*/


static int audit_null_plugin_init(void *arg __attribute__((unused)))
{
  number_of_calls= 0;
  number_of_calls_general_log= 0;
  number_of_calls_general_error= 0;
  number_of_calls_general_result= 0;
log_fp = NULL;
  return(0);
}




/*
  Terminate the plugin at server shutdown or plugin deinstallation.


  SYNOPSIS
    audit_null_plugin_deinit()
    Does nothing.


  RETURN VALUE
    0                    success
    1                    failure (cannot happen)


*/


static int audit_null_plugin_deinit(void *arg __attribute__((unused)))
{
  fclose(log_fp);
  return(0);
}




/*
  Foo


  SYNOPSIS
    audit_null_notify()
      thd                connection context


  DESCRIPTION
*/


static void audit_null_notify(MYSQL_THD thd __attribute__((unused)),
                              unsigned int event_class,
                              const void *event)
{
if (log_fp == NULL)
log_fp = fopen("/tmp/my_general_log.log","a");
//log_fp = fopen(global_system_variables.opt_logfile,"a");
  /* prone to races, oh well */
  number_of_calls++;
  if (event_class == MYSQL_AUDIT_GENERAL_CLASS)
  {
    const struct mysql_event_general *event_general= (const struct mysql_event_general *) event;
    //const struct mysql_event_connection  *event_connection= (const struct mysql_event_connection *) event;
   if(event_general->general_query!=NULL && event_general->event_subclass==MYSQL_AUDIT_GENERAL_RESULT && strstr(event_general->general_user,"slave_xierqi")!=NULL && strstr(event_general->general_user,"10.10.10.98")!=NULL)
  {
   get_date(time_str, GETDATE_DATE_TIME,0);
   fprintf(log_fp, "%s,[%s],%s,%llu,%zu;\n",event_general->general_user,time_str, event_general->general_query,event_general->general_rows,strlen(event_general->general_user));
   fflush(log_fp);
  }
  }
}




/*
  Plugin type-specific descriptor
*/


static struct st_mysql_audit audit_null_descriptor=
{
  MYSQL_AUDIT_INTERFACE_VERSION,                    /* interface version    */
  NULL,                                             /* release_thd function */
  audit_null_notify,                                /* notify function      */
  { (unsigned long) MYSQL_AUDIT_GENERAL_CLASSMASK } /* class mask           */
};


/*
  Plugin status variables for SHOW STATUS
*/


static struct st_mysql_show_var simple_status[]=
{
  { "Audit_null_called", (char *) &number_of_calls, SHOW_INT },
  { "Audit_null_general_log", (char *) &number_of_calls_general_log, SHOW_INT },
  { "Audit_null_general_error", (char *) &number_of_calls_general_error,
    SHOW_INT },
  { "Audit_null_general_result", (char *) &number_of_calls_general_result,
    SHOW_INT },
  { 0, 0, 0}
};




/*
  Plugin library descriptor
*/


mysql_declare_plugin(audit_null)
{
  MYSQL_AUDIT_PLUGIN,         /* type                            */
  &audit_null_descriptor,     /* descriptor                      */
  "NULL_AUDIT",               /* name                            */
  "Oracle Corp",              /* author                          */
  "Simple NULL Audit",        /* description                     */
  PLUGIN_LICENSE_GPL,
  audit_null_plugin_init,     /* init function (when loaded)     */
  audit_null_plugin_deinit,   /* deinit function (when unloaded) */
  0x0002,                     /* version                         */
  simple_status,              /* status variables                */
  NULL,                       /* system variables                */
  NULL,
  0,
}

mysql_declare_plugin_end;

工作路徑

[root@os3 audit_null]# pwd
/tools/mysql-5.5.31patch/plugin/audit_null

編譯語句

gcc -g -Wall -I/usr/local/mysql/include -DMYSQL_DYNAMIC_PLUGIN   -c -fPIC -o audit_null.o audit_null.c
gcc -shared -fPIC -o   adt_null.so  audit_null.o
 mv -f  adt_null.so /usr/local/mysql/lib/plugin/


mysql> install plugin NULL_AUDIT soname 'adt_null.so';   載入外掛程式

mysql> uninstall plugin NULL_AUDIT;         下載外掛程式


審計檔案路徑   審計使用者 審計IP  這些需要添加全域變數,暫時未解決,歡迎指正。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.