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