SlideShare a Scribd company logo
Developing INFORMATION_SCHEMA Plugins Mark Leith Support Manager, Americas [email_address]
Overview INFORMATION_SCHEMA overview Plugin Overview Developing a simple Hello World I_S table Building and installing  Interacting with another program Interacting with another library
INFORMATION_SCHEMA Specified within the SQL Standard  (ISO/IEC 9075-11:2003) ‏ Virtual tables which give database metadata MySQL implements a subset of the Standard http://dev.mysql.com/doc/refman/5.1/en/information-schema.html http://www. xcdsql .org/MySQL/information_schema/5.1/MySQL_5_1_INFORMATION_SCHEMA.html
 
Plugins A means to load shared libraries in to a running MySQL instance Developed in C/C++ http://dev.mysql.com/doc/refman/5.1/en/plugin-api.html User Defined Functions Storage Engines Fulltext parsers Daemons INFORMATION_SCHEMA Tables!
Located in include/mysql/plugin.h The Plugin Interface struct  st_mysql_plugin {  int  type;  /* the plugin type (a MYSQL_XXX_PLUGIN value)  */   void  *info;  /* pointer to type-specific plugin descriptor  */   const   char  *name;  /* plugin name  */   const   char  *author;  /* plugin author (for SHOW PLUGINS)  */   const   char  *descr;  /* general descriptive text (for SHOW PLUGINS )  */   int  license;  /* the plugin license (PLUGIN_LICENSE_XXX)  */   int  (*init)( void  *);  /* the function to invoke when plugin is loaded  */   int  (*deinit)( void  *) /* the function to invoke when plugin is unloaded  */   unsigned   int  version;  /* plugin version (for SHOW PLUGINS)  */   struct  st_mysql_show_var *status_vars;  struct  st_mysql_sys_var **system_vars;  void  * __reserved1;  /* reserved for dependency checking  */ };
Plugin Types Define the plugin type (types listed below) Used within plugin functions such as plugin_foreach() MYSQL_UDF_PLUGIN   0  /* User-defined function  */ MYSQL_STORAGE_ENGINE_PLUGIN   1  /* Storage Engine  */ MYSQL_FTPARSER_PLUGIN   2  /* Full-text parser plugin  */ MYSQL_DAEMON_PLUGIN   3  /* The daemon/raw plugin type */ MYSQL_INFORMATION_SCHEMA_PLUGIN  4  /* The I_S plugin type  */ I_S plugins loaded within sql/sql_show.cc int  schema_tables_add(THD *thd, List<LEX_STRING> *files,  const   char  *wild) { … if  (plugin_foreach(thd, add_schema_table, MYSQL_INFORMATION_SCHEMA_PLUGIN, &add_data))  DBUG_RETURN( 1 );
Starting a HELLO_WORLD Table Must have MySQL source tree Create a new plugin/hello_world/hello_world.cc file plugin.h and mysql_priv.h includes required Add a prototype for schema_table_store_record() /* Required for schema_table_store_record() * / #include &quot;mysql_priv.h&quot; #include <mysql/plugin.h> bool  schema_table_store_record(THD *thd, TABLE *table);
Define the Table Structure Uses an array of  ST_FIELD_INFO  structs field_name Column name field_length Column length or display length field_type Column datatype value Not used within I_S plugins field_flags Set NULL / UNSIGNED attributes old_name Internal mapping for I_S tables to SHOW output open_method Open table using supplied method Last entry in the array is an end marker ST_FIELD_INFO hello_world_fields[]= { { &quot;HELLO&quot; ,  10 , MYSQL_TYPE_STRING,  0 ,  0 ,  &quot;Hello&quot; ,  0 }, { &quot;WORLD&quot; ,  10 , MYSQL_TYPE_STRING,  0 ,  0 ,  &quot;World&quot; ,  0 }, { 0 ,  0 , MYSQL_TYPE_NULL,  0 ,  0 ,  0 ,  0 } };
A Closer Look Field_type Field_flags Open_method VARCHAR INT BIGINT DECIMAL DATETIME MYSQL_TYPE_STRING MYSQL_TYPE_LONG MYSQL_TYPE_LONGLONG MYSQL_TYPE_DECIMAL MYSQL_TYPE_DATETIME MY_I_S_MAYBE_NULL MY_I_S_UNSIGNED SKIP_OPEN_TABLE  OPEN_FRM_ONLY OPEN_FULL_TABLE
Define Function to Fill Table Called every time the table is accessed Fills the virtual table with data int  fill_hello_world(THD *thd, TABLE_LIST *tables, COND *cond) { DBUG_ENTER( &quot;fill_hello_world_is_plugin&quot; ); CHARSET_INFO *scs= system_charset_info; TABLE *table= tables->table; int  rc=  0 ; table->field[ 0 ]->store( &quot;Hello&quot; , strlen( &quot;Hello&quot; ), scs); table->field[ 1 ]->store( &quot;World&quot; , strlen( &quot;World&quot; ), scs); if  (schema_table_store_record(thd, table)) rc=  1 ; DBUG_RETURN(rc); }
Create the Init Function Runs when the plugin is installed/loaded ST_SCHEMA_TABLE  is an internal table representation Points to the  ST_FIELD_INFO  struct / table definition Points to the function to fill the table Manages any other init needed for the plugin int  hello_world_plugin_init( void  *p) { DBUG_ENTER( &quot;init_hello_world_is_plugin&quot; ); ST_SCHEMA_TABLE *schema= (ST_SCHEMA_TABLE*) p;  schema->fields_info= hello_world_fields; schema->fill_table= fill_hello_world; DBUG_RETURN( 0 ); }
Create the Deinit Function Run when the plugin is unloaded Does nothing for HELLO_WORLD Use this function to do any clean up in your plugin int  hello_world_plugin_deinit( void  *p) { DBUG_ENTER( &quot;deinit_info_schema_example_plugin&quot; ); DBUG_RETURN( 0 ); }
Create the Plugin Definition  Info struct points to the interface version built against struct  st_mysql_information_schema hello_world_plugin_info= { MYSQL_INFORMATION_SCHEMA_INTERFACE_VERSION };  mysql_declare_plugin(hello_world_plugin) { MYSQL_INFORMATION_SCHEMA_PLUGIN, &hello_world_plugin_info,  /* Pointer to info struct */ &quot;HELLO_WORLD&quot; ,  /* Plugin Name (used in INSTALL PLUGIN) */ &quot;Mark Leith, MySQL AB&quot; ,  /* Plugin Author */ &quot;HELLO_WORLD example plugin&quot; , /* Plugin Description */ PLUGIN_LICENSE_GPL,  /* _GPL, _BSD or _PROPRIETARY */ hello_world_plugin_init,  /* Pointer to plugin init function */ hello_world_plugin_deinit,  /* Pointer to plugin deinit function */ 0x0100 ,  /* 1.0 */ NULL ,  /* status variables */ NULL ,  /* system variables */ NULL   /* config options */ } mysql_declare_plugin_end;
Build the Plugin Define MYSQL_DYNAMIC_PLUGIN Make shared Include the  sql  and  include  source directories g++ -DMYSQL_DYNAMIC_PLUGIN -shared \ > -I/home/leithal/mysql/mysql-5.1/include \ > -I/home/leithal/mysql/mysql-5.1/sql \ > -o is_hello_world.so hello_world.cc ls -l total 16 -rw-r--r-- 1 leithal leithal 1712 2008-04-11 14:20 hello_world.cc -rwxr-xr-x 1 leithal leithal 8226 2008-04-11 14:27 is_hello_world.so cp is_hello_world.so /usr/local/mysql/lib/mysql/plugin
Install and Use! mysql> INSTALL PLUGIN HELLO_WORLD SONAME 'is_hello_world.so'; Query OK, 0 rows affected (0.01 sec) mysql> USE INFORMATION_SCHEMA; Database changed mysql> SHOW TABLES LIKE 'HELL%'; +--------------------------------------+ | Tables_in_information_schema (HELL%) | +--------------------------------------+ | HELLO_WORLD  | +--------------------------------------+ 1 row in set (0.00 sec) mysql> SELECT * FROM HELLO_WORLD; +-------+-------+ | HELLO | WORLD | +-------+-------+ | Hello | World | +-------+-------+ 1 row in set (0.00 sec)
So We'll Do Something Interesting How's the disk space doing on my db server? root@achilles:~# df -h Filesystem  Size  Used Avail Use% Mounted on /dev/hda1  4.6G  3.5G  886M  81% / varrun  126M  84K  125M  1% /var/run varlock  126M  0  126M  0% /var/lock procbususb  10M  76K  10M  1% /proc/bus/usb udev  10M  76K  10M  1% /dev devshm  126M  0  126M  0% /dev/shm lrm  126M  18M  108M  14% /lib/modules/2.6.17-12-generic/volatile If I can get this in a table I can track it over time I can also report on it easily How about an I_S table with events to catalog?
Create Template & Table Structure  Add stdio.h,string.h and stdlib.h includes Mirror init, deinit and info functions Fill in the plugin declaration scruct appropriately Define the new table: ST_FIELD_INFO fs_info_schema_fields[]= { { &quot;FILESYSTEM&quot; ,  120 , MYSQL_TYPE_STRING,  0 ,  0 ,  &quot;Filesystem&quot; ,  0 }, { &quot;SIZE&quot; ,  8 , MYSQL_TYPE_LONGLONG,  0 ,  0 ,  &quot;Mountpoint Size&quot; ,  0 }, { &quot;USED&quot; ,  8 , MYSQL_TYPE_LONGLONG,  0 ,  0 ,  &quot;Used Space&quot; ,  0 }, { &quot;AVAILABLE&quot; ,  8 , MYSQL_TYPE_LONGLONG,  0 ,  0 ,  &quot;Available Space&quot; ,  0 }, { &quot;CAPACITY&quot; ,  4 , MYSQL_TYPE_STRING,  0 ,  0 ,  &quot;Percent Used&quot; ,  0 }, { &quot;MOUNTED_ON&quot; ,  120 , MYSQL_TYPE_STRING,  0 ,  0 ,  &quot;Filesystem Mounted On&quot;  ,  0 }, { 0 ,  0 , MYSQL_TYPE_STRING,  0 ,  0 ,  0 ,  0 } };
Running Other Programs Open with popen() and read in the results /* get filesystem information from df on linux like systems */ FILE *f; char  buf[ 128 ]; char   *c, *s, *size; const   char  delim[]=  &quot; &quot; ; unsigned   long   long  uli; double  d; if  ( NULL  != (f= popen( &quot;/bin/df -h&quot; ,  &quot;r&quot; ))) { int  ln=  0 ; while (!feof(f)) { fgets(buf,  sizeof (buf), f); switch (ln++)  {
Tokenize and Push Results Can do this how you like /* skip the header line of df */ case   0 :  break ; default : c= buf;  /* hack to stop processing when falling off the end of output */   if  (strchr(c,  ' ' ) ==  NULL ) break ;  /* Filesystem */   s= strtok(c, delim); table->field[ 0 ]->store(s, strlen(s), scs); /* Size */   s= strtok( NULL , delim);  d= strtod(s, &size);  uli= get_bytes(d, size); table->field[ 1 ]->store(uli,  TRUE );  … .
Store the Row and Finish up /* Use% */ s= strtok( NULL , delim); table->field[ 4 ]->store(s, strlen(s), scs); /* Mountpoint */ s= strtok( NULL , delim); stripnl(s); table->field[ 5 ]->store(s, strlen(s), scs); /* store the row */ if  (schema_table_store_record(thd, table)) rc=  1 ; break ; } } pclose(f); } else   rc=  1 ; DBUG_RETURN(rc); }
Build, Install, Try.. Build as before and try it out! mysql> select * from file_system_mountpoints; +------------+------------+------------+-----------+----------+-----------------------------------------+ | FILESYSTEM | SIZE  | USED  | AVAILABLE | CAPACITY | MOUNTED_ON  | +------------+------------+------------+-----------+----------+-----------------------------------------+ | /dev/hda1  | 4939212390 | 3758096384 | 930086912 | 81%  | /  |  | varrun  |  132120576 |  86016 | 131072000 | 1%  | /var/run  |  | varlock  |  132120576 |  0 | 132120576 | 0%  | /var/lock  |  | procbususb |  10485760 |  77824 |  10485760 | 1%  | /proc/bus/usb  |  | udev  |  10485760 |  77824 |  10485760 | 1%  | /dev  |  | devshm  |  132120576 |  0 | 132120576 | 0%  | /dev/shm  |  | lrm  |  132120576 |  18874368 | 113246208 | 14%  | /lib/modules/2.6.17-12-generic/volatile |  +------------+------------+------------+-----------+----------+-----------------------------------------+ 7 rows in set (0.06 sec) mysql> desc file_system_mountpoints; +------------+--------------+------+-----+---------+-------+ | Field  | Type  | Null | Key | Default | Extra | +------------+--------------+------+-----+---------+-------+ | FILESYSTEM | varchar(120) | NO  |  |  |  |  | SIZE  | bigint(11)  | NO  |  | 0  |  |  | USED  | bigint(11)  | NO  |  | 0  |  |  | AVAILABLE  | bigint(11)  | NO  |  | 0  |  |  | CAPACITY  | varchar(4)  | NO  |  |  |  |  | MOUNTED_ON | varchar(120) | NO  |  |  |  |  +------------+--------------+------+-----+---------+-------+ 6 rows in set (0.01 sec)
Now Let's Use another Library The SIGAR library from Hyperic Lets us do cross platform OS stats monitoring Released under GPL Again, start with the same base functions etc. as before
Include Lib Header, Use Lib in Fill sigar_t *t; sigar_open(&t); sigar_file_system_list_t fslist; sigar_file_system_list_get(t, &fslist); for  (uint i =  0 ; i < fslist.number; i++)  { sigar_file_system_t fs = fslist.data[i]; sigar_file_system_usage_t fsusage; rc= sigar_file_system_usage_get(t, fs.dir_name, &fsusage); if  (fs.type ==  2  || fs.type ==  3 ) { table->field[ 0 ]->store(fs.dir_name, strlen(fs.dir_name), scs); table->field[ 1 ]->store(fsusage.total,  TRUE ); table->field[ 2 ]->store(fsusage.used,  TRUE ); table->field[ 3 ]->store(fsusage.free,  TRUE ); table->field[ 4 ]->store(fsusage.files,  TRUE ); if  (schema_table_store_record(thd, table)) rc=  1 ; } } sigar_file_system_list_destroy(t, &fslist); sigar_close(t);
Building and Including the Library -lsigar links the library Use -rpath to pass in library location  This is for runtime Can also use LD_LIBRARY_PATH  -L (linker) and -I (include) paths  -L points to the directory with the built library g++ -DMYSQL_DYNAMIC_PLUGIN -Wall -shared \ -lsigar -Wl,-rpath -Wl,/home/leithal/os_stats_info_schema/sigar/ \ -L/home/leithal/os_stats_info_schema/sigar/ \ -I/home/leithal/os_stats_info_schema/sigar/include/ \ -I/home/leithal/mysql/mysql-5.1/include \ -I/home/leithal/mysql/mysql-5.1/sql \ -o os_stats_info_schema.so os_stats_info_schema.cc
Try it out! mysql> INSTALL PLUGIN os_disk_usage SONAME 'os_stats_info_schema.so'; Query OK, 0 rows affected (0.00 sec) mysql> SHOW TABLES LIKE 'OS%'; +------------------------------------+ | Tables_in_information_schema (OS%) | +------------------------------------+ | OS_DISK_USAGE  |  +------------------------------------+ 1 row in set (0.00 sec) mysql> SELECT * FROM OS_DISK_USAGE; +------------+---------+---------+---------+--------+ | FILESYSTEM | SIZE  | USED  | FREE  | FILES  | +------------+---------+---------+---------+--------+ | /  | 4799024 | 3656800 | 1142224 | 610432 |  +------------+---------+---------+---------+--------+ 1 row in set (0.00 sec)
Resources and Questions! http://www.markleith.co.uk/?p=18 http://rpbouman.blogspot.com/2008/02/mysql-information-schema-plugins-best.html http://rpbouman.blogspot.com/2008/02/reporting-mysql-internals-with.html  (Great resource for monitoring server internals) Questions?

