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