MySQL DDL Monitoring in C

A simple C MySQL plugin to write DDL (specifically ALTER & DROP statements) to the SYSLOG:

audit_adb.c class


#include
#include
#include
#include
#include

#if !defined(__attribute__) && (defined(__cplusplus) || !defined(__GNUC__) || __GNUC__ == 2 && __GNUC_MINOR__ general_query != NULL && *(event_general->general_query) != ”) {
size_t len = event_general->general_query_length;
char query_val[len];
strcpy (query_val, event_general->general_query);
char final_query_val[len];
int i=0;
while (query_val[i])
{
final_query_val[i]=toupper(query_val[i]);
i++;
}
// strncpy (query_val, event_general->general_query, strlen(event_general->general_query)+1);
if (strstr (final_query_val, “ALTER”) != NULL || strstr (final_query_val, “DROP”) != NULL) {
syslog(0,”%s %s\n”,event_general->general_query,event_general->general_command);
}
}
}
}

/*
Plugin type-specific descriptor
*/

static struct st_mysql_audit audit_adb_descriptor=
{
MYSQL_AUDIT_INTERFACE_VERSION, /* interface version */
NULL, /* release_thd function */
audit_adb_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_adb_called”, (char *) &number_of_calls, SHOW_INT },
//{ “Audit_adb_general_log”, (char *) &number_of_calls_general_log, SHOW_INT },
//{ “Audit_adb_general_error”, (char *) &number_of_calls_general_error, SHOW_INT },
//{ “Audit_adb_general_result”, (char *) &number_of_calls_general_result, SHOW_INT },
// { “ZZZ_value”, (char *) &query_sql, SHOW_CHAR_PTR },
{ 0, 0, SHOW_INT}
};

/*
Plugin library descriptor
*/

mysql_declare_plugin(audit_adb)
{
MYSQL_AUDIT_PLUGIN, /* type */
&audit_adb_descriptor, /* descriptor */
“adb_AUDIT”, /* name */
“Nikolaos Vizas”, /* author */
“MySQL DDL Auditing plugin”, /* description */
PLUGIN_LICENSE_GPL,
audit_adb_plugin_init, /* init function (when loaded) */
audit_adb_plugin_deinit, /* deinit function (when unloaded) */
0x0002, /* version */
simple_status, /* status variables */
NULL, /* system variables */
NULL,
0,
}
mysql_declare_plugin_end;

Makefile (adjust for your relative pathnames):


MYSQL_BASE="/root/inst_images/percona/5.5.31/dev/Percona-XtraDB-Cluster-5.5.31_SRC_squeeze-438"
MYSQL_INST="/usr/lib/mysql/plugin"
CFLAGS=-D_HAVE_CONFIG_H -DMYSQL_DYNAMIC_PLUGIN -fPIC
all: audit_adb.so

audit_adb.o:
gcc $(CFLAGS) -I$(MYSQL_BASE)/include -I$(MYSQL_BASE)/regex -I$(MYSQL_BASE)/sql -c audit_adb.c

audit_adb.so: audit_adb.o
gcc -shared audit_adb.o -o audit_adb.so

clean:
rm -fr ./audit_adb.o ./audit_adb.so

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s