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