7b264e832efd18c6fb2e9aaf41f27146bb1349b8
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;
44 int operating_mode;
46 static void sigIntHandler (int signal)
47 {
48 loop++;
49 }
51 static void sigTermHandler (int signal)
52 {
53 loop++;
54 }
56 static 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): %s", dir, strerror (errno));
73 return (-1);
74 }
75 else if (chdir (dir) == -1)
76 {
77 syslog (LOG_ERR, "chdir (%s): %s", dir, 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 } /* static int change_basedir (char *dir) */
91 #if HAVE_LIBKSTAT
92 static 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 } /* static void update_kstat (void) */
115 #endif /* HAVE_LIBKSTAT */
117 /* TODO
118 * Remove all settings but `-f' and `-C'
119 */
120 static void exit_usage (char *name)
121 {
122 printf ("Usage: "PACKAGE" [OPTIONS]\n\n"
124 "Available options:\n"
125 " General:\n"
126 " -C <file> Configuration file.\n"
127 " Default: "CONFIGFILE"\n"
128 #if COLLECT_DAEMON
129 " -f Don't fork to the background.\n"
130 #endif
131 "\nBuiltin defaults:\n"
132 " Config-File "CONFIGFILE"\n"
133 " PID-File "PIDFILE"\n"
134 " Data-Directory "PKGLOCALSTATEDIR"\n"
135 #if COLLECT_DEBUG
136 " Log-File "LOGFILE"\n"
137 #endif
138 "\n"PACKAGE" "VERSION", http://verplant.org/collectd/\n"
139 "by Florian octo Forster <octo@verplant.org>\n"
140 "for contributions see `AUTHORS'\n");
141 exit (0);
142 } /* static void exit_usage (char *name) */
144 static int start_client (void)
145 {
146 int sleepingtime;
148 #if HAVE_LIBKSTAT
149 kc = NULL;
150 update_kstat ();
151 #endif
153 #if 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 #if 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 } /* static int start_client (void) */
189 #if HAVE_LIBRRD
190 static int start_server (void)
191 {
192 /* FIXME use stack here! */
193 char *host;
194 char *type;
195 char *instance;
196 char *values;
198 while (loop == 0)
199 {
200 if (network_receive (&host, &type, &instance, &values) == 0)
201 plugin_write (host, type, instance, values);
203 if (host != NULL) free (host); host = NULL;
204 if (type != NULL) free (type); type = NULL;
205 if (instance != NULL) free (instance); instance = NULL;
206 if (values != NULL) free (values); values = NULL;
207 }
209 return (0);
210 } /* static int start_server (void) */
211 #endif /* HAVE_LIBRRD */
213 #if COLLECT_DAEMON
214 static int pidfile_create (const char *file)
215 {
216 FILE *fh;
218 if (file == NULL)
219 file = PIDFILE;
221 if ((fh = fopen (file, "w")) == NULL)
222 {
223 syslog (LOG_ERR, "fopen (%s): %s", file, strerror (errno));
224 return (1);
225 }
227 fprintf (fh, "%d\n", getpid());
228 fclose(fh);
230 return (0);
231 } /* static int pidfile_create (const char *file) */
232 #endif /* COLLECT_DAEMON */
234 #if COLLECT_DAEMON
235 static int pidfile_remove (const char *file)
236 {
237 if (file == NULL) {
238 file = PIDFILE;
239 }
240 return (unlink (file));
241 } /* static int pidfile_remove (const char *file) */
242 #endif /* COLLECT_DAEMON */
244 int main (int argc, char **argv)
245 {
246 struct sigaction sigIntAction;
247 struct sigaction sigTermAction;
248 char *datadir = PKGLOCALSTATEDIR;
249 char *configfile = CONFIGFILE;
250 #if COLLECT_DAEMON
251 struct sigaction sigChldAction;
252 char *pidfile = PIDFILE;
253 pid_t pid;
254 int daemonize = 1;
255 #endif
256 #if COLLECT_DEBUG
257 char *logfile = LOGFILE;
258 #endif
260 #if HAVE_LIBRRD
261 operating_mode = MODE_LOCAL;
262 #endif
264 /* open syslog */
265 openlog (PACKAGE, LOG_CONS | LOG_PID, LOG_DAEMON);
267 /* read options */
268 while (1)
269 {
270 int c;
272 c = getopt (argc, argv, "hC:"
273 #if COLLECT_DAEMON
274 "f"
275 #endif
276 );
278 if (c == -1)
279 break;
281 switch (c)
282 {
283 case 'C':
284 configfile = optarg;
285 break;
286 #if COLLECT_DAEMON
287 case 'f':
288 daemonize = 0;
289 break;
290 #endif /* COLLECT_DAEMON */
291 case 'h':
292 default:
293 exit_usage (argv[0]);
294 } /* switch (c) */
295 } /* while (1) */
297 #if COLLECT_DEBUG
298 if ((logfile = cf_get_option ("LogFile", LOGFILE)) != NULL)
299 DBG_STARTFILE (logfile, "Debug file opened.");
300 #endif
302 /*
303 * Read options from the config file, the environment and the command
304 * line (in that order, with later options overwriting previous ones in
305 * general).
306 * Also, this will automatically load modules.
307 */
308 if (cf_read (configfile))
309 {
310 fprintf (stderr, "Error: Reading the config file failed!\n"
311 "Read the syslog for details.\n");
312 return (1);
313 }
315 /*
316 * Change directory. We do this _after_ reading the config and loading
317 * modules to relative paths work as expected.
318 */
319 if ((datadir = cf_get_option ("DataDir", PKGLOCALSTATEDIR)) == NULL)
320 {
321 fprintf (stderr, "Don't have a datadir to use. This should not happen. Ever.");
322 return (1);
323 }
324 if (change_basedir (datadir))
325 {
326 fprintf (stderr, "Error: Unable to change to directory `%s'.\n", datadir);
327 return (1);
328 }
330 #if COLLECT_DAEMON
331 /*
332 * fork off child
333 */
334 sigChldAction.sa_handler = SIG_IGN;
335 sigaction (SIGCHLD, &sigChldAction, NULL);
337 if ((pidfile = cf_get_option ("PIDFile", PIDFILE)) == NULL)
338 {
339 fprintf (stderr, "Cannot obtain pidfile. This shoud not happen. Ever.");
340 return (1);
341 }
343 if (daemonize)
344 {
345 if ((pid = fork ()) == -1)
346 {
347 /* error */
348 fprintf (stderr, "fork: %s", strerror (errno));
349 return (1);
350 }
351 else if (pid != 0)
352 {
353 /* parent */
354 /* printf ("Running (PID %i)\n", pid); */
355 return (0);
356 }
358 /* Detach from session */
359 setsid ();
361 /* Write pidfile */
362 if (pidfile_create (pidfile))
363 exit (2);
365 /* close standard descriptors */
366 close (2);
367 close (1);
368 close (0);
370 if (open ("/dev/null", O_RDWR) != 0)
371 {
372 syslog (LOG_ERR, "Error: Could not connect `STDIN' to `/dev/null'");
373 return (1);
374 }
375 if (dup (0) != 1)
376 {
377 syslog (LOG_ERR, "Error: Could not connect `STDOUT' to `/dev/null'");
378 return (1);
379 }
380 if (dup (0) != 2)
381 {
382 syslog (LOG_ERR, "Error: Could not connect `STDERR' to `/dev/null'");
383 return (1);
384 }
385 } /* if (daemonize) */
386 #endif /* COLLECT_DAEMON */
388 /*
389 * install signal handlers
390 */
391 sigIntAction.sa_handler = sigIntHandler;
392 sigaction (SIGINT, &sigIntAction, NULL);
394 sigTermAction.sa_handler = sigTermHandler;
395 sigaction (SIGTERM, &sigTermAction, NULL);
397 /*
398 * run the actual loops
399 */
400 #if HAVE_LIBRRD
401 if (operating_mode == MODE_SERVER)
402 start_server ();
403 else /* if (operating_mode == MODE_CLIENT || operating_mode == MODE_LOCAL || operating_mode == MODE_LOG) */
404 #endif
405 start_client ();
407 #if COLLECT_DEBUG
408 if (logfile != NULL)
409 DBG_STOPFILE("debug file closed.");
410 #endif
412 /* close syslog */
413 syslog (LOG_INFO, "Exiting normally");
414 closelog ();
416 #if COLLECT_DAEMON
417 if (daemonize)
418 pidfile_remove (pidfile);
419 #endif /* COLLECT_DAEMON */
421 return (0);
422 } /* int main (int argc, char **argv) */