Added contribution for Tommie Gannert's PID-file patch to `AUTHORS'
[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 static void exit_usage (char *name)
126 {
127         printf ("Usage: "PACKAGE" [OPTIONS]\n\n"
128                         
129                         "Available options:\n"
130                         "  General:\n"
131                         "    -C <file>       Configuration file.\n"
132                         "                    Default: "CONFIGFILE"\n"
133 #if COLLECT_DAEMON
134                         "    -P <file>       PID file.\n"
135                         "                    Default: "PIDFILE"\n"
136 #endif
137                         "    -M <dir>        Module/Plugin directory.\n"
138                         "                    Default: "PLUGINDIR"\n"
139                         "    -D <dir>        Data storage directory.\n"
140                         "                    Default: "PKGLOCALSTATEDIR"\n"
141 #if COLLECT_DEBUG
142                         "    -L <file>       Log file.\n"
143                         "                    Default: "LOGFILE"\n"
144 #endif
145 #if COLLECT_DAEMON
146                         "    -f              Don't fork to the background.\n"
147 #endif
148 #if HAVE_LIBRRD
149                         "    -l              Start in local mode (no network).\n"
150                         "    -c              Start in client (sender) mode.\n"
151                         "    -s              Start in server (listener) mode.\n"
152 #endif /* HAVE_LIBRRD */
153 #if COLLECT_PING
154                         "  Ping:\n"
155                         "    -p <host>       Host to ping periodically, may be repeated to ping\n"
156                         "                    more than one host.\n"
157 #endif /* COLLECT_PING */
158                         "\n"PACKAGE" "VERSION", http://verplant.org/collectd/\n"
159                         "by Florian octo Forster <octo@verplant.org>\n"
160                         "for contributions see `AUTHORS'\n");
161         exit (0);
162 } /* static void exit_usage (char *name) */
163
164 static int start_client (void)
165 {
166         int sleepingtime;
167
168 #if HAVE_LIBKSTAT
169         kc = NULL;
170         update_kstat ();
171 #endif
172
173 #if HAVE_LIBSTATGRAB
174         if (sg_init ())
175         {
176                 syslog (LOG_ERR, "sg_init: %s", sg_str_error (sg_get_error ()));
177                 return (-1);
178         }
179
180         if (sg_drop_privileges ())
181         {
182                 syslog (LOG_ERR, "sg_drop_privileges: %s", sg_str_error (sg_get_error ()));
183                 return (-1);
184         }
185 #endif
186
187         plugin_init_all ();
188
189         while (loop == 0)
190         {
191                 curtime = time (NULL);
192 #if HAVE_LIBKSTAT
193                 update_kstat ();
194 #endif
195                 plugin_read_all ();
196
197                 sleepingtime = 10;
198                 while (sleepingtime != 0)
199                 {
200                         if (loop != 0)
201                                 break;
202                         sleepingtime = sleep (sleepingtime);
203                 }
204         }
205
206         return (0);
207 } /* static int start_client (void) */
208
209 #if HAVE_LIBRRD
210 static int start_server (void)
211 {
212         char *host;
213         char *type;
214         char *instance;
215         char *values;
216
217         while (loop == 0)
218         {
219                 if (multicast_receive (&host, &type, &instance, &values) == 0)
220                         plugin_write (host, type, instance, values);
221
222                 if (host     != NULL) free (host);     host     = NULL;
223                 if (type     != NULL) free (type);     type     = NULL;
224                 if (instance != NULL) free (instance); instance = NULL;
225                 if (values   != NULL) free (values);   values   = NULL;
226         }
227         
228         return (0);
229 } /* static int start_server (void) */
230 #endif /* HAVE_LIBRRD */
231
232 #if COLLECT_DAEMON
233 static int pidfile_create (const char *file)
234 {
235         FILE *fh;
236
237         if (file == NULL)
238                 file = PIDFILE;
239
240         if ((fh = fopen (file, "w")) == NULL)
241         {
242                 syslog (LOG_ERR, "fopen (%s): %s", file, strerror (errno));
243                 return (1);
244         }
245
246         fprintf (fh, "%d\n", getpid());
247         fclose(fh);
248
249         return (0);
250 } /* static int pidfile_create (const char *file) */
251 #endif /* COLLECT_DAEMON */
252
253 #if COLLECT_DAEMON
254 static int pidfile_remove (const char *file)
255 {
256         if (file == NULL) {
257                 file = PIDFILE;
258         }
259         return (unlink (file));
260 } /* static int pidfile_remove (const char *file) */
261 #endif /* COLLECT_DAEMON */
262
263 int main (int argc, char **argv)
264 {
265         struct sigaction sigIntAction;
266         struct sigaction sigTermAction;
267         struct sigaction sigChldAction;
268         char *configfile = CONFIGFILE;
269         char *plugindir  = PLUGINDIR;
270         char *datadir    = PKGLOCALSTATEDIR;
271 #if COLLECT_DAEMON
272         char *pidfile    = PIDFILE;
273         pid_t pid;
274         int daemonize    = 1;
275 #endif
276 #if COLLECT_DEBUG
277         char *logfile    = LOGFILE;
278 #endif
279
280 #if HAVE_LIBRRD
281         operating_mode = MODE_LOCAL;
282 #endif
283
284         /* open syslog */
285         openlog (PACKAGE, LOG_CONS | LOG_PID, LOG_DAEMON);
286
287         /* read options */
288         while (1)
289         {
290                 int c;
291
292                 c = getopt (argc, argv, "C:M:D:h"
293 #if COLLECT_DAEMON
294                                 "fP:"
295 #endif
296 #if COLLECT_DEBUG
297                                 "L:"
298 #endif
299 #if HAVE_LIBRRD
300                                 "csl"
301 #endif /* HAVE_LIBRRD */
302 #if COLLECT_PING
303                                 "p:"
304 #endif /* COLLECT_PING */
305                 );
306
307                 if (c == -1)
308                         break;
309
310                 switch (c)
311                 {
312 #if HAVE_LIBRRD
313                         case 'c':
314                                 operating_mode = MODE_CLIENT;
315                                 break;
316
317                         case 's':
318                                 operating_mode = MODE_SERVER;
319                                 break;
320
321                         case 'l':
322                                 operating_mode = MODE_LOCAL;
323                                 break;
324 #endif /* HAVE_LIBRRD */
325                         case 'C':
326                                 configfile = optarg;
327                                 break;
328 #if COLLECT_DAEMON
329                         case 'P':
330                                 pidfile = optarg;
331                                 break;
332                         case 'f':
333                                 daemonize = 0;
334                                 break;
335 #endif /* COLLECT_DAEMON */
336                         case 'M':
337                                 plugindir = optarg;
338                                 break;
339                         case 'D':
340                                 datadir = optarg;
341                                 break;
342 #if COLLECT_DEBUG
343                         case 'L':
344                                 logfile = optarg;
345                                 break;
346 #endif
347 #if COLLECT_PING
348                         case 'p':
349                                 if (num_pinghosts < MAX_PINGHOSTS)
350                                         pinghosts[num_pinghosts++] = optarg;
351                                 else
352                                         fprintf (stderr, "Maximum of %i ping hosts reached.\n", MAX_PINGHOSTS);
353                                 break;
354 #endif /* COLLECT_PING */
355                         case 'h':
356                         default:
357                                 exit_usage (argv[0]);
358                 } /* switch (c) */
359         } /* while (1) */
360
361         DBG_STARTFILE(logfile, "debug file opened.");
362
363         /*
364          * Read the config file. This will load any modules automagically.
365          */
366         plugin_set_dir (plugindir);
367
368         if (cf_read (configfile))
369         {
370                 fprintf (stderr, "Error: Reading the config file failed!\n"
371                                 "Read the syslog for details.\n");
372                 return (1);
373         }
374
375         /*
376          * Change directory. We do this _after_ reading the config and loading
377          * modules to relative paths work as expected.
378          */
379         if (change_basedir (datadir))
380         {
381                 fprintf (stderr, "Error: Unable to change to directory `%s'.\n", datadir);
382                 return (1);
383         }
384
385         /*
386          * install signal handlers
387          */
388         sigIntAction.sa_handler = sigIntHandler;
389         sigaction (SIGINT, &sigIntAction, NULL);
390
391         sigIntAction.sa_handler = sigTermHandler;
392         sigaction (SIGTERM, &sigTermAction, NULL);
393
394         sigChldAction.sa_handler = SIG_IGN;
395         sigaction (SIGCHLD, &sigChldAction, NULL);
396
397         /*
398          * fork off child
399          */
400 #if COLLECT_DAEMON
401         if (daemonize)
402         {
403                 if ((pid = fork ()) == -1)
404                 {
405                         /* error */
406                         fprintf (stderr, "fork: %s", strerror (errno));
407                         return (1);
408                 }
409                 else if (pid != 0)
410                 {
411                         /* parent */
412                         /* printf ("Running (PID %i)\n", pid); */
413                         return (0);
414                 }
415
416                 /* Detach from session */
417                 setsid ();
418
419                 /* Write pidfile */
420                 if (pidfile_create (pidfile))
421                         exit (2);
422
423                 /* close standard descriptors */
424                 close (2);
425                 close (1);
426                 close (0);
427
428                 if (open ("/dev/null", O_RDWR) != 0)
429                 {
430                         syslog (LOG_ERR, "Error: Could not connect `STDIN' to `/dev/null'");
431                         return (1);
432                 }
433                 if (dup (0) != 1)
434                 {
435                         syslog (LOG_ERR, "Error: Could not connect `STDOUT' to `/dev/null'");
436                         return (1);
437                 }
438                 if (dup (0) != 2)
439                 {
440                         syslog (LOG_ERR, "Error: Could not connect `STDERR' to `/dev/null'");
441                         return (1);
442                 }
443         } /* if (daemonize) */
444 #endif /* COLLECT_DAEMON */
445
446         /*
447          * run the actual loops
448          */
449 #if HAVE_LIBRRD
450         if (operating_mode == MODE_SERVER)
451                 start_server ();
452         else /* if (operating_mode == MODE_CLIENT || operating_mode == MODE_LOCAL) */
453 #endif
454                 start_client ();
455
456         DBG_STOPFILE("debug file closed.");
457
458         /* close syslog */
459         syslog (LOG_INFO, "Exiting normally");
460         closelog ();
461
462 #if COLLECT_DAEMON
463         if (daemonize)
464                 pidfile_remove(pidfile);
465 #endif /* COLLECT_DAEMON */
466
467         return (0);
468 } /* int main (int argc, char **argv) */