348623c091499f9fb46d8c42ced47d2bae3b7cf1
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"
28 #include "configfile.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 void sigIntHandler (int signal)
53 {
54 loop++;
55 }
57 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 }
92 #ifdef HAVE_LIBKSTAT
93 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 }
116 #endif /* HAVE_LIBKSTAT */
118 void exit_usage (char *name)
119 {
120 printf ("Usage: %s [OPTIONS]\n\n"
122 "Available options:\n"
123 " General:\n"
124 " -d <dir> Base directory to use.\n"
125 " Default: %s\n"
126 " -P <dir> Set the plugin-directory\n"
127 " Default: %s\n"
128 " -f Don't fork to the background\n"
129 #ifdef HAVE_LIBRRD
130 " -l Start in local mode (no network)\n"
131 " -c Start in client (sender) mode\n"
132 " -s Start in server (listener) mode\n"
133 #endif /* HAVE_LIBRRD */
134 #if COLLECT_PING
135 " Ping:\n"
136 " -p <host> Host to ping periodically, may be repeated to ping\n"
137 " more than one host.\n"
138 #endif /* COLLECT_PING */
139 "\n%s %s, http://verplant.org/collectd/\n"
140 "by Florian octo Forster <octo@verplant.org>\n"
141 "for contributions see `AUTHORS'\n",
142 PACKAGE, DATADIR, PLUGINDIR, PACKAGE, VERSION);
143 exit (0);
144 }
146 int start_client (void)
147 {
148 int sleepingtime;
150 #ifdef HAVE_LIBKSTAT
151 kc = NULL;
152 update_kstat ();
153 #endif
155 #ifdef HAVE_LIBSTATGRAB
156 if (sg_init ())
157 {
158 syslog (LOG_ERR, "sg_init: %s", sg_str_error (sg_get_error ()));
159 return (-1);
160 }
162 if (sg_drop_privileges ())
163 {
164 syslog (LOG_ERR, "sg_drop_privileges: %s", sg_str_error (sg_get_error ()));
165 return (-1);
166 }
167 #endif
169 plugin_init_all ();
171 while (loop == 0)
172 {
173 curtime = time (NULL);
174 #ifdef HAVE_LIBKSTAT
175 update_kstat ();
176 #endif
177 plugin_read_all ();
179 sleepingtime = 10;
180 while (sleepingtime != 0)
181 {
182 if (loop != 0)
183 break;
184 sleepingtime = sleep (sleepingtime);
185 }
186 }
188 return (0);
189 }
191 #ifdef HAVE_LIBRRD
192 int start_server (void)
193 {
194 char *host;
195 char *type;
196 char *instance;
197 char *values;
199 while (loop == 0)
200 {
201 if (multicast_receive (&host, &type, &instance, &values) == 0)
202 plugin_write (host, type, instance, values);
204 if (host != NULL) free (host); host = NULL;
205 if (type != NULL) free (type); type = NULL;
206 if (instance != NULL) free (instance); instance = NULL;
207 if (values != NULL) free (values); values = NULL;
208 }
210 return (0);
211 }
212 #endif /* HAVE_LIBRRD */
214 int pidfile_create (void)
215 {
216 FILE *fh;
218 if ((fh = fopen (PIDFILE, "w")) == NULL)
219 {
220 syslog (LOG_ERR, "fopen (pidfile): %s", strerror (errno));
221 return (1);
222 }
224 fprintf (fh, "%d\n", getpid());
225 fclose(fh);
227 return (0);
228 }
230 int pidfile_remove (void)
231 {
232 return (unlink (PIDFILE));
233 }
235 int main (int argc, char **argv)
236 {
237 struct sigaction sigIntAction, sigChldAction;
238 #if COLLECT_DAEMON
239 pid_t pid;
240 #endif
242 char *plugindir = NULL;
243 char *basedir = DATADIR;
244 char *configfile = NULL;
246 int daemonize = 1;
248 #ifdef HAVE_LIBRRD
249 operating_mode = MODE_LOCAL;
250 #endif
252 /*
253 * open syslog
254 */
255 openlog (PACKAGE, LOG_CONS | LOG_PID, LOG_DAEMON);
257 /*
258 * read options
259 */
260 while (1)
261 {
262 int c;
264 c = getopt (argc, argv, "d:fP:h"
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 'd':
292 basedir = optarg;
293 break;
294 case 'f':
295 daemonize = 0;
296 break;
297 #if COLLECT_PING
298 case 'p':
299 if (num_pinghosts < MAX_PINGHOSTS)
300 pinghosts[num_pinghosts++] = optarg;
301 else
302 fprintf (stderr, "Maximum of %i ping hosts reached.\n", MAX_PINGHOSTS);
303 break;
304 #endif /* COLLECT_PING */
305 case 'P':
306 plugindir = optarg;
307 break;
309 case 'h':
310 default:
311 exit_usage (argv[0]);
312 }
314 }
316 /*
317 * Read the config file. This will load any modules automagically.
318 */
319 plugin_set_dir (plugindir);
321 if (cf_read (configfile))
322 {
323 fprintf (stderr, "Error: Reading the config file failed!\n"
324 "Read the syslog for details.\n");
325 return (1);
326 }
328 /*
329 * Change directory. We do this _after_ reading the config and loading
330 * modules to relative paths work as expected.
331 */
332 if (change_basedir (basedir))
333 {
334 fprintf (stderr, "Error: Unable to change to directory `%s'.\n", basedir);
335 return (1);
336 }
338 /*
339 * install signal handlers
340 */
341 sigIntAction.sa_handler = sigIntHandler;
342 sigaction (SIGINT, &sigIntAction, NULL);
344 sigChldAction.sa_handler = SIG_IGN;
345 sigaction (SIGCHLD, &sigChldAction, NULL);
347 /*
348 * fork off child
349 */
350 #if COLLECT_DAEMON
351 if (daemonize)
352 {
353 if ((pid = fork ()) == -1)
354 {
355 /* error */
356 fprintf (stderr, "fork: %s", strerror (errno));
357 return (1);
358 }
359 else if (pid != 0)
360 {
361 /* parent */
362 /* printf ("Running (PID %i)\n", pid); */
363 return (0);
364 }
366 /* Detach from session */
367 setsid ();
369 /* Write pidfile */
370 if (pidfile_create ())
371 exit (2);
373 /* close standard descriptors */
374 close (2);
375 close (1);
376 close (0);
378 if (open ("/dev/null", O_RDWR) != 0)
379 {
380 syslog (LOG_ERR, "Error: Could not connect `STDIN' to `/dev/null'");
381 return (1);
382 }
383 if (dup (0) != 1)
384 {
385 syslog (LOG_ERR, "Error: Could not connect `STDOUT' to `/dev/null'");
386 return (1);
387 }
388 if (dup (0) != 2)
389 {
390 syslog (LOG_ERR, "Error: Could not connect `STDERR' to `/dev/null'");
391 return (1);
392 }
393 } /* if (daemonize) */
394 #endif /* COLLECT_DAEMON */
396 /*
397 * run the actual loops
398 */
399 #ifdef HAVE_LIBRRD
400 if (operating_mode == MODE_SERVER)
401 start_server ();
402 else /* if (operating_mode == MODE_CLIENT || operating_mode == MODE_LOCAL) */
403 #endif
404 start_client ();
406 /*
407 * close syslog
408 */
409 syslog (LOG_INFO, "Exiting normally");
410 closelog ();
412 if (daemonize)
413 pidfile_remove();
415 return (0);
416 }