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