af3d106182d9ce93ea6f0a75b1e89cc3eee56873
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"
30 #include "ping.h"
32 static int loop = 0;
34 #if HAVE_LIBKSTAT
35 kstat_ctl_t *kc;
36 #endif /* HAVE_LIBKSTAT */
38 #if COLLECT_PING
39 char *pinghosts[MAX_PINGHOSTS];
40 int num_pinghosts = 0;
41 #endif
43 /*
44 * exported variables
45 */
46 time_t curtime;
48 #if HAVE_LIBRRD
49 int operating_mode;
50 #endif
52 static void
53 sigIntHandler (int signal)
54 {
55 loop++;
56 }
58 static int
59 change_basedir (char *dir)
60 {
61 int dirlen = strlen (dir);
63 while ((dirlen > 0) && (dir[dirlen - 1] == '/'))
64 dir[--dirlen] = '\0';
66 if (dirlen <= 0)
67 return (-1);
69 if (chdir (dir) == -1)
70 {
71 if (errno == ENOENT)
72 {
73 if (mkdir (dir, 0755) == -1)
74 {
75 syslog (LOG_ERR, "mkdir: %s", strerror (errno));
76 return (-1);
77 }
78 else if (chdir (dir) == -1)
79 {
80 syslog (LOG_ERR, "chdir: %s", strerror (errno));
81 return (-1);
82 }
83 }
84 else
85 {
86 syslog (LOG_ERR, "chdir: %s", strerror (errno));
87 return (-1);
88 }
89 }
91 return (0);
92 } /* static int change_basedir (char *dir) */
94 #if HAVE_LIBKSTAT
95 static void
96 update_kstat (void)
97 {
98 if (kc == NULL)
99 {
100 if ((kc = kstat_open ()) == NULL)
101 syslog (LOG_ERR, "Unable to open kstat control structure");
102 }
103 else
104 {
105 kid_t kid;
106 kid = kstat_chain_update (kc);
107 if (kid > 0)
108 {
109 syslog (LOG_INFO, "kstat chain has been updated");
110 plugin_init_all ();
111 }
112 else if (kid < 0)
113 syslog (LOG_ERR, "kstat chain update failed");
114 /* else: everything works as expected */
115 }
117 return;
118 } /* static void update_kstat (void) */
119 #endif /* HAVE_LIBKSTAT */
121 static void
122 exit_usage (char *name)
123 {
124 printf ("Usage: "PACKAGE" [OPTIONS]\n\n"
126 "Available options:\n"
127 " General:\n"
128 " -C <file> Configuration file.\n"
129 " Default: "CONFIGFILE"\n"
130 /* sure you want a configFILE?
131 what about a configDIR? - niki */
132 /*
133 " -C <dir> Configuration directory.\n"
134 " Default: "CONFIGDIR"\n"
135 */
136 #if COLLECT_DAEMON
137 " -P <file> PID file.\n"
138 " Default: "PIDFILE"\n"
139 #endif
140 " -M <dir> Module/Plugin directory.\n"
141 " Default: "PLUGINDIR"\n"
142 " -D <dir> Data storage directory.\n"
143 " Default: "PKGLOCALSTATEDIR"\n"
144 #if COLLECT_DEBUG
145 " -L <file> Log file.\n"
146 " Default: "LOGFILE"\n"
147 #endif
148 #if COLLECT_DAEMON
149 " -f Don't fork to the background.\n"
150 #endif
151 #if HAVE_LIBRRD
152 " -l Start in local mode (no network).\n"
153 " -c Start in client (sender) mode.\n"
154 " -s Start in server (listener) mode.\n"
155 #endif /* HAVE_LIBRRD */
156 #if COLLECT_PING
157 " Ping:\n"
158 " -p <host> Host to ping periodically, may be repeated to ping\n"
159 " more than one host.\n"
160 #endif /* COLLECT_PING */
161 "\n"PACKAGE" "VERSION", http://verplant.org/collectd/\n"
162 "by Florian octo Forster <octo@verplant.org>\n"
163 "for contributions see `AUTHORS'\n");
164 exit (0);
165 } /* static void exit_usage (char *name) */
167 static int
168 start_client (void)
169 {
170 int sleepingtime;
172 #if HAVE_LIBKSTAT
173 kc = NULL;
174 update_kstat ();
175 #endif
177 #if HAVE_LIBSTATGRAB
178 if (sg_init ())
179 {
180 syslog (LOG_ERR, "sg_init: %s", sg_str_error (sg_get_error ()));
181 return (-1);
182 }
184 if (sg_drop_privileges ())
185 {
186 syslog (LOG_ERR, "sg_drop_privileges: %s", sg_str_error (sg_get_error ()));
187 return (-1);
188 }
189 #endif
191 plugin_init_all ();
193 while (loop == 0)
194 {
195 curtime = time (NULL);
196 #if HAVE_LIBKSTAT
197 update_kstat ();
198 #endif
199 plugin_read_all ();
201 sleepingtime = 10;
202 while (sleepingtime != 0)
203 {
204 if (loop != 0)
205 break;
206 sleepingtime = sleep (sleepingtime);
207 }
208 }
210 return (0);
211 } /* static int start_client (void) */
213 #if HAVE_LIBRRD
214 static int
215 start_server (void)
216 {
217 char *host;
218 char *type;
219 char *instance;
220 char *values;
222 while (loop == 0)
223 {
224 if (multicast_receive (&host, &type, &instance, &values) == 0)
225 plugin_write (host, type, instance, values);
227 if (host != NULL) free (host); host = NULL;
228 if (type != NULL) free (type); type = NULL;
229 if (instance != NULL) free (instance); instance = NULL;
230 if (values != NULL) free (values); values = NULL;
231 }
233 return (0);
234 } /* static int start_server (void) */
235 #endif /* HAVE_LIBRRD */
237 #if COLLECT_DAEMON
238 static int
239 pidfile_create (const char *file)
240 {
241 FILE *fh;
243 if (file == NULL)
244 file = PIDFILE;
246 if ((fh = fopen (file, "w")) == NULL)
247 {
248 syslog (LOG_ERR, "fopen (%s): %s", file, strerror (errno));
249 return (1);
250 }
252 fprintf (fh, "%d\n", getpid());
253 fclose(fh);
255 return (0);
256 } /* static int pidfile_create (const char *file) */
257 #endif /* COLLECT_DAEMON */
259 #if COLLECT_DAEMON
260 static int
261 pidfile_remove (const char *file)
262 {
263 if (file == NULL) {
264 file = PIDFILE;
265 }
266 return (unlink (file));
267 } /* static int pidfile_remove (const char *file) */
268 #endif /* COLLECT_DAEMON */
270 int
271 main (int argc, char **argv)
272 {
273 struct sigaction sigIntAction, sigChldAction;
274 char *configfile = CONFIGFILE;
275 /* or char *configdir = CONFIGDIR; */
276 char *plugindir = PLUGINDIR;
277 char *datadir = PKGLOCALSTATEDIR;
278 #if COLLECT_DAEMON
279 char *pidfile = PIDFILE;
280 pid_t pid;
281 int daemonize = 1;
282 #endif
283 #if COLLECT_DEBUG
284 char *logfile = LOGFILE;
285 #endif
287 #if HAVE_LIBRRD
288 operating_mode = MODE_LOCAL;
289 #endif
291 /* open syslog */
292 openlog (PACKAGE, LOG_CONS | LOG_PID, LOG_DAEMON);
294 /* read options */
295 while (1)
296 {
297 int c;
299 c = getopt (argc, argv, "C:M:D:h"
300 #if COLLECT_DAEMON
301 "fP:"
302 #endif
303 #if COLLECT_DEBUG
304 "L:"
305 #endif
306 #if HAVE_LIBRRD
307 "csl"
308 #endif /* HAVE_LIBRRD */
309 #if COLLECT_PING
310 "p:"
311 #endif /* COLLECT_PING */
312 );
314 if (c == -1)
315 break;
317 switch (c)
318 {
319 #if HAVE_LIBRRD
320 case 'c':
321 operating_mode = MODE_CLIENT;
322 break;
324 case 's':
325 operating_mode = MODE_SERVER;
326 break;
328 case 'l':
329 operating_mode = MODE_LOCAL;
330 break;
331 #endif /* HAVE_LIBRRD */
332 case 'C':
333 configfile = optarg;
334 /* configdir = optarg; */
335 break;
336 #if COLLECT_DAEMON
337 case 'P':
338 pidfile = optarg;
339 break;
340 case 'f':
341 daemonize = 0;
342 break;
343 #endif /* COLLECT_DAEMON */
344 case 'M':
345 plugindir = optarg;
346 break;
347 case 'D':
348 datadir = optarg;
349 break;
350 #if COLLECT_DEBUG
351 case 'L':
352 logfile = optarg;
353 break;
354 #endif
355 #if COLLECT_PING
356 case 'p':
357 if (num_pinghosts < MAX_PINGHOSTS)
358 pinghosts[num_pinghosts++] = optarg;
359 else
360 fprintf (stderr, "Maximum of %i ping hosts reached.\n", MAX_PINGHOSTS);
361 break;
362 #endif /* COLLECT_PING */
363 case 'h':
364 default:
365 exit_usage (argv[0]);
366 } /* switch (c) */
367 } /* while (1) */
369 DBG_STARTFILE(logfile, "debug file opened.");
371 /*
372 * Load plugins and change to output directory
373 * Loading plugins is done first so relative paths work as expected..
374 */
375 if (plugin_load_all (plugindir) < 1)
376 {
377 fprintf (stderr, "Error: No plugins found.\n");
378 return (1);
379 }
381 if (change_basedir (datadir))
382 {
383 fprintf (stderr, "Error: Unable to change to directory `%s'.\n", datadir);
384 return (1);
385 }
387 /*
388 * install signal handlers
389 */
390 sigIntAction.sa_handler = sigIntHandler;
391 sigaction (SIGINT, &sigIntAction, 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) */