From: Florian Forster Date: Sun, 28 Jan 2007 19:00:28 +0000 (+0100) Subject: unixsock plugin, collectd-nagios: Use the hostname, too, to identify values. X-Git-Tag: collectd-4.0.0-rc4~126^2~8 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=b32df633638e1869ecba45290bf502b243092241;p=collectd.git unixsock plugin, collectd-nagios: Use the hostname, too, to identify values. This is, of course, necessary, because many hosts may submit their values. --- diff --git a/src/collectd-nagios.c b/src/collectd-nagios.c index d3369b24..160412c5 100644 --- a/src/collectd-nagios.c +++ b/src/collectd-nagios.c @@ -44,6 +44,8 @@ extern int optind, opterr, optopt; static char *socket_file_g = NULL; static char *value_string_g = NULL; +static char *hostname_g = NULL; + static range_t range_critical_g; static range_t range_warning_g; static int consolitation_g = CON_NONE; @@ -163,7 +165,7 @@ static int get_values (int *ret_values_num, double **ret_values, return (-1); } - fprintf (fh, "GETVAL %s\n", value_string_g); + fprintf (fh, "GETVAL %s/%s\n", hostname_g, value_string_g); fflush (fh); if (fgets (buffer, sizeof (buffer), fh) == NULL) @@ -232,7 +234,7 @@ static int get_values (int *ret_values_num, double **ret_values, static void usage (const char *name) { - fprintf (stderr, "Usage: %s <-s socket> <-n value_spec> [options]\n" + fprintf (stderr, "Usage: %s <-s socket> <-n value_spec> <-H hostname> [options]\n" "\n" "Valid options are:\n" " -s Path to collectd's UNIX-socket.\n" @@ -242,6 +244,7 @@ static void usage (const char *name) " DSes. By default all DSes are used.\n" " -g Method to use to consolidate several DSes.\n" " Valid arguments are `none', `average' and `sum'\n" + " -H Hostname to query the values for.\n" " -c Critical range\n" " -w Warning range\n" "\n" @@ -422,7 +425,7 @@ int main (int argc, char **argv) { int c; - c = getopt (argc, argv, "w:c:s:n:g:d:h"); + c = getopt (argc, argv, "w:c:s:n:H:g:d:h"); if (c < 0) break; @@ -440,6 +443,9 @@ int main (int argc, char **argv) case 'n': value_string_g = optarg; break; + case 'H': + hostname_g = optarg; + break; case 'g': if (strcasecmp (optarg, "none") == 0) consolitation_g = CON_NONE; @@ -478,7 +484,8 @@ int main (int argc, char **argv) } /* switch (c) */ } - if ((socket_file_g == NULL) || (value_string_g == NULL)) + if ((socket_file_g == NULL) || (value_string_g == NULL) + || (hostname_g == NULL)) usage (argv[0]); return (do_check ()); diff --git a/src/unixsock.c b/src/unixsock.c index 21c9cc65..e6017ef2 100644 --- a/src/unixsock.c +++ b/src/unixsock.c @@ -98,6 +98,7 @@ static value_cache_t *cache_search (const char *name) } /* value_cache_t *cache_search */ static int cache_alloc_name (char *ret, int ret_len, + const char *hostname, const char *plugin, const char *plugin_instance, const char *type, const char *type_instance) { @@ -109,21 +110,20 @@ static int cache_alloc_name (char *ret, int ret_len, if ((plugin_instance == NULL) || (strlen (plugin_instance) == 0)) { if ((type_instance == NULL) || (strlen (type_instance) == 0)) - status = snprintf (ret, ret_len, "%s/%s", - plugin, type); + status = snprintf (ret, ret_len, "%s/%s/%s", + hostname, plugin, type); else - status = snprintf (ret, ret_len, "%s/%s-%s", - plugin, type, type_instance); + status = snprintf (ret, ret_len, "%s/%s/%s-%s", + hostname, plugin, type, type_instance); } else { if ((type_instance == NULL) || (strlen (type_instance) == 0)) - status = snprintf (ret, ret_len, "%s-%s/%s", - plugin, plugin_instance, type); + status = snprintf (ret, ret_len, "%s/%s-%s/%s", + hostname, plugin, plugin_instance, type); else - status = snprintf (ret, ret_len, "%s-%s/%s-%s", - plugin, plugin_instance, - type, type_instance); + status = snprintf (ret, ret_len, "%s/%s-%s/%s-%s", + hostname, plugin, plugin_instance, type, type_instance); } if ((status < 1) || (status >= ret_len)) @@ -172,7 +172,7 @@ static int cache_insert (const data_set_t *ds, const value_list_t *vl) } if (cache_alloc_name (vc->name, sizeof (vc->name), - vl->plugin, vl->plugin_instance, + vl->host, vl->plugin, vl->plugin_instance, ds->type, vl->type_instance) != 0) { pthread_mutex_unlock (&cache_lock); @@ -222,6 +222,7 @@ static int cache_update (const data_set_t *ds, const value_list_t *vl) int i; if (cache_alloc_name (name, sizeof (name), + vl->host, vl->plugin, vl->plugin_instance, ds->type, vl->type_instance) != 0) return (-1); @@ -425,7 +426,8 @@ static int us_open_socket (void) static int us_handle_getval (FILE *fh, char **fields, int fields_num) { - char *plugin = fields[1]; + char *hostname = fields[1]; + char *plugin; char *plugin_instance; char *type; char *type_instance; @@ -437,6 +439,11 @@ static int us_handle_getval (FILE *fh, char **fields, int fields_num) if (fields_num != 2) return (-1); + plugin = strchr (hostname, '/'); + if (plugin == NULL) + return (-1); + *plugin = '\0'; plugin++; + type = strchr (plugin, '/'); if (type == NULL) return (-1); @@ -457,7 +464,7 @@ static int us_handle_getval (FILE *fh, char **fields, int fields_num) } status = cache_alloc_name (name, sizeof (name), - plugin, plugin_instance, type, type_instance); + hostname, plugin, plugin_instance, type, type_instance); if (status != 0) return (-1);