rrd based system stats
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

205 lines
5.7 KiB

  1. /***************************************************************************
  2. * Copyright (C) 10/2006 by Olaf Rempel *
  3. * razzor@kopf-tisch.de *
  4. * *
  5. * This program is free software; you can redistribute it and/or modify *
  6. * it under the terms of the GNU General Public License as published by *
  7. * the Free Software Foundation; either version 2 of the License, or *
  8. * (at your option) any later version. *
  9. * *
  10. * This program is distributed in the hope that it will be useful, *
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  13. * GNU General Public License for more details. *
  14. * *
  15. * You should have received a copy of the GNU General Public License *
  16. * along with this program; if not, write to the *
  17. * Free Software Foundation, Inc., *
  18. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  19. ***************************************************************************/
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include "configfile.h"
  24. #include "helper.h"
  25. #include "list.h"
  26. #include "logging.h"
  27. #include "plugins.h"
  28. #include "probe.h"
  29. #include "mysql_helper.h"
  30. #define DS_TRAFFIC 1
  31. #define DS_COMMANDS 2
  32. #define DS_QCACHE 3
  33. #define DS_THREADS 4
  34. struct sammler_plugin plugin;
  35. struct server_entry {
  36. struct list_head list;
  37. void *mysql;
  38. char *name;
  39. };
  40. static LIST_HEAD(server_list);
  41. static const char *traffic_ds_def = {
  42. "DS:bytes_received:COUNTER:90:0:U "
  43. "DS:bytes_sent:COUNTER:90:0:U "
  44. };
  45. static const char *commands_ds_def = {
  46. "DS:com_delete:COUNTER:90:0:U "
  47. "DS:com_insert:COUNTER:90:0:U "
  48. "DS:com_select:COUNTER:90:0:U "
  49. "DS:com_update:COUNTER:90:0:U "
  50. "DS:connections:COUNTER:90:0:U "
  51. "DS:questions:COUNTER:90:0:U "
  52. };
  53. static const char *qcache_ds_def = {
  54. "DS:qc_free_blocks:GAUGE:90:0:U "
  55. "DS:qc_free_memory:GAUGE:90:0:U "
  56. "DS:qc_hits:COUNTER:90:0:U "
  57. "DS:qc_inserts:COUNTER:90:0:U "
  58. "DS:qc_lowmem_prunes:COUNTER:90:0:U "
  59. "DS:qc_not_cached:COUNTER:90:0:U "
  60. "DS:qc_queries_in_cache:GAUGE:90:0:U "
  61. "DS:qc_total_blocks:GAUGE:90:0:U "
  62. };
  63. static const char *threads_ds_def = {
  64. "DS:threads_cached:GAUGE:90:0:U "
  65. "DS:threads_connected:GAUGE:90:0:U "
  66. "DS:threads_created:COUNTER:90:0:U "
  67. "DS:threads_running:GAUGE:90:0:U "
  68. };
  69. static const char * get_ds(int ds_id)
  70. {
  71. switch (ds_id) {
  72. case DS_TRAFFIC:
  73. return traffic_ds_def;
  74. case DS_COMMANDS:
  75. return commands_ds_def;
  76. case DS_QCACHE:
  77. return qcache_ds_def;
  78. case DS_THREADS:
  79. return threads_ds_def;
  80. default:
  81. return NULL;
  82. }
  83. }
  84. static int probe(void)
  85. {
  86. struct mysql_stats stats;
  87. char filename[32];
  88. int len;
  89. memset(&stats, 0, sizeof(struct mysql_stats));
  90. struct server_entry *entry;
  91. list_for_each_entry(entry, &server_list, list) {
  92. if (ping_connection(entry->mysql) != 0)
  93. continue;
  94. if (get_stats(entry->mysql, &stats) != 0)
  95. continue;
  96. len = snprintf(filename, sizeof(filename), "mysql-traffic-%s.rrd", entry->name);
  97. if (len < 0 || len >= sizeof(filename))
  98. continue;
  99. probe_submit(&plugin, filename, DS_TRAFFIC, "%llu:%llu",
  100. stats.bytes_received, stats.bytes_sent);
  101. len = snprintf(filename, sizeof(filename), "mysql-commands-%s.rrd", entry->name);
  102. if (len < 0 || len >= sizeof(filename))
  103. continue;
  104. probe_submit(&plugin, filename, DS_COMMANDS, "%llu:%llu:%llu:%llu:%llu:%llu",
  105. stats.com_delete, stats.com_insert,
  106. stats.com_select, stats.com_update,
  107. stats.connections, stats.questions);
  108. len = snprintf(filename, sizeof(filename), "mysql-qcache-%s.rrd", entry->name);
  109. if (len < 0 || len >= sizeof(filename))
  110. continue;
  111. probe_submit(&plugin, filename, DS_QCACHE, "%llu:%llu:%llu:%llu:%llu:%llu:%llu:%llu",
  112. stats.qc_free_blocks, stats.qc_free_memory,
  113. stats.qc_hits, stats.qc_inserts,
  114. stats.qc_lowmem_prunes, stats.qc_not_cached,
  115. stats.qc_queries_in_cache, stats.qc_total_blocks);
  116. len = snprintf(filename, sizeof(filename), "mysql-threads-%s.rrd", entry->name);
  117. if (len < 0 || len >= sizeof(filename))
  118. continue;
  119. probe_submit(&plugin, filename, DS_THREADS, "%llu:%llu:%llu:%llu",
  120. stats.threads_cached, stats.threads_connected,
  121. stats.threads_created, stats.threads_running);
  122. }
  123. return 0;
  124. }
  125. static int init_cb(struct strtoken *tokens, void *privdata)
  126. {
  127. if (tokens->count != 4) {
  128. log_print(LOG_ERROR, "p_mysql: parse error");
  129. return -1;
  130. }
  131. void *mysql = init_connection(tokens->field[1], tokens->field[2], tokens->field[3]);
  132. if (mysql == NULL) {
  133. return -1;
  134. }
  135. struct server_entry *entry = malloc(sizeof(struct server_entry));
  136. if (entry == NULL) {
  137. log_print(LOG_ERROR, "p_mysql: out of memory");
  138. close_connection(mysql);
  139. return -1;
  140. }
  141. entry->name = strdup(tokens->field[0]);
  142. entry->mysql = mysql;
  143. log_print(LOG_DEBUG, "p_mysql: added server '%s'", entry->name);
  144. list_add_tail(&entry->list, &server_list);
  145. return 0;
  146. }
  147. static int init(void)
  148. {
  149. config_get_strtokens("p_mysql", "server", ",", 4, init_cb, NULL);
  150. return 0;
  151. }
  152. static int fini(void)
  153. {
  154. struct server_entry *entry, *tmp;
  155. list_for_each_entry_safe(entry, tmp, &server_list, list) {
  156. close_connection(entry->mysql);
  157. free(entry->name);
  158. free(entry);
  159. }
  160. return 0;
  161. }
  162. struct sammler_plugin plugin = {
  163. .name = "mysql",
  164. .interval = 60,
  165. .init = &init,
  166. .fini = &fini,
  167. .probe = &probe,
  168. .get_ds = &get_ds,
  169. };