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.
 
 
 

204 lines
5.6 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 "plugins.h"
  27. #include "probe.h"
  28. #include "mysql_helper.h"
  29. #define DS_TRAFFIC 1
  30. #define DS_COMMANDS 2
  31. #define DS_QCACHE 3
  32. #define DS_THREADS 4
  33. struct sammler_plugin plugin;
  34. struct server_entry {
  35. struct list_head list;
  36. void *mysql;
  37. char *name;
  38. };
  39. static LIST_HEAD(server_list);
  40. static const char *traffic_ds_def = {
  41. "DS:bytes_received:COUNTER:90:0:U "
  42. "DS:bytes_sent:COUNTER:90:0:U "
  43. };
  44. static const char *commands_ds_def = {
  45. "DS:com_delete:COUNTER:90:0:U "
  46. "DS:com_insert:COUNTER:90:0:U "
  47. "DS:com_select:COUNTER:90:0:U "
  48. "DS:com_update:COUNTER:90:0:U "
  49. "DS:connections:COUNTER:90:0:U "
  50. "DS:questions:COUNTER:90:0:U "
  51. };
  52. static const char *qcache_ds_def = {
  53. "DS:qc_free_blocks:GAUGE:90:0:U "
  54. "DS:qc_free_memory:GAUGE:90:0:U "
  55. "DS:qc_hits:COUNTER:90:0:U "
  56. "DS:qc_inserts:COUNTER:90:0:U "
  57. "DS:qc_lowmem_prunes:COUNTER:90:0:U "
  58. "DS:qc_not_cached:COUNTER:90:0:U "
  59. "DS:qc_queries_in_cache:GAUGE:90:0:U "
  60. "DS:qc_total_blocks:GAUGE:90:0:U "
  61. };
  62. static const char *threads_ds_def = {
  63. "DS:threads_cached:GAUGE:90:0:U "
  64. "DS:threads_connected:GAUGE:90:0:U "
  65. "DS:threads_created:COUNTER:90:0:U "
  66. "DS:threads_running:GAUGE:90:0:U "
  67. };
  68. static const char * get_ds(int ds_id)
  69. {
  70. switch (ds_id) {
  71. case DS_TRAFFIC:
  72. return traffic_ds_def;
  73. case DS_COMMANDS:
  74. return commands_ds_def;
  75. case DS_QCACHE:
  76. return qcache_ds_def;
  77. case DS_THREADS:
  78. return threads_ds_def;
  79. default:
  80. return NULL;
  81. }
  82. }
  83. static int probe(void)
  84. {
  85. struct mysql_stats stats;
  86. char filename[32];
  87. int len;
  88. memset(&stats, 0, sizeof(struct mysql_stats));
  89. struct server_entry *entry;
  90. list_for_each_entry(entry, &server_list, list) {
  91. if (ping_connection(entry->mysql) != 0)
  92. continue;
  93. if (get_stats(entry->mysql, &stats) != 0)
  94. continue;
  95. len = snprintf(filename, sizeof(filename), "mysql-traffic-%s.rrd", entry->name);
  96. if (len < 0 || len >= sizeof(filename))
  97. continue;
  98. probe_submit(&plugin, filename, DS_TRAFFIC, "%llu:%llu",
  99. stats.bytes_received, stats.bytes_sent);
  100. len = snprintf(filename, sizeof(filename), "mysql-commands-%s.rrd", entry->name);
  101. if (len < 0 || len >= sizeof(filename))
  102. continue;
  103. probe_submit(&plugin, filename, DS_COMMANDS, "%llu:%llu:%llu:%llu:%llu:%llu",
  104. stats.com_delete, stats.com_insert,
  105. stats.com_select, stats.com_update,
  106. stats.connections, stats.questions);
  107. len = snprintf(filename, sizeof(filename), "mysql-qcache-%s.rrd", entry->name);
  108. if (len < 0 || len >= sizeof(filename))
  109. continue;
  110. probe_submit(&plugin, filename, DS_QCACHE, "%llu:%llu:%llu:%llu:%llu:%llu:%llu:%llu",
  111. stats.qc_free_blocks, stats.qc_free_memory,
  112. stats.qc_hits, stats.qc_inserts,
  113. stats.qc_lowmem_prunes, stats.qc_not_cached,
  114. stats.qc_queries_in_cache, stats.qc_total_blocks);
  115. len = snprintf(filename, sizeof(filename), "mysql-threads-%s.rrd", entry->name);
  116. if (len < 0 || len >= sizeof(filename))
  117. continue;
  118. probe_submit(&plugin, filename, DS_THREADS, "%llu:%llu:%llu:%llu",
  119. stats.threads_cached, stats.threads_connected,
  120. stats.threads_created, stats.threads_running);
  121. }
  122. return 0;
  123. }
  124. static int init_cb(const char *parameter, void *privdata)
  125. {
  126. char *part[4];
  127. int ret = strsplit(parameter, ",", part, 4);
  128. if (ret != 4) {
  129. log_print(LOG_ERROR, "p_mysql: parse error");
  130. return -1;
  131. }
  132. void *mysql = init_connection(part[1], part[2], part[3]);
  133. if (mysql == NULL)
  134. return -1;
  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 = part[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_strings("p_mysql", "server", 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);
  158. }
  159. return 0;
  160. }
  161. struct sammler_plugin plugin = {
  162. .name = "mysql",
  163. .interval = 60,
  164. .init = &init,
  165. .fini = &fini,
  166. .probe = &probe,
  167. .get_ds = &get_ds,
  168. };