9932fd90a4830ed21f00104f3d940558c92a8913
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 " -C <dir> Configuration directory.\n"
124 " Default: %s\n"
125 " -P <file> PID File.\n"
126 " Default: %s\n"
127 " -M <dir> Module/Plugin directory.\n"
128 " Default: %s\n"
129 " -D <dir> Data storage directory.\n"
130 " Default: %s\n"
131 " -f Don't fork to the background.\n"
132 #ifdef HAVE_LIBRRD
133 " -l Start in local mode (no network).\n"
134 " -c Start in client (sender) mode.\n"
135 " -s Start in server (listener) mode.\n"
136 #endif /* HAVE_LIBRRD */
137 #if COLLECT_PING
138 " Ping:\n"
139 " -p <host> Host to ping periodically, may be repeated to ping\n"
140 " more than one host.\n"
141 #endif /* COLLECT_PING */
142 "\n%s %s, http://verplant.org/collectd/\n"
143 "by Florian octo Forster <octo@verplant.org>\n"
144 "for contributions see `AUTHORS'\n",
145 PACKAGE, SYSCONFDIR, PIDFILE, PLUGINDIR, PKGLOCALSTATEDIR, PACKAGE, VERSION);
146 exit (0);
147 }
149 int start_client (void)
150 {
151 int sleepingtime;
153 #ifdef HAVE_LIBKSTAT
154 kc = NULL;
155 update_kstat ();
156 #endif
158 #ifdef HAVE_LIBSTATGRAB
159 if (sg_init ())
160 {
161 syslog (LOG_ERR, "sg_init: %s", sg_str_error (sg_get_error ()));
162 return (-1);
163 }
165 if (sg_drop_privileges ())
166 {
167 syslog (LOG_ERR, "sg_drop_privileges: %s", sg_str_error (sg_get_error ()));
168 return (-1);
169 }
170 #endif
172 plugin_init_all ();
174 while (loop == 0)
175 {
176 curtime = time (NULL);
177 #ifdef HAVE_LIBKSTAT
178 update_kstat ();
179 #endif
180 plugin_read_all ();
182 sleepingtime = 10;
183 while (sleepingtime != 0)
184 {
185 if (loop != 0)
186 break;
187 sleepingtime = sleep (sleepingtime);
188 }
189 }
191 return (0);
192 }
194 #ifdef HAVE_LIBRRD
195 int start_server (void)
196 {
197 char *host;
198 char *type;
199 char *instance;
200 char *values;
202 while (loop == 0)
203 {
204 if (multicast_receive (&host, &type, &instance, &values) == 0)
205 plugin_write (host, type, instance, values);
207 if (host != NULL) free (host); host = NULL;
208 if (type != NULL) free (type); type = NULL;
209 if (instance != NULL) free (instance); instance = NULL;
210 if (values != NULL) free (values); values = NULL;
211 }
213 return (0);
214 }
215 #endif /* HAVE_LIBRRD */
217 int pidfile_create (void)
218 {
219 FILE *fh;
221 if ((fh = fopen (PIDFILE, "w")) == NULL)
222 {
223 syslog (LOG_ERR, "fopen (pidfile): %s", strerror (errno));
224 return (1);
225 }
227 fprintf (fh, "%d\n", getpid());
228 fclose(fh);
230 return (0);
231 }
233 int pidfile_remove (void)
234 {
235 return (unlink (PIDFILE));
236 }
238 int main (int argc, char **argv)
239 {
240 struct sigaction sigIntAction, sigChldAction;
241 #if COLLECT_DAEMON
242 pid_t pid;
243 #endif
245 char *confdir = SYSCONFDIR;
246 char *pidfile = PIDFILE;
247 char *plugindir = PLUGINDIR;
248 char *datadir = PKGLOCALSTATEDIR;
250 int daemonize = 1;
252 #ifdef HAVE_LIBRRD
253 operating_mode = MODE_LOCAL;
254 #endif
256 /* open syslog */
257 openlog (PACKAGE, LOG_CONS | LOG_PID, LOG_DAEMON);
259 /* read options */
260 while (1)
261 {
262 int c;
264 c = getopt (argc, argv, "C:P:M:D:fh"
265 #if HAVE_LIBRRD
266 "csl"
267 #endif /* HAVE_LIBRRD */
268 #if COLLECT_PING
269 "p:"
270 #endif /* COLLECT_PING */
271 );
273 if (c == -1)
274 break;
276 switch (c)
277 {
278 #ifdef HAVE_LIBRRD
279 case 'c':
280 operating_mode = MODE_CLIENT;
281 break;
283 case 's':
284 operating_mode = MODE_SERVER;
285 break;
287 case 'l':
288 operating_mode = MODE_LOCAL;
289 break;
290 #endif /* HAVE_LIBRRD */
291 case 'C':
292 confdir = optarg;
293 break;
294 case 'P':
295 pidfile = optarg;
296 break;
297 case 'M':
298 plugindir = optarg;
299 break;
300 case 'D':
301 datadir = optarg;
302 break;
303 case 'f':
304 daemonize = 0;
305 break;
306 #if COLLECT_PING
307 case 'p':
308 if (num_pinghosts < MAX_PINGHOSTS)
309 pinghosts[num_pinghosts++] = optarg;
310 else
311 fprintf (stderr, "Maximum of %i ping hosts reached.\n", MAX_PINGHOSTS);
312 break;
313 #endif /* COLLECT_PING */
314 case 'h':
315 default:
316 exit_usage (argv[0]);
317 }
319 }
321 /*
322 * Load plugins and change to output directory
323 * Loading plugins is done first so relative paths work as expected..
324 */
325 if (plugin_load_all (plugindir) < 1)
326 {
327 fprintf (stderr, "Error: No plugins found.\n");
328 return (1);
329 }
331 if (change_basedir (datadir))
332 {
333 fprintf (stderr, "Error: Unable to change to directory `%s'.\n", datadir);
334 return (1);
335 }
337 /*
338 * install signal handlers
339 */
340 sigIntAction.sa_handler = sigIntHandler;
341 sigaction (SIGINT, &sigIntAction, NULL);
343 sigChldAction.sa_handler = SIG_IGN;
344 sigaction (SIGCHLD, &sigChldAction, NULL);
346 /*
347 * fork off child
348 */
349 #if COLLECT_DAEMON
350 if (daemonize)
351 {
352 if ((pid = fork ()) == -1)
353 {
354 /* error */
355 fprintf (stderr, "fork: %s", strerror (errno));
356 return (1);
357 }
358 else if (pid != 0)
359 {
360 /* parent */
361 /* printf ("Running (PID %i)\n", pid); */
362 return (0);
363 }
365 /* Detach from session */
366 setsid ();
368 /* Write pidfile */
369 if (pidfile_create ())
370 exit (2);
372 /* close standard descriptors */
373 close (2);
374 close (1);
375 close (0);
377 if (open ("/dev/null", O_RDWR) != 0)
378 {
379 syslog (LOG_ERR, "Error: Could not connect `STDIN' to `/dev/null'");
380 return (1);
381 }
382 if (dup (0) != 1)
383 {
384 syslog (LOG_ERR, "Error: Could not connect `STDOUT' to `/dev/null'");
385 return (1);
386 }
387 if (dup (0) != 2)
388 {
389 syslog (LOG_ERR, "Error: Could not connect `STDERR' to `/dev/null'");
390 return (1);
391 }
392 } /* if (daemonize) */
393 #endif /* COLLECT_DAEMON */
395 /*
396 * run the actual loops
397 */
398 #ifdef HAVE_LIBRRD
399 if (operating_mode == MODE_SERVER)
400 start_server ();
401 else /* if (operating_mode == MODE_CLIENT || operating_mode == MODE_LOCAL) */
402 #endif
403 start_client ();
405 /* close syslog */
406 syslog (LOG_INFO, "Exiting normally");
407 closelog ();
409 if (daemonize)
410 pidfile_remove();
412 return (0);
413 }