Your SlideShare is downloading. ×
0
Developing INFORMATION_SCHEMA Plugins <ul><li>Mark Leith </li></ul><ul><li>Support Manager, Americas </li></ul><ul><li>[em...
Overview <ul><li>INFORMATION_SCHEMA overview </li></ul><ul><li>Plugin Overview </li></ul><ul><li>Developing a simple Hello...
INFORMATION_SCHEMA <ul><li>Specified within the SQL Standard  </li></ul><ul><li>(ISO/IEC 9075-11:2003) ‏ </li></ul><ul><li...
 
Plugins <ul><li>A means to load shared libraries in to a running MySQL instance </li></ul><ul><li>Developed in C/C++ </li>...
<ul><li>Located in include/mysql/plugin.h </li></ul>The Plugin Interface struct  st_mysql_plugin {  int  type;  /* the plu...
Plugin Types <ul><li>Define the plugin type (types listed below) </li></ul><ul><li>Used within plugin functions such as pl...
Starting a HELLO_WORLD Table <ul><li>Must have MySQL source tree </li></ul><ul><li>Create a new plugin/hello_world/hello_w...
Define the Table Structure <ul><li>Uses an array of  ST_FIELD_INFO  structs </li></ul><ul><ul><li>field_name Column name <...
A Closer Look <ul><li>Field_type </li></ul><ul><li>Field_flags </li></ul><ul><li>Open_method </li></ul>VARCHAR INT BIGINT ...
Define Function to Fill Table <ul><li>Called every time the table is accessed </li></ul><ul><li>Fills the virtual table wi...
Create the Init Function <ul><li>Runs when the plugin is installed/loaded </li></ul><ul><li>ST_SCHEMA_TABLE  is an interna...
Create the Deinit Function <ul><li>Run when the plugin is unloaded </li></ul><ul><li>Does nothing for HELLO_WORLD </li></u...
Create the Plugin Definition  <ul><li>Info struct points to the interface version built against </li></ul><ul><ul><ul><li>...
Build the Plugin <ul><li>Define MYSQL_DYNAMIC_PLUGIN </li></ul><ul><li>Make shared </li></ul><ul><li>Include the  sql  and...
Install and Use! mysql> INSTALL PLUGIN HELLO_WORLD SONAME 'is_hello_world.so'; Query OK, 0 rows affected (0.01 sec) mysql>...
So We'll Do Something Interesting <ul><li>How's the disk space doing on my db server? </li></ul>root@achilles:~# df -h Fil...
Create Template & Table Structure  <ul><li>Add stdio.h,string.h and stdlib.h includes </li></ul><ul><li>Mirror init, deini...
Running Other Programs <ul><li>Open with popen() and read in the results </li></ul><ul><ul><ul><li>/* get filesystem infor...
Tokenize and Push Results <ul><li>Can do this how you like </li></ul><ul><ul><ul><li>/* skip the header line of df */ </li...
Store the Row and Finish up <ul><ul><ul><ul><li>/* Use% */ </li></ul></ul></ul></ul><ul><ul><ul><ul><li>s= strtok( NULL , ...
Build, Install, Try.. <ul><li>Build as before and try it out! </li></ul>mysql> select * from file_system_mountpoints; +---...
Now Let's Use another Library <ul><li>The SIGAR library from Hyperic </li></ul><ul><ul><li>Lets us do cross platform OS st...
Include Lib Header, Use Lib in Fill <ul><ul><ul><ul><ul><li>sigar_t *t; </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul>...
Building and Including the Library <ul><li>-lsigar links the library </li></ul><ul><li>Use -rpath to pass in library locat...
Try it out! <ul><ul><li>mysql> INSTALL PLUGIN os_disk_usage SONAME 'os_stats_info_schema.so'; </li></ul></ul><ul><ul><li>Q...
Resources and Questions! <ul><li>http://www.markleith.co.uk/?p=18 </li></ul><ul><li>http://rpbouman.blogspot.com/2008/02/m...
Upcoming SlideShare
Loading in...5
×

Developing Information Schema Plugins

3,774

Published on

In-depth look at developing INFORMATION_SCHEMA plugins for MySQL.

Published in: Technology, Art & Photos
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
3,774
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
34
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "Developing Information Schema Plugins"

  1. 1. Developing INFORMATION_SCHEMA Plugins <ul><li>Mark Leith </li></ul><ul><li>Support Manager, Americas </li></ul><ul><li>[email_address] </li></ul>
  2. 2. Overview <ul><li>INFORMATION_SCHEMA overview </li></ul><ul><li>Plugin Overview </li></ul><ul><li>Developing a simple Hello World I_S table </li></ul><ul><li>Building and installing </li></ul><ul><li>Interacting with another program </li></ul><ul><li>Interacting with another library </li></ul>
  3. 3. INFORMATION_SCHEMA <ul><li>Specified within the SQL Standard </li></ul><ul><li>(ISO/IEC 9075-11:2003) ‏ </li></ul><ul><li>Virtual tables which give database metadata </li></ul><ul><li>MySQL implements a subset of the Standard </li></ul><ul><li>http://dev.mysql.com/doc/refman/5.1/en/information-schema.html </li></ul><ul><li>http://www. xcdsql .org/MySQL/information_schema/5.1/MySQL_5_1_INFORMATION_SCHEMA.html </li></ul>
  4. 5. Plugins <ul><li>A means to load shared libraries in to a running MySQL instance </li></ul><ul><li>Developed in C/C++ </li></ul><ul><li>http://dev.mysql.com/doc/refman/5.1/en/plugin-api.html </li></ul><ul><ul><li>User Defined Functions </li></ul></ul><ul><ul><li>Storage Engines </li></ul></ul><ul><ul><li>Fulltext parsers </li></ul></ul><ul><ul><li>Daemons </li></ul></ul><ul><ul><li>INFORMATION_SCHEMA Tables! </li></ul></ul>
  5. 6. <ul><li>Located in include/mysql/plugin.h </li></ul>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 */ };
  6. 7. Plugin Types <ul><li>Define the plugin type (types listed below) </li></ul><ul><li>Used within plugin functions such as plugin_foreach() </li></ul>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 */ <ul><li>I_S plugins loaded within sql/sql_show.cc </li></ul>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 );
  7. 8. Starting a HELLO_WORLD Table <ul><li>Must have MySQL source tree </li></ul><ul><li>Create a new plugin/hello_world/hello_world.cc file </li></ul><ul><li>plugin.h and mysql_priv.h includes required </li></ul><ul><li>Add a prototype for schema_table_store_record() </li></ul><ul><ul><ul><li>/* Required for schema_table_store_record() * </li></ul></ul></ul><ul><ul><ul><li>/ #include &quot;mysql_priv.h&quot; </li></ul></ul></ul><ul><ul><ul><li>#include <mysql/plugin.h> </li></ul></ul></ul><ul><ul><ul><li>bool schema_table_store_record(THD *thd, TABLE *table); </li></ul></ul></ul>
  8. 9. Define the Table Structure <ul><li>Uses an array of ST_FIELD_INFO structs </li></ul><ul><ul><li>field_name Column name </li></ul></ul><ul><ul><li>field_length Column length or display length </li></ul></ul><ul><ul><li>field_type Column datatype </li></ul></ul><ul><ul><li>value Not used within I_S plugins </li></ul></ul><ul><ul><li>field_flags Set NULL / UNSIGNED attributes </li></ul></ul><ul><ul><li>old_name Internal mapping for I_S tables to SHOW output </li></ul></ul><ul><ul><li>open_method Open table using supplied method </li></ul></ul><ul><li>Last entry in the array is an end marker </li></ul><ul><ul><ul><li>ST_FIELD_INFO hello_world_fields[]= </li></ul></ul></ul><ul><ul><ul><li>{ </li></ul></ul></ul><ul><ul><ul><li>{ &quot;HELLO&quot; , 10 , MYSQL_TYPE_STRING, 0 , 0 , &quot;Hello&quot; , 0 }, </li></ul></ul></ul><ul><ul><ul><li>{ &quot;WORLD&quot; , 10 , MYSQL_TYPE_STRING, 0 , 0 , &quot;World&quot; , 0 }, </li></ul></ul></ul><ul><ul><ul><li>{ 0 , 0 , MYSQL_TYPE_NULL, 0 , 0 , 0 , 0 } </li></ul></ul></ul><ul><ul><ul><li>}; </li></ul></ul></ul>
  9. 10. A Closer Look <ul><li>Field_type </li></ul><ul><li>Field_flags </li></ul><ul><li>Open_method </li></ul>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
  10. 11. Define Function to Fill Table <ul><li>Called every time the table is accessed </li></ul><ul><li>Fills the virtual table with data </li></ul><ul><ul><ul><li>int fill_hello_world(THD *thd, TABLE_LIST *tables, COND *cond) </li></ul></ul></ul><ul><ul><ul><li>{ </li></ul></ul></ul><ul><ul><ul><li>DBUG_ENTER( &quot;fill_hello_world_is_plugin&quot; ); </li></ul></ul></ul><ul><ul><ul><li>CHARSET_INFO *scs= system_charset_info; </li></ul></ul></ul><ul><ul><ul><li>TABLE *table= tables->table; </li></ul></ul></ul><ul><ul><ul><li>int rc= 0 ; </li></ul></ul></ul><ul><ul><ul><li>table->field[ 0 ]->store( &quot;Hello&quot; , strlen( &quot;Hello&quot; ), scs); </li></ul></ul></ul><ul><ul><ul><li>table->field[ 1 ]->store( &quot;World&quot; , strlen( &quot;World&quot; ), scs); </li></ul></ul></ul><ul><ul><ul><li>if (schema_table_store_record(thd, table)) </li></ul></ul></ul><ul><ul><ul><li>rc= 1 ; </li></ul></ul></ul><ul><ul><ul><li>DBUG_RETURN(rc); </li></ul></ul></ul><ul><ul><ul><li>} </li></ul></ul></ul>
  11. 12. Create the Init Function <ul><li>Runs when the plugin is installed/loaded </li></ul><ul><li>ST_SCHEMA_TABLE is an internal table representation </li></ul><ul><li>Points to the ST_FIELD_INFO struct / table definition </li></ul><ul><li>Points to the function to fill the table </li></ul><ul><li>Manages any other init needed for the plugin </li></ul><ul><ul><ul><li>int hello_world_plugin_init( void *p) </li></ul></ul></ul><ul><ul><ul><li>{ </li></ul></ul></ul><ul><ul><ul><li>DBUG_ENTER( &quot;init_hello_world_is_plugin&quot; ); </li></ul></ul></ul><ul><ul><ul><li>ST_SCHEMA_TABLE *schema= (ST_SCHEMA_TABLE*) p; </li></ul></ul></ul><ul><ul><ul><li>schema->fields_info= hello_world_fields; </li></ul></ul></ul><ul><ul><ul><li>schema->fill_table= fill_hello_world; </li></ul></ul></ul><ul><ul><ul><li>DBUG_RETURN( 0 ); </li></ul></ul></ul><ul><ul><ul><li>} </li></ul></ul></ul>
  12. 13. Create the Deinit Function <ul><li>Run when the plugin is unloaded </li></ul><ul><li>Does nothing for HELLO_WORLD </li></ul><ul><li>Use this function to do any clean up in your plugin </li></ul><ul><ul><ul><li>int hello_world_plugin_deinit( void *p) </li></ul></ul></ul><ul><ul><ul><li>{ </li></ul></ul></ul><ul><ul><ul><li>DBUG_ENTER( &quot;deinit_info_schema_example_plugin&quot; ); </li></ul></ul></ul><ul><ul><ul><li>DBUG_RETURN( 0 ); </li></ul></ul></ul><ul><ul><ul><li>} </li></ul></ul></ul>
  13. 14. Create the Plugin Definition <ul><li>Info struct points to the interface version built against </li></ul><ul><ul><ul><li>struct st_mysql_information_schema hello_world_plugin_info= </li></ul></ul></ul><ul><ul><ul><li>{ MYSQL_INFORMATION_SCHEMA_INTERFACE_VERSION }; </li></ul></ul></ul><ul><ul><ul><li>mysql_declare_plugin(hello_world_plugin) </li></ul></ul></ul><ul><ul><ul><li>{ </li></ul></ul></ul><ul><ul><ul><li>MYSQL_INFORMATION_SCHEMA_PLUGIN, </li></ul></ul></ul><ul><ul><ul><li>&hello_world_plugin_info, /* Pointer to info struct */ </li></ul></ul></ul><ul><ul><ul><li>&quot;HELLO_WORLD&quot; , /* Plugin Name (used in INSTALL PLUGIN) */ </li></ul></ul></ul><ul><ul><ul><li>&quot;Mark Leith, MySQL AB&quot; , /* Plugin Author */ </li></ul></ul></ul><ul><ul><ul><li>&quot;HELLO_WORLD example plugin&quot; , /* Plugin Description */ </li></ul></ul></ul><ul><ul><ul><li>PLUGIN_LICENSE_GPL, /* _GPL, _BSD or _PROPRIETARY */ </li></ul></ul></ul><ul><ul><ul><li>hello_world_plugin_init, /* Pointer to plugin init function */ </li></ul></ul></ul><ul><ul><ul><li>hello_world_plugin_deinit, /* Pointer to plugin deinit function */ </li></ul></ul></ul><ul><ul><ul><li>0x0100 , /* 1.0 */ </li></ul></ul></ul><ul><ul><ul><li>NULL , /* status variables */ </li></ul></ul></ul><ul><ul><ul><li>NULL , /* system variables */ </li></ul></ul></ul><ul><ul><ul><li>NULL /* config options */ </li></ul></ul></ul><ul><ul><ul><li>} </li></ul></ul></ul><ul><ul><ul><li>mysql_declare_plugin_end; </li></ul></ul></ul>
  14. 15. Build the Plugin <ul><li>Define MYSQL_DYNAMIC_PLUGIN </li></ul><ul><li>Make shared </li></ul><ul><li>Include the sql and include source directories </li></ul>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
  15. 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)
  16. 17. So We'll Do Something Interesting <ul><li>How's the disk space doing on my db server? </li></ul>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 <ul><li>If I can get this in a table I can track it over time </li></ul><ul><li>I can also report on it easily </li></ul><ul><li>How about an I_S table with events to catalog? </li></ul>
  17. 18. Create Template & Table Structure <ul><li>Add stdio.h,string.h and stdlib.h includes </li></ul><ul><li>Mirror init, deinit and info functions </li></ul><ul><li>Fill in the plugin declaration scruct appropriately </li></ul><ul><li>Define the new table: </li></ul><ul><ul><ul><li>ST_FIELD_INFO fs_info_schema_fields[]= </li></ul></ul></ul><ul><ul><ul><li>{ </li></ul></ul></ul><ul><ul><ul><li>{ &quot;FILESYSTEM&quot; , 120 , MYSQL_TYPE_STRING, 0 , 0 , &quot;Filesystem&quot; , 0 }, </li></ul></ul></ul><ul><ul><ul><li>{ &quot;SIZE&quot; , 8 , MYSQL_TYPE_LONGLONG, 0 , 0 , &quot;Mountpoint Size&quot; , 0 }, </li></ul></ul></ul><ul><ul><ul><li>{ &quot;USED&quot; , 8 , MYSQL_TYPE_LONGLONG, 0 , 0 , &quot;Used Space&quot; , 0 }, </li></ul></ul></ul><ul><ul><ul><li>{ &quot;AVAILABLE&quot; , 8 , MYSQL_TYPE_LONGLONG, 0 , 0 , &quot;Available Space&quot; , 0 }, </li></ul></ul></ul><ul><ul><ul><li>{ &quot;CAPACITY&quot; , 4 , MYSQL_TYPE_STRING, 0 , 0 , &quot;Percent Used&quot; , 0 }, </li></ul></ul></ul><ul><ul><ul><li>{ &quot;MOUNTED_ON&quot; , 120 , MYSQL_TYPE_STRING, 0 , 0 , &quot;Filesystem Mounted On&quot; , 0 }, </li></ul></ul></ul><ul><ul><ul><li>{ 0 , 0 , MYSQL_TYPE_STRING, 0 , 0 , 0 , 0 } </li></ul></ul></ul><ul><ul><ul><li>}; </li></ul></ul></ul>
  18. 19. Running Other Programs <ul><li>Open with popen() and read in the results </li></ul><ul><ul><ul><li>/* get filesystem information from df on linux like systems */ </li></ul></ul></ul><ul><ul><ul><li>FILE *f; </li></ul></ul></ul><ul><ul><ul><li>char buf[ 128 ]; </li></ul></ul></ul><ul><ul><ul><li>char *c, *s, *size; </li></ul></ul></ul><ul><ul><ul><li>const char delim[]= &quot; &quot; ; </li></ul></ul></ul><ul><ul><ul><li>unsigned long long uli; </li></ul></ul></ul><ul><ul><ul><li>double d; </li></ul></ul></ul><ul><ul><ul><li>if ( NULL != (f= popen( &quot;/bin/df -h&quot; , &quot;r&quot; ))) </li></ul></ul></ul><ul><ul><ul><li>{ </li></ul></ul></ul><ul><ul><ul><li>int ln= 0 ; </li></ul></ul></ul><ul><ul><ul><li>while (!feof(f)) </li></ul></ul></ul><ul><ul><ul><li>{ </li></ul></ul></ul><ul><ul><ul><li>fgets(buf, sizeof (buf), f); </li></ul></ul></ul><ul><ul><ul><li>switch (ln++) </li></ul></ul></ul><ul><ul><ul><li>{ </li></ul></ul></ul>
  19. 20. Tokenize and Push Results <ul><li>Can do this how you like </li></ul><ul><ul><ul><li>/* skip the header line of df */ </li></ul></ul></ul><ul><ul><ul><li>case 0 : break ; </li></ul></ul></ul><ul><ul><ul><li>default : </li></ul></ul></ul><ul><ul><ul><li>c= buf; </li></ul></ul></ul><ul><ul><ul><li>/* hack to stop processing when falling off the end of output */ </li></ul></ul></ul><ul><ul><ul><li>if (strchr(c, ' ' ) == NULL ) </li></ul></ul></ul><ul><ul><ul><li>break ; </li></ul></ul></ul><ul><ul><ul><li>/* Filesystem */ </li></ul></ul></ul><ul><ul><ul><li>s= strtok(c, delim); </li></ul></ul></ul><ul><ul><ul><li>table->field[ 0 ]->store(s, strlen(s), scs); </li></ul></ul></ul><ul><ul><ul><li>/* Size */ </li></ul></ul></ul><ul><ul><ul><li>s= strtok( NULL , delim); </li></ul></ul></ul><ul><ul><ul><li>d= strtod(s, &size); </li></ul></ul></ul><ul><ul><ul><li>uli= get_bytes(d, size); </li></ul></ul></ul><ul><ul><ul><li>table->field[ 1 ]->store(uli, TRUE ); </li></ul></ul></ul><ul><ul><ul><li>… . </li></ul></ul></ul>
  20. 21. Store the Row and Finish up <ul><ul><ul><ul><li>/* Use% */ </li></ul></ul></ul></ul><ul><ul><ul><ul><li>s= strtok( NULL , delim); </li></ul></ul></ul></ul><ul><ul><ul><ul><li>table->field[ 4 ]->store(s, strlen(s), scs); </li></ul></ul></ul></ul><ul><ul><ul><ul><li>/* Mountpoint */ </li></ul></ul></ul></ul><ul><ul><ul><ul><li>s= strtok( NULL , delim); </li></ul></ul></ul></ul><ul><ul><ul><ul><li>stripnl(s); </li></ul></ul></ul></ul><ul><ul><ul><ul><li>table->field[ 5 ]->store(s, strlen(s), scs); </li></ul></ul></ul></ul><ul><ul><ul><ul><li>/* store the row */ </li></ul></ul></ul></ul><ul><ul><ul><ul><li>if (schema_table_store_record(thd, table)) </li></ul></ul></ul></ul><ul><ul><ul><ul><li>rc= 1 ; </li></ul></ul></ul></ul><ul><ul><ul><ul><li>break ; </li></ul></ul></ul></ul><ul><ul><ul><ul><li>} </li></ul></ul></ul></ul><ul><ul><ul><ul><li>} </li></ul></ul></ul></ul><ul><ul><ul><ul><li>pclose(f); </li></ul></ul></ul></ul><ul><ul><ul><ul><li>} </li></ul></ul></ul></ul><ul><ul><ul><ul><li>else </li></ul></ul></ul></ul><ul><ul><ul><ul><li>rc= 1 ; </li></ul></ul></ul></ul><ul><ul><ul><ul><li>DBUG_RETURN(rc); </li></ul></ul></ul></ul><ul><ul><ul><ul><li>} </li></ul></ul></ul></ul>
  21. 22. Build, Install, Try.. <ul><li>Build as before and try it out! </li></ul>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)
  22. 23. Now Let's Use another Library <ul><li>The SIGAR library from Hyperic </li></ul><ul><ul><li>Lets us do cross platform OS stats monitoring </li></ul></ul><ul><ul><li>Released under GPL </li></ul></ul><ul><li>Again, start with the same base functions etc. as before </li></ul>
  23. 24. Include Lib Header, Use Lib in Fill <ul><ul><ul><ul><ul><li>sigar_t *t; </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>sigar_open(&t); </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>sigar_file_system_list_t fslist; </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>sigar_file_system_list_get(t, &fslist); </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>for (uint i = 0 ; i < fslist.number; i++) </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>{ </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>sigar_file_system_t fs = fslist.data[i]; </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>sigar_file_system_usage_t fsusage; </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>rc= sigar_file_system_usage_get(t, fs.dir_name, &fsusage); </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>if (fs.type == 2 || fs.type == 3 ) </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>{ </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>table->field[ 0 ]->store(fs.dir_name, strlen(fs.dir_name), scs); </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>table->field[ 1 ]->store(fsusage.total, TRUE ); </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>table->field[ 2 ]->store(fsusage.used, TRUE ); </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>table->field[ 3 ]->store(fsusage.free, TRUE ); </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>table->field[ 4 ]->store(fsusage.files, TRUE ); </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>if (schema_table_store_record(thd, table)) </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>rc= 1 ; </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>} </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>} </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>sigar_file_system_list_destroy(t, &fslist); </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>sigar_close(t); </li></ul></ul></ul></ul></ul>
  24. 25. Building and Including the Library <ul><li>-lsigar links the library </li></ul><ul><li>Use -rpath to pass in library location </li></ul><ul><ul><li>This is for runtime </li></ul></ul><ul><ul><li>Can also use LD_LIBRARY_PATH </li></ul></ul><ul><li>-L (linker) and -I (include) paths </li></ul><ul><ul><li>-L points to the directory with the built library </li></ul></ul><ul><ul><ul><li>g++ -DMYSQL_DYNAMIC_PLUGIN -Wall -shared </li></ul></ul></ul><ul><ul><ul><li>-lsigar -Wl,-rpath -Wl,/home/leithal/os_stats_info_schema/sigar/ </li></ul></ul></ul><ul><ul><ul><li>-L/home/leithal/os_stats_info_schema/sigar/ </li></ul></ul></ul><ul><ul><ul><li>-I/home/leithal/os_stats_info_schema/sigar/include/ </li></ul></ul></ul><ul><ul><ul><li>-I/home/leithal/mysql/mysql-5.1/include </li></ul></ul></ul><ul><ul><ul><li>-I/home/leithal/mysql/mysql-5.1/sql </li></ul></ul></ul><ul><ul><ul><li>-o os_stats_info_schema.so os_stats_info_schema.cc </li></ul></ul></ul>
  25. 26. Try it out! <ul><ul><li>mysql> INSTALL PLUGIN os_disk_usage SONAME 'os_stats_info_schema.so'; </li></ul></ul><ul><ul><li>Query OK, 0 rows affected (0.00 sec) </li></ul></ul><ul><ul><li>mysql> SHOW TABLES LIKE 'OS%'; </li></ul></ul><ul><ul><li>+------------------------------------+ </li></ul></ul><ul><ul><li>| Tables_in_information_schema (OS%) | </li></ul></ul><ul><ul><li>+------------------------------------+ </li></ul></ul><ul><ul><li>| OS_DISK_USAGE | </li></ul></ul><ul><ul><li>+------------------------------------+ </li></ul></ul><ul><ul><li>1 row in set (0.00 sec) </li></ul></ul><ul><ul><li>mysql> SELECT * FROM OS_DISK_USAGE; </li></ul></ul><ul><ul><li>+------------+---------+---------+---------+--------+ </li></ul></ul><ul><ul><li>| FILESYSTEM | SIZE | USED | FREE | FILES | </li></ul></ul><ul><ul><li>+------------+---------+---------+---------+--------+ </li></ul></ul><ul><ul><li>| / | 4799024 | 3656800 | 1142224 | 610432 | </li></ul></ul><ul><ul><li>+------------+---------+---------+---------+--------+ </li></ul></ul><ul><ul><li>1 row in set (0.00 sec) </li></ul></ul>
  26. 27. Resources and Questions! <ul><li>http://www.markleith.co.uk/?p=18 </li></ul><ul><li>http://rpbouman.blogspot.com/2008/02/mysql-information-schema-plugins-best.html </li></ul><ul><li>http://rpbouman.blogspot.com/2008/02/reporting-mysql-internals-with.html (Great resource for monitoring server internals) </li></ul><ul><li>Questions? </li></ul>
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×