633d0230cdec080c7f5a924eba6e4e6fb44e112e
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"
26 #include "multicast.h"
27 #include "plugin.h"
29 #include "ping.h"
31 static int loop = 0;
33 #ifdef HAVE_LIBKSTAT
34 kstat_ctl_t *kc;
35 #endif /* HAVE_LIBKSTAT */
37 #if COLLECT_PING
38 char *pinghosts[MAX_PINGHOSTS];
39 int num_pinghosts = 0;
40 #endif
42 /*
43 * exported variables
44 */
45 time_t curtime;
47 #ifdef HAVE_LIBRRD
48 int operating_mode;
49 #endif
51 void sigIntHandler (int signal)
52 {
53 loop++;
54 }
56 int change_basedir (char *dir)
57 {
58 int dirlen = strlen (dir);
60 while ((dirlen > 0) && (dir[dirlen - 1] == '/'))
61 dir[--dirlen] = '\0';
63 if (dirlen <= 0)
64 return (-1);
66 if (chdir (dir) == -1)
67 {
68 if (errno == ENOENT)
69 {
70 if (mkdir (dir, 0755) == -1)
71 {
72 syslog (LOG_ERR, "mkdir: %s", strerror (errno));
73 return (-1);
74 }
75 else if (chdir (dir) == -1)
76 {
77 syslog (LOG_ERR, "chdir: %s", strerror (errno));
78 return (-1);
79 }
80 }
81 else
82 {
83 syslog (LOG_ERR, "chdir: %s", strerror (errno));
84 return (-1);
85 }
86 }
88 return (0);
89 }
91 #ifdef HAVE_LIBKSTAT
92 void update_kstat (void)
93 {
94 if (kc == NULL)
95 {
96 if ((kc = kstat_open ()) == NULL)
97 syslog (LOG_ERR, "Unable to open kstat control structure");
98 }
99 else
100 {
101 kid_t kid;
102 kid = kstat_chain_update (kc);
103 if (kid > 0)
104 {
105 syslog (LOG_INFO, "kstat chain has been updated");
106 plugin_init_all ();
107 }
108 else if (kid < 0)
109 syslog (LOG_ERR, "kstat chain update failed");
110 /* else: everything works as expected */
111 }
113 return;
114 }
115 #endif /* HAVE_LIBKSTAT */
117 void exit_usage (char *name)
118 {
119 printf ("Usage: %s [OPTIONS]\n\n"
121 "Available options:\n"
122 " General:\n"
123 " -d <dir> Base directory to use.\n"
124 " Default: %s\n"
125 " -P <dir> Set the plugin-directory\n"
126 " Default: %s\n"
127 " -f Don't fork to the background\n"
128 #ifdef HAVE_LIBRRD
129 " -l Start in local mode (no network)\n"
130 " -c Start in client (sender) mode\n"
131 " -s Start in server (listener) mode\n"
132 #endif /* HAVE_LIBRRD */
133 #if COLLECT_PING
134 " Ping:\n"
135 " -p <host> Host to ping periodically, may be repeated to ping\n"
136 " more than one host.\n"
137 #endif /* COLLECT_PING */
138 "\n%s %s, http://verplant.org/collectd/\n"
139 "by Florian octo Forster <octo@verplant.org>\n"
140 "for contributions see `AUTHORS'\n",
141 PACKAGE, DATADIR, PLUGINDIR, PACKAGE, VERSION);
142 exit (0);
143 }
145 int start_client (void)
146 {
147 int sleepingtime;
149 #ifdef HAVE_LIBKSTAT
150 kc = NULL;
151 update_kstat ();
152 #endif
154 #ifdef HAVE_LIBSTATGRAB
155 if (sg_init ())
156 {
157 syslog (LOG_ERR, "sg_init: %s", sg_str_error (sg_get_error ()));
158 return (-1);
159 }
161 if (sg_drop_privileges ())
162 {
163 syslog (LOG_ERR, "sg_drop_privileges: %s", sg_str_error (sg_get_error ()));
164 return (-1);
165 }
166 #endif
168 plugin_init_all ();
170 while (loop == 0)
171 {
172 curtime = time (NULL);
173 #ifdef HAVE_LIBKSTAT
174 update_kstat ();
175 #endif
176 plugin_read_all ();
178 sleepingtime = 10;
179 while (sleepingtime != 0)
180 {
181 if (loop != 0)
182 break;
183 sleepingtime = sleep (sleepingtime);
184 }
185 }
187 return (0);
188 }
190 #ifdef HAVE_LIBRRD
191 int start_server (void)
192 {
193 char *host;
194 char *type;
195 char *instance;
196 char *values;
198 while (loop == 0)
199 {
200 if (multicast_receive (&host, &type, &instance, &values) == 0)
201 plugin_write (host, type, instance, values);
203 if (host != NULL) free (host); host = NULL;
204 if (type != NULL) free (type); type = NULL;
205 if (instance != NULL) free (instance); instance = NULL;
206 if (values != NULL) free (values); values = NULL;
207 }
209 return (0);
210 }
211 #endif /* HAVE_LIBRRD */
213 int pidfile_create (void)
214 {
215 FILE *fh;
217 if ((fh = fopen (PIDFILE, "w")) == NULL)
218 {
219 syslog (LOG_ERR, "fopen (pidfile): %s", strerror (errno));
220 return (1);
221 }
223 fprintf (fh, "%d\n", getpid());
224 fclose(fh);
226 return (0);
227 }
229 int pidfile_remove (void)
230 {
231 return (unlink (PIDFILE));
232 }
234 int main (int argc, char **argv)
235 {
236 struct sigaction sigIntAction, sigChldAction;
237 #if COLLECT_DAEMON
238 pid_t pid;
239 #endif
241 char *plugindir = NULL;
242 char *basedir = DATADIR;
244 int daemonize = 1;
246 #ifdef HAVE_LIBRRD
247 operating_mode = MODE_LOCAL;
248 #endif
250 /*
251 * open syslog
252 */
253 openlog (PACKAGE, LOG_CONS | LOG_PID, LOG_DAEMON);
255 /*
256 * read options
257 */
258 while (1)
259 {
260 int c;
262 c = getopt (argc, argv, "d:fP:h"
263 #if HAVE_LIBRRD
264 "csl"
265 #endif /* HAVE_LIBRRD */
266 #if COLLECT_PING
267 "p:"
268 #endif /* COLLECT_PING */
269 );
271 if (c == -1)
272 break;
274 switch (c)
275 {
276 #ifdef HAVE_LIBRRD
277 case 'c':
278 operating_mode = MODE_CLIENT;
279 break;
281 case 's':
282 operating_mode = MODE_SERVER;
283 break;
285 case 'l':
286 operating_mode = MODE_LOCAL;
287 break;
288 #endif /* HAVE_LIBRRD */
289 case 'd':
290 basedir = optarg;
291 break;
292 case 'f':
293 daemonize = 0;
294 break;
295 #if COLLECT_PING
296 case 'p':
297 if (num_pinghosts < MAX_PINGHOSTS)
298 pinghosts[num_pinghosts++] = optarg;
299 else
300 fprintf (stderr, "Maximum of %i ping hosts reached.\n", MAX_PINGHOSTS);
301 break;
302 #endif /* COLLECT_PING */
303 case 'P':
304 plugindir = optarg;
305 break;
307 case 'h':
308 default:
309 exit_usage (argv[0]);
310 }
312 }
314 /*
315 * Load plugins and change to output directory
316 * Loading plugins is done first so relative paths work as expected..
317 */
318 if (plugin_load_all (plugindir) < 1)
319 {
320 fprintf (stderr, "Error: No plugins found.\n");
321 return (1);
322 }
324 if (change_basedir (basedir))
325 {
326 fprintf (stderr, "Error: Unable to change to directory `%s'.\n", basedir);
327 return (1);
328 }
330 /*
331 * install signal handlers
332 */
333 sigIntAction.sa_handler = sigIntHandler;
334 sigaction (SIGINT, &sigIntAction, NULL);
336 sigChldAction.sa_handler = SIG_IGN;
337 sigaction (SIGCHLD, &sigChldAction, NULL);
339 /*
340 * fork off child
341 */
342 #if COLLECT_DAEMON
343 if (daemonize)
344 {
345 if ((pid = fork ()) == -1)
346 {
347 /* error */
348 fprintf (stderr, "fork: %s", strerror (errno));
349 return (1);
350 }
351 else if (pid != 0)
352 {
353 /* parent */
354 /* printf ("Running (PID %i)\n", pid); */
355 return (0);
356 }
358 /* Detach from session */
359 setsid ();
361 /* Write pidfile */
362 if (pidfile_create ())
363 exit (2);
365 /* close standard descriptors */
366 close (2);
367 close (1);
368 close (0);
370 if (open ("/dev/null", O_RDWR) != 0)
371 {
372 syslog (LOG_ERR, "Error: Could not connect `STDIN' to `/dev/null'");
373 return (1);
374 }
375 if (dup (0) != 1)
376 {
377 syslog (LOG_ERR, "Error: Could not connect `STDOUT' to `/dev/null'");
378 return (1);
379 }
380 if (dup (0) != 2)
381 {
382 syslog (LOG_ERR, "Error: Could not connect `STDERR' to `/dev/null'");
383 return (1);
384 }
385 } /* if (daemonize) */
386 #endif /* COLLECT_DAEMON */
388 /*
389 * run the actual loops
390 */
391 #ifdef HAVE_LIBRRD
392 if (operating_mode == MODE_SERVER)
393 start_server ();
394 else /* if (operating_mode == MODE_CLIENT || operating_mode == MODE_LOCAL) */
395 #endif
396 start_client ();
398 /*
399 * close syslog
400 */
401 syslog (LOG_INFO, "Exiting normally");
402 closelog ();
404 if (daemonize)
405 pidfile_remove();
407 return (0);
408 }