X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fmysql.c;h=1c009a07e56c3631446ff5fe2fa9f3b8d60743f7;hb=bc7992ed0693313a2b1fe282a5bf23f1cc9f8e42;hp=5fbe5a662d02cb17b1f779a412e2fc52a9968320;hpb=dc80c73c20ef0e69c3850fd9679a827ad79e61a1;p=collectd.git diff --git a/src/mysql.c b/src/mysql.c index 5fbe5a66..1c009a07 100644 --- a/src/mysql.c +++ b/src/mysql.c @@ -24,86 +24,32 @@ #include "plugin.h" #include "configfile.h" -#ifdef HAVE_MYSQL_MYSQL_H +#ifdef HAVE_MYSQL_H +#include +#elif defined(HAVE_MYSQL_MYSQL_H) #include #endif -#if COLLECT_LIBMYSQL -# define MYSQL_HAVE_READ 1 -#else -# define MYSQL_HAVE_READ 0 -#endif - /* TODO: Understand `Select_*' and possibly do that stuff as well.. */ -static data_source_t data_source_counter[1] = -{ - {"value", DS_TYPE_COUNTER, 0, NAN} -}; - -static data_set_t ds_commands = -{ - "mysql_commands", 1, data_source_counter -}; - -static data_set_t ds_handler = -{ - "mysql_handler", 1, data_source_counter -}; - -static data_source_t data_source_qcache[5] = -{ - {"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 data_set_t ds_qcache = -{ - "mysql_qcache", 5, data_source_qcache -}; - -static data_source_t data_source_threads[4] = -{ - {"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 data_set_t ds_threads = -{ - "mysql_threads", 4, data_source_threads -}; - -static data_source_t data_source_octets[2] = -{ - {"rx", DS_TYPE_COUNTER, 0, 4294967295.0}, - {"tx", DS_TYPE_COUNTER, 0, 4294967295.0} -}; - -static data_set_t ds_octets = -{ - "mysql_octets", 2, data_source_octets -}; - -#if MYSQL_HAVE_READ static const char *config_keys[] = { "Host", "User", "Password", "Database", + "Port", + "Socket", NULL }; -static int config_keys_num = 4; +static int config_keys_num = 6; static char *host = "localhost"; static char *user; static char *pass; static char *db = NULL; +static char *socket = NULL; +static int port = 0; static MYSQL *getconnection (void) { @@ -146,7 +92,7 @@ static MYSQL *getconnection (void) return (NULL); } - if (mysql_real_connect (con, host, user, pass, db, 0, NULL, 0) == NULL) + if (mysql_real_connect (con, host, user, pass, db, port, socket, 0) == NULL) { ERROR ("mysql_real_connect failed: %s", mysql_error (con)); state = 0; @@ -171,9 +117,36 @@ static int config (const char *key, const char *value) return ((pass = strdup (value)) == NULL ? 1 : 0); else if (strcasecmp (key, "database") == 0) return ((db = strdup (value)) == NULL ? 1 : 0); + else if (strcasecmp (key, "socket") == 0) + return ((socket = strdup (value)) == NULL ? 1 : 0); + else if (strcasecmp (key, "port") == 0) + { + char *endptr = NULL; + int temp; + + errno = 0; + temp = strtol (value, &endptr, 0); + if ((errno != 0) || (value == endptr)) + { + ERROR ("mysql plugin: Invalid \"Port\" argument: %s", + value); + port = 0; + return (1); + } + else if ((temp < 0) || (temp >= 65535)) + { + ERROR ("mysql plugin: Port number out of range: %i", + temp); + port = 0; + return (1); + } + + port = temp; + return (0); + } else return (-1); -} +} /* int config */ static void counter_submit (const char *type, const char *type_instance, counter_t value) @@ -185,12 +158,12 @@ static void counter_submit (const char *type, const char *type_instance, vl.values = values; vl.values_len = 1; - vl.time = time (NULL); - strcpy (vl.host, hostname_g); - strcpy (vl.plugin, "mysql"); - strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance)); + sstrncpy (vl.host, hostname_g, sizeof (vl.host)); + sstrncpy (vl.plugin, "mysql", sizeof (vl.plugin)); + sstrncpy (vl.type, type, sizeof (vl.type)); + sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance)); - plugin_dispatch_values (type, &vl); + plugin_dispatch_values (&vl); } /* void counter_submit */ static void qcache_submit (counter_t hits, counter_t inserts, @@ -208,11 +181,11 @@ static void qcache_submit (counter_t hits, counter_t inserts, vl.values = values; vl.values_len = 5; - vl.time = time (NULL); - strcpy (vl.host, hostname_g); - strcpy (vl.plugin, "mysql"); + sstrncpy (vl.host, hostname_g, sizeof (vl.host)); + sstrncpy (vl.plugin, "mysql", sizeof (vl.plugin)); + sstrncpy (vl.type, "mysql_qcache", sizeof (vl.type)); - plugin_dispatch_values ("mysql_qcache", &vl); + plugin_dispatch_values (&vl); } /* void qcache_submit */ static void threads_submit (gauge_t running, gauge_t connected, gauge_t cached, @@ -228,11 +201,11 @@ static void threads_submit (gauge_t running, gauge_t connected, gauge_t cached, vl.values = values; vl.values_len = 4; - vl.time = time (NULL); - strcpy (vl.host, hostname_g); - strcpy (vl.plugin, "mysql"); + sstrncpy (vl.host, hostname_g, sizeof (vl.host)); + sstrncpy (vl.plugin, "mysql", sizeof (vl.plugin)); + sstrncpy (vl.type, "mysql_threads", sizeof (vl.type)); - plugin_dispatch_values ("mysql_threads", &vl); + plugin_dispatch_values (&vl); } /* void threads_submit */ static void traffic_submit (counter_t rx, counter_t tx) @@ -245,11 +218,11 @@ static void traffic_submit (counter_t rx, counter_t tx) vl.values = values; vl.values_len = 2; - vl.time = time (NULL); - strcpy (vl.host, hostname_g); - strcpy (vl.plugin, "mysql"); + sstrncpy (vl.host, hostname_g, sizeof (vl.host)); + sstrncpy (vl.plugin, "mysql", sizeof (vl.plugin)); + sstrncpy (vl.type, "mysql_octets", sizeof (vl.type)); - plugin_dispatch_values ("mysql_octets", &vl); + plugin_dispatch_values (&vl); } /* void traffic_submit */ static int mysql_read (void) @@ -375,24 +348,9 @@ static int mysql_read (void) return (0); } /* int mysql_read */ -#endif /* MYSQL_HAVE_READ */ -void module_register (modreg_e load) +void module_register (void) { - if (load & MR_DATASETS) - { - 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); - } - -#if MYSQL_HAVE_READ - if (load & MR_READ) - { - plugin_register_config ("mysql", config, config_keys, config_keys_num); - plugin_register_read ("mysql", mysql_read); - } -#endif /* MYSQL_HAVE_READ */ + plugin_register_config ("mysql", config, config_keys, config_keys_num); + plugin_register_read ("mysql", mysql_read); } /* void module_register */