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