5458a2caa2a4881be174ce7cb7bae66e014098e6
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 #ifdef 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 #ifdef HAVE_LIBRRD
49 int operating_mode;
50 #endif
52 static void sigIntHandler (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", strerror (errno));
74 return (-1);
75 }
76 else if (chdir (dir) == -1)
77 {
78 syslog (LOG_ERR, "chdir: %s", 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 #ifdef 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 static void exit_usage (char *name)
119 {
120 printf ("Usage: "PACKAGE" [OPTIONS]\n\n"
122 "Available options:\n"
123 " General:\n"
124 /*
125 " -C <file> Configuration file.\n"
126 " Default: "CONFIGFILE"\n"
127 */
128 #if COLLECT_DAEMON
129 " -P <file> PID file.\n"
130 " Default: "PIDFILE"\n"
131 #endif
132 " -M <dir> Module/Plugin directory.\n"
133 " Default: "PLUGINDIR"\n"
134 " -D <dir> Data storage directory.\n"
135 " Default: "PKGLOCALSTATEDIR"\n"
136 #if COLLECT_DEBUG
137 " -L <file> Log file.\n"
138 " Default: "LOGFILE"\n"
139 #endif
140 #if COLLECT_DAEMON
141 " -f Don't fork to the background.\n"
142 #endif
143 #if HAVE_LIBRRD
144 " -l Start in local mode (no network).\n"
145 " -c Start in client (sender) mode.\n"
146 " -s Start in server (listener) mode.\n"
147 #endif /* HAVE_LIBRRD */
148 #if COLLECT_PING
149 " Ping:\n"
150 " -p <host> Host to ping periodically, may be repeated to ping\n"
151 " more than one host.\n"
152 #endif /* COLLECT_PING */
153 "\n"PACKAGE" "VERSION", http://verplant.org/collectd/\n"
154 "by Florian octo Forster <octo@verplant.org>\n"
155 "for contributions see `AUTHORS'\n");
156 exit (0);
157 } /* static void exit_usage (char *name) */
159 static int start_client (void)
160 {
161 int sleepingtime;
163 #ifdef HAVE_LIBKSTAT
164 kc = NULL;
165 update_kstat ();
166 #endif
168 #ifdef HAVE_LIBSTATGRAB
169 if (sg_init ())
170 {
171 syslog (LOG_ERR, "sg_init: %s", sg_str_error (sg_get_error ()));
172 return (-1);
173 }
175 if (sg_drop_privileges ())
176 {
177 syslog (LOG_ERR, "sg_drop_privileges: %s", sg_str_error (sg_get_error ()));
178 return (-1);
179 }
180 #endif
182 plugin_init_all ();
184 while (loop == 0)
185 {
186 curtime = time (NULL);
187 #ifdef HAVE_LIBKSTAT
188 update_kstat ();
189 #endif
190 plugin_read_all ();
192 sleepingtime = 10;
193 while (sleepingtime != 0)
194 {
195 if (loop != 0)
196 break;
197 sleepingtime = sleep (sleepingtime);
198 }
199 }
201 return (0);
202 } /* static int start_client (void) */
204 #ifdef HAVE_LIBRRD
205 static int start_server (void)
206 {
207 char *host;
208 char *type;
209 char *instance;
210 char *values;
212 while (loop == 0)
213 {
214 if (multicast_receive (&host, &type, &instance, &values) == 0)
215 plugin_write (host, type, instance, values);
217 if (host != NULL) free (host); host = NULL;
218 if (type != NULL) free (type); type = NULL;
219 if (instance != NULL) free (instance); instance = NULL;
220 if (values != NULL) free (values); values = NULL;
221 }
223 return (0);
224 } /* static int start_server (void) */
225 #endif /* HAVE_LIBRRD */
227 #if COLLECT_DAEMON
228 static int pidfile_create (char *file)
229 {
230 FILE *fh;
232 if (file == NULL)
233 file = PIDFILE;
235 if ((fh = fopen (file, "w")) == NULL)
236 {
237 syslog (LOG_ERR, "fopen (%s): %s", file, strerror (errno));
238 return (1);
239 }
241 fprintf (fh, "%d\n", getpid());
242 fclose(fh);
244 return (0);
245 } /* static int pidfile_create (char *file) */
246 #endif /* COLLECT_DAEMON */
248 #if COLLECT_DAEMON
249 static int pidfile_remove (void)
250 {
251 return (unlink (PIDFILE));
252 } /* static int pidfile_remove (void) */
253 #endif /* COLLECT_DAEMON */
255 int main (int argc, char **argv)
256 {
257 struct sigaction sigIntAction, sigChldAction;
258 char *configfile = CONFIGFILE;
259 char *plugindir = PLUGINDIR;
260 char *datadir = PKGLOCALSTATEDIR;
261 #if COLLECT_DAEMON
262 char *pidfile = PIDFILE;
263 pid_t pid;
264 int daemonize = 1;
265 #endif
266 #if COLLECT_DEBUG
267 char *logfile = LOGFILE;
268 #endif
270 #ifdef HAVE_LIBRRD
271 operating_mode = MODE_LOCAL;
272 #endif
274 /* open syslog */
275 openlog (PACKAGE, LOG_CONS | LOG_PID, LOG_DAEMON);
277 /* read options */
278 while (1)
279 {
280 int c;
282 c = getopt (argc, argv, "C:M:D:h"
283 #if COLLECT_DAEMON
284 "fP:"
285 #endif
286 #if COLLECT_DEBUG
287 "L:"
288 #endif
289 #if HAVE_LIBRRD
290 "csl"
291 #endif /* HAVE_LIBRRD */
292 #if COLLECT_PING
293 "p:"
294 #endif /* COLLECT_PING */
295 );
297 if (c == -1)
298 break;
300 switch (c)
301 {
302 #ifdef HAVE_LIBRRD
303 case 'c':
304 operating_mode = MODE_CLIENT;
305 break;
307 case 's':
308 operating_mode = MODE_SERVER;
309 break;
311 case 'l':
312 operating_mode = MODE_LOCAL;
313 break;
314 #endif /* HAVE_LIBRRD */
315 case 'C':
316 configfile = optarg;
317 break;
318 #if COLLECT_DAEMON
319 case 'P':
320 pidfile = optarg;
321 break;
322 case 'f':
323 daemonize = 0;
324 break;
325 #endif /* COLLECT_DAEMON */
326 case 'M':
327 plugindir = optarg;
328 break;
329 case 'D':
330 datadir = optarg;
331 break;
332 #if COLLECT_DEBUG
333 case 'L':
334 logfile = optarg;
335 break;
336 #endif
337 #if COLLECT_PING
338 case 'p':
339 if (num_pinghosts < MAX_PINGHOSTS)
340 pinghosts[num_pinghosts++] = optarg;
341 else
342 fprintf (stderr, "Maximum of %i ping hosts reached.\n", MAX_PINGHOSTS);
343 break;
344 #endif /* COLLECT_PING */
345 case 'h':
346 default:
347 exit_usage (argv[0]);
348 } /* switch (c) */
349 } /* while (1) */
351 DBG_STARTFILE(logfile, "debug file opened.");
353 /*
354 * Load plugins and change to output directory
355 * Loading plugins is done first so relative paths work as expected..
356 */
357 if (plugin_load_all (plugindir) < 1)
358 {
359 fprintf (stderr, "Error: No plugins found.\n");
360 return (1);
361 }
363 if (change_basedir (datadir))
364 {
365 fprintf (stderr, "Error: Unable to change to directory `%s'.\n", datadir);
366 return (1);
367 }
369 /*
370 * install signal handlers
371 */
372 sigIntAction.sa_handler = sigIntHandler;
373 sigaction (SIGINT, &sigIntAction, NULL);
375 sigChldAction.sa_handler = SIG_IGN;
376 sigaction (SIGCHLD, &sigChldAction, NULL);
378 /*
379 * fork off child
380 */
381 #if COLLECT_DAEMON
382 if (daemonize)
383 {
384 if ((pid = fork ()) == -1)
385 {
386 /* error */
387 fprintf (stderr, "fork: %s", strerror (errno));
388 return (1);
389 }
390 else if (pid != 0)
391 {
392 /* parent */
393 /* printf ("Running (PID %i)\n", pid); */
394 return (0);
395 }
397 /* Detach from session */
398 setsid ();
400 /* Write pidfile */
401 if (pidfile_create (pidfile))
402 exit (2);
404 /* close standard descriptors */
405 close (2);
406 close (1);
407 close (0);
409 if (open ("/dev/null", O_RDWR) != 0)
410 {
411 syslog (LOG_ERR, "Error: Could not connect `STDIN' to `/dev/null'");
412 return (1);
413 }
414 if (dup (0) != 1)
415 {
416 syslog (LOG_ERR, "Error: Could not connect `STDOUT' to `/dev/null'");
417 return (1);
418 }
419 if (dup (0) != 2)
420 {
421 syslog (LOG_ERR, "Error: Could not connect `STDERR' to `/dev/null'");
422 return (1);
423 }
424 } /* if (daemonize) */
425 #endif /* COLLECT_DAEMON */
427 /*
428 * run the actual loops
429 */
430 #ifdef HAVE_LIBRRD
431 if (operating_mode == MODE_SERVER)
432 start_server ();
433 else /* if (operating_mode == MODE_CLIENT || operating_mode == MODE_LOCAL) */
434 #endif
435 start_client ();
437 DBG_STOPFILE("debug file closed.");
439 /* close syslog */
440 syslog (LOG_INFO, "Exiting normally");
441 closelog ();
443 #if COLLECT_DAEMON
444 if (daemonize)
445 pidfile_remove();
446 #endif /* COLLECT_DAEMON */
448 return (0);
449 }