1 /**
2 * collectd - src/collectd.c
3 * Copyright (C) 2005,2006 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 (char *dir)
54 {
55 int dirlen = strlen (dir);
57 while ((dirlen > 0) && (dir[dirlen - 1] == '/'))
58 dir[--dirlen] = '\0';
60 if (dirlen <= 0)
61 return (-1);
63 if (chdir (dir) == -1)
64 {
65 if (errno == ENOENT)
66 {
67 if (mkdir (dir, 0755) == -1)
68 {
69 syslog (LOG_ERR, "mkdir (%s): %s", dir, strerror (errno));
70 return (-1);
71 }
72 else if (chdir (dir) == -1)
73 {
74 syslog (LOG_ERR, "chdir (%s): %s", dir, strerror (errno));
75 return (-1);
76 }
77 }
78 else
79 {
80 syslog (LOG_ERR, "chdir: %s", strerror (errno));
81 return (-1);
82 }
83 }
85 return (0);
86 } /* static int change_basedir (char *dir) */
88 #if HAVE_LIBKSTAT
89 static void update_kstat (void)
90 {
91 if (kc == NULL)
92 {
93 if ((kc = kstat_open ()) == NULL)
94 syslog (LOG_ERR, "Unable to open kstat control structure");
95 }
96 else
97 {
98 kid_t kid;
99 kid = kstat_chain_update (kc);
100 if (kid > 0)
101 {
102 syslog (LOG_INFO, "kstat chain has been updated");
103 plugin_init_all ();
104 }
105 else if (kid < 0)
106 syslog (LOG_ERR, "kstat chain update failed");
107 /* else: everything works as expected */
108 }
110 return;
111 } /* static void update_kstat (void) */
112 #endif /* HAVE_LIBKSTAT */
114 /* TODO
115 * Remove all settings but `-f' and `-C'
116 */
117 static void exit_usage (char *name)
118 {
119 printf ("Usage: "PACKAGE" [OPTIONS]\n\n"
121 "Available options:\n"
122 " General:\n"
123 " -C <file> Configuration file.\n"
124 " Default: "CONFIGFILE"\n"
125 " -P <file> PID-file.\n"
126 " Default: "PIDFILE"\n"
127 #if COLLECT_DAEMON
128 " -f Don't fork to the background.\n"
129 #endif
130 "\nBuiltin defaults:\n"
131 " Config-File "CONFIGFILE"\n"
132 " PID-File "PIDFILE"\n"
133 " Data-Directory "PKGLOCALSTATEDIR"\n"
134 #if COLLECT_DEBUG
135 " Log-File "LOGFILE"\n"
136 #endif
137 " Step "COLLECTD_STEP" seconds\n"
138 " Heartbeat "COLLECTD_HEARTBEAT" seconds\n"
139 "\n"PACKAGE" "VERSION", http://collectd.org/\n"
140 "by Florian octo Forster <octo@verplant.org>\n"
141 "for contributions see `AUTHORS'\n");
142 exit (0);
143 } /* static void exit_usage (char *name) */
145 static int start_client (void)
146 {
147 int step;
149 struct timeval tv_now;
150 struct timeval tv_next;
151 struct timespec ts_wait;
153 step = atoi (COLLECTD_STEP);
154 if (step <= 0)
155 step = 10;
157 #if HAVE_LIBKSTAT
158 kc = NULL;
159 update_kstat ();
160 #endif
162 #if HAVE_LIBSTATGRAB
163 if (sg_init ())
164 {
165 syslog (LOG_ERR, "sg_init: %s", sg_str_error (sg_get_error ()));
166 return (-1);
167 }
169 if (sg_drop_privileges ())
170 {
171 syslog (LOG_ERR, "sg_drop_privileges: %s", sg_str_error (sg_get_error ()));
172 return (-1);
173 }
174 #endif
176 plugin_init_all ();
178 while (loop == 0)
179 {
180 if (gettimeofday (&tv_next, NULL) < 0)
181 {
182 syslog (LOG_ERR, "gettimeofday failed: %s", strerror (errno));
183 return (-1);
184 }
185 tv_next.tv_sec += step;
187 #if HAVE_LIBKSTAT
188 update_kstat ();
189 #endif
190 /* `curtime' is used by many (all?) plugins as the
191 * data-sample-time passed to RRDTool */
192 curtime = time (NULL);
194 /* Issue all plugins */
195 plugin_read_all (&loop);
197 if (gettimeofday (&tv_now, NULL) < 0)
198 {
199 syslog (LOG_ERR, "gettimeofday failed: %s", strerror (errno));
200 return (-1);
201 }
203 if (timeval_sub_timespec (&tv_next, &tv_now, &ts_wait) != 0)
204 {
205 syslog (LOG_WARNING, "Not sleeping because `timeval_sub_timespec' returned non-zero!");
206 continue;
207 }
209 while ((loop == 0) && (nanosleep (&ts_wait, &ts_wait) == -1))
210 {
211 if (errno != EINTR)
212 {
213 syslog (LOG_ERR, "nanosleep failed: %s", strerror (errno));
214 break;
215 }
216 }
217 }
219 return (0);
220 } /* static int start_client (void) */
222 #if HAVE_LIBRRD
223 static int start_server (void)
224 {
225 /* FIXME */
226 #if 0
227 char *host;
228 char *type;
229 char *instance;
230 char *values;
232 int error_counter = 0;
233 int status;
235 while ((loop == 0) && (error_counter < 3))
236 {
237 status = network_receive (&host, &type, &instance, &values);
239 if (status != 0)
240 {
241 if (status < 0)
242 error_counter++;
243 continue;
244 }
245 error_counter = 0;
247 plugin_write (host, type, instance, values);
249 if (host != NULL) free (host); host = NULL;
250 if (type != NULL) free (type); type = NULL;
251 if (instance != NULL) free (instance); instance = NULL;
252 if (values != NULL) free (values); values = NULL;
253 }
255 #endif
256 return (0);
257 } /* static int start_server (void) */
258 #endif /* HAVE_LIBRRD */
260 #if COLLECT_DAEMON
261 static int pidfile_create (const char *file)
262 {
263 FILE *fh;
265 if (file == NULL)
266 file = PIDFILE;
268 if ((fh = fopen (file, "w")) == NULL)
269 {
270 syslog (LOG_ERR, "fopen (%s): %s", file, strerror (errno));
271 return (1);
272 }
274 fprintf (fh, "%i\n", (int) getpid ());
275 fclose(fh);
277 return (0);
278 } /* static int pidfile_create (const char *file) */
279 #endif /* COLLECT_DAEMON */
281 #if COLLECT_DAEMON
282 static int pidfile_remove (const char *file)
283 {
284 if (file == NULL) {
285 file = PIDFILE;
286 }
287 return (unlink (file));
288 } /* static int pidfile_remove (const char *file) */
289 #endif /* COLLECT_DAEMON */
291 int main (int argc, char **argv)
292 {
293 struct sigaction sigIntAction;
294 struct sigaction sigTermAction;
295 char *datadir = PKGLOCALSTATEDIR;
296 char *configfile = CONFIGFILE;
297 #if COLLECT_DAEMON
298 struct sigaction sigChldAction;
299 char *pidfile = NULL;
300 pid_t pid;
301 int daemonize = 1;
302 #endif
303 #if COLLECT_DEBUG
304 char *logfile = LOGFILE;
305 #endif
307 #if HAVE_LIBRRD
308 operating_mode = MODE_LOCAL;
309 #else
310 operating_mode = MODE_CLIENT;
311 #endif
313 /* open syslog */
314 openlog (PACKAGE, LOG_CONS | LOG_PID, LOG_DAEMON);
316 /* read options */
317 while (1)
318 {
319 int c;
321 c = getopt (argc, argv, "hC:"
322 #if COLLECT_DAEMON
323 "fP:"
324 #endif
325 );
327 if (c == -1)
328 break;
330 switch (c)
331 {
332 case 'C':
333 configfile = optarg;
334 break;
335 #if COLLECT_DAEMON
336 case 'P':
337 pidfile = optarg;
338 break;
339 case 'f':
340 daemonize = 0;
341 break;
342 #endif /* COLLECT_DAEMON */
343 case 'h':
344 default:
345 exit_usage (argv[0]);
346 } /* switch (c) */
347 } /* while (1) */
349 #if COLLECT_DEBUG
350 if ((logfile = cf_get_option ("LogFile", LOGFILE)) != NULL)
351 DBG_STARTFILE (logfile, "Debug file opened.");
352 #endif
354 /*
355 * Read options from the config file, the environment and the command
356 * line (in that order, with later options overwriting previous ones in
357 * general).
358 * Also, this will automatically load modules.
359 */
360 if (cf_read (configfile))
361 {
362 fprintf (stderr, "Error: Reading the config file failed!\n"
363 "Read the syslog for details.\n");
364 return (1);
365 }
367 /*
368 * Change directory. We do this _after_ reading the config and loading
369 * modules to relative paths work as expected.
370 */
371 if ((datadir = cf_get_option ("DataDir", PKGLOCALSTATEDIR)) == NULL)
372 {
373 fprintf (stderr, "Don't have a datadir to use. This should not happen. Ever.");
374 return (1);
375 }
376 if (change_basedir (datadir))
377 {
378 fprintf (stderr, "Error: Unable to change to directory `%s'.\n", datadir);
379 return (1);
380 }
382 #if COLLECT_DAEMON
383 /*
384 * fork off child
385 */
386 sigChldAction.sa_handler = SIG_IGN;
387 sigaction (SIGCHLD, &sigChldAction, NULL);
389 if ((pidfile == NULL)
390 && ((pidfile = cf_get_option ("PIDFile", PIDFILE)) == NULL))
391 {
392 fprintf (stderr, "Cannot obtain pidfile. This shoud not happen. Ever.");
393 return (1);
394 }
396 if (daemonize)
397 {
398 if ((pid = fork ()) == -1)
399 {
400 /* error */
401 fprintf (stderr, "fork: %s", strerror (errno));
402 return (1);
403 }
404 else if (pid != 0)
405 {
406 /* parent */
407 /* printf ("Running (PID %i)\n", pid); */
408 return (0);
409 }
411 /* Detach from session */
412 setsid ();
414 /* Write pidfile */
415 if (pidfile_create (pidfile))
416 exit (2);
418 /* close standard descriptors */
419 close (2);
420 close (1);
421 close (0);
423 if (open ("/dev/null", O_RDWR) != 0)
424 {
425 syslog (LOG_ERR, "Error: Could not connect `STDIN' to `/dev/null'");
426 return (1);
427 }
428 if (dup (0) != 1)
429 {
430 syslog (LOG_ERR, "Error: Could not connect `STDOUT' to `/dev/null'");
431 return (1);
432 }
433 if (dup (0) != 2)
434 {
435 syslog (LOG_ERR, "Error: Could not connect `STDERR' to `/dev/null'");
436 return (1);
437 }
438 } /* if (daemonize) */
439 #endif /* COLLECT_DAEMON */
441 /*
442 * install signal handlers
443 */
444 sigIntAction.sa_handler = sigIntHandler;
445 sigaction (SIGINT, &sigIntAction, NULL);
447 sigTermAction.sa_handler = sigTermHandler;
448 sigaction (SIGTERM, &sigTermAction, NULL);
450 /*
451 * run the actual loops
452 */
453 #if HAVE_LIBRRD
454 if (operating_mode == MODE_SERVER)
455 start_server ();
456 else /* if (operating_mode == MODE_CLIENT || operating_mode == MODE_LOCAL || operating_mode == MODE_LOG) */
457 #endif
458 start_client ();
460 plugin_shutdown_all ();
462 #if COLLECT_DEBUG
463 if (logfile != NULL)
464 DBG_STOPFILE("debug file closed.");
465 #endif
467 /* close syslog */
468 syslog (LOG_INFO, "Exiting normally");
469 closelog ();
471 #if COLLECT_DAEMON
472 if (daemonize)
473 pidfile_remove (pidfile);
474 #endif /* COLLECT_DAEMON */
476 return (0);
477 } /* int main (int argc, char **argv) */