More Related Content

ODP
MySQL Monitoring Mechanisms
PDF
Extending MySQL Enterprise Monitor
PDF
MySQL for Oracle DBAs
PDF
Instrumenting plugins for Performance Schema
ODP
MySQL Administration and Monitoring
ODP
Performance schema and_ps_helper
PDF
Performance Schema and Sys Schema in MySQL 5.7
ODP
MySQL Monitoring Mechanisms
MySQL Monitoring Mechanisms
Extending MySQL Enterprise Monitor
MySQL for Oracle DBAs
Instrumenting plugins for Performance Schema
MySQL Administration and Monitoring
Performance schema and_ps_helper
Performance Schema and Sys Schema in MySQL 5.7
MySQL Monitoring Mechanisms

What's hot (20)

ODP
Getting to Know MySQL Enterprise Monitor
PDF
Mysql tech day_paris_ps_and_sys
PDF
Performance schema and sys schema
PDF
The MySQL SYS Schema
PDF
MySQL's Performance Schema, SYS Schema and Workbench Integration
PDF
The MySQL Performance Schema & New SYS Schema
PDF
MySQL sys schema deep dive
PDF
MySQL sys schema deep dive
PDF
Performance Schema for MySQL troubleshooting
PDF
Capturing, Analyzing, and Optimizing your SQL
PDF
Oracle Database 11g Product Family
 
