[转帖]在mysql 的information_schema中 添加 DISKUSAGE表  _MySQL, Oracle及数据库讨论区_Weblogic技术|Tuxedo技术|中间件技术|Oracle论坛|JAVA论坛|Linux/Unix技术|hadoop论坛_联动北方技术论坛  
网站首页 | 关于我们 | 服务中心 | 经验交流 | 公司荣誉 | 成功案例 | 合作伙伴 | 联系我们 |
联动北方-国内领先的云技术服务提供商
»  游客             当前位置:  论坛首页 »  自由讨论区 »  MySQL, Oracle及数据库讨论区 »
总帖数
1
每页帖数
101/1页1
返回列表
0
发起投票  发起投票 发新帖子
查看: 2111 | 回复: 0   主题: [转帖]在mysql 的information_schema中 添加 DISKUSAGE表          下一篇 
冰释清风
注册用户
等级:新兵
经验:71
发帖:7
精华:0
注册:2016-9-26
状态:离线
发送短消息息给冰释清风 加好友    发送短消息息给冰释清风 发消息
发表于: IP:您无权察看 2018-8-28 16:43:27 | [全部帖] [楼主帖] 楼主

修改 handler.h 中的 enum_schema_tables 

SCH_COLUMNS,
SCH_COLUMN_PRIVILEGES,
/* BEGIN CAB MODIFICATION */
SCH_DISKUSAGE,
/* END CAB MODIFICATION */
SCH_ENGINES,


修改 sql_parse.cc 中的 prepare_schema_table

int prepare_schema_table(THD *thd, LEX *lex, Table_ident *table_ident,
enum enum_schema_tables schema_table_idx)
{
      ...
      DBUG_ENTER("prepare_schema_table");
      switch (schema_table_idx) {
            /* BEGIN CAB MODIFICATION */
            /* Reason for Modification: */
            /* Add case statement for the new SHOW DISK_USAGE view. */
            case SCH_DISKUSAGE:
            /* END CAB MODIFICATION */
            case SCH_SCHEMATA:
            #if defined(DONT_ALLOW_SHOW_COMMANDS)
            my_message(ER_NOT_ALLOWED_COMMAND,

修改 sql_show.cc , 在 ST_FIELD_INFO schema_privileges_fields_info 后面添加

/* BEGIN CAB MODIFICATION */

/* Reason for Modification: */

/* This section adds the code to call the new SHOW DISK_USAGE command. */

ST_FIELD_INFO disk_usage_fields_info[]=

{

{"DATABASE", 40, MYSQL_TYPE_STRING, 0, 0, NULL, SKIP_OPEN_TABLE},

{"Size_in_bytes", 21 , MYSQL_TYPE_LONG, 0, 0, NULL, SKIP_OPEN_TABLE },

{0, 0, MYSQL_TYPE_STRING, 0, 0, 0, SKIP_OPEN_TABLE}

};

/* END CAB MODIFICATION */

修改 sql_show.cc sql_show.cc 中的 schema_tables

ST_SCHEMA_TABLE schema_tables[]=

{

...

{"ENGINES", engines_fields_info, create_schema_table,

fill_schema_engines, make_old_format, 0, -1, -1, 0, 0},

/* BEGIN CAB MODIFICATION */

/* Reason for Modification: */

/* This section adds the code to call the new SHOW DISK_USAGE command. */

{"DISKUSAGE", disk_usage_fields_info, create_schema_table,

fill_disk_usage, make_old_format, 0, -1, -1, 0, 0},

/* END CAB MODIFICATION */

#ifdef HAVE_EVENT_SCHEDULER

{"EVENTS", events_fields_info, create_schema_table,

fill_schema_events, make_old_format, 0, -1, -1, 0, 0},

修改 sql_show.cc , 在schema_tables之前的地方添加实现方法



/* BEGIN CAB MODIFICATION */

/* Reason for Modification: */

/* Add code to fill the output for the new SHOW DISK_USAGE view. */

int fill_disk_usage(THD *thd, TABLE_LIST *tables, Item *cond)
            {
                  TABLE *table= tables->table;
                  CHARSET_INFO *scs= system_charset_info;
                  List<Item> field_list;
                  List<LEX_STRING> dbs;
                  LEX_STRING *db_name;
                  char *path;
                  MY_DIR *dirp;
                  FILEINFO *file;
                  longlong fsizes = 0;
                  longlong lsizes = 0;
                  DBUG_ENTER("fill_disk_usage");
                  find_files_result res = find_files(thd, &dbs, 0, mysql_data_home,0,1);
                  if (res != FIND_FILES_OK)
                  DBUG_RETURN(1);
                  List_iterator_fast<LEX_STRING> it_dbs(dbs);
                  path = (char *)my_malloc(PATH_MAX, MYF(MY_ZEROFILL));
                  dirp = my_dir(mysql_data_home, MYF(MY_WANT_STAT));
                  fsizes = 0;
                  for (int i = 0; i < (int)dirp->number_off_files; i++)
                  {
                        file = dirp->dir_entry + i;
                        if (strncasecmp(file->name, "ibdata", 6) == 0)
                        fsizes = fsizes + file->mystat->st_size;
                        else if (strncasecmp(file->name, "ib", 2) == 0)
                        lsizes = lsizes + file->mystat->st_size;
                  }
                  /* send InnoDB data to client */
                  table->field[0]->store("InnoDB TableSpace",
                  strlen("InnoDB TableSpace"), scs);
                  table->field[1]->store((longlong)fsizes, TRUE);
                  if (schema_table_store_record(thd, table))
                  DBUG_RETURN(1);
                  table->field[0]->store("InnoDB Logs", strlen("InnoDB Logs"), scs);
                  table->field[1]->store((longlong)lsizes, TRUE);
                  if (schema_table_store_record(thd, table))
                  DBUG_RETURN(1);
                  /* now send database name and sizes of the databases */
                  while ((db_name = it_dbs++))
                  {
                        fsizes = 0;
                        strcpy(path, mysql_data_home);
                        strcat(path, "/");
                        strcat(path, db_name->str);
                        dirp = my_dir(path, MYF(MY_WANT_STAT));
                        for (int i = 0; i < (int)dirp->number_off_files; i++)
                        {
                              file = dirp->dir_entry + i;
                              fsizes = fsizes + file->mystat->st_size;
                        }
                        restore_record(table, s->default_values);
                        table->field[0]->store(db_name->str, db_name->length, scs);
                        table->field[1]->store((longlong)fsizes, TRUE);
                        if (schema_table_store_record(thd, table))
                        DBUG_RETURN(1);
                  }
                  /* free memory */
                  my_free(path);
                  DBUG_RETURN(0);
            }
            /* END CAB MODIFICATION */


重新编译mysql,测试

mysql> select *  from information_schema.DISKUSAGE;
+--------------------+---------------+
| DATABASE           | Size_in_bytes |
+--------------------+---------------+
| InnoDB TableSpace  |      67108864 |
| InnoDB Logs        |     268435456 |
| mysql              |       1503225 |
| performance_schema |        493639 |
| test               |        140848 |
+--------------------+---------------+




赞(0)    操作        顶端 
总帖数
1
每页帖数
101/1页1
返回列表
发新帖子
请输入验证码: 点击刷新验证码
您需要登录后才可以回帖 登录 | 注册
技术讨论