From: Florian Forster Date: Sun, 4 Feb 2007 09:50:17 +0000 (+0100) Subject: mysql plugin: Converted to the new plugin interface. X-Git-Tag: collectd-4.0.0-rc4~115 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=e18c9f45c1890ba515c3b14419ba0cfe01991865;p=collectd.git mysql plugin: Converted to the new plugin interface. --- diff --git a/src/mysql.c b/src/mysql.c index caebdf31..a928172b 100644 --- a/src/mysql.c +++ b/src/mysql.c @@ -1,11 +1,10 @@ /** * collectd - src/mysql.c - * Copyright (C) 2006 Florian octo Forster + * Copyright (C) 2006,2007 Florian octo Forster * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the - * Free Software Foundation; either version 2 of the License, or (at your - * option) any later version. + * Free Software Foundation; only version 2 of the License is applicable. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of @@ -29,75 +28,69 @@ #include #endif -#define MODULE_NAME "mysql" - #if COLLECT_LIBMYSQL # define MYSQL_HAVE_READ 1 #else # define MYSQL_HAVE_READ 0 #endif -#define BUFSIZE 512 +/* TODO: Understand `Select_*' and possibly do that stuff as well.. */ -static char *host = "localhost"; -static char *user; -static char *pass; -static char *db = NULL; +static data_source_t data_source_counter[1] = +{ + {"value", DS_TYPE_COUNTER, 0, NAN} +}; -/* TODO - * understand `Select_*' and possibly do that stuff as well.. - */ +static data_set_t ds_commands = +{ + "mysql_commands", 1, data_source_counter +}; -static char *commands_file = "mysql/mysql_commands-%s.rrd"; -static char *handler_file = "mysql/mysql_handler-%s.rrd"; -static char *qcache_file = "mysql/mysql_qcache.rrd"; -static char *threads_file = "mysql/mysql_threads.rrd"; -static char *traffic_file = "traffic-mysql.rrd"; +static data_set_t ds_handler = +{ + "mysql_handler", 1, data_source_counter +}; -static char *commands_ds_def[] = +static data_source_t data_source_qcache[5] = { - "DS:value:COUNTER:"COLLECTD_HEARTBEAT":0:U", - NULL + {"hits", DS_TYPE_COUNTER, 0, NAN}, + {"inserts", DS_TYPE_COUNTER, 0, NAN}, + {"not_cached", DS_TYPE_COUNTER, 0, NAN}, + {"lowmem_prunes", DS_TYPE_COUNTER, 0, NAN}, + {"queries_in_cache", DS_TYPE_GAUGE, 0, NAN} }; -static int commands_ds_num = 1; -static char *handler_ds_def[] = +static data_set_t ds_qcache = { - "DS:value:COUNTER:"COLLECTD_HEARTBEAT":0:U", - NULL + "mysql_qcache", 5, data_source_qcache }; -static int handler_ds_num = 1; -static char *qcache_ds_def[] = +static data_source_t data_source_threads[4] = { - "DS:hits:COUNTER:"COLLECTD_HEARTBEAT":0:U", - "DS:inserts:COUNTER:"COLLECTD_HEARTBEAT":0:U", - "DS:not_cached:COUNTER:"COLLECTD_HEARTBEAT":0:U", - "DS:lowmem_prunes:COUNTER:"COLLECTD_HEARTBEAT":0:U", - "DS:queries_in_cache:GAUGE:"COLLECTD_HEARTBEAT":0:U", - NULL + {"running", DS_TYPE_GAUGE, 0, NAN}, + {"connected", DS_TYPE_GAUGE, 0, NAN}, + {"cached", DS_TYPE_GAUGE, 0, NAN}, + {"created", DS_TYPE_COUNTER, 0, NAN} }; -static int qcache_ds_num = 5; -static char *threads_ds_def[] = +static data_set_t ds_threads = { - "DS:running:GAUGE:"COLLECTD_HEARTBEAT":0:U", - "DS:connected:GAUGE:"COLLECTD_HEARTBEAT":0:U", - "DS:cached:GAUGE:"COLLECTD_HEARTBEAT":0:U", - "DS:created:COUNTER:"COLLECTD_HEARTBEAT":0:U", - NULL + "mysql_threads", 4, data_source_threads }; -static int threads_ds_num = 4; -static char *traffic_ds_def[] = +static data_source_t data_source_octets[2] = { - "DS:incoming:COUNTER:"COLLECTD_HEARTBEAT":0:U", - "DS:outgoing:COUNTER:"COLLECTD_HEARTBEAT":0:U", - NULL + {"rx", DS_TYPE_COUNTER, 0, 4294967295.0}, + {"tx", DS_TYPE_COUNTER, 0, 4294967295.0} }; -static int traffic_ds_num = 2; -static char *config_keys[] = +static data_set_t ds_octets = +{ + "mysql_octets", 2, data_source_octets +}; + +#if MYSQL_HAVE_READ +static const char *config_keys[] = { "Host", "User", @@ -107,7 +100,11 @@ static char *config_keys[] = }; static int config_keys_num = 4; -#if MYSQL_HAVE_READ +static char *host = "localhost"; +static char *user; +static char *pass; +static char *db = NULL; + static MYSQL *getconnection (void) { static MYSQL *con; @@ -167,14 +164,8 @@ static MYSQL *getconnection (void) return (con); } } /* static MYSQL *getconnection (void) */ -#endif /* MYSQL_HAVE_READ */ - -static void init (void) -{ - return; -} -static int config (char *key, char *value) +static int config (const char *key, const char *value) { if (strcasecmp (key, "host") == 0) return ((host = strdup (value)) == NULL ? 1 : 0); @@ -188,160 +179,84 @@ static int config (char *key, char *value) return (-1); } -static void commands_write (char *host, char *inst, char *val) -{ - char buf[BUFSIZE]; - - if (snprintf (buf, BUFSIZE, commands_file, inst) >= BUFSIZE) - return; - - rrd_update_file (host, buf, val, commands_ds_def, commands_ds_num); -} - -static void handler_write (char *host, char *inst, char *val) -{ - char buf[BUFSIZE]; - - if (snprintf (buf, BUFSIZE, handler_file, inst) >= BUFSIZE) - return; - - rrd_update_file (host, buf, val, handler_ds_def, handler_ds_num); -} - -static void qcache_write (char *host, char *inst, char *val) -{ - rrd_update_file (host, qcache_file, val, - qcache_ds_def, qcache_ds_num); -} - -static void threads_write (char *host, char *inst, char *val) -{ - rrd_update_file (host, threads_file, val, - threads_ds_def, threads_ds_num); -} - -static void traffic_write (char *host, char *inst, char *val) +static void counter_submit (const char *type, const char *type_instance, + counter_t value) { - rrd_update_file (host, traffic_file, val, - traffic_ds_def, traffic_ds_num); -} - -#if MYSQL_HAVE_READ -static void commands_submit (char *inst, unsigned long long value) -{ - char buf[BUFSIZE]; - int status; + value_t values[1]; + value_list_t vl = VALUE_LIST_INIT; - status = snprintf (buf, BUFSIZE, "%u:%llu", (unsigned int) curtime, value); + values[0].counter = value; - if (status < 0) - { - syslog (LOG_ERR, "snprintf failed"); - return; - } - else if (status >= BUFSIZE) - { - syslog (LOG_WARNING, "snprintf was truncated"); - return; - } + vl.values = values; + vl.values_len = 1; + vl.time = time (NULL); + strcpy (vl.host, hostname); + strcpy (vl.plugin, "mysql"); + strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance)); - plugin_submit ("mysql_commands", inst, buf); -} + plugin_dispatch_values (type, &vl); +} /* void counter_submit */ -static void handler_submit (char *inst, unsigned long long value) +static void qcache_submit (counter_t hits, counter_t inserts, + counter_t not_cached, counter_t lowmem_prunes, + gauge_t queries_in_cache) { - char buf[BUFSIZE]; - int status; - - status = snprintf (buf, BUFSIZE, "%u:%llu", (unsigned int) curtime, value); - - if (status < 0) - { - syslog (LOG_ERR, "snprintf failed"); - return; - } - else if (status >= BUFSIZE) - { - syslog (LOG_WARNING, "snprintf was truncated"); - return; - } - - plugin_submit ("mysql_handler", inst, buf); -} - -static void qcache_submit (unsigned long long hits, unsigned long long inserts, - unsigned long long not_cached, unsigned long long lowmem_prunes, - int queries_in_cache) -{ - char buf[BUFSIZE]; - int status; - - status = snprintf (buf, BUFSIZE, "%u:%llu:%llu:%llu:%llu:%i", - (unsigned int) curtime, hits, inserts, not_cached, - lowmem_prunes, queries_in_cache); - - if (status < 0) - { - syslog (LOG_ERR, "snprintf failed"); - return; - } - else if (status >= BUFSIZE) - { - syslog (LOG_WARNING, "snprintf was truncated"); - return; - } - - plugin_submit ("mysql_qcache", "-", buf); -} - -static void threads_submit (int running, int connected, int cached, - unsigned long long created) + value_t values[5]; + value_list_t vl = VALUE_LIST_INIT; + + values[0].counter = hits; + values[1].counter = inserts; + values[2].counter = not_cached; + values[3].counter = lowmem_prunes; + values[4].gauge = queries_in_cache; + + vl.values = values; + vl.values_len = 5; + vl.time = time (NULL); + strcpy (vl.host, hostname); + strcpy (vl.plugin, "mysql"); + + plugin_dispatch_values ("mysql_qcache", &vl); +} /* void qcache_submit */ + +static void threads_submit (gauge_t running, gauge_t connected, gauge_t cached, + counter_t created) { - char buf[BUFSIZE]; - int status; + value_t values[4]; + value_list_t vl = VALUE_LIST_INIT; - status = snprintf (buf, BUFSIZE, "%u:%i:%i:%i:%llu", - (unsigned int) curtime, - running, connected, cached, created); + values[0].gauge = running; + values[1].gauge = connected; + values[2].gauge = cached; + values[3].counter = created; - if (status < 0) - { - syslog (LOG_ERR, "snprintf failed"); - return; - } - else if (status >= BUFSIZE) - { - syslog (LOG_WARNING, "snprintf was truncated"); - return; - } + vl.values = values; + vl.values_len = 4; + vl.time = time (NULL); + strcpy (vl.host, hostname); + strcpy (vl.plugin, "mysql"); - plugin_submit ("mysql_threads", "-", buf); -} + plugin_dispatch_values ("mysql_threads", &vl); +} /* void threads_submit */ -static void traffic_submit (unsigned long long incoming, - unsigned long long outgoing) +static void traffic_submit (counter_t rx, counter_t tx) { - char buf[BUFSIZE]; - int status; + value_t values[2]; + value_list_t vl = VALUE_LIST_INIT; - status = snprintf (buf, BUFSIZE, "%u:%llu:%llu", (unsigned int) curtime, - incoming, outgoing); + values[0].counter = rx; + values[1].counter = tx; - if (status < 0) - { - syslog (LOG_ERR, "snprintf failed"); - return; - } - else if (status >= BUFSIZE) - { - syslog (LOG_WARNING, "snprintf was truncated"); - return; - } + vl.values = values; + vl.values_len = 2; + vl.time = time (NULL); + strcpy (vl.host, hostname); + strcpy (vl.plugin, "mysql"); - plugin_submit ("mysql_traffic", "-", buf); -} + plugin_dispatch_values ("mysql_octets", &vl); +} /* void traffic_submit */ -static void mysql_read (void) +static int mysql_read (void) { MYSQL *con; MYSQL_RES *res; @@ -366,7 +281,7 @@ static void mysql_read (void) /* An error message will have been printed in this case */ if ((con = getconnection ()) == NULL) - return; + return (-1); query = "SHOW STATUS"; if (mysql_get_server_version (con) >= 50002) @@ -378,14 +293,14 @@ static void mysql_read (void) { syslog (LOG_ERR, "mysql_real_query failed: %s\n", mysql_error (con)); - return; + return (-1); } if ((res = mysql_store_result (con)) == NULL) { syslog (LOG_ERR, "mysql_store_result failed: %s\n", mysql_error (con)); - return; + return (-1); } field_num = mysql_num_fields (res); @@ -404,14 +319,14 @@ static void mysql_read (void) /* Ignore `prepared statements' */ if (strncmp (key, "Com_stmt_", 9) != 0) - commands_submit (key + 4, val); + counter_submit ("mysql_commands", key + 4, val); } else if (strncmp (key, "Handler_", 8) == 0) { if (val == 0ULL) continue; - handler_submit (key + 8, val); + counter_submit ("mysql_handler", key + 8, val); } else if (strncmp (key, "Qcache_", 7) == 0) { @@ -462,22 +377,20 @@ static void mysql_read (void) /* mysql_close (con); */ - return; -} -#else -# define mysql_read NULL + return (0); +} /* int mysql_read */ #endif /* MYSQL_HAVE_READ */ void module_register (void) { - plugin_register (MODULE_NAME, init, mysql_read, NULL); - plugin_register ("mysql_commands", NULL, NULL, commands_write); - plugin_register ("mysql_handler", NULL, NULL, handler_write); - plugin_register ("mysql_qcache", NULL, NULL, qcache_write); - plugin_register ("mysql_threads", NULL, NULL, threads_write); - plugin_register ("mysql_traffic", NULL, NULL, traffic_write); - cf_register (MODULE_NAME, config, config_keys, config_keys_num); -} + plugin_register_data_set (&ds_commands); + plugin_register_data_set (&ds_handler); + plugin_register_data_set (&ds_qcache); + plugin_register_data_set (&ds_threads); + plugin_register_data_set (&ds_octets); -#undef BUFSIZE -#undef MODULE_NAME +#if MYSQL_HAVE_READ + plugin_register_config ("mysql", config, config_keys, config_keys_num); + plugin_register_read ("mysql", mysql_read); +#endif /* MYSQL_HAVE_READ */ +} /* void module_register */