1 /**
2 * collectd - src/collectd.c
3 * Copyright (C) 2005 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;
43 #if HAVE_LIBRRD
44 int operating_mode;
45 #endif
47 static void sigIntHandler (int signal)
48 {
49 loop++;
50 }
52 static void sigTermHandler (int signal)
53 {
54 loop++;
55 }
57 static int change_basedir (char *dir)
58 {
59 int dirlen = strlen (dir);
61 while ((dirlen > 0) && (dir[dirlen - 1] == '/'))
62 dir[--dirlen] = '\0';
64 if (dirlen <= 0)
65 return (-1);
67 if (chdir (dir) == -1)
68 {
69 if (errno == ENOENT)
70 {
71 if (mkdir (dir, 0755) == -1)
72 {
73 syslog (LOG_ERR, "mkdir (%s): %s", dir, strerror (errno));
74 return (-1);
75 }
76 else if (chdir (dir) == -1)
77 {
78 syslog (LOG_ERR, "chdir (%s): %s", dir, strerror (errno));
79 return (-1);
80 }
81 }
82 else
83 {
84 syslog (LOG_ERR, "chdir: %s", strerror (errno));
85 return (-1);
86 }
87 }
89 return (0);
90 } /* static int change_basedir (char *dir) */
92 #if HAVE_LIBKSTAT
93 static void update_kstat (void)
94 {
95 if (kc == NULL)
96 {
97 if ((kc = kstat_open ()) == NULL)
98 syslog (LOG_ERR, "Unable to open kstat control structure");
99 }
100 else
101 {
102 kid_t kid;
103 kid = kstat_chain_update (kc);
104 if (kid > 0)
105 {
106 syslog (LOG_INFO, "kstat chain has been updated");
107 plugin_init_all ();
108 }
109 else if (kid < 0)
110 syslog (LOG_ERR, "kstat chain update failed");
111 /* else: everything works as expected */
112 }
114 return;
115 } /* static void update_kstat (void) */
116 #endif /* HAVE_LIBKSTAT */
118 /* TODO
119 * Remove all settings but `-f' and `-C'
120 */
121 static void exit_usage (char *name)
122 {
123 printf ("Usage: "PACKAGE" [OPTIONS]\n\n"
125 "Available options:\n"
126 " General:\n"
127 " -C <file> Configuration file.\n"
128 " Default: "CONFIGFILE"\n"
129 #if COLLECT_DAEMON
130 " -f Don't fork to the background.\n"
131 #endif
132 "\nBuiltin defaults:\n"
133 " Config-File "CONFIGFILE"\n"
134 " PID-File "PIDFILE"\n"
135 " Data-Directory "PKGLOCALSTATEDIR"\n"
136 #if COLLECT_DEBUG
137 " Log-File "LOGFILE"\n"
138 #endif
139 " Step "COLLECTD_STEP" seconds\n"
140 " Heartbeat "COLLECTD_HEARTBEAT" seconds\n"
141 "\n"PACKAGE" "VERSION", http://verplant.org/collectd/\n"
142 "by Florian octo Forster <octo@verplant.org>\n"
143 "for contributions see `AUTHORS'\n");
144 exit (0);
145 } /* static void exit_usage (char *name) */
147 static int start_client (void)
148 {
149 int step;
151 struct timeval tv_now;
152 struct timeval tv_next;
153 struct timespec ts_wait;
155 step = atoi (COLLECTD_STEP);
156 if (step <= 0)
157 step = 10;
159 #if HAVE_LIBKSTAT
160 kc = NULL;
161 update_kstat ();
162 #endif
164 #if HAVE_LIBSTATGRAB
165 if (sg_init ())
166 {
167 syslog (LOG_ERR, "sg_init: %s", sg_str_error (sg_get_error ()));
168 return (-1);
169 }
171 if (sg_drop_privileges ())
172 {
173 syslog (LOG_ERR, "sg_drop_privileges: %s", sg_str_error (sg_get_error ()));
174 return (-1);
175 }
176 #endif
178 plugin_init_all ();
180 while (loop == 0)
181 {
182 if (gettimeofday (&tv_next, NULL) < 0)
183 {
184 syslog (LOG_ERR, "gettimeofday failed: %s", strerror (errno));
185 return (-1);
186 }
187 tv_next.tv_sec += step;
189 #if HAVE_LIBKSTAT
190 update_kstat ();
191 #endif
192 /* `curtime' is used by many (all?) plugins as the
193 * data-sample-time passed to RRDTool */
194 curtime = time (NULL);
196 /* Issue all plugins */
197 plugin_read_all ();
199 if (gettimeofday (&tv_now, NULL) < 0)
200 {
201 syslog (LOG_ERR, "gettimeofday failed: %s", strerror (errno));
202 return (-1);
203 }
205 if (timeval_sub_timespec (&tv_next, &tv_now, &ts_wait) != 0)
206 {
207 syslog (LOG_WARNING, "No sleeping because `timeval_sub_timespec' returned non-zero!");
208 continue;
209 }
211 while (nanosleep (&ts_wait, &ts_wait) == -1)
212 {
213 if (errno != EINTR)
214 {
215 syslog (LOG_ERR, "nanosleep failed: %s", strerror (errno));
216 break;
217 }
218 }
219 }
221 return (0);
222 } /* static int start_client (void) */
224 #if HAVE_LIBRRD
225 static int start_server (void)
226 {
227 /* FIXME use stack here! */
228 char *host;
229 char *type;
230 char *instance;
231 char *values;
233 while (loop == 0)
234 {
235 if (network_receive (&host, &type, &instance, &values) == 0)
236 plugin_write (host, type, instance, values);
238 if (host != NULL) free (host); host = NULL;
239 if (type != NULL) free (type); type = NULL;
240 if (instance != NULL) free (instance); instance = NULL;
241 if (values != NULL) free (values); values = NULL;
242 }
244 return (0);
245 } /* static int start_server (void) */
246 #endif /* HAVE_LIBRRD */
248 #if COLLECT_DAEMON
249 static int pidfile_create (const char *file)
250 {
251 FILE *fh;
253 if (file == NULL)
254 file = PIDFILE;
256 if ((fh = fopen (file, "w")) == NULL)
257 {
258 syslog (LOG_ERR, "fopen (%s): %s", file, strerror (errno));
259 return (1);
260 }
262 fprintf (fh, "%d\n", getpid());
263 fclose(fh);
265 return (0);
266 } /* static int pidfile_create (const char *file) */
267 #endif /* COLLECT_DAEMON */
269 #if COLLECT_DAEMON
270 static int pidfile_remove (const char *file)
271 {
272 if (file == NULL) {
273 file = PIDFILE;
274 }
275 return (unlink (file));
276 } /* static int pidfile_remove (const char *file) */
277 #endif /* COLLECT_DAEMON */
279 int main (int argc, char **argv)
280 {
281 struct sigaction sigIntAction;
282 struct sigaction sigTermAction;
283 char *datadir = PKGLOCALSTATEDIR;
284 char *configfile = CONFIGFILE;
285 #if COLLECT_DAEMON
286 struct sigaction sigChldAction;
287 char *pidfile = PIDFILE;
288 pid_t pid;
289 int daemonize = 1;
290 #endif
291 #if COLLECT_DEBUG
292 char *logfile = LOGFILE;
293 #endif
295 #if HAVE_LIBRRD
296 operating_mode = MODE_LOCAL;
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 "f"
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 'f':
323 daemonize = 0;
324 break;
325 #endif /* COLLECT_DAEMON */
326 case 'h':
327 default:
328 exit_usage (argv[0]);
329 } /* switch (c) */
330 } /* while (1) */
332 #if COLLECT_DEBUG
333 if ((logfile = cf_get_option ("LogFile", LOGFILE)) != NULL)
334 DBG_STARTFILE (logfile, "Debug file opened.");
335 #endif
337 /*
338 * Read options from the config file, the environment and the command
339 * line (in that order, with later options overwriting previous ones in
340 * general).
341 * Also, this will automatically load modules.
342 */
343 if (cf_read (configfile))
344 {
345 fprintf (stderr, "Error: Reading the config file failed!\n"
346 "Read the syslog for details.\n");
347 return (1);
348 }
350 /*
351 * Change directory. We do this _after_ reading the config and loading
352 * modules to relative paths work as expected.
353 */
354 if ((datadir = cf_get_option ("DataDir", PKGLOCALSTATEDIR)) == NULL)
355 {
356 fprintf (stderr, "Don't have a datadir to use. This should not happen. Ever.");
357 return (1);
358 }
359 if (change_basedir (datadir))
360 {
361 fprintf (stderr, "Error: Unable to change to directory `%s'.\n", datadir);
362 return (1);
363 }
365 #if COLLECT_DAEMON
366 /*
367 * fork off child
368 */
369 sigChldAction.sa_handler = SIG_IGN;
370 sigaction (SIGCHLD, &sigChldAction, NULL);
372 if ((pidfile = cf_get_option ("PIDFile", PIDFILE)) == NULL)
373 {
374 fprintf (stderr, "Cannot obtain pidfile. This shoud not happen. Ever.");
375 return (1);
376 }
378 if (daemonize)
379 {
380 if ((pid = fork ()) == -1)
381 {
382 /* error */
383 fprintf (stderr, "fork: %s", strerror (errno));
384 return (1);
385 }
386 else if (pid != 0)
387 {
388 /* parent */
389 /* printf ("Running (PID %i)\n", pid); */
390 return (0);
391 }
393 /* Detach from session */
394 setsid ();
396 /* Write pidfile */
397 if (pidfile_create (pidfile))
398 exit (2);
400 /* close standard descriptors */
401 close (2);
402 close (1);
403 close (0);
405 if (open ("/dev/null", O_RDWR) != 0)
406 {
407 syslog (LOG_ERR, "Error: Could not connect `STDIN' to `/dev/null'");
408 return (1);
409 }
410 if (dup (0) != 1)
411 {
412 syslog (LOG_ERR, "Error: Could not connect `STDOUT' to `/dev/null'");
413 return (1);
414 }
415 if (dup (0) != 2)
416 {
417 syslog (LOG_ERR, "Error: Could not connect `STDERR' to `/dev/null'");
418 return (1);
419 }
420 } /* if (daemonize) */
421 #endif /* COLLECT_DAEMON */
423 /*
424 * install signal handlers
425 */
426 sigIntAction.sa_handler = sigIntHandler;
427 sigaction (SIGINT, &sigIntAction, NULL);
429 sigTermAction.sa_handler = sigTermHandler;
430 sigaction (SIGTERM, &sigTermAction, NULL);
432 /*
433 * run the actual loops
434 */
435 #if HAVE_LIBRRD
436 if (operating_mode == MODE_SERVER)
437 start_server ();
438 else /* if (operating_mode == MODE_CLIENT || operating_mode == MODE_LOCAL) */
439 #endif
440 start_client ();
442 #if COLLECT_DEBUG
443 if (logfile != NULL)
444 DBG_STOPFILE("debug file closed.");
445 #endif
447 /* close syslog */
448 syslog (LOG_INFO, "Exiting normally");
449 closelog ();
451 #if COLLECT_DAEMON
452 if (daemonize)
453 pidfile_remove (pidfile);
454 #endif /* COLLECT_DAEMON */
456 return (0);
457 } /* int main (int argc, char **argv) */