From: Florian Forster Date: Tue, 13 Nov 2007 16:55:50 +0000 (+0000) Subject: collectd: The new `FQDNLookup' option controls whether or not the FQDN should be... X-Git-Tag: collectd-4.3.0beta0~105^2 X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=6c9b15cd22b4c1f56b5012b89dd12f0996fa0e4e;p=collectd.git collectd: The new `FQDNLookup' option controls whether or not the FQDN should be used. So far the hostname as returned by `gethostname(2)' was used. This is not practical for large setups. To stay backwards compatible the option is disabled by default, but the sample config file includes a line which sets this option so that (new) default installations will have it enabled. --- diff --git a/src/collectd.c b/src/collectd.c index 4e18fd6e..70223b7d 100644 --- a/src/collectd.c +++ b/src/collectd.c @@ -23,6 +23,10 @@ #include "collectd.h" #include "common.h" +#include +#include +#include + #include "plugin.h" #include "configfile.h" #include "types_list.h" @@ -48,25 +52,63 @@ static void sigTermHandler (int signal) loop++; } -static int init_global_variables (void) +static int init_hostname (void) { const char *str; + struct addrinfo ai_hints; + struct addrinfo *ai_list; + struct addrinfo *ai_ptr; + int status; + str = global_option_get ("Hostname"); if (str != NULL) { strncpy (hostname_g, str, sizeof (hostname_g)); + hostname_g[sizeof (hostname_g) - 1] = '\0'; + return (0); } - else + + if (gethostname (hostname_g, sizeof (hostname_g)) != 0) { - if (gethostname (hostname_g, sizeof (hostname_g)) != 0) - { - fprintf (stderr, "`gethostname' failed and no " - "hostname was configured.\n"); - return (-1); - } + fprintf (stderr, "`gethostname' failed and no " + "hostname was configured.\n"); + return (-1); + } + + str = global_option_get ("FQDNLookup"); + if ((strcasecmp ("false", str) == 0) + || (strcasecmp ("no", str) == 0) + || (strcasecmp ("off", str) == 0)) + return (0); + + memset (&ai_hints, '\0', sizeof (ai_hints)); + ai_hints.ai_flags = AI_CANONNAME; + + status = getaddrinfo (hostname_g, NULL, &ai_hints, &ai_list); + if (status != 0) + { + ERROR ("getaddrinfo failed."); + return (-1); } - DEBUG ("hostname_g = %s;", hostname_g); + + for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next) + { + if (ai_ptr->ai_canonname == NULL) + continue; + + strncpy (hostname_g, ai_ptr->ai_canonname, sizeof (hostname_g)); + hostname_g[sizeof (hostname_g) - 1] = '\0'; + break; + } + + freeaddrinfo (ai_list); + return (0); +} /* int init_hostname */ + +static int init_global_variables (void) +{ + const char *str; str = global_option_get ("Interval"); if (str == NULL) @@ -80,6 +122,10 @@ static int init_global_variables (void) } DEBUG ("interval_g = %i;", interval_g); + if (init_hostname () != 0) + return (-1); + DEBUG ("hostname_g = %s;", hostname_g); + return (0); } /* int init_global_variables */ diff --git a/src/collectd.conf.in b/src/collectd.conf.in index 58de5258..61789990 100644 --- a/src/collectd.conf.in +++ b/src/collectd.conf.in @@ -5,6 +5,7 @@ # #Hostname "localhost" +FQDNLookup true #BaseDir "@prefix@/var/lib/@PACKAGE_NAME@" #PIDFile "@prefix@/var/run/@PACKAGE_NAME@.pid" #PluginDir "@prefix@/lib/@PACKAGE_NAME@" diff --git a/src/collectd.conf.pod b/src/collectd.conf.pod index da12f863..d33b2806 100644 --- a/src/collectd.conf.pod +++ b/src/collectd.conf.pod @@ -83,6 +83,23 @@ you may want to increase this if you have more than five plugins that take a long time to read. Mostly those are plugin that do network-IO. Setting this to a value higher than the number of plugins you've loaded is totally useless. +=item B I + +Sets the hostname that identifies a host. If you omit this setting, the +hostname will be determinded using the L system call. + +=item B B + +If B is determined automatically this setting controls whether or not +the daemon should try to figure out the "full qualified domain name", FQDN. +This is done using a lookup of the name returned by C. + +Using this feature (i.Ee. setting this option to B) is recommended. +However, to preserve backwards compatibility the default is set to B. +The sample config file that is installed with Cinstall> includes a +line with sets this option, though, so that default installations will have +this setting enabled. + =back =head1 PLUGIN OPTIONS diff --git a/src/configfile.c b/src/configfile.c index 0310ca8a..a04a10d5 100644 --- a/src/configfile.c +++ b/src/configfile.c @@ -83,12 +83,13 @@ static int cf_value_map_num = STATIC_ARRAY_LEN (cf_value_map); static cf_global_option_t cf_global_options[] = { - {"BaseDir", NULL, PKGLOCALSTATEDIR}, - {"PIDFile", NULL, PIDFILE}, - {"Hostname", NULL, NULL}, - {"Interval", NULL, "10"}, + {"BaseDir", NULL, PKGLOCALSTATEDIR}, + {"PIDFile", NULL, PIDFILE}, + {"Hostname", NULL, NULL}, + {"FQDNLookup", NULL, "false"}, + {"Interval", NULL, "10"}, {"ReadThreads", NULL, "5"}, - {"TypesDB", NULL, PLUGINDIR"/types.db"} /* FIXME: Configure path */ + {"TypesDB", NULL, PLUGINDIR"/types.db"} /* FIXME: Configure path */ }; static int cf_global_options_num = STATIC_ARRAY_LEN (cf_global_options); @@ -173,6 +174,13 @@ static int dispatch_global_option (const oconfig_item_t *ci) tmp[127] = '\0'; return (global_option_set (ci->key, tmp)); } + else if (ci->values[0].type == OCONFIG_TYPE_BOOLEAN) + { + if (ci->values[0].value.boolean) + return (global_option_set (ci->key, "true")); + else + return (global_option_set (ci->key, "false")); + } return (-1); } /* int dispatch_global_option */