f4dd76cdef045c890df5675007702c330a2ad045
1 /**
2 * collectd - src/collectd.c
3 * Copyright (C) 2005-2007 Florian octo Forster
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 *
18 * Authors:
19 * Florian octo Forster <octo at verplant.org>
20 * Alvaro Barcellos <alvaro.barcellos at gmail.com>
21 **/
23 #include "collectd.h"
24 #include "common.h"
25 #include "utils_debug.h"
27 #include "network.h"
28 #include "plugin.h"
29 #include "configfile.h"
31 static int loop = 0;
33 #if HAVE_LIBKSTAT
34 kstat_ctl_t *kc;
35 #endif /* HAVE_LIBKSTAT */
37 /*
38 * exported variables
39 */
40 time_t curtime;
41 int operating_mode;
43 static void sigIntHandler (int signal)
44 {
45 loop++;
46 }
48 static void sigTermHandler (int signal)
49 {
50 loop++;
51 }
53 static int change_basedir (const char *orig_dir)
54 {
55 char *dir = strdup (orig_dir);
56 int dirlen;
57 int status;
59 if (dir == NULL)
60 {
61 syslog (LOG_ERR, "strdup failed: %s", strerror (errno));
62 return (-1);
63 }
65 dirlen = strlen (dir);
66 while ((dirlen > 0) && (dir[dirlen - 1] == '/'))
67 dir[--dirlen] = '\0';
69 if (dirlen <= 0)
70 return (-1);
72 status = chdir (dir);
73 free (dir);
75 if (status != 0)
76 {
77 if (errno == ENOENT)
78 {
79 if (mkdir (orig_dir, 0755) == -1)
80 {
81 syslog (LOG_ERR, "mkdir (%s): %s", orig_dir,
82 strerror (errno));
83 return (-1);
84 }
85 else if (chdir (orig_dir) == -1)
86 {
87 syslog (LOG_ERR, "chdir (%s): %s", orig_dir,
88 strerror (errno));
89 return (-1);
90 }
91 }
92 else
93 {
94 syslog (LOG_ERR, "chdir (%s): %s", orig_dir,
95 strerror (errno));
96 return (-1);
97 }
98 }
100 return (0);
101 } /* static int change_basedir (char *dir) */
103 #if HAVE_LIBKSTAT
104 static void update_kstat (void)
105 {
106 if (kc == NULL)
107 {
108 if ((kc = kstat_open ()) == NULL)
109 syslog (LOG_ERR, "Unable to open kstat control structure");
110 }
111 else
112 {
113 kid_t kid;
114 kid = kstat_chain_update (kc);
115 if (kid > 0)
116 {
117 syslog (LOG_INFO, "kstat chain has been updated");
118 plugin_init_all ();
119 }
120 else if (kid < 0)
121 syslog (LOG_ERR, "kstat chain update failed");
122 /* else: everything works as expected */
123 }
125 return;
126 } /* static void update_kstat (void) */
127 #endif /* HAVE_LIBKSTAT */
129 /* TODO
130 * Remove all settings but `-f' and `-C'
131 */
132 static void exit_usage (char *name)
133 {
134 printf ("Usage: "PACKAGE" [OPTIONS]\n\n"
136 "Available options:\n"
137 " General:\n"
138 " -C <file> Configuration file.\n"
139 " Default: "CONFIGFILE"\n"
140 " -P <file> PID-file.\n"
141 " Default: "PIDFILE"\n"
142 #if COLLECT_DAEMON
143 " -f Don't fork to the background.\n"
144 #endif
145 "\nBuiltin defaults:\n"
146 " Config-File "CONFIGFILE"\n"
147 " PID-File "PIDFILE"\n"
148 " Data-Directory "PKGLOCALSTATEDIR"\n"
149 #if COLLECT_DEBUG
150 " Log-File "LOGFILE"\n"
151 #endif
152 " Step "COLLECTD_STEP" seconds\n"
153 " Heartbeat "COLLECTD_HEARTBEAT" seconds\n"
154 "\n"PACKAGE" "VERSION", http://collectd.org/\n"
155 "by Florian octo Forster <octo@verplant.org>\n"
156 "for contributions see `AUTHORS'\n");
157 exit (0);
158 } /* static void exit_usage (char *name) */
160 static int start_client (void)
161 {
162 int step;
164 struct timeval tv_now;
165 struct timeval tv_next;
166 struct timespec ts_wait;
168 step = atoi (COLLECTD_STEP);
169 if (step <= 0)
170 step = 10;
172 #if HAVE_LIBKSTAT
173 kc = NULL;
174 update_kstat ();
175 #endif
177 #if HAVE_LIBSTATGRAB
178 if (sg_init ())
179 {
180 syslog (LOG_ERR, "sg_init: %s", sg_str_error (sg_get_error ()));
181 return (-1);
182 }
184 if (sg_drop_privileges ())
185 {
186 syslog (LOG_ERR, "sg_drop_privileges: %s", sg_str_error (sg_get_error ()));
187 return (-1);
188 }
189 #endif
191 plugin_init_all ();
193 while (loop == 0)
194 {
195 if (gettimeofday (&tv_next, NULL) < 0)
196 {
197 syslog (LOG_ERR, "gettimeofday failed: %s", strerror (errno));
198 return (-1);
199 }
200 tv_next.tv_sec += step;
202 #if HAVE_LIBKSTAT
203 update_kstat ();
204 #endif
205 /* `curtime' is used by many (all?) plugins as the
206 * data-sample-time passed to RRDTool */
207 curtime = time (NULL);
209 /* Issue all plugins */
210 plugin_read_all (&loop);
212 if (gettimeofday (&tv_now, NULL) < 0)
213 {
214 syslog (LOG_ERR, "gettimeofday failed: %s", strerror (errno));
215 return (-1);
216 }
218 if (timeval_sub_timespec (&tv_next, &tv_now, &ts_wait) != 0)
219 {
220 syslog (LOG_WARNING, "Not sleeping because `timeval_sub_timespec' returned non-zero!");
221 continue;
222 }
224 while ((loop == 0) && (nanosleep (&ts_wait, &ts_wait) == -1))
225 {
226 if (errno != EINTR)
227 {
228 syslog (LOG_ERR, "nanosleep failed: %s", strerror (errno));
229 break;
230 }
231 }
232 }
234 return (0);
235 } /* static int start_client (void) */
237 #if HAVE_LIBRRD
238 static int start_server (void)
239 {
240 /* FIXME */
241 #if 0
242 char *host;
243 char *type;
244 char *instance;
245 char *values;
247 int error_counter = 0;
248 int status;
250 while ((loop == 0) && (error_counter < 3))
251 {
252 status = network_receive (&host, &type, &instance, &values);
254 if (status != 0)
255 {
256 if (status < 0)
257 error_counter++;
258 continue;
259 }
260 error_counter = 0;
262 plugin_write (host, type, instance, values);
264 if (host != NULL) free (host); host = NULL;
265 if (type != NULL) free (type); type = NULL;
266 if (instance != NULL) free (instance); instance = NULL;
267 if (values != NULL) free (values); values = NULL;
268 }
270 #endif
271 return (0);
272 } /* static int start_server (void) */
273 #endif /* HAVE_LIBRRD */
275 #if COLLECT_DAEMON
276 static int pidfile_create (void)
277 {
278 FILE *fh;
279 const char *file = global_option_get ("PIDFile");
281 if ((fh = fopen (file, "w")) == NULL)
282 {
283 syslog (LOG_ERR, "fopen (%s): %s", file, strerror (errno));
284 return (1);
285 }
287 fprintf (fh, "%i\n", (int) getpid ());
288 fclose(fh);
290 return (0);
291 } /* static int pidfile_create (const char *file) */
293 static int pidfile_remove (void)
294 {
295 const char *file = global_option_get ("PIDFile");
297 return (unlink (file));
298 } /* static int pidfile_remove (const char *file) */
299 #endif /* COLLECT_DAEMON */
301 int main (int argc, char **argv)
302 {
303 struct sigaction sigIntAction;
304 struct sigaction sigTermAction;
305 char *configfile = CONFIGFILE;
306 const char *datadir;
307 #if COLLECT_DAEMON
308 struct sigaction sigChldAction;
309 pid_t pid;
310 int daemonize = 1;
311 #endif
312 #if COLLECT_DEBUG
313 const char *logfile;
314 #endif
316 #if HAVE_LIBRRD
317 operating_mode = MODE_LOCAL;
318 #else
319 operating_mode = MODE_CLIENT;
320 #endif
322 /* open syslog */
323 openlog (PACKAGE, LOG_CONS | LOG_PID, LOG_DAEMON);
325 /* read options */
326 while (1)
327 {
328 int c;
330 c = getopt (argc, argv, "hC:"
331 #if COLLECT_DAEMON
332 "fP:"
333 #endif
334 );
336 if (c == -1)
337 break;
339 switch (c)
340 {
341 case 'C':
342 configfile = optarg;
343 break;
344 #if COLLECT_DAEMON
345 case 'P':
346 global_option_set ("PIDFile", optarg);
347 break;
348 case 'f':
349 daemonize = 0;
350 break;
351 #endif /* COLLECT_DAEMON */
352 case 'h':
353 default:
354 exit_usage (argv[0]);
355 } /* switch (c) */
356 } /* while (1) */
358 #if COLLECT_DEBUG
359 if ((logfile = global_option_get ("LogFile")) != NULL)
360 DBG_STARTFILE (logfile, "Debug file opened.");
361 #endif
363 /*
364 * Read options from the config file, the environment and the command
365 * line (in that order, with later options overwriting previous ones in
366 * general).
367 * Also, this will automatically load modules.
368 */
369 if (cf_read (configfile))
370 {
371 fprintf (stderr, "Error: Reading the config file failed!\n"
372 "Read the syslog for details.\n");
373 return (1);
374 }
376 /*
377 * Change directory. We do this _after_ reading the config and loading
378 * modules to relative paths work as expected.
379 */
380 if ((datadir = global_option_get ("BaseDir")) != NULL)
381 {
382 fprintf (stderr, "Don't have a datadir to use. This should not happen. Ever.");
383 return (1);
384 }
385 if (change_basedir (datadir))
386 {
387 fprintf (stderr, "Error: Unable to change to directory `%s'.\n", datadir);
388 return (1);
389 }
391 #if COLLECT_DAEMON
392 /*
393 * fork off child
394 */
395 sigChldAction.sa_handler = SIG_IGN;
396 sigaction (SIGCHLD, &sigChldAction, NULL);
398 if (daemonize)
399 {
400 if ((pid = fork ()) == -1)
401 {
402 /* error */
403 fprintf (stderr, "fork: %s", strerror (errno));
404 return (1);
405 }
406 else if (pid != 0)
407 {
408 /* parent */
409 /* printf ("Running (PID %i)\n", pid); */
410 return (0);
411 }
413 /* Detach from session */
414 setsid ();
416 /* Write pidfile */
417 if (pidfile_create ())
418 exit (2);
420 /* close standard descriptors */
421 close (2);
422 close (1);
423 close (0);
425 if (open ("/dev/null", O_RDWR) != 0)
426 {
427 syslog (LOG_ERR, "Error: Could not connect `STDIN' to `/dev/null'");
428 return (1);
429 }
430 if (dup (0) != 1)
431 {
432 syslog (LOG_ERR, "Error: Could not connect `STDOUT' to `/dev/null'");
433 return (1);
434 }
435 if (dup (0) != 2)
436 {
437 syslog (LOG_ERR, "Error: Could not connect `STDERR' to `/dev/null'");
438 return (1);
439 }
440 } /* if (daemonize) */
441 #endif /* COLLECT_DAEMON */
443 /*
444 * install signal handlers
445 */
446 sigIntAction.sa_handler = sigIntHandler;
447 sigaction (SIGINT, &sigIntAction, NULL);
449 sigTermAction.sa_handler = sigTermHandler;
450 sigaction (SIGTERM, &sigTermAction, NULL);
452 /*
453 * run the actual loops
454 */
455 #if HAVE_LIBRRD
456 if (operating_mode == MODE_SERVER)
457 start_server ();
458 else /* if (operating_mode == MODE_CLIENT || operating_mode == MODE_LOCAL || operating_mode == MODE_LOG) */
459 #endif
460 start_client ();
462 plugin_shutdown_all ();
464 #if COLLECT_DEBUG
465 if (logfile != NULL)
466 DBG_STOPFILE("debug file closed.");
467 #endif
469 /* close syslog */
470 syslog (LOG_INFO, "Exiting normally");
471 closelog ();
473 #if COLLECT_DAEMON
474 if (daemonize)
475 pidfile_remove ();
476 #endif /* COLLECT_DAEMON */
478 return (0);
479 } /* int main (int argc, char **argv) */