1 /**
2 * collectd - src/unixsock.c
3 * Copyright (C) 2007 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; only version 2 of the License is applicable.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 *
18 * Author:
19 * Florian octo Forster <octo at verplant.org>
20 **/
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
25 #include "configfile.h"
26 #include "utils_debug.h"
28 /* Folks without pthread will need to disable this plugin. */
29 #include <pthread.h>
31 #include <sys/socket.h>
32 #include <sys/un.h>
33 #include <sys/poll.h>
35 #include <grp.h>
37 #ifndef UNIX_PATH_MAX
38 # define UNIX_PATH_MAX sizeof (((struct sockaddr_un *)0)->sun_path)
39 #endif
41 #define US_DEFAULT_PATH PREFIX"/var/run/"PACKAGE_NAME"-unixsock"
43 /*
44 * Private data structures
45 */
46 /* linked list of cached values */
47 typedef struct value_cache_s
48 {
49 char name[4*DATA_MAX_NAME_LEN];
50 int values_num;
51 gauge_t *gauge;
52 counter_t *counter;
53 const data_set_t *ds;
54 time_t time;
55 struct value_cache_s *next;
56 } value_cache_t;
58 /*
59 * Private variables
60 */
61 /* valid configuration file keys */
62 static const char *config_keys[] =
63 {
64 "SocketFile",
65 "SocketGroup",
66 "SocketPerms",
67 NULL
68 };
69 static int config_keys_num = 3;
71 /* socket configuration */
72 static int sock_fd = -1;
73 static char *sock_file = NULL;
74 static char *sock_group = NULL;
75 static int sock_perms = S_IRWXU | S_IRWXG;
77 static pthread_t listen_thread = (pthread_t) 0;
79 /* Linked list and auxilliary variables for saving values */
80 static value_cache_t *cache_head = NULL;
81 static pthread_mutex_t cache_lock = PTHREAD_MUTEX_INITIALIZER;
82 static unsigned int cache_oldest = UINT_MAX;
84 /*
85 * Functions
86 */
87 static value_cache_t *cache_search (const char *name)
88 {
89 value_cache_t *vc;
91 for (vc = cache_head; vc != NULL; vc = vc->next)
92 {
93 if (strcmp (vc->name, name) == 0)
94 break;
95 } /* for vc = cache_head .. NULL */
97 return (vc);
98 } /* value_cache_t *cache_search */
100 static int cache_alloc_name (char *ret, int ret_len,
101 const char *hostname,
102 const char *plugin, const char *plugin_instance,
103 const char *type, const char *type_instance)
104 {
105 int status;
107 assert (plugin != NULL);
108 assert (type != NULL);
110 if ((plugin_instance == NULL) || (strlen (plugin_instance) == 0))
111 {
112 if ((type_instance == NULL) || (strlen (type_instance) == 0))
113 status = snprintf (ret, ret_len, "%s/%s/%s",
114 hostname, plugin, type);
115 else
116 status = snprintf (ret, ret_len, "%s/%s/%s-%s",
117 hostname, plugin, type, type_instance);
118 }
119 else
120 {
121 if ((type_instance == NULL) || (strlen (type_instance) == 0))
122 status = snprintf (ret, ret_len, "%s/%s-%s/%s",
123 hostname, plugin, plugin_instance, type);
124 else
125 status = snprintf (ret, ret_len, "%s/%s-%s/%s-%s",
126 hostname, plugin, plugin_instance, type, type_instance);
127 }
129 if ((status < 1) || (status >= ret_len))
130 return (-1);
131 return (0);
132 } /* int cache_alloc_name */
134 static int cache_insert (const data_set_t *ds, const value_list_t *vl)
135 {
136 /* We're called from `cache_update' so we don't need to lock the mutex */
137 value_cache_t *vc;
138 int i;
140 DBG ("ds->ds_num = %i; vl->values_len = %i;",
141 ds->ds_num, vl->values_len);
142 assert (ds->ds_num == vl->values_len);
144 vc = (value_cache_t *) malloc (sizeof (value_cache_t));
145 if (vc == NULL)
146 {
147 pthread_mutex_unlock (&cache_lock);
148 syslog (LOG_ERR, "unixsock plugin: malloc failed: %s",
149 strerror (errno));
150 return (-1);
151 }
153 vc->gauge = (gauge_t *) malloc (sizeof (gauge_t) * vl->values_len);
154 if (vc->gauge == NULL)
155 {
156 pthread_mutex_unlock (&cache_lock);
157 syslog (LOG_ERR, "unixsock plugin: malloc failed: %s",
158 strerror (errno));
159 free (vc);
160 return (-1);
161 }
163 vc->counter = (counter_t *) malloc (sizeof (counter_t) * vl->values_len);
164 if (vc->counter == NULL)
165 {
166 pthread_mutex_unlock (&cache_lock);
167 syslog (LOG_ERR, "unixsock plugin: malloc failed: %s",
168 strerror (errno));
169 free (vc->gauge);
170 free (vc);
171 return (-1);
172 }
174 if (cache_alloc_name (vc->name, sizeof (vc->name),
175 vl->host, vl->plugin, vl->plugin_instance,
176 ds->type, vl->type_instance) != 0)
177 {
178 pthread_mutex_unlock (&cache_lock);
179 syslog (LOG_ERR, "unixsock plugin: cache_alloc_name failed.");
180 free (vc->counter);
181 free (vc->gauge);
182 free (vc);
183 return (-1);
184 }
186 for (i = 0; i < ds->ds_num; i++)
187 {
188 if (ds->ds[i].type == DS_TYPE_COUNTER)
189 {
190 vc->gauge[i] = 0.0;
191 vc->counter[i] = vl->values[i].counter;
192 }
193 else if (ds->ds[i].type == DS_TYPE_GAUGE)
194 {
195 vc->gauge[i] = vl->values[i].gauge;
196 vc->counter[i] = 0;
197 }
198 else
199 {
200 vc->gauge[i] = 0.0;
201 vc->counter[i] = 0;
202 }
203 }
204 vc->values_num = ds->ds_num;
205 vc->ds = ds;
207 vc->next = cache_head;
208 cache_head = vc;
210 vc->time = vl->time;
211 if (vc->time < cache_oldest)
212 cache_oldest = vc->time;
214 pthread_mutex_unlock (&cache_lock);
215 return (0);
216 } /* int cache_insert */
218 static int cache_update (const data_set_t *ds, const value_list_t *vl)
219 {
220 char name[4*DATA_MAX_NAME_LEN];;
221 value_cache_t *vc;
222 int i;
224 if (cache_alloc_name (name, sizeof (name),
225 vl->host,
226 vl->plugin, vl->plugin_instance,
227 ds->type, vl->type_instance) != 0)
228 return (-1);
230 pthread_mutex_lock (&cache_lock);
232 vc = cache_search (name);
234 if (vc == NULL)
235 return (cache_insert (ds, vl));
237 assert (vc->values_num == ds->ds_num);
238 assert (vc->values_num == vl->values_len);
240 /*
241 * Update the values. This is possibly a lot more that you'd expect
242 * because we honor min and max values and handle counter overflows here.
243 */
244 for (i = 0; i < ds->ds_num; i++)
245 {
246 if (ds->ds[i].type == DS_TYPE_COUNTER)
247 {
248 if (vl->values[i].counter < vc->counter[i])
249 {
250 if (vl->values[i].counter <= 4294967295U)
251 {
252 vc->gauge[i] = ((4294967295U - vl->values[i].counter)
253 + vc->counter[i]) / (vl->time - vc->time);
254 }
255 else
256 {
257 vc->gauge[i] = ((18446744073709551615ULL - vl->values[i].counter)
258 + vc->counter[i]) / (vl->time - vc->time);
259 }
260 }
261 else
262 {
263 vc->gauge[i] = (vl->values[i].counter - vc->counter[i])
264 / (vl->time - vc->time);
265 }
267 vc->counter[i] = vl->values[i].counter;
268 }
269 else if (ds->ds[i].type == DS_TYPE_GAUGE)
270 {
271 vc->gauge[i] = vl->values[i].gauge;
272 vc->counter[i] = 0;
273 }
274 else
275 {
276 vc->gauge[i] = NAN;
277 vc->counter[i] = 0;
278 }
280 if ((vc->gauge[i] == NAN)
281 || ((ds->ds[i].min != NAN) && (vc->gauge[i] < ds->ds[i].min))
282 || ((ds->ds[i].max != NAN) && (vc->gauge[i] > ds->ds[i].max)))
283 vc->gauge[i] = NAN;
284 } /* for i = 0 .. ds->ds_num */
286 vc->ds = ds;
287 vc->time = vl->time;
289 if (vc->time < cache_oldest)
290 cache_oldest = vc->time;
292 pthread_mutex_unlock (&cache_lock);
293 return (0);
294 } /* int cache_update */
296 static void cache_flush (int max_age)
297 {
298 value_cache_t *this;
299 value_cache_t *prev;
300 time_t now;
302 pthread_mutex_lock (&cache_lock);
304 now = time (NULL);
306 if ((now - cache_oldest) <= max_age)
307 {
308 pthread_mutex_unlock (&cache_lock);
309 return;
310 }
312 cache_oldest = now;
314 prev = NULL;
315 this = cache_head;
317 while (this != NULL)
318 {
319 if ((now - this->time) <= max_age)
320 {
321 if (this->time < cache_oldest)
322 cache_oldest = this->time;
324 prev = this;
325 this = this->next;
326 continue;
327 }
329 if (prev == NULL)
330 cache_head = this->next;
331 else
332 prev->next = this->next;
334 free (this->gauge);
335 free (this->counter);
336 free (this);
338 if (prev == NULL)
339 this = cache_head;
340 else
341 this = prev->next;
342 } /* while (this != NULL) */
344 pthread_mutex_unlock (&cache_lock);
345 } /* int cache_flush */
347 static int us_open_socket (void)
348 {
349 struct sockaddr_un sa;
350 int status;
352 sock_fd = socket (PF_UNIX, SOCK_STREAM, 0);
353 if (sock_fd < 0)
354 {
355 syslog (LOG_ERR, "unixsock plugin: socket failed: %s",
356 strerror (errno));
357 return (-1);
358 }
360 memset (&sa, '\0', sizeof (sa));
361 sa.sun_family = AF_UNIX;
362 strncpy (sa.sun_path, (sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
363 sizeof (sa.sun_path) - 1);
364 /* unlink (sa.sun_path); */
366 status = bind (sock_fd, (struct sockaddr *) &sa, sizeof (sa));
367 if (status != 0)
368 {
369 DBG ("bind failed: %s; sa.sun_path = %s",
370 strerror (errno), sa.sun_path);
371 syslog (LOG_ERR, "unixsock plugin: bind failed: %s",
372 strerror (errno));
373 close (sock_fd);
374 sock_fd = -1;
375 return (-1);
376 }
378 status = listen (sock_fd, 8);
379 if (status != 0)
380 {
381 syslog (LOG_ERR, "unixsock plugin: listen failed: %s",
382 strerror (errno));
383 close (sock_fd);
384 sock_fd = -1;
385 return (-1);
386 }
388 do
389 {
390 char *grpname;
391 struct group *g;
392 struct group sg;
393 char grbuf[2048];
395 grpname = (sock_group != NULL) ? sock_group : COLLECTD_GRP_NAME;
396 g = NULL;
398 status = getgrnam_r (grpname, &sg, grbuf, sizeof (grbuf), &g);
399 if (status != 0)
400 {
401 syslog (LOG_WARNING, "unixsock plugin: getgrnam_r (%s) failed: %s",
402 grpname, strerror (status));
403 break;
404 }
405 if (g == NULL)
406 {
407 syslog (LOG_WARNING, "unixsock plugin: No such group: `%s'",
408 grpname);
409 break;
410 }
412 if (chown ((sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
413 (uid_t) -1, g->gr_gid) != 0)
414 {
415 syslog (LOG_WARNING, "unixsock plugin: chown (%s, -1, %i) failed: %s",
416 (sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
417 (int) g->gr_gid,
418 strerror (errno));
419 }
420 } while (0);
422 return (0);
423 } /* int us_open_socket */
425 static int us_handle_getval (FILE *fh, char **fields, int fields_num)
426 {
427 char *hostname = fields[1];
428 char *plugin;
429 char *plugin_instance;
430 char *type;
431 char *type_instance;
432 char name[4*DATA_MAX_NAME_LEN];
433 value_cache_t *vc;
434 int status;
435 int i;
437 if (fields_num != 2)
438 return (-1);
440 plugin = strchr (hostname, '/');
441 if (plugin == NULL)
442 return (-1);
443 *plugin = '\0'; plugin++;
445 type = strchr (plugin, '/');
446 if (type == NULL)
447 return (-1);
448 *type = '\0'; type++;
450 plugin_instance = strchr (plugin, '-');
451 if (plugin_instance != NULL)
452 {
453 *plugin_instance = '\0';
454 plugin_instance++;
455 }
457 type_instance = strchr (type, '-');
458 if (type_instance != NULL)
459 {
460 *type_instance = '\0';
461 type_instance++;
462 }
464 status = cache_alloc_name (name, sizeof (name),
465 hostname, plugin, plugin_instance, type, type_instance);
466 if (status != 0)
467 return (-1);
469 pthread_mutex_lock (&cache_lock);
471 DBG ("vc = cache_search (%s)", name);
472 vc = cache_search (name);
474 if (vc == NULL)
475 {
476 DBG ("Did not find cache entry.");
477 fprintf (fh, "-1 No such value");
478 }
479 else
480 {
481 DBG ("Found cache entry.");
482 fprintf (fh, "%i", vc->values_num);
483 for (i = 0; i < vc->values_num; i++)
484 {
485 fprintf (fh, " %s=", vc->ds->ds[i].name);
486 if (vc->gauge[i] == NAN)
487 fprintf (fh, "NaN");
488 else
489 fprintf (fh, "%12e", vc->gauge[i]);
490 }
491 }
493 /* Free the mutex as soon as possible and definitely before flushing */
494 pthread_mutex_unlock (&cache_lock);
496 fprintf (fh, "\n");
497 fflush (fh);
499 return (0);
500 } /* int us_handle_getval */
502 static void *us_handle_client (void *arg)
503 {
504 int fd;
505 FILE *fh;
506 char buffer[1024];
507 char *fields[128];
508 int fields_num;
510 fd = *((int *) arg);
511 free (arg);
512 arg = NULL;
514 DBG ("Reading from fd #%i", fd);
516 fh = fdopen (fd, "r+");
517 if (fh == NULL)
518 {
519 syslog (LOG_ERR, "unixsock plugin: fdopen failed: %s",
520 strerror (errno));
521 close (fd);
522 pthread_exit ((void *) 1);
523 }
525 while (fgets (buffer, sizeof (buffer), fh) != NULL)
526 {
527 int len;
529 len = strlen (buffer);
530 while ((len > 0)
531 && ((buffer[len - 1] == '\n') || (buffer[len - 1] == '\r')))
532 buffer[--len] = '\0';
534 if (len == 0)
535 continue;
537 DBG ("fgets -> buffer = %s; len = %i;", buffer, len);
539 fields_num = strsplit (buffer, fields,
540 sizeof (fields) / sizeof (fields[0]));
542 if (fields_num < 1)
543 {
544 close (fd);
545 break;
546 }
548 if (strcasecmp (fields[0], "getval") == 0)
549 {
550 us_handle_getval (fh, fields, fields_num);
551 }
552 else
553 {
554 fprintf (fh, "Unknown command: %s\n", fields[0]);
555 fflush (fh);
556 }
557 } /* while (fgets) */
559 DBG ("Exiting..");
560 close (fd);
562 pthread_exit ((void *) 0);
563 } /* void *us_handle_client */
565 static void *us_server_thread (void *arg)
566 {
567 int status;
568 int *remote_fd;
569 pthread_t th;
570 pthread_attr_t th_attr;
572 if (us_open_socket () != 0)
573 pthread_exit ((void *) 1);
575 while (42)
576 {
577 DBG ("Calling accept..");
578 status = accept (sock_fd, NULL, NULL);
579 if (status < 0)
580 {
581 if (errno == EINTR)
582 continue;
584 syslog (LOG_ERR, "unixsock plugin: accept failed: %s",
585 strerror (errno));
586 close (sock_fd);
587 sock_fd = -1;
588 pthread_exit ((void *) 1);
589 }
591 remote_fd = (int *) malloc (sizeof (int));
592 if (remote_fd == NULL)
593 {
594 syslog (LOG_WARNING, "unixsock plugin: malloc failed: %s",
595 strerror (errno));
596 close (status);
597 continue;
598 }
599 *remote_fd = status;
601 DBG ("Spawning child to handle connection on fd #%i", *remote_fd);
603 pthread_attr_init (&th_attr);
604 pthread_attr_setdetachstate (&th_attr, PTHREAD_CREATE_DETACHED);
606 status = pthread_create (&th, &th_attr, us_handle_client, (void *) remote_fd);
607 if (status != 0)
608 {
609 syslog (LOG_WARNING, "unixsock plugin: pthread_create failed: %s",
610 strerror (status));
611 close (*remote_fd);
612 free (remote_fd);
613 continue;
614 }
615 } /* while (42) */
617 return ((void *) 0);
618 } /* void *us_server_thread */
620 static int us_config (const char *key, const char *val)
621 {
622 if (strcasecmp (key, "SocketFile") == 0)
623 {
624 sfree (sock_file);
625 sock_file = strdup (val);
626 }
627 else if (strcasecmp (key, "SocketGroup") == 0)
628 {
629 sfree (sock_group);
630 sock_group = strdup (val);
631 }
632 else if (strcasecmp (key, "SocketPerms") == 0)
633 {
634 sock_perms = (int) strtol (val, NULL, 8);
635 }
636 else
637 {
638 return (-1);
639 }
641 return (0);
642 } /* int us_config */
644 static int us_init (void)
645 {
646 int status;
648 status = pthread_create (&listen_thread, NULL, us_server_thread, NULL);
649 if (status != 0)
650 {
651 syslog (LOG_ERR, "unixsock plugin: pthread_create failed: %s",
652 strerror (status));
653 return (-1);
654 }
656 return (0);
657 } /* int us_init */
659 static int us_shutdown (void)
660 {
661 void *ret;
663 if (listen_thread != (pthread_t) 0)
664 {
665 pthread_kill (listen_thread, SIGTERM);
666 pthread_join (listen_thread, &ret);
667 listen_thread = (pthread_t) 0;
668 }
670 plugin_unregister_init ("unixsock");
671 plugin_unregister_write ("unixsock");
672 plugin_unregister_shutdown ("unixsock");
674 return (0);
675 } /* int us_shutdown */
677 static int us_write (const data_set_t *ds, const value_list_t *vl)
678 {
679 cache_update (ds, vl);
680 cache_flush (2 * atoi (COLLECTD_STEP));
682 return (0);
683 }
685 void module_register (void)
686 {
687 plugin_register_config ("unixsock", us_config,
688 config_keys, config_keys_num);
689 plugin_register_init ("unixsock", us_init);
690 plugin_register_write ("unixsock", us_write);
691 plugin_register_shutdown ("unixsock", us_shutdown);
692 } /* void module_register (void) */
694 /* vim: set sw=4 ts=4 sts=4 tw=78 : */