e8081785564cacc572cb5161907d48166606aa7b
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 "common.h"
25 #include "utils_debug.h"
27 #include "multicast.h"
28 #include "plugin.h"
29 #include "configfile.h"
31 #include "ping.h"
33 static int loop = 0;
35 #if HAVE_LIBKSTAT
36 kstat_ctl_t *kc;
37 #endif /* HAVE_LIBKSTAT */
39 #if COLLECT_PING
40 char *pinghosts[MAX_PINGHOSTS];
41 int num_pinghosts = 0;
42 #endif
44 /*
45 * exported variables
46 */
47 time_t curtime;
49 #if HAVE_LIBRRD
50 int operating_mode;
51 #endif
53 static void sigIntHandler (int signal)
54 {
55 loop++;
56 }
58 static void sigTermHandler (int signal)
59 {
60 loop++;
61 }
63 static int change_basedir (char *dir)
64 {
65 int dirlen = strlen (dir);
67 while ((dirlen > 0) && (dir[dirlen - 1] == '/'))
68 dir[--dirlen] = '\0';
70 if (dirlen <= 0)
71 return (-1);
73 if (chdir (dir) == -1)
74 {
75 if (errno == ENOENT)
76 {
77 if (mkdir (dir, 0755) == -1)
78 {
79 syslog (LOG_ERR, "mkdir (%s): %s", dir, strerror (errno));
80 return (-1);
81 }
82 else if (chdir (dir) == -1)
83 {
84 syslog (LOG_ERR, "chdir (%s): %s", dir, strerror (errno));
85 return (-1);
86 }
87 }
88 else
89 {
90 syslog (LOG_ERR, "chdir: %s", strerror (errno));
91 return (-1);
92 }
93 }
95 return (0);
96 } /* static int change_basedir (char *dir) */
98 #if HAVE_LIBKSTAT
99 static void update_kstat (void)
100 {
101 if (kc == NULL)
102 {
103 if ((kc = kstat_open ()) == NULL)
104 syslog (LOG_ERR, "Unable to open kstat control structure");
105 }
106 else
107 {
108 kid_t kid;
109 kid = kstat_chain_update (kc);
110 if (kid > 0)
111 {
112 syslog (LOG_INFO, "kstat chain has been updated");
113 plugin_init_all ();
114 }
115 else if (kid < 0)
116 syslog (LOG_ERR, "kstat chain update failed");
117 /* else: everything works as expected */
118 }
120 return;
121 } /* static void update_kstat (void) */
122 #endif /* HAVE_LIBKSTAT */
124 static void exit_usage (char *name)
125 {
126 printf ("Usage: "PACKAGE" [OPTIONS]\n\n"
128 "Available options:\n"
129 " General:\n"
130 " -C <file> Configuration file.\n"
131 " Default: "CONFIGFILE"\n"
132 #if COLLECT_DAEMON
133 " -P <file> PID file.\n"
134 " Default: "PIDFILE"\n"
135 #endif
136 " -M <dir> Module/Plugin directory.\n"
137 " Default: "PLUGINDIR"\n"
138 " -D <dir> Data storage directory.\n"
139 " Default: "PKGLOCALSTATEDIR"\n"
140 #if COLLECT_DEBUG
141 " -L <file> Log file.\n"
142 " Default: "LOGFILE"\n"
143 #endif
144 #if COLLECT_DAEMON
145 " -f Don't fork to the background.\n"
146 #endif
147 #if HAVE_LIBRRD
148 " -l Start in local mode (no network).\n"
149 " -c Start in client (sender) mode.\n"
150 " -s Start in server (listener) mode.\n"
151 #endif /* HAVE_LIBRRD */
152 #if COLLECT_PING
153 " Ping:\n"
154 " -p <host> Host to ping periodically, may be repeated to ping\n"
155 " more than one host.\n"
156 #endif /* COLLECT_PING */
157 "\n"PACKAGE" "VERSION", http://verplant.org/collectd/\n"
158 "by Florian octo Forster <octo@verplant.org>\n"
159 "for contributions see `AUTHORS'\n");
160 exit (0);
161 } /* static void exit_usage (char *name) */
163 static int start_client (void)
164 {
165 int sleepingtime;
167 #if HAVE_LIBKSTAT
168 kc = NULL;
169 update_kstat ();
170 #endif
172 #if HAVE_LIBSTATGRAB
173 if (sg_init ())
174 {
175 syslog (LOG_ERR, "sg_init: %s", sg_str_error (sg_get_error ()));
176 return (-1);
177 }
179 if (sg_drop_privileges ())
180 {
181 syslog (LOG_ERR, "sg_drop_privileges: %s", sg_str_error (sg_get_error ()));
182 return (-1);
183 }
184 #endif
186 plugin_init_all ();
188 while (loop == 0)
189 {
190 curtime = time (NULL);
191 #if HAVE_LIBKSTAT
192 update_kstat ();
193 #endif
194 plugin_read_all ();
196 sleepingtime = 10;
197 while (sleepingtime != 0)
198 {
199 if (loop != 0)
200 break;
201 sleepingtime = sleep (sleepingtime);
202 }
203 }
205 return (0);
206 } /* static int start_client (void) */
208 #if HAVE_LIBRRD
209 static int start_server (void)
210 {
211 char *host;
212 char *type;
213 char *instance;
214 char *values;
216 while (loop == 0)
217 {
218 if (multicast_receive (&host, &type, &instance, &values) == 0)
219 plugin_write (host, type, instance, values);
221 if (host != NULL) free (host); host = NULL;
222 if (type != NULL) free (type); type = NULL;
223 if (instance != NULL) free (instance); instance = NULL;
224 if (values != NULL) free (values); values = NULL;
225 }
227 return (0);
228 } /* static int start_server (void) */
229 #endif /* HAVE_LIBRRD */
231 #if COLLECT_DAEMON
232 static int pidfile_create (const char *file)
233 {
234 FILE *fh;
236 if (file == NULL)
237 file = PIDFILE;
239 if ((fh = fopen (file, "w")) == NULL)
240 {
241 syslog (LOG_ERR, "fopen (%s): %s", file, strerror (errno));
242 return (1);
243 }
245 fprintf (fh, "%d\n", getpid());
246 fclose(fh);
248 return (0);
249 } /* static int pidfile_create (const char *file) */
250 #endif /* COLLECT_DAEMON */
252 #if COLLECT_DAEMON
253 static int pidfile_remove (const char *file)
254 {
255 if (file == NULL) {
256 file = PIDFILE;
257 }
258 return (unlink (file));
259 } /* static int pidfile_remove (const char *file) */
260 #endif /* COLLECT_DAEMON */
262 int main (int argc, char **argv)
263 {
264 struct sigaction sigIntAction;
265 struct sigaction sigTermAction;
266 struct sigaction sigChldAction;
267 char *configfile = CONFIGFILE;
268 char *plugindir = PLUGINDIR;
269 char *datadir = PKGLOCALSTATEDIR;
270 #if COLLECT_DAEMON
271 char *pidfile = PIDFILE;
272 pid_t pid;
273 int daemonize = 1;
274 #endif
275 #if COLLECT_DEBUG
276 char *logfile = LOGFILE;
277 #endif
279 #if HAVE_LIBRRD
280 operating_mode = MODE_LOCAL;
281 #endif
283 /* open syslog */
284 openlog (PACKAGE, LOG_CONS | LOG_PID, LOG_DAEMON);
286 /* read options */
287 while (1)
288 {
289 int c;
291 c = getopt (argc, argv, "C:M:D:h"
292 #if COLLECT_DAEMON
293 "fP:"
294 #endif
295 #if COLLECT_DEBUG
296 "L:"
297 #endif
298 #if HAVE_LIBRRD
299 "csl"
300 #endif /* HAVE_LIBRRD */
301 #if COLLECT_PING
302 "p:"
303 #endif /* COLLECT_PING */
304 );
306 if (c == -1)
307 break;
309 switch (c)
310 {
311 #if HAVE_LIBRRD
312 case 'c':
313 operating_mode = MODE_CLIENT;
314 break;
316 case 's':
317 operating_mode = MODE_SERVER;
318 break;
320 case 'l':
321 operating_mode = MODE_LOCAL;
322 break;
323 #endif /* HAVE_LIBRRD */
324 case 'C':
325 configfile = optarg;
326 break;
327 #if COLLECT_DAEMON
328 case 'P':
329 pidfile = optarg;
330 break;
331 case 'f':
332 daemonize = 0;
333 break;
334 #endif /* COLLECT_DAEMON */
335 case 'M':
336 plugindir = optarg;
337 break;
338 case 'D':
339 datadir = optarg;
340 break;
341 #if COLLECT_DEBUG
342 case 'L':
343 logfile = optarg;
344 break;
345 #endif
346 #if COLLECT_PING
347 case 'p':
348 if (num_pinghosts < MAX_PINGHOSTS)
349 pinghosts[num_pinghosts++] = optarg;
350 else
351 fprintf (stderr, "Maximum of %i ping hosts reached.\n", MAX_PINGHOSTS);
352 break;
353 #endif /* COLLECT_PING */
354 case 'h':
355 default:
356 exit_usage (argv[0]);
357 } /* switch (c) */
358 } /* while (1) */
360 DBG_STARTFILE(logfile, "debug file opened.");
362 /*
363 * Read the config file. This will load any modules automagically.
364 */
365 plugin_set_dir (plugindir);
367 if (cf_read (configfile))
368 {
369 fprintf (stderr, "Error: Reading the config file failed!\n"
370 "Read the syslog for details.\n");
371 return (1);
372 }
374 /*
375 * Change directory. We do this _after_ reading the config and loading
376 * modules to relative paths work as expected.
377 */
378 if (change_basedir (datadir))
379 {
380 fprintf (stderr, "Error: Unable to change to directory `%s'.\n", datadir);
381 return (1);
382 }
384 /*
385 * install signal handlers
386 */
387 sigIntAction.sa_handler = sigIntHandler;
388 sigaction (SIGINT, &sigIntAction, NULL);
390 sigIntAction.sa_handler = sigTermHandler;
391 sigaction (SIGTERM, &sigTermAction, NULL);
393 sigChldAction.sa_handler = SIG_IGN;
394 sigaction (SIGCHLD, &sigChldAction, NULL);
396 /*
397 * fork off child
398 */
399 #if COLLECT_DAEMON
400 if (daemonize)
401 {
402 if ((pid = fork ()) == -1)
403 {
404 /* error */
405 fprintf (stderr, "fork: %s", strerror (errno));
406 return (1);
407 }
408 else if (pid != 0)
409 {
410 /* parent */
411 /* printf ("Running (PID %i)\n", pid); */
412 return (0);
413 }
415 /* Detach from session */
416 setsid ();
418 /* Write pidfile */
419 if (pidfile_create (pidfile))
420 exit (2);
422 /* close standard descriptors */
423 close (2);
424 close (1);
425 close (0);
427 if (open ("/dev/null", O_RDWR) != 0)
428 {
429 syslog (LOG_ERR, "Error: Could not connect `STDIN' to `/dev/null'");
430 return (1);
431 }
432 if (dup (0) != 1)
433 {
434 syslog (LOG_ERR, "Error: Could not connect `STDOUT' to `/dev/null'");
435 return (1);
436 }
437 if (dup (0) != 2)
438 {
439 syslog (LOG_ERR, "Error: Could not connect `STDERR' to `/dev/null'");
440 return (1);
441 }
442 } /* if (daemonize) */
443 #endif /* COLLECT_DAEMON */
445 /*
446 * run the actual loops
447 */
448 #if HAVE_LIBRRD
449 if (operating_mode == MODE_SERVER)
450 start_server ();
451 else /* if (operating_mode == MODE_CLIENT || operating_mode == MODE_LOCAL) */
452 #endif
453 start_client ();
455 DBG_STOPFILE("debug file closed.");
457 /* close syslog */
458 syslog (LOG_INFO, "Exiting normally");
459 closelog ();
461 #if COLLECT_DAEMON
462 if (daemonize)
463 pidfile_remove(pidfile);
464 #endif /* COLLECT_DAEMON */
466 return (0);
467 } /* int main (int argc, char **argv) */