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 PREFIX"/var/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 int parse_identifier (char *str, char **ret_host,
88 char **ret_plugin, char **ret_plugin_instance,
89 char **ret_type, char **ret_type_instance)
91 char *hostname = NULL;
93 char *plugin_instance = NULL;
95 char *type_instance = NULL;
101 plugin = strchr (hostname, '/');
104 *plugin = '\0'; plugin++;
106 type = strchr (plugin, '/');
109 *type = '\0'; type++;
111 plugin_instance = strchr (plugin, '-');
112 if (plugin_instance != NULL)
114 *plugin_instance = '\0';
118 type_instance = strchr (type, '-');
119 if (type_instance != NULL)
121 *type_instance = '\0';
125 *ret_host = hostname;
126 *ret_plugin = plugin;
127 *ret_plugin_instance = plugin_instance;
129 *ret_type_instance = type_instance;
131 } /* int parse_identifier */
133 static value_cache_t *cache_search (const char *name)
137 for (vc = cache_head; vc != NULL; vc = vc->next)
139 if (strcmp (vc->name, name) == 0)
141 } /* for vc = cache_head .. NULL */
144 } /* value_cache_t *cache_search */
146 static int cache_insert (const data_set_t *ds, const value_list_t *vl)
148 /* We're called from `cache_update' so we don't need to lock the mutex */
152 DEBUG ("unixsock plugin: cache_insert: ds->type = %s; ds->ds_num = %i;"
153 " vl->values_len = %i;",
154 ds->type, ds->ds_num, vl->values_len);
156 assert (ds->ds_num == vl->values_len);
158 if (ds->ds_num != vl->values_len)
160 ERROR ("unixsock plugin: ds->type = %s: (ds->ds_num = %i) != "
161 "(vl->values_len = %i)",
162 ds->type, ds->ds_num, vl->values_len);
167 vc = (value_cache_t *) malloc (sizeof (value_cache_t));
171 pthread_mutex_unlock (&cache_lock);
172 ERROR ("unixsock plugin: malloc failed: %s",
173 sstrerror (errno, errbuf, sizeof (errbuf)));
177 vc->gauge = (gauge_t *) malloc (sizeof (gauge_t) * vl->values_len);
178 if (vc->gauge == NULL)
181 pthread_mutex_unlock (&cache_lock);
182 ERROR ("unixsock plugin: malloc failed: %s",
183 sstrerror (errno, errbuf, sizeof (errbuf)));
188 vc->counter = (counter_t *) malloc (sizeof (counter_t) * vl->values_len);
189 if (vc->counter == NULL)
192 pthread_mutex_unlock (&cache_lock);
193 ERROR ("unixsock plugin: malloc failed: %s",
194 sstrerror (errno, errbuf, sizeof (errbuf)));
200 if (FORMAT_VL (vc->name, sizeof (vc->name), vl, ds))
202 pthread_mutex_unlock (&cache_lock);
203 ERROR ("unixsock plugin: FORMAT_VL failed.");
210 for (i = 0; i < ds->ds_num; i++)
212 if (ds->ds[i].type == DS_TYPE_COUNTER)
215 vc->counter[i] = vl->values[i].counter;
217 else if (ds->ds[i].type == DS_TYPE_GAUGE)
219 vc->gauge[i] = vl->values[i].gauge;
228 vc->values_num = ds->ds_num;
231 vc->next = cache_head;
235 if (vc->time < cache_oldest)
236 cache_oldest = vc->time;
238 pthread_mutex_unlock (&cache_lock);
240 } /* int cache_insert */
242 static int cache_update (const data_set_t *ds, const value_list_t *vl)
244 char name[4*DATA_MAX_NAME_LEN];;
248 if (FORMAT_VL (name, sizeof (name), vl, ds) != 0)
251 pthread_mutex_lock (&cache_lock);
253 vc = cache_search (name);
255 /* pthread_mutex_lock is called by cache_insert. */
257 return (cache_insert (ds, vl));
259 assert (vc->values_num == ds->ds_num);
260 assert (vc->values_num == vl->values_len);
262 /* Avoid floating-point exceptions due to division by zero. */
263 if (vc->time >= vl->time)
265 pthread_mutex_unlock (&cache_lock);
266 ERROR ("unixsock plugin: vc->time >= vl->time. vc->time = %u; "
267 "vl->time = %u; vl = %s;",
268 (unsigned int) vc->time, (unsigned int) vl->time,
271 } /* if (vc->time >= vl->time) */
274 * Update the values. This is possibly a lot more that you'd expect
275 * because we honor min and max values and handle counter overflows here.
277 for (i = 0; i < ds->ds_num; i++)
279 if (ds->ds[i].type == DS_TYPE_COUNTER)
281 if (vl->values[i].counter < vc->counter[i])
283 if (vl->values[i].counter <= 4294967295U)
285 vc->gauge[i] = ((4294967295U - vl->values[i].counter)
286 + vc->counter[i]) / (vl->time - vc->time);
290 vc->gauge[i] = ((18446744073709551615ULL - vl->values[i].counter)
291 + vc->counter[i]) / (vl->time - vc->time);
296 vc->gauge[i] = (vl->values[i].counter - vc->counter[i])
297 / (vl->time - vc->time);
300 vc->counter[i] = vl->values[i].counter;
302 else if (ds->ds[i].type == DS_TYPE_GAUGE)
304 vc->gauge[i] = vl->values[i].gauge;
313 if (isnan (vc->gauge[i])
314 || (!isnan (ds->ds[i].min) && (vc->gauge[i] < ds->ds[i].min))
315 || (!isnan (ds->ds[i].max) && (vc->gauge[i] > ds->ds[i].max)))
317 } /* for i = 0 .. ds->ds_num */
322 if (vc->time < cache_oldest)
323 cache_oldest = vc->time;
325 pthread_mutex_unlock (&cache_lock);
327 } /* int cache_update */
329 static void cache_flush (int max_age)
335 pthread_mutex_lock (&cache_lock);
339 if ((now - cache_oldest) <= max_age)
341 pthread_mutex_unlock (&cache_lock);
352 if ((now - this->time) <= max_age)
354 if (this->time < cache_oldest)
355 cache_oldest = this->time;
363 cache_head = this->next;
365 prev->next = this->next;
368 free (this->counter);
375 } /* while (this != NULL) */
377 pthread_mutex_unlock (&cache_lock);
378 } /* int cache_flush */
380 static int us_open_socket (void)
382 struct sockaddr_un sa;
385 sock_fd = socket (PF_UNIX, SOCK_STREAM, 0);
389 ERROR ("unixsock plugin: socket failed: %s",
390 sstrerror (errno, errbuf, sizeof (errbuf)));
394 memset (&sa, '\0', sizeof (sa));
395 sa.sun_family = AF_UNIX;
396 strncpy (sa.sun_path, (sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
397 sizeof (sa.sun_path) - 1);
398 /* unlink (sa.sun_path); */
400 status = bind (sock_fd, (struct sockaddr *) &sa, sizeof (sa));
404 sstrerror (errno, errbuf, sizeof (errbuf));
405 DEBUG ("bind failed: %s; sa.sun_path = %s", errbuf, sa.sun_path);
406 ERROR ("unixsock plugin: bind failed: %s", errbuf);
412 status = listen (sock_fd, 8);
416 ERROR ("unixsock plugin: listen failed: %s",
417 sstrerror (errno, errbuf, sizeof (errbuf)));
430 grpname = (sock_group != NULL) ? sock_group : COLLECTD_GRP_NAME;
433 status = getgrnam_r (grpname, &sg, grbuf, sizeof (grbuf), &g);
437 WARNING ("unixsock plugin: getgrnam_r (%s) failed: %s", grpname,
438 sstrerror (errno, errbuf, sizeof (errbuf)));
443 WARNING ("unixsock plugin: No such group: `%s'",
448 if (chown ((sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
449 (uid_t) -1, g->gr_gid) != 0)
452 WARNING ("unixsock plugin: chown (%s, -1, %i) failed: %s",
453 (sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
455 sstrerror (errno, errbuf, sizeof (errbuf)));
460 } /* int us_open_socket */
462 static int us_handle_getval (FILE *fh, char **fields, int fields_num)
466 char *plugin_instance;
469 char name[4*DATA_MAX_NAME_LEN];
476 DEBUG ("unixsock plugin: Wrong number of fields: %i", fields_num);
477 fprintf (fh, "-1 Wrong number of fields: Got %i, expected 2.\n",
482 DEBUG ("unixsock plugin: Got query for `%s'", fields[1]);
484 status = parse_identifier (fields[1], &hostname,
485 &plugin, &plugin_instance,
486 &type, &type_instance);
489 DEBUG ("unixsock plugin: Cannot parse `%s'", fields[1]);
490 fprintf (fh, "-1 Cannot parse identifier.\n");
495 status = format_name (name, sizeof (name),
496 hostname, plugin, plugin_instance, type, type_instance);
499 fprintf (fh, "-1 format_name failed.\n");
503 pthread_mutex_lock (&cache_lock);
505 DEBUG ("vc = cache_search (%s)", name);
506 vc = cache_search (name);
510 DEBUG ("Did not find cache entry.");
511 fprintf (fh, "-1 No such value");
515 DEBUG ("Found cache entry.");
516 fprintf (fh, "%i", vc->values_num);
517 for (i = 0; i < vc->values_num; i++)
519 fprintf (fh, " %s=", vc->ds->ds[i].name);
520 if (isnan (vc->gauge[i]))
523 fprintf (fh, "%12e", vc->gauge[i]);
527 /* Free the mutex as soon as possible and definitely before flushing */
528 pthread_mutex_unlock (&cache_lock);
534 } /* int us_handle_getval */
536 static int us_handle_putval (FILE *fh, char **fields, int fields_num)
540 char *plugin_instance;
546 const data_set_t *ds;
547 value_list_t vl = VALUE_LIST_INIT;
553 DEBUG ("unixsock plugin: Wrong number of fields: %i", fields_num);
554 fprintf (fh, "-1 Wrong number of fields: Got %i, expected 3.\n",
560 status = parse_identifier (fields[1], &hostname,
561 &plugin, &plugin_instance,
562 &type, &type_instance);
565 DEBUG ("unixsock plugin: Cannot parse `%s'", fields[1]);
566 fprintf (fh, "-1 Cannot parse identifier.\n");
571 if ((strlen (hostname) > sizeof (vl.host))
572 || (strlen (plugin) > sizeof (vl.plugin))
573 || ((plugin_instance != NULL)
574 && (strlen (plugin_instance) > sizeof (vl.plugin_instance)))
575 || ((type_instance != NULL)
576 && (strlen (type_instance) > sizeof (vl.type_instance))))
578 fprintf (fh, "-1 Identifier too long.");
582 strcpy (vl.host, hostname);
583 strcpy (vl.plugin, plugin);
584 if (plugin_instance != NULL)
585 strcpy (vl.plugin_instance, plugin_instance);
586 if (type_instance != NULL)
587 strcpy (vl.type_instance, type_instance);
589 { /* parse the time */
591 char *v = strchr (t, ':');
594 fprintf (fh, "-1 No time found.");
599 vl.time = (time_t) atoi (t);
601 vl.time = time (NULL);
606 ds = plugin_get_ds (type);
610 value_ptr = (char **) calloc (ds->ds_num, sizeof (char *));
611 if (value_ptr == NULL)
613 fprintf (fh, "-1 calloc failed.");
617 { /* parse the value-list. It's colon-separated. */
625 while ((ptr = strtok_r (dummy, ":", &saveptr)) != NULL)
640 fprintf (fh, "-1 Number of values incorrect: Got %i, "
641 "expected %i.", i, ds->ds_num);
644 } /* done parsing the value-list */
646 vl.values_len = ds->ds_num;
647 vl.values = (value_t *) malloc (vl.values_len * sizeof (value_t));
648 if (vl.values == NULL)
651 fprintf (fh, "-1 malloc failed.");
654 DEBUG ("value_ptr = 0x%p; vl.values = 0x%p;", (void *) value_ptr, (void *) vl.values);
656 for (i = 0; i < ds->ds_num; i++)
658 if (strcmp (value_ptr[i], "U") == 0)
659 vl.values[i].gauge = NAN;
660 else if (ds->ds[i].type == DS_TYPE_COUNTER)
661 vl.values[i].counter = atoll (value_ptr[i]);
662 else if (ds->ds[i].type == DS_TYPE_GAUGE)
663 vl.values[i].gauge = atof (value_ptr[i]);
664 } /* for (i = 2 .. fields_num) */
666 plugin_dispatch_values (type, &vl);
668 DEBUG ("value_ptr = 0x%p; vl.values = 0x%p;", (void *) value_ptr, (void *) vl.values);
673 fprintf (fh, "0 Success\n");
677 } /* int us_handle_putval */
679 static void *us_handle_client (void *arg)
691 DEBUG ("Reading from fd #%i", fd);
693 fh = fdopen (fd, "r+");
697 ERROR ("unixsock plugin: fdopen failed: %s",
698 sstrerror (errno, errbuf, sizeof (errbuf)));
700 pthread_exit ((void *) 1);
703 while (fgets (buffer, sizeof (buffer), fh) != NULL)
707 len = strlen (buffer);
709 && ((buffer[len - 1] == '\n') || (buffer[len - 1] == '\r')))
710 buffer[--len] = '\0';
715 DEBUG ("fgets -> buffer = %s; len = %i;", buffer, len);
717 fields_num = strsplit (buffer, fields,
718 sizeof (fields) / sizeof (fields[0]));
726 if (strcasecmp (fields[0], "getval") == 0)
728 us_handle_getval (fh, fields, fields_num);
730 else if (strcasecmp (fields[0], "putval") == 0)
732 us_handle_putval (fh, fields, fields_num);
736 fprintf (fh, "-1 Unknown command: %s\n", fields[0]);
739 } /* while (fgets) */
744 pthread_exit ((void *) 0);
745 } /* void *us_handle_client */
747 static void *us_server_thread (void *arg)
752 pthread_attr_t th_attr;
754 if (us_open_socket () != 0)
755 pthread_exit ((void *) 1);
759 DEBUG ("Calling accept..");
760 status = accept (sock_fd, NULL, NULL);
768 ERROR ("unixsock plugin: accept failed: %s",
769 sstrerror (errno, errbuf, sizeof (errbuf)));
772 pthread_exit ((void *) 1);
775 remote_fd = (int *) malloc (sizeof (int));
776 if (remote_fd == NULL)
779 WARNING ("unixsock plugin: malloc failed: %s",
780 sstrerror (errno, errbuf, sizeof (errbuf)));
786 DEBUG ("Spawning child to handle connection on fd #%i", *remote_fd);
788 pthread_attr_init (&th_attr);
789 pthread_attr_setdetachstate (&th_attr, PTHREAD_CREATE_DETACHED);
791 status = pthread_create (&th, &th_attr, us_handle_client, (void *) remote_fd);
795 WARNING ("unixsock plugin: pthread_create failed: %s",
796 sstrerror (errno, errbuf, sizeof (errbuf)));
806 status = unlink ((sock_file != NULL) ? sock_file : US_DEFAULT_PATH);
810 NOTICE ("unixsock plugin: unlink (%s) failed: %s",
811 (sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
812 sstrerror (errno, errbuf, sizeof (errbuf)));
816 } /* void *us_server_thread */
818 static int us_config (const char *key, const char *val)
820 if (strcasecmp (key, "SocketFile") == 0)
823 sock_file = strdup (val);
825 else if (strcasecmp (key, "SocketGroup") == 0)
828 sock_group = strdup (val);
830 else if (strcasecmp (key, "SocketPerms") == 0)
832 sock_perms = (int) strtol (val, NULL, 8);
840 } /* int us_config */
842 static int us_init (void)
848 status = pthread_create (&listen_thread, NULL, us_server_thread, NULL);
852 ERROR ("unixsock plugin: pthread_create failed: %s",
853 sstrerror (errno, errbuf, sizeof (errbuf)));
860 static int us_shutdown (void)
866 if (listen_thread != (pthread_t) 0)
868 pthread_kill (listen_thread, SIGTERM);
869 pthread_join (listen_thread, &ret);
870 listen_thread = (pthread_t) 0;
873 plugin_unregister_init ("unixsock");
874 plugin_unregister_write ("unixsock");
875 plugin_unregister_shutdown ("unixsock");
878 } /* int us_shutdown */
880 static int us_write (const data_set_t *ds, const value_list_t *vl)
882 cache_update (ds, vl);
883 cache_flush (2 * interval_g);
888 void module_register (void)
890 plugin_register_config ("unixsock", us_config,
891 config_keys, config_keys_num);
892 plugin_register_init ("unixsock", us_init);
893 plugin_register_write ("unixsock", us_write);
894 plugin_register_shutdown ("unixsock", us_shutdown);
895 } /* void module_register (void) */
897 /* vim: set sw=4 ts=4 sts=4 tw=78 : */