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"
25 #include "common.h"
26 #include "utils_debug.h"
28 #include "network.h"
29 #include "plugin.h"
30 #include "configfile.h"
32 #include "ping.h"
34 static int loop = 0;
36 #if HAVE_LIBKSTAT
37 kstat_ctl_t *kc;
38 #endif /* HAVE_LIBKSTAT */
40 /*
41 * exported variables
42 */
43 time_t curtime;
45 int operating_mode;
47 static void sigIntHandler (int signal)
48 {
49 loop++;
50 }
52 static void sigTermHandler (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): %s", dir, strerror (errno));
74 return (-1);
75 }
76 else if (chdir (dir) == -1)
77 {
78 syslog (LOG_ERR, "chdir (%s): %s", dir, 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 #if 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 /* TODO
119 * Remove all settings but `-f' and `-C'
120 */
121 static void exit_usage (char *name)
122 {
123 printf ("Usage: "PACKAGE" [OPTIONS]\n\n"
125 "Available options:\n"
126 " General:\n"
127 " -C <file> Configuration file.\n"
128 " Default: "CONFIGFILE"\n"
129 #if COLLECT_DAEMON
130 " -f Don't fork to the background.\n"
131 #endif
132 "\nBuiltin defaults:\n"
133 " Config-File "CONFIGFILE"\n"
134 " PID-File "PIDFILE"\n"
135 " Data-Directory "PKGLOCALSTATEDIR"\n"
136 #if COLLECT_DEBUG
137 " Log-File "LOGFILE"\n"
138 #endif
139 "\n"PACKAGE" "VERSION", http://verplant.org/collectd/\n"
140 "by Florian octo Forster <octo@verplant.org>\n"
141 "for contributions see `AUTHORS'\n");
142 exit (0);
143 } /* static void exit_usage (char *name) */
145 static int start_client (void)
146 {
147 int sleepingtime;
149 #if HAVE_LIBKSTAT
150 kc = NULL;
151 update_kstat ();
152 #endif
154 #if 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 #if 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 } /* static int start_client (void) */
190 #if HAVE_LIBRRD
191 static int start_server (void)
192 {
193 /* FIXME use stack here! */
194 char *host;
195 char *type;
196 char *instance;
197 char *values;
199 while (loop == 0)
200 {
201 if (network_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 } /* static int start_server (void) */
212 #endif /* HAVE_LIBRRD */
214 #if COLLECT_DAEMON
215 static int pidfile_create (const char *file)
216 {
217 FILE *fh;
219 if (file == NULL)
220 file = PIDFILE;
222 if ((fh = fopen (file, "w")) == NULL)
223 {
224 syslog (LOG_ERR, "fopen (%s): %s", file, strerror (errno));
225 return (1);
226 }
228 fprintf (fh, "%d\n", getpid());
229 fclose(fh);
231 return (0);
232 } /* static int pidfile_create (const char *file) */
233 #endif /* COLLECT_DAEMON */
235 #if COLLECT_DAEMON
236 static int pidfile_remove (const char *file)
237 {
238 if (file == NULL) {
239 file = PIDFILE;
240 }
241 return (unlink (file));
242 } /* static int pidfile_remove (const char *file) */
243 #endif /* COLLECT_DAEMON */
245 int main (int argc, char **argv)
246 {
247 struct sigaction sigIntAction;
248 struct sigaction sigTermAction;
249 char *datadir = PKGLOCALSTATEDIR;
250 char *configfile = CONFIGFILE;
251 #if COLLECT_DAEMON
252 struct sigaction sigChldAction;
253 char *pidfile = PIDFILE;
254 pid_t pid;
255 int daemonize = 1;
256 #endif
257 #if COLLECT_DEBUG
258 char *logfile = LOGFILE;
259 #endif
261 #if HAVE_LIBRRD
262 operating_mode = MODE_LOCAL;
263 #endif
265 /* open syslog */
266 openlog (PACKAGE, LOG_CONS | LOG_PID, LOG_DAEMON);
268 /* read options */
269 while (1)
270 {
271 int c;
273 c = getopt (argc, argv, "hC:"
274 #if COLLECT_DAEMON
275 "f"
276 #endif
277 );
279 if (c == -1)
280 break;
282 switch (c)
283 {
284 case 'C':
285 configfile = optarg;
286 break;
287 #if COLLECT_DAEMON
288 case 'f':
289 daemonize = 0;
290 break;
291 #endif /* COLLECT_DAEMON */
292 case 'h':
293 default:
294 exit_usage (argv[0]);
295 } /* switch (c) */
296 } /* while (1) */
298 #if COLLECT_DEBUG
299 if ((logfile = cf_get_option ("LogFile", LOGFILE)) != NULL)
300 DBG_STARTFILE (logfile, "Debug file opened.");
301 #endif
303 /*
304 * Read options from the config file, the environment and the command
305 * line (in that order, with later options overwriting previous ones in
306 * general).
307 * Also, this will automatically load modules.
308 */
309 if (cf_read (configfile))
310 {
311 fprintf (stderr, "Error: Reading the config file failed!\n"
312 "Read the syslog for details.\n");
313 return (1);
314 }
316 /*
317 * Change directory. We do this _after_ reading the config and loading
318 * modules to relative paths work as expected.
319 */
320 if ((datadir = cf_get_option ("DataDir", PKGLOCALSTATEDIR)) == NULL)
321 {
322 fprintf (stderr, "Don't have a datadir to use. This should not happen. Ever.");
323 return (1);
324 }
325 if (change_basedir (datadir))
326 {
327 fprintf (stderr, "Error: Unable to change to directory `%s'.\n", datadir);
328 return (1);
329 }
331 #if COLLECT_DAEMON
332 /*
333 * fork off child
334 */
335 sigChldAction.sa_handler = SIG_IGN;
336 sigaction (SIGCHLD, &sigChldAction, NULL);
338 if ((pidfile = cf_get_option ("PIDFile", PIDFILE)) == NULL)
339 {
340 fprintf (stderr, "Cannot obtain pidfile. This shoud not happen. Ever.");
341 return (1);
342 }
344 if (daemonize)
345 {
346 if ((pid = fork ()) == -1)
347 {
348 /* error */
349 fprintf (stderr, "fork: %s", strerror (errno));
350 return (1);
351 }
352 else if (pid != 0)
353 {
354 /* parent */
355 /* printf ("Running (PID %i)\n", pid); */
356 return (0);
357 }
359 /* Detach from session */
360 setsid ();
362 /* Write pidfile */
363 if (pidfile_create (pidfile))
364 exit (2);
366 /* close standard descriptors */
367 close (2);
368 close (1);
369 close (0);
371 if (open ("/dev/null", O_RDWR) != 0)
372 {
373 syslog (LOG_ERR, "Error: Could not connect `STDIN' to `/dev/null'");
374 return (1);
375 }
376 if (dup (0) != 1)
377 {
378 syslog (LOG_ERR, "Error: Could not connect `STDOUT' to `/dev/null'");
379 return (1);
380 }
381 if (dup (0) != 2)
382 {
383 syslog (LOG_ERR, "Error: Could not connect `STDERR' to `/dev/null'");
384 return (1);
385 }
386 } /* if (daemonize) */
387 #endif /* COLLECT_DAEMON */
389 /*
390 * install signal handlers
391 */
392 sigIntAction.sa_handler = sigIntHandler;
393 sigaction (SIGINT, &sigIntAction, NULL);
395 sigTermAction.sa_handler = sigTermHandler;
396 sigaction (SIGTERM, &sigTermAction, NULL);
398 /*
399 * run the actual loops
400 */
401 #if HAVE_LIBRRD
402 if (operating_mode == MODE_SERVER)
403 start_server ();
404 else /* if (operating_mode == MODE_CLIENT || operating_mode == MODE_LOCAL || operating_mode == MODE_LOG) */
405 #endif
406 start_client ();
408 #if COLLECT_DEBUG
409 if (logfile != NULL)
410 DBG_STOPFILE("debug file closed.");
411 #endif
413 /* close syslog */
414 syslog (LOG_INFO, "Exiting normally");
415 closelog ();
417 #if COLLECT_DAEMON
418 if (daemonize)
419 pidfile_remove (pidfile);
420 #endif /* COLLECT_DAEMON */
422 return (0);
423 } /* int main (int argc, char **argv) */