PDF
What's next after Upgrade to 12c
PDF
In Memory Database In Action by Tanel Poder and Kerry Osborne
PDF
MySQL Troubleshooting with the Performance Schema
PDF
REST in Piece - Administration of an Oracle Cluster/Database using REST
PPTX
Flex Cluster e Flex ASM - GUOB Tech Day - OTN TOUR LA Brazil 2014
PPTX
Oracle Data redaction - GUOB - OTN TOUR LA - 2015
PPTX
OTN TOUR 2016 - DBA Commands and Concepts That Every Developer Should Know
PDF
Pluggable database tutorial 2
PDF
MySQL Best Practices - OTN LAD Tour
Getting to Know MySQL Enterprise Monitor
Mysql tech day_paris_ps_and_sys
Performance schema and sys schema
The MySQL SYS Schema
MySQL's Performance Schema, SYS Schema and Workbench Integration
The MySQL Performance Schema & New SYS Schema
MySQL sys schema deep dive
MySQL sys schema deep dive
Performance Schema for MySQL troubleshooting
Capturing, Analyzing, and Optimizing your SQL
Oracle Database 11g Product Family
 
What's next after Upgrade to 12c
In Memory Database In Action by Tanel Poder and Kerry Osborne
MySQL Troubleshooting with the Performance Schema
REST in Piece - Administration of an Oracle Cluster/Database using REST
Flex Cluster e Flex ASM - GUOB Tech Day - OTN TOUR LA Brazil 2014
Oracle Data redaction - GUOB - OTN TOUR LA - 2015
OTN TOUR 2016 - DBA Commands and Concepts That Every Developer Should Know
Pluggable database tutorial 2
MySQL Best Practices - OTN LAD Tour
Ad

