2 * collectd - src/unixsock.c
3 * Copyright (C) 2007 Florian octo Forster
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.
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.
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
19 * Florian octo Forster <octo at verplant.org>
25 #include "configfile.h"
27 /* Folks without pthread will need to disable this plugin. */
30 #include <sys/socket.h>
36 # define UNIX_PATH_MAX sizeof (((struct sockaddr_un *)0)->sun_path)
39 #define US_DEFAULT_PATH LOCALSTATEDIR"/run/"PACKAGE_NAME"-unixsock"
42 * Private data structures
44 /* linked list of cached values */
45 typedef struct value_cache_s
47 char name[4*DATA_MAX_NAME_LEN];
53 struct value_cache_s *next;
59 /* valid configuration file keys */
60 static const char *config_keys[] =
67 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;
87 static value_cache_t *cache_search (const char *name)
91 for (vc = cache_head; vc != NULL; vc = vc->next)
93 if (strcmp (vc->name, name) == 0)
95 } /* for vc = cache_head .. NULL */
98 } /* value_cache_t *cache_search */
100 static int cache_insert (const data_set_t *ds, const value_list_t *vl)
102 /* We're called from `cache_update' so we don't need to lock the mutex */
106 DEBUG ("unixsock plugin: cache_insert: ds->type = %s; ds->ds_num = %i;"
107 " vl->values_len = %i;",
108 ds->type, ds->ds_num, vl->values_len);
110 assert (ds->ds_num == vl->values_len);
112 if (ds->ds_num != vl->values_len)
114 ERROR ("unixsock plugin: ds->type = %s: (ds->ds_num = %i) != "
115 "(vl->values_len = %i)",
116 ds->type, ds->ds_num, vl->values_len);
121 vc = (value_cache_t *) malloc (sizeof (value_cache_t));
125 pthread_mutex_unlock (&cache_lock);
126 ERROR ("unixsock plugin: malloc failed: %s",
127 sstrerror (errno, errbuf, sizeof (errbuf)));
131 vc->gauge = (gauge_t *) malloc (sizeof (gauge_t) * vl->values_len);
132 if (vc->gauge == NULL)
135 pthread_mutex_unlock (&cache_lock);
136 ERROR ("unixsock plugin: malloc failed: %s",
137 sstrerror (errno, errbuf, sizeof (errbuf)));
142 vc->counter = (counter_t *) malloc (sizeof (counter_t) * vl->values_len);
143 if (vc->counter == NULL)
146 pthread_mutex_unlock (&cache_lock);
147 ERROR ("unixsock plugin: malloc failed: %s",
148 sstrerror (errno, errbuf, sizeof (errbuf)));
154 if (FORMAT_VL (vc->name, sizeof (vc->name), vl, ds))
156 pthread_mutex_unlock (&cache_lock);
157 ERROR ("unixsock plugin: FORMAT_VL failed.");
164 for (i = 0; i < ds->ds_num; i++)
166 if (ds->ds[i].type == DS_TYPE_COUNTER)
169 vc->counter[i] = vl->values[i].counter;
171 else if (ds->ds[i].type == DS_TYPE_GAUGE)
173 vc->gauge[i] = vl->values[i].gauge;
182 vc->values_num = ds->ds_num;
185 vc->next = cache_head;
189 if (vc->time < cache_oldest)
190 cache_oldest = vc->time;
192 pthread_mutex_unlock (&cache_lock);
194 } /* int cache_insert */
196 static int cache_update (const data_set_t *ds, const value_list_t *vl)
198 char name[4*DATA_MAX_NAME_LEN];;
202 if (FORMAT_VL (name, sizeof (name), vl, ds) != 0)
205 pthread_mutex_lock (&cache_lock);
207 vc = cache_search (name);
209 /* pthread_mutex_lock is called by cache_insert. */
211 return (cache_insert (ds, vl));
213 assert (vc->values_num == ds->ds_num);
214 assert (vc->values_num == vl->values_len);
216 /* Avoid floating-point exceptions due to division by zero. */
217 if (vc->time >= vl->time)
219 pthread_mutex_unlock (&cache_lock);
220 ERROR ("unixsock plugin: vc->time >= vl->time. vc->time = %u; "
221 "vl->time = %u; vl = %s;",
222 (unsigned int) vc->time, (unsigned int) vl->time,
225 } /* if (vc->time >= vl->time) */
228 * Update the values. This is possibly a lot more that you'd expect
229 * because we honor min and max values and handle counter overflows here.
231 for (i = 0; i < ds->ds_num; i++)
233 if (ds->ds[i].type == DS_TYPE_COUNTER)
235 if (vl->values[i].counter < vc->counter[i])
237 if (vl->values[i].counter <= 4294967295U)
239 vc->gauge[i] = ((4294967295U - vl->values[i].counter)
240 + vc->counter[i]) / (vl->time - vc->time);
244 vc->gauge[i] = ((18446744073709551615ULL - vl->values[i].counter)
245 + vc->counter[i]) / (vl->time - vc->time);
250 vc->gauge[i] = (vl->values[i].counter - vc->counter[i])
251 / (vl->time - vc->time);
254 vc->counter[i] = vl->values[i].counter;
256 else if (ds->ds[i].type == DS_TYPE_GAUGE)
258 vc->gauge[i] = vl->values[i].gauge;
267 if (isnan (vc->gauge[i])
268 || (!isnan (ds->ds[i].min) && (vc->gauge[i] < ds->ds[i].min))
269 || (!isnan (ds->ds[i].max) && (vc->gauge[i] > ds->ds[i].max)))
271 } /* for i = 0 .. ds->ds_num */
276 if (vc->time < cache_oldest)
277 cache_oldest = vc->time;
279 pthread_mutex_unlock (&cache_lock);
281 } /* int cache_update */
283 static void cache_flush (int max_age)
289 pthread_mutex_lock (&cache_lock);
293 if ((now - cache_oldest) <= max_age)
295 pthread_mutex_unlock (&cache_lock);
306 if ((now - this->time) <= max_age)
308 if (this->time < cache_oldest)
309 cache_oldest = this->time;
317 cache_head = this->next;
319 prev->next = this->next;
322 free (this->counter);
329 } /* while (this != NULL) */
331 pthread_mutex_unlock (&cache_lock);
332 } /* void cache_flush */
334 static int us_open_socket (void)
336 struct sockaddr_un sa;
339 sock_fd = socket (PF_UNIX, SOCK_STREAM, 0);
343 ERROR ("unixsock plugin: socket failed: %s",
344 sstrerror (errno, errbuf, sizeof (errbuf)));
348 memset (&sa, '\0', sizeof (sa));
349 sa.sun_family = AF_UNIX;
350 strncpy (sa.sun_path, (sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
351 sizeof (sa.sun_path) - 1);
352 /* unlink (sa.sun_path); */
354 status = bind (sock_fd, (struct sockaddr *) &sa, sizeof (sa));
358 sstrerror (errno, errbuf, sizeof (errbuf));
359 DEBUG ("bind failed: %s; sa.sun_path = %s", errbuf, sa.sun_path);
360 ERROR ("unixsock plugin: bind failed: %s", errbuf);
366 status = listen (sock_fd, 8);
370 ERROR ("unixsock plugin: listen failed: %s",
371 sstrerror (errno, errbuf, sizeof (errbuf)));
384 grpname = (sock_group != NULL) ? sock_group : COLLECTD_GRP_NAME;
387 status = getgrnam_r (grpname, &sg, grbuf, sizeof (grbuf), &g);
391 WARNING ("unixsock plugin: getgrnam_r (%s) failed: %s", grpname,
392 sstrerror (errno, errbuf, sizeof (errbuf)));
397 WARNING ("unixsock plugin: No such group: `%s'",
402 if (chown ((sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
403 (uid_t) -1, g->gr_gid) != 0)
406 WARNING ("unixsock plugin: chown (%s, -1, %i) failed: %s",
407 (sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
409 sstrerror (errno, errbuf, sizeof (errbuf)));
414 } /* int us_open_socket */
416 static int us_handle_getval (FILE *fh, char **fields, int fields_num)
420 char *plugin_instance;
423 char name[4*DATA_MAX_NAME_LEN];
430 DEBUG ("unixsock plugin: Wrong number of fields: %i", fields_num);
431 fprintf (fh, "-1 Wrong number of fields: Got %i, expected 2.\n",
436 DEBUG ("unixsock plugin: Got query for `%s'", fields[1]);
438 status = parse_identifier (fields[1], &hostname,
439 &plugin, &plugin_instance,
440 &type, &type_instance);
443 DEBUG ("unixsock plugin: Cannot parse `%s'", fields[1]);
444 fprintf (fh, "-1 Cannot parse identifier.\n");
449 status = format_name (name, sizeof (name),
450 hostname, plugin, plugin_instance, type, type_instance);
453 fprintf (fh, "-1 format_name failed.\n");
457 pthread_mutex_lock (&cache_lock);
459 DEBUG ("vc = cache_search (%s)", name);
460 vc = cache_search (name);
464 DEBUG ("Did not find cache entry.");
465 fprintf (fh, "-1 No such value");
469 DEBUG ("Found cache entry.");
470 fprintf (fh, "%i", vc->values_num);
471 for (i = 0; i < vc->values_num; i++)
473 fprintf (fh, " %s=", vc->ds->ds[i].name);
474 if (isnan (vc->gauge[i]))
477 fprintf (fh, "%12e", vc->gauge[i]);
481 /* Free the mutex as soon as possible and definitely before flushing */
482 pthread_mutex_unlock (&cache_lock);
488 } /* int us_handle_getval */
490 static int us_handle_putval (FILE *fh, char **fields, int fields_num)
494 char *plugin_instance;
500 const data_set_t *ds;
501 value_list_t vl = VALUE_LIST_INIT;
507 DEBUG ("unixsock plugin: Wrong number of fields: %i", fields_num);
508 fprintf (fh, "-1 Wrong number of fields: Got %i, expected 3.\n",
514 status = parse_identifier (fields[1], &hostname,
515 &plugin, &plugin_instance,
516 &type, &type_instance);
519 DEBUG ("unixsock plugin: Cannot parse `%s'", fields[1]);
520 fprintf (fh, "-1 Cannot parse identifier.\n");
525 if ((strlen (hostname) >= sizeof (vl.host))
526 || (strlen (plugin) >= sizeof (vl.plugin))
527 || ((plugin_instance != NULL)
528 && (strlen (plugin_instance) >= sizeof (vl.plugin_instance)))
529 || ((type_instance != NULL)
530 && (strlen (type_instance) >= sizeof (vl.type_instance))))
532 fprintf (fh, "-1 Identifier too long.");
536 strcpy (vl.host, hostname);
537 strcpy (vl.plugin, plugin);
538 if (plugin_instance != NULL)
539 strcpy (vl.plugin_instance, plugin_instance);
540 if (type_instance != NULL)
541 strcpy (vl.type_instance, type_instance);
543 { /* parse the time */
545 char *v = strchr (t, ':');
548 fprintf (fh, "-1 No time found.");
553 vl.time = (time_t) atoi (t);
555 vl.time = time (NULL);
560 ds = plugin_get_ds (type);
564 value_ptr = (char **) calloc (ds->ds_num, sizeof (char *));
565 if (value_ptr == NULL)
567 fprintf (fh, "-1 calloc failed.");
571 { /* parse the value-list. It's colon-separated. */
579 while ((ptr = strtok_r (dummy, ":", &saveptr)) != NULL)
594 fprintf (fh, "-1 Number of values incorrect: Got %i, "
595 "expected %i.", i, ds->ds_num);
598 } /* done parsing the value-list */
600 vl.values_len = ds->ds_num;
601 vl.values = (value_t *) malloc (vl.values_len * sizeof (value_t));
602 if (vl.values == NULL)
605 fprintf (fh, "-1 malloc failed.");
608 DEBUG ("value_ptr = 0x%p; vl.values = 0x%p;", (void *) value_ptr, (void *) vl.values);
610 for (i = 0; i < ds->ds_num; i++)
612 if (strcmp (value_ptr[i], "U") == 0)
613 vl.values[i].gauge = NAN;
614 else if (ds->ds[i].type == DS_TYPE_COUNTER)
615 vl.values[i].counter = atoll (value_ptr[i]);
616 else if (ds->ds[i].type == DS_TYPE_GAUGE)
617 vl.values[i].gauge = atof (value_ptr[i]);
618 } /* for (i = 2 .. fields_num) */
620 plugin_dispatch_values (type, &vl);
622 DEBUG ("value_ptr = 0x%p; vl.values = 0x%p;", (void *) value_ptr, (void *) vl.values);
627 fprintf (fh, "0 Success\n");
631 } /* int us_handle_putval */
633 static int us_handle_listval (FILE *fh, char **fields, int fields_num)
636 char **value_list = NULL;
637 int value_list_len = 0;
638 value_cache_t *entry;
643 DEBUG ("unixsock plugin: us_handle_listval: "
644 "Wrong number of fields: %i", fields_num);
645 fprintf (fh, "-1 Wrong number of fields: Got %i, expected 1.\n",
651 pthread_mutex_lock (&cache_lock);
653 for (entry = cache_head; entry != NULL; entry = entry->next)
657 snprintf (buffer, sizeof (buffer), "%u %s\n",
658 (unsigned int) entry->time, entry->name);
659 buffer[sizeof (buffer) - 1] = '\0';
661 tmp = realloc (value_list, sizeof (char *) * (value_list_len + 1));
666 value_list[value_list_len] = strdup (buffer);
668 if (value_list[value_list_len] != NULL)
672 pthread_mutex_unlock (&cache_lock);
674 DEBUG ("unixsock plugin: us_handle_listval: value_list_len = %i", value_list_len);
675 fprintf (fh, "%i Values found\n", value_list_len);
676 for (i = 0; i < value_list_len; i++)
677 fputs (value_list[i], fh);
681 } /* int us_handle_listval */
683 static void *us_handle_client (void *arg)
695 DEBUG ("Reading from fd #%i", fd);
697 fh = fdopen (fd, "r+");
701 ERROR ("unixsock plugin: fdopen failed: %s",
702 sstrerror (errno, errbuf, sizeof (errbuf)));
704 pthread_exit ((void *) 1);
707 while (fgets (buffer, sizeof (buffer), fh) != NULL)
711 len = strlen (buffer);
713 && ((buffer[len - 1] == '\n') || (buffer[len - 1] == '\r')))
714 buffer[--len] = '\0';
719 DEBUG ("fgets -> buffer = %s; len = %i;", buffer, len);
721 fields_num = strsplit (buffer, fields,
722 sizeof (fields) / sizeof (fields[0]));
730 if (strcasecmp (fields[0], "getval") == 0)
732 us_handle_getval (fh, fields, fields_num);
734 else if (strcasecmp (fields[0], "putval") == 0)
736 us_handle_putval (fh, fields, fields_num);
738 else if (strcasecmp (fields[0], "listval") == 0)
740 us_handle_listval (fh, fields, fields_num);
744 fprintf (fh, "-1 Unknown command: %s\n", fields[0]);
747 } /* while (fgets) */
752 pthread_exit ((void *) 0);
753 } /* void *us_handle_client */
755 static void *us_server_thread (void *arg)
760 pthread_attr_t th_attr;
762 if (us_open_socket () != 0)
763 pthread_exit ((void *) 1);
767 DEBUG ("Calling accept..");
768 status = accept (sock_fd, NULL, NULL);
776 ERROR ("unixsock plugin: accept failed: %s",
777 sstrerror (errno, errbuf, sizeof (errbuf)));
780 pthread_exit ((void *) 1);
783 remote_fd = (int *) malloc (sizeof (int));
784 if (remote_fd == NULL)
787 WARNING ("unixsock plugin: malloc failed: %s",
788 sstrerror (errno, errbuf, sizeof (errbuf)));
794 DEBUG ("Spawning child to handle connection on fd #%i", *remote_fd);
796 pthread_attr_init (&th_attr);
797 pthread_attr_setdetachstate (&th_attr, PTHREAD_CREATE_DETACHED);
799 status = pthread_create (&th, &th_attr, us_handle_client, (void *) remote_fd);
803 WARNING ("unixsock plugin: pthread_create failed: %s",
804 sstrerror (errno, errbuf, sizeof (errbuf)));
814 status = unlink ((sock_file != NULL) ? sock_file : US_DEFAULT_PATH);
818 NOTICE ("unixsock plugin: unlink (%s) failed: %s",
819 (sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
820 sstrerror (errno, errbuf, sizeof (errbuf)));
824 } /* void *us_server_thread */
826 static int us_config (const char *key, const char *val)
828 if (strcasecmp (key, "SocketFile") == 0)
831 sock_file = strdup (val);
833 else if (strcasecmp (key, "SocketGroup") == 0)
836 sock_group = strdup (val);
838 else if (strcasecmp (key, "SocketPerms") == 0)
840 sock_perms = (int) strtol (val, NULL, 8);
848 } /* int us_config */
850 static int us_init (void)
856 status = pthread_create (&listen_thread, NULL, us_server_thread, NULL);
860 ERROR ("unixsock plugin: pthread_create failed: %s",
861 sstrerror (errno, errbuf, sizeof (errbuf)));
868 static int us_shutdown (void)
874 if (listen_thread != (pthread_t) 0)
876 pthread_kill (listen_thread, SIGTERM);
877 pthread_join (listen_thread, &ret);
878 listen_thread = (pthread_t) 0;
881 plugin_unregister_init ("unixsock");
882 plugin_unregister_write ("unixsock");
883 plugin_unregister_shutdown ("unixsock");
886 } /* int us_shutdown */
888 static int us_write (const data_set_t *ds, const value_list_t *vl)
890 cache_update (ds, vl);
891 cache_flush (2 * interval_g);
896 void module_register (void)
898 plugin_register_config ("unixsock", us_config,
899 config_keys, config_keys_num);
900 plugin_register_init ("unixsock", us_init);
901 plugin_register_write ("unixsock", us_write);
902 plugin_register_shutdown ("unixsock", us_shutdown);
903 } /* void module_register (void) */
905 /* vim: set sw=4 ts=4 sts=4 tw=78 : */