47745cb0c05929dd2fac3047ead5f0908dc5f31d
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 #if HAVE_LIBRRD
46 int operating_mode;
47 #endif
49 static void sigIntHandler (int signal)
50 {
51 loop++;
52 }
54 static void sigTermHandler (int signal)
55 {
56 loop++;
57 }
59 static int change_basedir (char *dir)
60 {
61 int dirlen = strlen (dir);
63 while ((dirlen > 0) && (dir[dirlen - 1] == '/'))
64 dir[--dirlen] = '\0';
66 if (dirlen <= 0)
67 return (-1);
69 if (chdir (dir) == -1)
70 {
71 if (errno == ENOENT)
72 {
73 if (mkdir (dir, 0755) == -1)
74 {
75 syslog (LOG_ERR, "mkdir (%s): %s", dir, strerror (errno));
76 return (-1);
77 }
78 else if (chdir (dir) == -1)
79 {
80 syslog (LOG_ERR, "chdir (%s): %s", dir, strerror (errno));
81 return (-1);
82 }
83 }
84 else
85 {
86 syslog (LOG_ERR, "chdir: %s", strerror (errno));
87 return (-1);
88 }
89 }
91 return (0);
92 } /* static int change_basedir (char *dir) */
94 #if HAVE_LIBKSTAT
95 static void update_kstat (void)
96 {
97 if (kc == NULL)
98 {
99 if ((kc = kstat_open ()) == NULL)
100 syslog (LOG_ERR, "Unable to open kstat control structure");
101 }
102 else
103 {
104 kid_t kid;
105 kid = kstat_chain_update (kc);
106 if (kid > 0)
107 {
108 syslog (LOG_INFO, "kstat chain has been updated");
109 plugin_init_all ();
110 }
111 else if (kid < 0)
112 syslog (LOG_ERR, "kstat chain update failed");
113 /* else: everything works as expected */
114 }
116 return;
117 } /* static void update_kstat (void) */
118 #endif /* HAVE_LIBKSTAT */
120 /* TODO
121 * Remove all settings but `-f' and `-C'
122 */
123 static void exit_usage (char *name)
124 {
125 printf ("Usage: "PACKAGE" [OPTIONS]\n\n"
127 "Available options:\n"
128 " General:\n"
129 " -C <file> Configuration file.\n"
130 " Default: "CONFIGFILE"\n"
131 #if COLLECT_DAEMON
132 " -f Don't fork to the background.\n"
133 #endif
134 "\nBuiltin defaults:\n"
135 " Config-File "CONFIGFILE"\n"
136 " PID-File "PIDFILE"\n"
137 " Data-Directory "PKGLOCALSTATEDIR"\n"
138 #if COLLECT_DEBUG
139 " Log-File "LOGFILE"\n"
140 #endif
141 " Step "COLLECTD_STEP" seconds\n"
142 " Heartbeat "COLLECTD_HEARTBEAT" seconds\n"
143 "\n"PACKAGE" "VERSION", http://verplant.org/collectd/\n"
144 "by Florian octo Forster <octo@verplant.org>\n"
145 "for contributions see `AUTHORS'\n");
146 exit (0);
147 } /* static void exit_usage (char *name) */
149 static int start_client (void)
150 {
151 int sleepingtime;
152 int step;
154 step = atoi (COLLECTD_STEP);
155 if (step <= 0)
156 step = 10;
158 #if HAVE_LIBKSTAT
159 kc = NULL;
160 update_kstat ();
161 #endif
163 #if HAVE_LIBSTATGRAB
164 if (sg_init ())
165 {
166 syslog (LOG_ERR, "sg_init: %s", sg_str_error (sg_get_error ()));
167 return (-1);
168 }
170 if (sg_drop_privileges ())
171 {
172 syslog (LOG_ERR, "sg_drop_privileges: %s", sg_str_error (sg_get_error ()));
173 return (-1);
174 }
175 #endif
177 plugin_init_all ();
179 while (loop == 0)
180 {
181 curtime = time (NULL);
182 #if HAVE_LIBKSTAT
183 update_kstat ();
184 #endif
185 plugin_read_all ();
187 sleepingtime = step;
188 while (sleepingtime != 0)
189 {
190 if (loop != 0)
191 break;
192 sleepingtime = sleep (sleepingtime);
193 }
194 }
196 return (0);
197 } /* static int start_client (void) */
199 #if HAVE_LIBRRD
200 static int start_server (void)
201 {
202 /* FIXME use stack here! */
203 char *host;
204 char *type;
205 char *instance;
206 char *values;
208 while (loop == 0)
209 {
210 if (network_receive (&host, &type, &instance, &values) == 0)
211 plugin_write (host, type, instance, values);
213 if (host != NULL) free (host); host = NULL;
214 if (type != NULL) free (type); type = NULL;
215 if (instance != NULL) free (instance); instance = NULL;
216 if (values != NULL) free (values); values = NULL;
217 }
219 return (0);
220 } /* static int start_server (void) */
221 #endif /* HAVE_LIBRRD */
223 #if COLLECT_DAEMON
224 static int pidfile_create (const char *file)
225 {
226 FILE *fh;
228 if (file == NULL)
229 file = PIDFILE;
231 if ((fh = fopen (file, "w")) == NULL)
232 {
233 syslog (LOG_ERR, "fopen (%s): %s", file, strerror (errno));
234 return (1);
235 }
237 fprintf (fh, "%d\n", getpid());
238 fclose(fh);
240 return (0);
241 } /* static int pidfile_create (const char *file) */
242 #endif /* COLLECT_DAEMON */
244 #if COLLECT_DAEMON
245 static int pidfile_remove (const char *file)
246 {
247 if (file == NULL) {
248 file = PIDFILE;
249 }
250 return (unlink (file));
251 } /* static int pidfile_remove (const char *file) */
252 #endif /* COLLECT_DAEMON */
254 int main (int argc, char **argv)
255 {
256 struct sigaction sigIntAction;
257 struct sigaction sigTermAction;
258 char *datadir = PKGLOCALSTATEDIR;
259 char *configfile = CONFIGFILE;
260 #if COLLECT_DAEMON
261 struct sigaction sigChldAction;
262 char *pidfile = PIDFILE;
263 pid_t pid;
264 int daemonize = 1;
265 #endif
266 #if COLLECT_DEBUG
267 char *logfile = LOGFILE;
268 #endif
270 #if HAVE_LIBRRD
271 operating_mode = MODE_LOCAL;
272 #endif
274 /* open syslog */
275 openlog (PACKAGE, LOG_CONS | LOG_PID, LOG_DAEMON);
277 /* read options */
278 while (1)
279 {
280 int c;
282 c = getopt (argc, argv, "hC:"
283 #if COLLECT_DAEMON
284 "f"
285 #endif
286 );
288 if (c == -1)
289 break;
291 switch (c)
292 {
293 case 'C':
294 configfile = optarg;
295 break;
296 #if COLLECT_DAEMON
297 case 'f':
298 daemonize = 0;
299 break;
300 #endif /* COLLECT_DAEMON */
301 case 'h':
302 default:
303 exit_usage (argv[0]);
304 } /* switch (c) */
305 } /* while (1) */
307 #if COLLECT_DEBUG
308 if ((logfile = cf_get_option ("LogFile", LOGFILE)) != NULL)
309 DBG_STARTFILE (logfile, "Debug file opened.");
310 #endif
312 /*
313 * Read options from the config file, the environment and the command
314 * line (in that order, with later options overwriting previous ones in
315 * general).
316 * Also, this will automatically load modules.
317 */
318 if (cf_read (configfile))
319 {
320 fprintf (stderr, "Error: Reading the config file failed!\n"
321 "Read the syslog for details.\n");
322 return (1);
323 }
325 /*
326 * Change directory. We do this _after_ reading the config and loading
327 * modules to relative paths work as expected.
328 */
329 if ((datadir = cf_get_option ("DataDir", PKGLOCALSTATEDIR)) == NULL)
330 {
331 fprintf (stderr, "Don't have a datadir to use. This should not happen. Ever.");
332 return (1);
333 }
334 if (change_basedir (datadir))
335 {
336 fprintf (stderr, "Error: Unable to change to directory `%s'.\n", datadir);
337 return (1);
338 }
340 #if COLLECT_DAEMON
341 /*
342 * fork off child
343 */
344 sigChldAction.sa_handler = SIG_IGN;
345 sigaction (SIGCHLD, &sigChldAction, NULL);
347 if ((pidfile = cf_get_option ("PIDFile", PIDFILE)) == NULL)
348 {
349 fprintf (stderr, "Cannot obtain pidfile. This shoud not happen. Ever.");
350 return (1);
351 }
353 if (daemonize)
354 {
355 if ((pid = fork ()) == -1)
356 {
357 /* error */
358 fprintf (stderr, "fork: %s", strerror (errno));
359 return (1);
360 }
361 else if (pid != 0)
362 {
363 /* parent */
364 /* printf ("Running (PID %i)\n", pid); */
365 return (0);
366 }
368 /* Detach from session */
369 setsid ();
371 /* Write pidfile */
372 if (pidfile_create (pidfile))
373 exit (2);
375 /* close standard descriptors */
376 close (2);
377 close (1);
378 close (0);
380 if (open ("/dev/null", O_RDWR) != 0)
381 {
382 syslog (LOG_ERR, "Error: Could not connect `STDIN' to `/dev/null'");
383 return (1);
384 }
385 if (dup (0) != 1)
386 {
387 syslog (LOG_ERR, "Error: Could not connect `STDOUT' to `/dev/null'");
388 return (1);
389 }
390 if (dup (0) != 2)
391 {
392 syslog (LOG_ERR, "Error: Could not connect `STDERR' to `/dev/null'");
393 return (1);
394 }
395 } /* if (daemonize) */
396 #endif /* COLLECT_DAEMON */
398 /*
399 * install signal handlers
400 */
401 sigIntAction.sa_handler = sigIntHandler;
402 sigaction (SIGINT, &sigIntAction, NULL);
404 sigTermAction.sa_handler = sigTermHandler;
405 sigaction (SIGTERM, &sigTermAction, NULL);
407 /*
408 * run the actual loops
409 */
410 #if HAVE_LIBRRD
411 if (operating_mode == MODE_SERVER)
412 start_server ();
413 else /* if (operating_mode == MODE_CLIENT || operating_mode == MODE_LOCAL) */
414 #endif
415 start_client ();
417 #if COLLECT_DEBUG
418 if (logfile != NULL)
419 DBG_STOPFILE("debug file closed.");
420 #endif
422 /* close syslog */
423 syslog (LOG_INFO, "Exiting normally");
424 closelog ();
426 #if COLLECT_DAEMON
427 if (daemonize)
428 pidfile_remove (pidfile);
429 #endif /* COLLECT_DAEMON */
431 return (0);
432 } /* int main (int argc, char **argv) */