Similar to Developing Information Schema Plugins (20)

ODP
3. writing MySql plugins for the information schema
PPT
Libmysqld Introduction
ODP
2. writing MySql plugins general
PDF
Drizzles Approach To Improving Performance Of The Server
ODP
1. MySql plugins
PDF
External Language Stored Procedures for MySQL
ODP
Drizzle plugins
ODP
Writing MySQL UDFs
PDF
Orasta500.c
PPT
Recovery of lost or corrupted inno db tables(mysql uc 2010)
ODP
Common schema my sql uc 2012
ODP
Common schema my sql uc 2012
DOC
Using MySQL Meta Data Effectively
PDF
Collaborate 2012 - Administering MySQL for Oracle DBAs
PDF
Collaborate 2012 - Administering MySQL for Oracle DBAs
ODP
HailDB: A NoSQL API Direct to InnoDB
PPT
Recovery of lost or corrupted inno db tables(mysql uc 2010)
PDF
Memcached Functions For My Sql Seemless Caching In My Sql
PPT
Direct SGA access without SQL
PDF
UNIT 3 SQL 10.pdf ORACEL DATABASE QUERY OPTIMIZATION
3. writing MySql plugins for the information schema
Libmysqld Introduction
2. writing MySql plugins general
Drizzles Approach To Improving Performance Of The Server
1. MySql plugins
External Language Stored Procedures for MySQL
Drizzle plugins
Writing MySQL UDFs
Orasta500.c
Recovery of lost or corrupted inno db tables(mysql uc 2010)
Common schema my sql uc 2012
Common schema my sql uc 2012
Using MySQL Meta Data Effectively
Collaborate 2012 - Administering MySQL for Oracle DBAs
Collaborate 2012 - Administering MySQL for Oracle DBAs
HailDB: A NoSQL API Direct to InnoDB
Recovery of lost or corrupted inno db tables(mysql uc 2010)
Memcached Functions For My Sql Seemless Caching In My Sql
Direct SGA access without SQL
UNIT 3 SQL 10.pdf ORACEL DATABASE QUERY OPTIMIZATION
Ad

