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);
497 /* FIXME: Send some response */
501 pthread_mutex_lock (&cache_lock);
503 DEBUG ("vc = cache_search (%s)", name);
504 vc = cache_search (name);
508 DEBUG ("Did not find cache entry.");
509 fprintf (fh, "-1 No such value");
513 DEBUG ("Found cache entry.");
514 fprintf (fh, "%i", vc->values_num);
515 for (i = 0; i < vc->values_num; i++)
517 fprintf (fh, " %s=", vc->ds->ds[i].name);
518 if (isnan (vc->gauge[i]))
521 fprintf (fh, "%12e", vc->gauge[i]);
525 /* Free the mutex as soon as possible and definitely before flushing */
526 pthread_mutex_unlock (&cache_lock);
532 } /* int us_handle_getval */
534 static int us_handle_putval (FILE *fh, char **fields, int fields_num)
538 char *plugin_instance;
544 const data_set_t *ds;
545 value_list_t vl = VALUE_LIST_INIT;
551 DEBUG ("unixsock plugin: Wrong number of fields: %i", fields_num);
552 fprintf (fh, "-1 Wrong number of fields: Got %i, expected 3.\n",
558 status = parse_identifier (fields[1], &hostname,
559 &plugin, &plugin_instance,
560 &type, &type_instance);
563 DEBUG ("unixsock plugin: Cannot parse `%s'", fields[1]);
564 fprintf (fh, "-1 Cannot parse identifier.\n");
569 /* FIXME: Send some response */
570 if ((strlen (hostname) > sizeof (vl.host))
571 || (strlen (plugin) > sizeof (vl.plugin))
572 || ((plugin_instance != NULL)
573 && (strlen (plugin_instance) > sizeof (vl.plugin_instance)))
574 || ((type_instance != NULL)
575 && (strlen (type_instance) > sizeof (vl.type_instance))))
578 strcpy (vl.host, hostname);
579 strcpy (vl.plugin, plugin);
580 if (plugin_instance != NULL)
581 strcpy (vl.plugin_instance, plugin_instance);
582 if (type_instance != NULL)
583 strcpy (vl.type_instance, type_instance);
585 { /* parse the time */
587 char *v = strchr (t, ':');
592 vl.time = (time_t) atoi (t);
594 vl.time = time (NULL);
599 ds = plugin_get_ds (type);
603 value_ptr = (char **) calloc (ds->ds_num, sizeof (char *));
604 /* FIXME: Send some response */
605 if (value_ptr == NULL)
609 { /* parse the value-list. It's colon-separated. */
617 while ((ptr = strtok_r (dummy, ":", &saveptr)) != NULL)
632 /* FIXME: Send some response */
635 } /* done parsing the value-list */
637 vl.values_len = ds->ds_num;
638 vl.values = (value_t *) malloc (vl.values_len * sizeof (value_t));
639 if (vl.values == NULL)
644 DEBUG ("value_ptr = 0x%p; vl.values = 0x%p;", (void *) value_ptr, (void *) vl.values);
646 for (i = 0; i < ds->ds_num; i++)
648 if (strcmp (value_ptr[i], "U") == 0)
649 vl.values[i].gauge = NAN;
650 else if (ds->ds[i].type == DS_TYPE_COUNTER)
651 vl.values[i].counter = atoll (value_ptr[i]);
652 else if (ds->ds[i].type == DS_TYPE_GAUGE)
653 vl.values[i].gauge = atof (value_ptr[i]);
654 } /* for (i = 2 .. fields_num) */
656 plugin_dispatch_values (type, &vl);
658 DEBUG ("value_ptr = 0x%p; vl.values = 0x%p;", (void *) value_ptr, (void *) vl.values);
663 fprintf (fh, "0 Success\n");
667 } /* int us_handle_putval */
669 static void *us_handle_client (void *arg)
681 DEBUG ("Reading from fd #%i", fd);
683 fh = fdopen (fd, "r+");
687 ERROR ("unixsock plugin: fdopen failed: %s",
688 sstrerror (errno, errbuf, sizeof (errbuf)));
690 pthread_exit ((void *) 1);
693 while (fgets (buffer, sizeof (buffer), fh) != NULL)
697 len = strlen (buffer);
699 && ((buffer[len - 1] == '\n') || (buffer[len - 1] == '\r')))
700 buffer[--len] = '\0';
705 DEBUG ("fgets -> buffer = %s; len = %i;", buffer, len);
707 fields_num = strsplit (buffer, fields,
708 sizeof (fields) / sizeof (fields[0]));
716 if (strcasecmp (fields[0], "getval") == 0)
718 us_handle_getval (fh, fields, fields_num);
720 else if (strcasecmp (fields[0], "putval") == 0)
722 us_handle_putval (fh, fields, fields_num);
726 fprintf (fh, "Unknown command: %s\n", fields[0]);
729 } /* while (fgets) */
734 pthread_exit ((void *) 0);
735 } /* void *us_handle_client */
737 static void *us_server_thread (void *arg)
742 pthread_attr_t th_attr;
744 if (us_open_socket () != 0)
745 pthread_exit ((void *) 1);
749 DEBUG ("Calling accept..");
750 status = accept (sock_fd, NULL, NULL);
758 ERROR ("unixsock plugin: accept failed: %s",
759 sstrerror (errno, errbuf, sizeof (errbuf)));
762 pthread_exit ((void *) 1);
765 remote_fd = (int *) malloc (sizeof (int));
766 if (remote_fd == NULL)
769 WARNING ("unixsock plugin: malloc failed: %s",
770 sstrerror (errno, errbuf, sizeof (errbuf)));
776 DEBUG ("Spawning child to handle connection on fd #%i", *remote_fd);
778 pthread_attr_init (&th_attr);
779 pthread_attr_setdetachstate (&th_attr, PTHREAD_CREATE_DETACHED);
781 status = pthread_create (&th, &th_attr, us_handle_client, (void *) remote_fd);
785 WARNING ("unixsock plugin: pthread_create failed: %s",
786 sstrerror (errno, errbuf, sizeof (errbuf)));
796 status = unlink ((sock_file != NULL) ? sock_file : US_DEFAULT_PATH);
800 NOTICE ("unixsock plugin: unlink (%s) failed: %s",
801 (sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
802 sstrerror (errno, errbuf, sizeof (errbuf)));
806 } /* void *us_server_thread */
808 static int us_config (const char *key, const char *val)
810 if (strcasecmp (key, "SocketFile") == 0)
813 sock_file = strdup (val);
815 else if (strcasecmp (key, "SocketGroup") == 0)
818 sock_group = strdup (val);
820 else if (strcasecmp (key, "SocketPerms") == 0)
822 sock_perms = (int) strtol (val, NULL, 8);
830 } /* int us_config */
832 static int us_init (void)
838 status = pthread_create (&listen_thread, NULL, us_server_thread, NULL);
842 ERROR ("unixsock plugin: pthread_create failed: %s",
843 sstrerror (errno, errbuf, sizeof (errbuf)));
850 static int us_shutdown (void)
856 if (listen_thread != (pthread_t) 0)
858 pthread_kill (listen_thread, SIGTERM);
859 pthread_join (listen_thread, &ret);
860 listen_thread = (pthread_t) 0;
863 plugin_unregister_init ("unixsock");
864 plugin_unregister_write ("unixsock");
865 plugin_unregister_shutdown ("unixsock");
868 } /* int us_shutdown */
870 static int us_write (const data_set_t *ds, const value_list_t *vl)
872 cache_update (ds, vl);
873 cache_flush (2 * interval_g);
878 void module_register (void)
880 plugin_register_config ("unixsock", us_config,
881 config_keys, config_keys_num);
882 plugin_register_init ("unixsock", us_init);
883 plugin_register_write ("unixsock", us_write);
884 plugin_register_shutdown ("unixsock", us_shutdown);
885 } /* void module_register (void) */
887 /* vim: set sw=4 ts=4 sts=4 tw=78 : */