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