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"
26 #include "network.h"
27 #include "plugin.h"
28 #include "configfile.h"
30 /*
31 * Global variables
32 */
33 char hostname_g[DATA_MAX_NAME_LEN];
34 int interval_g;
35 #if HAVE_LIBKSTAT
36 kstat_ctl_t *kc;
37 #endif /* HAVE_LIBKSTAT */
39 static int loop = 0;
41 static void sigIntHandler (int signal)
42 {
43 loop++;
44 }
46 static void sigTermHandler (int signal)
47 {
48 loop++;
49 }
51 static int init_global_variables (void)
52 {
53 const char *str;
55 str = global_option_get ("Hostname");
56 if (str != NULL)
57 {
58 strncpy (hostname_g, str, sizeof (hostname_g));
59 }
60 else
61 {
62 if (gethostname (hostname_g, sizeof (hostname_g)) != 0)
63 {
64 fprintf (stderr, "`gethostname' failed and no "
65 "hostname was configured.\n");
66 return (-1);
67 }
68 }
69 DEBUG ("hostname_g = %s;", hostname_g);
71 str = global_option_get ("Interval");
72 if (str == NULL)
73 str = COLLECTD_STEP;
74 interval_g = atoi (str);
75 if (interval_g <= 0)
76 {
77 fprintf (stderr, "Cannot set the interval to a correct value.\n"
78 "Please check your settings.\n");
79 return (-1);
80 }
81 DEBUG ("interval_g = %i;", interval_g);
83 return (0);
84 } /* int init_global_variables */
86 static int change_basedir (const char *orig_dir)
87 {
88 char *dir = strdup (orig_dir);
89 int dirlen;
90 int status;
92 if (dir == NULL)
93 {
94 char errbuf[1024];
95 ERROR ("strdup failed: %s",
96 sstrerror (errno, errbuf, sizeof (errbuf)));
97 return (-1);
98 }
100 dirlen = strlen (dir);
101 while ((dirlen > 0) && (dir[dirlen - 1] == '/'))
102 dir[--dirlen] = '\0';
104 if (dirlen <= 0)
105 return (-1);
107 status = chdir (dir);
108 free (dir);
110 if (status != 0)
111 {
112 if (errno == ENOENT)
113 {
114 if (mkdir (orig_dir, 0755) == -1)
115 {
116 char errbuf[1024];
117 ERROR ("mkdir (%s): %s", orig_dir,
118 sstrerror (errno, errbuf,
119 sizeof (errbuf)));
120 return (-1);
121 }
122 else if (chdir (orig_dir) == -1)
123 {
124 char errbuf[1024];
125 ERROR ("chdir (%s): %s", orig_dir,
126 sstrerror (errno, errbuf,
127 sizeof (errbuf)));
128 return (-1);
129 }
130 }
131 else
132 {
133 char errbuf[1024];
134 ERROR ("chdir (%s): %s", orig_dir,
135 sstrerror (errno, errbuf,
136 sizeof (errbuf)));
137 return (-1);
138 }
139 }
141 return (0);
142 } /* static int change_basedir (char *dir) */
144 #if HAVE_LIBKSTAT
145 static void update_kstat (void)
146 {
147 if (kc == NULL)
148 {
149 if ((kc = kstat_open ()) == NULL)
150 ERROR ("Unable to open kstat control structure");
151 }
152 else
153 {
154 kid_t kid;
155 kid = kstat_chain_update (kc);
156 if (kid > 0)
157 {
158 INFO ("kstat chain has been updated");
159 plugin_init_all ();
160 }
161 else if (kid < 0)
162 ERROR ("kstat chain update failed");
163 /* else: everything works as expected */
164 }
166 return;
167 } /* static void update_kstat (void) */
168 #endif /* HAVE_LIBKSTAT */
170 /* TODO
171 * Remove all settings but `-f' and `-C'
172 */
173 static void exit_usage (char *name)
174 {
175 printf ("Usage: "PACKAGE" [OPTIONS]\n\n"
177 "Available options:\n"
178 " General:\n"
179 " -C <file> Configuration file.\n"
180 " Default: "CONFIGFILE"\n"
181 " -P <file> PID-file.\n"
182 " Default: "PIDFILE"\n"
183 #if COLLECT_DAEMON
184 " -f Don't fork to the background.\n"
185 #endif
186 "\nBuiltin defaults:\n"
187 " Config-File "CONFIGFILE"\n"
188 " PID-File "PIDFILE"\n"
189 " Data-Directory "PKGLOCALSTATEDIR"\n"
190 #if COLLECT_DEBUG
191 " Log-File "LOGFILE"\n"
192 #endif
193 " Step "COLLECTD_STEP" seconds\n"
194 " Heartbeat "COLLECTD_HEARTBEAT" seconds\n"
195 "\n"PACKAGE" "VERSION", http://collectd.org/\n"
196 "by Florian octo Forster <octo@verplant.org>\n"
197 "for contributions see `AUTHORS'\n");
198 exit (0);
199 } /* static void exit_usage (char *name) */
201 static int do_init (void)
202 {
203 #if HAVE_LIBKSTAT
204 kc = NULL;
205 update_kstat ();
206 #endif
208 #if HAVE_LIBSTATGRAB
209 if (sg_init ())
210 {
211 ERROR ("sg_init: %s", sg_str_error (sg_get_error ()));
212 return (-1);
213 }
215 if (sg_drop_privileges ())
216 {
217 ERROR ("sg_drop_privileges: %s", sg_str_error (sg_get_error ()));
218 return (-1);
219 }
220 #endif
222 plugin_init_all ();
224 return (0);
225 } /* int do_init () */
228 static int do_loop (void)
229 {
230 struct timeval tv_now;
231 struct timeval tv_next;
232 struct timespec ts_wait;
234 while (loop == 0)
235 {
236 if (gettimeofday (&tv_next, NULL) < 0)
237 {
238 char errbuf[1024];
239 ERROR ("gettimeofday failed: %s",
240 sstrerror (errno, errbuf,
241 sizeof (errbuf)));
242 return (-1);
243 }
244 tv_next.tv_sec += interval_g;
246 #if HAVE_LIBKSTAT
247 update_kstat ();
248 #endif
250 /* Issue all plugins */
251 plugin_read_all (&loop);
253 if (gettimeofday (&tv_now, NULL) < 0)
254 {
255 char errbuf[1024];
256 ERROR ("gettimeofday failed: %s",
257 sstrerror (errno, errbuf,
258 sizeof (errbuf)));
259 return (-1);
260 }
262 if (timeval_sub_timespec (&tv_next, &tv_now, &ts_wait) != 0)
263 {
264 WARNING ("Not sleeping because "
265 "`timeval_sub_timespec' returned "
266 "non-zero!");
267 continue;
268 }
270 while ((loop == 0) && (nanosleep (&ts_wait, &ts_wait) == -1))
271 {
272 if (errno != EINTR)
273 {
274 char errbuf[1024];
275 ERROR ("nanosleep failed: %s",
276 sstrerror (errno, errbuf,
277 sizeof (errbuf)));
278 return (-1);
279 }
280 }
281 } /* while (loop == 0) */
283 DEBUG ("return (0);");
284 return (0);
285 } /* int do_loop */
287 static int do_shutdown (void)
288 {
289 plugin_shutdown_all ();
290 return (0);
291 } /* int do_shutdown */
293 #if COLLECT_DAEMON
294 static int pidfile_create (void)
295 {
296 FILE *fh;
297 const char *file = global_option_get ("PIDFile");
299 if ((fh = fopen (file, "w")) == NULL)
300 {
301 char errbuf[1024];
302 ERROR ("fopen (%s): %s", file,
303 sstrerror (errno, errbuf, sizeof (errbuf)));
304 return (1);
305 }
307 fprintf (fh, "%i\n", (int) getpid ());
308 fclose(fh);
310 return (0);
311 } /* static int pidfile_create (const char *file) */
313 static int pidfile_remove (void)
314 {
315 const char *file = global_option_get ("PIDFile");
317 DEBUG ("unlink (%s)", (file != NULL) ? file : "<null>");
318 return (unlink (file));
319 } /* static int pidfile_remove (const char *file) */
320 #endif /* COLLECT_DAEMON */
322 int main (int argc, char **argv)
323 {
324 struct sigaction sigIntAction;
325 struct sigaction sigTermAction;
326 char *configfile = CONFIGFILE;
327 const char *basedir;
328 #if COLLECT_DAEMON
329 struct sigaction sigChldAction;
330 pid_t pid;
331 int daemonize = 1;
332 #endif
334 /* read options */
335 while (1)
336 {
337 int c;
339 c = getopt (argc, argv, "hC:"
340 #if COLLECT_DAEMON
341 "fP:"
342 #endif
343 );
345 if (c == -1)
346 break;
348 switch (c)
349 {
350 case 'C':
351 configfile = optarg;
352 break;
353 #if COLLECT_DAEMON
354 case 'P':
355 global_option_set ("PIDFile", optarg);
356 break;
357 case 'f':
358 daemonize = 0;
359 break;
360 #endif /* COLLECT_DAEMON */
361 case 'h':
362 default:
363 exit_usage (argv[0]);
364 } /* switch (c) */
365 } /* while (1) */
367 /*
368 * Read options from the config file, the environment and the command
369 * line (in that order, with later options overwriting previous ones in
370 * general).
371 * Also, this will automatically load modules.
372 */
373 if (cf_read (configfile))
374 {
375 fprintf (stderr, "Error: Reading the config file failed!\n"
376 "Read the syslog for details.\n");
377 return (1);
378 }
380 /*
381 * Change directory. We do this _after_ reading the config and loading
382 * modules to relative paths work as expected.
383 */
384 if ((basedir = global_option_get ("BaseDir")) == NULL)
385 {
386 fprintf (stderr, "Don't have a basedir to use. This should not happen. Ever.");
387 return (1);
388 }
389 else if (change_basedir (basedir))
390 {
391 fprintf (stderr, "Error: Unable to change to directory `%s'.\n", basedir);
392 return (1);
393 }
395 /*
396 * Set global variables or, if that failes, exit. We cannot run with
397 * them being uninitialized. If nothing is configured, then defaults
398 * are being used. So this means that the user has actually done
399 * something wrong.
400 */
401 if (init_global_variables () != 0)
402 return (1);
404 #if COLLECT_DAEMON
405 /*
406 * fork off child
407 */
408 memset (&sigChldAction, '\0', sizeof (sigChldAction));
409 sigChldAction.sa_handler = SIG_IGN;
410 sigaction (SIGCHLD, &sigChldAction, NULL);
412 if (daemonize)
413 {
414 if ((pid = fork ()) == -1)
415 {
416 /* error */
417 char errbuf[1024];
418 fprintf (stderr, "fork: %s",
419 sstrerror (errno, errbuf,
420 sizeof (errbuf)));
421 return (1);
422 }
423 else if (pid != 0)
424 {
425 /* parent */
426 /* printf ("Running (PID %i)\n", pid); */
427 return (0);
428 }
430 /* Detach from session */
431 setsid ();
433 /* Write pidfile */
434 if (pidfile_create ())
435 exit (2);
437 /* close standard descriptors */
438 close (2);
439 close (1);
440 close (0);
442 if (open ("/dev/null", O_RDWR) != 0)
443 {
444 ERROR ("Error: Could not connect `STDIN' to `/dev/null'");
445 return (1);
446 }
447 if (dup (0) != 1)
448 {
449 ERROR ("Error: Could not connect `STDOUT' to `/dev/null'");
450 return (1);
451 }
452 if (dup (0) != 2)
453 {
454 ERROR ("Error: Could not connect `STDERR' to `/dev/null'");
455 return (1);
456 }
457 } /* if (daemonize) */
458 #endif /* COLLECT_DAEMON */
460 /*
461 * install signal handlers
462 */
463 memset (&sigIntAction, '\0', sizeof (sigIntAction));
464 sigIntAction.sa_handler = sigIntHandler;
465 sigaction (SIGINT, &sigIntAction, NULL);
467 memset (&sigTermAction, '\0', sizeof (sigTermAction));
468 sigTermAction.sa_handler = sigTermHandler;
469 sigaction (SIGTERM, &sigTermAction, NULL);
471 /*
472 * run the actual loops
473 */
474 do_init ();
475 do_loop ();
477 /* close syslog */
478 INFO ("Exiting normally");
480 do_shutdown ();
482 #if COLLECT_DAEMON
483 if (daemonize)
484 pidfile_remove ();
485 #endif /* COLLECT_DAEMON */
487 return (0);
488 } /* int main */