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