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>
32 #include "configfile.h"
36 #include <sys/types.h>
50 #ifndef COLLECTD_LOCALE
51 #define COLLECTD_LOCALE "C"
56 static int init_hostname(void) {
57 const char *str = global_option_get("Hostname");
58 if (str && str[0] != '\0') {
63 long hostname_len = sysconf(_SC_HOST_NAME_MAX);
64 if (hostname_len == -1) {
65 hostname_len = NI_MAXHOST;
67 char hostname[hostname_len];
69 if (gethostname(hostname, hostname_len) != 0) {
70 fprintf(stderr, "`gethostname' failed and no "
71 "hostname was configured.\n");
75 hostname_set(hostname);
77 str = global_option_get("FQDNLookup");
81 struct addrinfo *ai_list;
82 struct addrinfo ai_hints = {.ai_flags = AI_CANONNAME};
84 int status = getaddrinfo(hostname, NULL, &ai_hints, &ai_list);
86 ERROR("Looking up \"%s\" failed. You have set the "
87 "\"FQDNLookup\" option, but I cannot resolve "
88 "my hostname to a fully qualified domain "
89 "name. Please fix the network "
95 for (struct addrinfo *ai_ptr = ai_list; ai_ptr; ai_ptr = ai_ptr->ai_next) {
96 if (ai_ptr->ai_canonname == NULL)
99 hostname_set(ai_ptr->ai_canonname);
103 freeaddrinfo(ai_list);
105 } /* int init_hostname */
107 static int init_global_variables(void) {
108 interval_g = cf_get_default_interval();
109 assert(interval_g > 0);
110 DEBUG("interval_g = %.3f;", CDTIME_T_TO_DOUBLE(interval_g));
112 const char *str = global_option_get("Timeout");
115 timeout_g = atoi(str);
116 if (timeout_g <= 1) {
117 fprintf(stderr, "Cannot set the timeout to a correct value.\n"
118 "Please check your settings.\n");
121 DEBUG("timeout_g = %i;", timeout_g);
123 if (init_hostname() != 0)
125 DEBUG("hostname_g = %s;", hostname_g);
128 } /* int init_global_variables */
130 static int change_basedir(const char *orig_dir, bool create) {
131 char *dir = strdup(orig_dir);
133 ERROR("strdup failed: %s", STRERRNO);
137 size_t dirlen = strlen(dir);
138 while ((dirlen > 0) && (dir[dirlen - 1] == '/'))
139 dir[--dirlen] = '\0';
146 int status = chdir(dir);
150 } else if (!create || (errno != ENOENT)) {
151 ERROR("change_basedir: chdir (%s): %s", dir, STRERRNO);
156 status = mkdir(dir, S_IRWXU | S_IRWXG | S_IRWXO);
158 ERROR("change_basedir: mkdir (%s): %s", dir, STRERRNO);
165 ERROR("change_basedir: chdir (%s): %s", dir, STRERRNO);
172 } /* static int change_basedir (char *dir) */
175 extern kstat_ctl_t *kc;
176 static void update_kstat(void) {
178 if ((kc = kstat_open()) == NULL)
179 ERROR("Unable to open kstat control structure");
181 kid_t kid = kstat_chain_update(kc);
183 INFO("kstat chain has been updated");
186 ERROR("kstat chain update failed");
187 /* else: everything works as expected */
191 } /* static void update_kstat (void) */
192 #endif /* HAVE_LIBKSTAT */
194 __attribute__((noreturn)) static void exit_usage(int status) {
195 printf("Usage: " PACKAGE_NAME " [OPTIONS]\n\n"
197 "Available options:\n"
199 " -C <file> Configuration file.\n"
200 " Default: " CONFIGFILE "\n"
201 " -t Test config and exit.\n"
202 " -T Test plugin read and exit.\n"
203 " -P <file> PID-file.\n"
204 " Default: " PIDFILE "\n"
206 " -f Don't fork to the background.\n"
208 " -B Don't create the BaseDir\n"
209 " -h Display help (this message)\n"
210 "\nBuiltin defaults:\n"
211 " Config file " CONFIGFILE "\n"
212 " PID file " PIDFILE "\n"
213 " Plugin directory " PLUGINDIR "\n"
214 " Data directory " PKGLOCALSTATEDIR "\n"
215 "\n" PACKAGE_NAME " " PACKAGE_VERSION ", http://collectd.org/\n"
216 "by Florian octo Forster <octo@collectd.org>\n"
217 "for contributions see `AUTHORS'\n");
219 } /* static void exit_usage (int status) */
221 static int do_init(void) {
223 if (setlocale(LC_NUMERIC, COLLECTD_LOCALE) == NULL)
224 WARNING("setlocale (\"%s\") failed.", COLLECTD_LOCALE);
226 /* Update the environment, so that libraries that are calling
227 * setlocale(LC_NUMERIC, "") don't accidentally revert these changes. */
229 setenv("LC_NUMERIC", COLLECTD_LOCALE, /* overwrite = */ 1);
239 #if HAVE_LIBSTATGRAB_0_90
243 ERROR("sg_init: %s", sg_str_error(sg_get_error()));
247 if (sg_drop_privileges()) {
248 ERROR("sg_drop_privileges: %s", sg_str_error(sg_get_error()));
253 return plugin_init_all();
254 } /* int do_init () */
256 static int do_loop(void) {
257 cdtime_t interval = cf_get_default_interval();
258 cdtime_t wait_until = cdtime() + interval;
265 /* Issue all plugins */
268 cdtime_t now = cdtime();
269 if (now >= wait_until) {
270 WARNING("Not sleeping because the next interval is "
271 "%.3f seconds in the past!",
272 CDTIME_T_TO_DOUBLE(now - wait_until));
273 wait_until = now + interval;
277 struct timespec ts_wait = CDTIME_T_TO_TIMESPEC(wait_until - now);
278 wait_until = wait_until + interval;
280 while ((loop == 0) && (nanosleep(&ts_wait, &ts_wait) != 0)) {
281 if (errno != EINTR) {
282 ERROR("nanosleep failed: %s", STRERRNO);
286 } /* while (loop == 0) */
291 static int do_shutdown(void) {
292 return plugin_shutdown_all();
293 } /* int do_shutdown */
295 static void read_cmdline(int argc, char **argv, struct cmdline_config *config) {
298 int c = getopt(argc, argv, "BhtTC:"
309 config->create_basedir = false;
312 config->configfile = optarg;
315 config->test_config = true;
318 config->test_readall = true;
319 global_option_set("ReadThreads", "-1", 1);
321 config->daemonize = false;
322 #endif /* COLLECT_DAEMON */
326 global_option_set("PIDFile", optarg, 1);
329 config->daemonize = false;
331 #endif /* COLLECT_DAEMON */
340 static int configure_collectd(struct cmdline_config *config) {
342 * Read options from the config file, the environment and the command
343 * line (in that order, with later options overwriting previous ones in
345 * Also, this will automatically load modules.
347 if (cf_read(config->configfile)) {
348 fprintf(stderr, "Error: Reading the config file failed!\n"
349 "Read the logs for details.\n");
354 * Change directory. We do this _after_ reading the config and loading
355 * modules to relative paths work as expected.
358 if ((basedir = global_option_get("BaseDir")) == NULL) {
360 "Don't have a basedir to use. This should not happen. Ever.");
362 } else if (change_basedir(basedir, config->create_basedir)) {
363 fprintf(stderr, "Error: Unable to change to directory `%s'.\n", basedir);
368 * Set global variables or, if that fails, exit. We cannot run with
369 * them being uninitialized. If nothing is configured, then defaults
370 * are being used. So this means that the user has actually done
373 if (init_global_variables() != 0)
379 void stop_collectd(void) { loop++; }
381 struct cmdline_config init_config(int argc, char **argv) {
382 struct cmdline_config config = {
383 .daemonize = true, .create_basedir = true, .configfile = CONFIGFILE,
386 read_cmdline(argc, argv, &config);
388 if (config.test_config)
396 if (configure_collectd(&config) != 0)
402 int run_loop(bool test_readall) {
405 if (do_init() != 0) {
406 ERROR("Error: one or more plugin init callbacks failed.");
411 if (plugin_read_all_once() != 0) {
412 ERROR("Error: one or more plugin read callbacks failed.");
416 INFO("Initialization complete, entering read-loop.");
421 INFO("Exiting normally.");
423 if (do_shutdown() != 0) {
424 ERROR("Error: one or more plugin shutdown callbacks failed.");