2 * collectd - src/collectd.c
3 * Copyright (C) 2005-2007 Florian octo Forster
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
24 * Florian octo Forster <octo at collectd.org>
25 * Alvaro Barcellos <alvaro.barcellos at gmail.com>
31 #include "configfile.h"
33 #include "utils/common/common.h"
36 #include <sys/types.h>
50 #ifndef COLLECTD_LOCALE
51 #define COLLECTD_LOCALE "C"
64 static int init_hostname(void) {
65 const char *str = global_option_get("Hostname");
66 if (str && str[0] != '\0') {
72 long hostname_len = NI_MAXHOST;
74 long hostname_len = sysconf(_SC_HOST_NAME_MAX);
75 if (hostname_len == -1) {
76 hostname_len = NI_MAXHOST;
79 char hostname[hostname_len];
81 if (gethostname(hostname, hostname_len) != 0) {
82 fprintf(stderr, "`gethostname' failed and no "
83 "hostname was configured.\n");
87 hostname_set(hostname);
89 str = global_option_get("FQDNLookup");
93 struct addrinfo *ai_list;
94 struct addrinfo ai_hints = {.ai_flags = AI_CANONNAME};
96 int status = getaddrinfo(hostname, NULL, &ai_hints, &ai_list);
98 ERROR("Looking up \"%s\" failed. You have set the "
99 "\"FQDNLookup\" option, but I cannot resolve "
100 "my hostname to a fully qualified domain "
101 "name. Please fix the network "
107 for (struct addrinfo *ai_ptr = ai_list; ai_ptr; ai_ptr = ai_ptr->ai_next) {
108 if (ai_ptr->ai_canonname == NULL)
111 hostname_set(ai_ptr->ai_canonname);
115 freeaddrinfo(ai_list);
117 } /* int init_hostname */
119 static int init_global_variables(void) {
120 interval_g = cf_get_default_interval();
121 assert(interval_g > 0);
122 DEBUG("interval_g = %.3f;", CDTIME_T_TO_DOUBLE(interval_g));
124 const char *str = global_option_get("Timeout");
127 timeout_g = atoi(str);
128 if (timeout_g <= 1) {
129 fprintf(stderr, "Cannot set the timeout to a correct value.\n"
130 "Please check your settings.\n");
133 DEBUG("timeout_g = %i;", timeout_g);
135 if (init_hostname() != 0)
137 DEBUG("hostname_g = %s;", hostname_g);
140 } /* int init_global_variables */
142 static int change_basedir(const char *orig_dir, bool create) {
143 char *dir = strdup(orig_dir);
145 ERROR("strdup failed: %s", STRERRNO);
149 size_t dirlen = strlen(dir);
150 while ((dirlen > 0) && (dir[dirlen - 1] == '/'))
151 dir[--dirlen] = '\0';
158 int status = chdir(dir);
162 } else if (!create || (errno != ENOENT)) {
163 ERROR("change_basedir: chdir (%s): %s", dir, STRERRNO);
168 status = mkdir(dir, S_IRWXU | S_IRWXG | S_IRWXO);
170 ERROR("change_basedir: mkdir (%s): %s", dir, STRERRNO);
177 ERROR("change_basedir: chdir (%s): %s", dir, STRERRNO);
184 } /* static int change_basedir (char *dir) */
187 extern kstat_ctl_t *kc;
188 static void update_kstat(void) {
190 if ((kc = kstat_open()) == NULL)
191 ERROR("Unable to open kstat control structure");
193 kid_t kid = kstat_chain_update(kc);
195 INFO("kstat chain has been updated");
198 ERROR("kstat chain update failed");
199 /* else: everything works as expected */
203 } /* static void update_kstat (void) */
204 #endif /* HAVE_LIBKSTAT */
206 __attribute__((noreturn)) static void exit_usage(int status) {
207 printf("Usage: " PACKAGE_NAME " [OPTIONS]\n\n"
209 "Available options:\n"
211 " -C <file> Configuration file.\n"
212 " Default: " CONFIGFILE "\n"
213 " -t Test config and exit.\n"
214 " -T Test plugin read and exit.\n"
215 " -P <file> PID-file.\n"
216 " Default: " PIDFILE "\n"
218 " -f Don't fork to the background.\n"
220 " -B Don't create the BaseDir\n"
221 " -h Display help (this message)\n"
222 "\nBuiltin defaults:\n"
223 " Config file " CONFIGFILE "\n"
224 " PID file " PIDFILE "\n"
225 " Plugin directory " PLUGINDIR "\n"
226 " Data directory " PKGLOCALSTATEDIR "\n"
227 "\n" PACKAGE_NAME " " PACKAGE_VERSION ", http://collectd.org/\n"
228 "by Florian octo Forster <octo@collectd.org>\n"
229 "for contributions see `AUTHORS'\n");
231 } /* static void exit_usage (int status) */
233 static int do_init(void) {
235 if (setlocale(LC_NUMERIC, COLLECTD_LOCALE) == NULL)
236 WARNING("setlocale (\"%s\") failed.", COLLECTD_LOCALE);
238 /* Update the environment, so that libraries that are calling
239 * setlocale(LC_NUMERIC, "") don't accidentally revert these changes. */
241 setenv("LC_NUMERIC", COLLECTD_LOCALE, /* overwrite = */ 1);
251 #if HAVE_LIBSTATGRAB_0_90
255 ERROR("sg_init: %s", sg_str_error(sg_get_error()));
259 if (sg_drop_privileges()) {
260 ERROR("sg_drop_privileges: %s", sg_str_error(sg_get_error()));
265 return plugin_init_all();
266 } /* int do_init () */
268 static int do_loop(void) {
269 cdtime_t interval = cf_get_default_interval();
270 cdtime_t wait_until = cdtime() + interval;
277 /* Issue all plugins */
280 cdtime_t now = cdtime();
281 if (now >= wait_until) {
282 WARNING("Not sleeping because the next interval is "
283 "%.3f seconds in the past!",
284 CDTIME_T_TO_DOUBLE(now - wait_until));
285 wait_until = now + interval;
289 struct timespec ts_wait = CDTIME_T_TO_TIMESPEC(wait_until - now);
290 wait_until = wait_until + interval;
292 while ((loop == 0) && (nanosleep(&ts_wait, &ts_wait) != 0)) {
293 if (errno != EINTR) {
294 ERROR("nanosleep failed: %s", STRERRNO);
298 } /* while (loop == 0) */
303 static int do_shutdown(void) {
304 return plugin_shutdown_all();
305 } /* int do_shutdown */
307 static void read_cmdline(int argc, char **argv, struct cmdline_config *config) {
310 int c = getopt(argc, argv, "BhtTC:"
321 config->create_basedir = false;
324 config->configfile = optarg;
327 config->test_config = true;
330 config->test_readall = true;
331 global_option_set("ReadThreads", "-1", 1);
333 config->daemonize = false;
334 #endif /* COLLECT_DAEMON */
338 global_option_set("PIDFile", optarg, 1);
341 config->daemonize = false;
343 #endif /* COLLECT_DAEMON */
352 static int configure_collectd(struct cmdline_config *config) {
354 * Read options from the config file, the environment and the command
355 * line (in that order, with later options overwriting previous ones in
357 * Also, this will automatically load modules.
359 if (cf_read(config->configfile)) {
360 fprintf(stderr, "Error: Parsing the config file failed!\n");
365 * Change directory. We do this _after_ reading the config and loading
366 * modules to relative paths work as expected.
369 if ((basedir = global_option_get("BaseDir")) == NULL) {
371 "Don't have a basedir to use. This should not happen. Ever.");
373 } else if (change_basedir(basedir, config->create_basedir)) {
374 fprintf(stderr, "Error: Unable to change to directory `%s'.\n", basedir);
379 * Set global variables or, if that fails, exit. We cannot run with
380 * them being uninitialized. If nothing is configured, then defaults
381 * are being used. So this means that the user has actually done
384 if (init_global_variables() != 0)
390 void stop_collectd(void) { loop++; }
392 struct cmdline_config init_config(int argc, char **argv) {
393 struct cmdline_config config = {
394 .daemonize = true, .create_basedir = true, .configfile = CONFIGFILE,
397 read_cmdline(argc, argv, &config);
399 if (config.test_config)
407 if (configure_collectd(&config) != 0)
413 int run_loop(bool test_readall) {
416 if (do_init() != 0) {
417 ERROR("Error: one or more plugin init callbacks failed.");
422 if (plugin_read_all_once() != 0) {
423 ERROR("Error: one or more plugin read callbacks failed.");
427 INFO("Initialization complete, entering read-loop.");
432 INFO("Exiting normally.");
434 if (do_shutdown() != 0) {
435 ERROR("Error: one or more plugin shutdown callbacks failed.");