Recently uploaded (20)

PPTX
A Presentation on Artificial Intelligence
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Machine learning based COVID-19 study performance prediction
PDF
Encapsulation theory and applications.pdf
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PPTX
Big Data Technologies - Introduction.pptx
PPTX
Understanding_Digital_Forensics_Presentation.pptx
A Presentation on Artificial Intelligence
CIFDAQ's Market Insight: SEC Turns Pro Crypto
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Building Integrated photovoltaic BIPV_UPV.pdf
The Rise and Fall of 3GPP – Time for a Sabbatical?
Chapter 3 Spatial Domain Image Processing.pdf
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Review of recent advances in non-invasive hemoglobin estimation
Mobile App Security Testing_ A Comprehensive Guide.pdf
Digital-Transformation-Roadmap-for-Companies.pptx
Per capita expenditure prediction using model stacking based on satellite ima...
Diabetes mellitus diagnosis method based random forest with bat algorithm
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
MYSQL Presentation for SQL database connectivity
Machine learning based COVID-19 study performance prediction
Encapsulation theory and applications.pdf
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
“AI and Expert System Decision Support & Business Intelligence Systems”
Big Data Technologies - Introduction.pptx
Understanding_Digital_Forensics_Presentation.pptx

Developing Information Schema Plugins

  • 1. Developing INFORMATION_SCHEMA Plugins Mark Leith Support Manager, Americas [email_address]
  • 2. Overview INFORMATION_SCHEMA overview Plugin Overview Developing a simple Hello World I_S table Building and installing Interacting with another program Interacting with another library
  • 3. INFORMATION_SCHEMA Specified within the SQL Standard (ISO/IEC 9075-11:2003) ‏ Virtual tables which give database metadata MySQL implements a subset of the Standard http://dev.mysql.com/doc/refman/5.1/en/information-schema.html http://www. xcdsql .org/MySQL/information_schema/5.1/MySQL_5_1_INFORMATION_SCHEMA.html
  • 4.  
  • 5. Plugins A means to load shared libraries in to a running MySQL instance Developed in C/C++ http://dev.mysql.com/doc/refman/5.1/en/plugin-api.html User Defined Functions Storage Engines Fulltext parsers Daemons INFORMATION_SCHEMA Tables!
  • 6. Located in include/mysql/plugin.h The Plugin Interface struct st_mysql_plugin { int type; /* the plugin type (a MYSQL_XXX_PLUGIN value) */ void *info; /* pointer to type-specific plugin descriptor */ const char *name; /* plugin name */ const char *author; /* plugin author (for SHOW PLUGINS) */ const char *descr; /* general descriptive text (for SHOW PLUGINS ) */ int license; /* the plugin license (PLUGIN_LICENSE_XXX) */ int (*init)( void *); /* the function to invoke when plugin is loaded */ int (*deinit)( void *) /* the function to invoke when plugin is unloaded */ unsigned int version; /* plugin version (for SHOW PLUGINS) */ struct st_mysql_show_var *status_vars; struct st_mysql_sys_var **system_vars; void * __reserved1; /* reserved for dependency checking */ };
  • 7. Plugin Types Define the plugin type (types listed below) Used within plugin functions such as plugin_foreach() MYSQL_UDF_PLUGIN 0 /* User-defined function */ MYSQL_STORAGE_ENGINE_PLUGIN 1 /* Storage Engine */ MYSQL_FTPARSER_PLUGIN 2 /* Full-text parser plugin */ MYSQL_DAEMON_PLUGIN 3 /* The daemon/raw plugin type */ MYSQL_INFORMATION_SCHEMA_PLUGIN 4 /* The I_S plugin type */ I_S plugins loaded within sql/sql_show.cc int schema_tables_add(THD *thd, List<LEX_STRING> *files, const char *wild) { … if (plugin_foreach(thd, add_schema_table, MYSQL_INFORMATION_SCHEMA_PLUGIN, &add_data)) DBUG_RETURN( 1 );
  • 8. Starting a HELLO_WORLD Table Must have MySQL source tree Create a new plugin/hello_world/hello_world.cc file plugin.h and mysql_priv.h includes required Add a prototype for schema_table_store_record() /* Required for schema_table_store_record() * / #include &quot;mysql_priv.h&quot; #include <mysql/plugin.h> bool schema_table_store_record(THD *thd, TABLE *table);
  • 9. Define the Table Structure Uses an array of ST_FIELD_INFO structs field_name Column name field_length Column length or display length field_type Column datatype value Not used within I_S plugins field_flags Set NULL / UNSIGNED attributes old_name Internal mapping for I_S tables to SHOW output open_method Open table using supplied method Last entry in the array is an end marker ST_FIELD_INFO hello_world_fields[]= { { &quot;HELLO&quot; , 10 , MYSQL_TYPE_STRING, 0 , 0 , &quot;Hello&quot; , 0 }, { &quot;WORLD&quot; , 10 , MYSQL_TYPE_STRING, 0 , 0 , &quot;World&quot; , 0 }, { 0 , 0 , MYSQL_TYPE_NULL, 0 , 0 , 0 , 0 } };
  • 10. A Closer Look Field_type Field_flags Open_method VARCHAR INT BIGINT DECIMAL DATETIME MYSQL_TYPE_STRING MYSQL_TYPE_LONG MYSQL_TYPE_LONGLONG MYSQL_TYPE_DECIMAL MYSQL_TYPE_DATETIME MY_I_S_MAYBE_NULL MY_I_S_UNSIGNED SKIP_OPEN_TABLE OPEN_FRM_ONLY OPEN_FULL_TABLE
  • 11. Define Function to Fill Table Called every time the table is accessed Fills the virtual table with data int fill_hello_world(THD *thd, TABLE_LIST *tables, COND *cond) { DBUG_ENTER( &quot;fill_hello_world_is_plugin&quot; ); CHARSET_INFO *scs= system_charset_info; TABLE *table= tables->table; int rc= 0 ; table->field[ 0 ]->store( &quot;Hello&quot; , strlen( &quot;Hello&quot; ), scs); table->field[ 1 ]->store( &quot;World&quot; , strlen( &quot;World&quot; ), scs); if (schema_table_store_record(thd, table)) rc= 1 ; DBUG_RETURN(rc); }
  • 12. Create the Init Function Runs when the plugin is installed/loaded ST_SCHEMA_TABLE is an internal table representation Points to the ST_FIELD_INFO struct / table definition Points to the function to fill the table Manages any other init needed for the plugin int hello_world_plugin_init( void *p) { DBUG_ENTER( &quot;init_hello_world_is_plugin&quot; ); ST_SCHEMA_TABLE *schema= (ST_SCHEMA_TABLE*) p; schema->fields_info= hello_world_fields; schema->fill_table= fill_hello_world; DBUG_RETURN( 0 ); }
  • 13. Create the Deinit Function Run when the plugin is unloaded Does nothing for HELLO_WORLD Use this function to do any clean up in your plugin int hello_world_plugin_deinit( void *p) { DBUG_ENTER( &quot;deinit_info_schema_example_plugin&quot; ); DBUG_RETURN( 0 ); }
  • 14. Create the Plugin Definition Info struct points to the interface version built against struct st_mysql_information_schema hello_world_plugin_info= { MYSQL_INFORMATION_SCHEMA_INTERFACE_VERSION }; mysql_declare_plugin(hello_world_plugin) { MYSQL_INFORMATION_SCHEMA_PLUGIN, &hello_world_plugin_info, /* Pointer to info struct */ &quot;HELLO_WORLD&quot; , /* Plugin Name (used in INSTALL PLUGIN) */ &quot;Mark Leith, MySQL AB&quot; , /* Plugin Author */ &quot;HELLO_WORLD example plugin&quot; , /* Plugin Description */ PLUGIN_LICENSE_GPL, /* _GPL, _BSD or _PROPRIETARY */ hello_world_plugin_init, /* Pointer to plugin init function */ hello_world_plugin_deinit, /* Pointer to plugin deinit function */ 0x0100 , /* 1.0 */ NULL , /* status variables */ NULL , /* system variables */ NULL /* config options */ } mysql_declare_plugin_end;
  • 15. Build the Plugin Define MYSQL_DYNAMIC_PLUGIN Make shared Include the sql and include source directories g++ -DMYSQL_DYNAMIC_PLUGIN -shared \ > -I/home/leithal/mysql/mysql-5.1/include \ > -I/home/leithal/mysql/mysql-5.1/sql \ > -o is_hello_world.so hello_world.cc ls -l total 16 -rw-r--r-- 1 leithal leithal 1712 2008-04-11 14:20 hello_world.cc -rwxr-xr-x 1 leithal leithal 8226 2008-04-11 14:27 is_hello_world.so cp is_hello_world.so /usr/local/mysql/lib/mysql/plugin
  • 16. Install and Use! mysql> INSTALL PLUGIN HELLO_WORLD SONAME 'is_hello_world.so'; Query OK, 0 rows affected (0.01 sec) mysql> USE INFORMATION_SCHEMA; Database changed mysql> SHOW TABLES LIKE 'HELL%'; +--------------------------------------+ | Tables_in_information_schema (HELL%) | +--------------------------------------+ | HELLO_WORLD | +--------------------------------------+ 1 row in set (0.00 sec) mysql> SELECT * FROM HELLO_WORLD; +-------+-------+ | HELLO | WORLD | +-------+-------+ | Hello | World | +-------+-------+ 1 row in set (0.00 sec)
  • 17. So We'll Do Something Interesting How's the disk space doing on my db server? root@achilles:~# df -h Filesystem Size Used Avail Use% Mounted on /dev/hda1 4.6G 3.5G 886M 81% / varrun 126M 84K 125M 1% /var/run varlock 126M 0 126M 0% /var/lock procbususb 10M 76K 10M 1% /proc/bus/usb udev 10M 76K 10M 1% /dev devshm 126M 0 126M 0% /dev/shm lrm 126M 18M 108M 14% /lib/modules/2.6.17-12-generic/volatile If I can get this in a table I can track it over time I can also report on it easily How about an I_S table with events to catalog?
  • 18. Create Template & Table Structure Add stdio.h,string.h and stdlib.h includes Mirror init, deinit and info functions Fill in the plugin declaration scruct appropriately Define the new table: ST_FIELD_INFO fs_info_schema_fields[]= { { &quot;FILESYSTEM&quot; , 120 , MYSQL_TYPE_STRING, 0 , 0 , &quot;Filesystem&quot; , 0 }, { &quot;SIZE&quot; , 8 , MYSQL_TYPE_LONGLONG, 0 , 0 , &quot;Mountpoint Size&quot; , 0 }, { &quot;USED&quot; , 8 , MYSQL_TYPE_LONGLONG, 0 , 0 , &quot;Used Space&quot; , 0 }, { &quot;AVAILABLE&quot; , 8 , MYSQL_TYPE_LONGLONG, 0 , 0 , &quot;Available Space&quot; , 0 }, { &quot;CAPACITY&quot; , 4 , MYSQL_TYPE_STRING, 0 , 0 , &quot;Percent Used&quot; , 0 }, { &quot;MOUNTED_ON&quot; , 120 , MYSQL_TYPE_STRING, 0 , 0 , &quot;Filesystem Mounted On&quot; , 0 }, { 0 , 0 , MYSQL_TYPE_STRING, 0 , 0 , 0 , 0 } };
  • 19. Running Other Programs Open with popen() and read in the results /* get filesystem information from df on linux like systems */ FILE *f; char buf[ 128 ]; char *c, *s, *size; const char delim[]= &quot; &quot; ; unsigned long long uli; double d; if ( NULL != (f= popen( &quot;/bin/df -h&quot; , &quot;r&quot; ))) { int ln= 0 ; while (!feof(f)) { fgets(buf, sizeof (buf), f); switch (ln++) {
  • 20. Tokenize and Push Results Can do this how you like /* skip the header line of df */ case 0 : break ; default : c= buf; /* hack to stop processing when falling off the end of output */ if (strchr(c, ' ' ) == NULL ) break ; /* Filesystem */ s= strtok(c, delim); table->field[ 0 ]->store(s, strlen(s), scs); /* Size */ s= strtok( NULL , delim); d= strtod(s, &size); uli= get_bytes(d, size); table->field[ 1 ]->store(uli, TRUE ); … .
  • 21. Store the Row and Finish up /* Use% */ s= strtok( NULL , delim); table->field[ 4 ]->store(s, strlen(s), scs); /* Mountpoint */ s= strtok( NULL , delim); stripnl(s); table->field[ 5 ]->store(s, strlen(s), scs); /* store the row */ if (schema_table_store_record(thd, table)) rc= 1 ; break ; } } pclose(f); } else rc= 1 ; DBUG_RETURN(rc); }
  • 22. Build, Install, Try.. Build as before and try it out! mysql> select * from file_system_mountpoints; +------------+------------+------------+-----------+----------+-----------------------------------------+ | FILESYSTEM | SIZE | USED | AVAILABLE | CAPACITY | MOUNTED_ON | +------------+------------+------------+-----------+----------+-----------------------------------------+ | /dev/hda1 | 4939212390 | 3758096384 | 930086912 | 81% | / | | varrun | 132120576 | 86016 | 131072000 | 1% | /var/run | | varlock | 132120576 | 0 | 132120576 | 0% | /var/lock | | procbususb | 10485760 | 77824 | 10485760 | 1% | /proc/bus/usb | | udev | 10485760 | 77824 | 10485760 | 1% | /dev | | devshm | 132120576 | 0 | 132120576 | 0% | /dev/shm | | lrm | 132120576 | 18874368 | 113246208 | 14% | /lib/modules/2.6.17-12-generic/volatile | +------------+------------+------------+-----------+----------+-----------------------------------------+ 7 rows in set (0.06 sec) mysql> desc file_system_mountpoints; +------------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +------------+--------------+------+-----+---------+-------+ | FILESYSTEM | varchar(120) | NO | | | | | SIZE | bigint(11) | NO | | 0 | | | USED | bigint(11) | NO | | 0 | | | AVAILABLE | bigint(11) | NO | | 0 | | | CAPACITY | varchar(4) | NO | | | | | MOUNTED_ON | varchar(120) | NO | | | | +------------+--------------+------+-----+---------+-------+ 6 rows in set (0.01 sec)
  • 23. Now Let's Use another Library The SIGAR library from Hyperic Lets us do cross platform OS stats monitoring Released under GPL Again, start with the same base functions etc. as before
  • 24. Include Lib Header, Use Lib in Fill sigar_t *t; sigar_open(&t); sigar_file_system_list_t fslist; sigar_file_system_list_get(t, &fslist); for (uint i = 0 ; i < fslist.number; i++) { sigar_file_system_t fs = fslist.data[i]; sigar_file_system_usage_t fsusage; rc= sigar_file_system_usage_get(t, fs.dir_name, &fsusage); if (fs.type == 2 || fs.type == 3 ) { table->field[ 0 ]->store(fs.dir_name, strlen(fs.dir_name), scs); table->field[ 1 ]->store(fsusage.total, TRUE ); table->field[ 2 ]->store(fsusage.used, TRUE ); table->field[ 3 ]->store(fsusage.free, TRUE ); table->field[ 4 ]->store(fsusage.files, TRUE ); if (schema_table_store_record(thd, table)) rc= 1 ; } } sigar_file_system_list_destroy(t, &fslist); sigar_close(t);
  • 25. Building and Including the Library -lsigar links the library Use -rpath to pass in library location This is for runtime Can also use LD_LIBRARY_PATH -L (linker) and -I (include) paths -L points to the directory with the built library g++ -DMYSQL_DYNAMIC_PLUGIN -Wall -shared \ -lsigar -Wl,-rpath -Wl,/home/leithal/os_stats_info_schema/sigar/ \ -L/home/leithal/os_stats_info_schema/sigar/ \ -I/home/leithal/os_stats_info_schema/sigar/include/ \ -I/home/leithal/mysql/mysql-5.1/include \ -I/home/leithal/mysql/mysql-5.1/sql \ -o os_stats_info_schema.so os_stats_info_schema.cc
  • 26. Try it out! mysql> INSTALL PLUGIN os_disk_usage SONAME 'os_stats_info_schema.so'; Query OK, 0 rows affected (0.00 sec) mysql> SHOW TABLES LIKE 'OS%'; +------------------------------------+ | Tables_in_information_schema (OS%) | +------------------------------------+ | OS_DISK_USAGE | +------------------------------------+ 1 row in set (0.00 sec) mysql> SELECT * FROM OS_DISK_USAGE; +------------+---------+---------+---------+--------+ | FILESYSTEM | SIZE | USED | FREE | FILES | +------------+---------+---------+---------+--------+ | / | 4799024 | 3656800 | 1142224 | 610432 | +------------+---------+---------+---------+--------+ 1 row in set (0.00 sec)
  • 27. Resources and Questions! http://www.markleith.co.uk/?p=18 http://rpbouman.blogspot.com/2008/02/mysql-information-schema-plugins-best.html http://rpbouman.blogspot.com/2008/02/reporting-mysql-internals-with.html (Great resource for monitoring server internals) Questions?