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>
37 # define UNIX_PATH_MAX sizeof (((struct sockaddr_un *)0)->sun_path)
40 #define US_DEFAULT_PATH PREFIX"/var/run/"PACKAGE_NAME"-unixsock"
43 * Private data structures
45 /* linked list of cached values */
46 typedef struct value_cache_s
48 char name[4*DATA_MAX_NAME_LEN];
54 struct value_cache_s *next;
60 /* valid configuration file keys */
61 static const char *config_keys[] =
68 static int config_keys_num = 3;
72 /* socket configuration */
73 static int sock_fd = -1;
74 static char *sock_file = NULL;
75 static char *sock_group = NULL;
76 static int sock_perms = S_IRWXU | S_IRWXG;
78 static pthread_t listen_thread = (pthread_t) 0;
80 /* Linked list and auxilliary variables for saving values */
81 static value_cache_t *cache_head = NULL;
82 static pthread_mutex_t cache_lock = PTHREAD_MUTEX_INITIALIZER;
83 static unsigned int cache_oldest = UINT_MAX;
88 static int parse_identifier (char *str, char **ret_host,
89 char **ret_plugin, char **ret_plugin_instance,
90 char **ret_type, char **ret_type_instance)
92 char *hostname = NULL;
94 char *plugin_instance = NULL;
96 char *type_instance = NULL;
102 plugin = strchr (hostname, '/');
105 *plugin = '\0'; plugin++;
107 type = strchr (plugin, '/');
110 *type = '\0'; type++;
112 plugin_instance = strchr (plugin, '-');
113 if (plugin_instance != NULL)
115 *plugin_instance = '\0';
119 type_instance = strchr (type, '-');
120 if (type_instance != NULL)
122 *type_instance = '\0';
126 *ret_host = hostname;
127 *ret_plugin = plugin;
128 *ret_plugin_instance = plugin_instance;
130 *ret_type_instance = type_instance;
132 } /* int parse_identifier */
134 static value_cache_t *cache_search (const char *name)
138 for (vc = cache_head; vc != NULL; vc = vc->next)
140 if (strcmp (vc->name, name) == 0)
142 } /* for vc = cache_head .. NULL */
145 } /* value_cache_t *cache_search */
147 static int cache_insert (const data_set_t *ds, const value_list_t *vl)
149 /* We're called from `cache_update' so we don't need to lock the mutex */
153 DEBUG ("ds->ds_num = %i; vl->values_len = %i;",
154 ds->ds_num, vl->values_len);
155 assert (ds->ds_num == vl->values_len);
157 vc = (value_cache_t *) malloc (sizeof (value_cache_t));
161 pthread_mutex_unlock (&cache_lock);
162 ERROR ("unixsock plugin: malloc failed: %s",
163 sstrerror (errno, errbuf, sizeof (errbuf)));
167 vc->gauge = (gauge_t *) malloc (sizeof (gauge_t) * vl->values_len);
168 if (vc->gauge == NULL)
171 pthread_mutex_unlock (&cache_lock);
172 ERROR ("unixsock plugin: malloc failed: %s",
173 sstrerror (errno, errbuf, sizeof (errbuf)));
178 vc->counter = (counter_t *) malloc (sizeof (counter_t) * vl->values_len);
179 if (vc->counter == NULL)
182 pthread_mutex_unlock (&cache_lock);
183 ERROR ("unixsock plugin: malloc failed: %s",
184 sstrerror (errno, errbuf, sizeof (errbuf)));
190 if (FORMAT_VL (vc->name, sizeof (vc->name), vl, ds))
192 pthread_mutex_unlock (&cache_lock);
193 ERROR ("unixsock plugin: FORMAT_VL failed.");
200 for (i = 0; i < ds->ds_num; i++)
202 if (ds->ds[i].type == DS_TYPE_COUNTER)
205 vc->counter[i] = vl->values[i].counter;
207 else if (ds->ds[i].type == DS_TYPE_GAUGE)
209 vc->gauge[i] = vl->values[i].gauge;
218 vc->values_num = ds->ds_num;
221 vc->next = cache_head;
225 if (vc->time < cache_oldest)
226 cache_oldest = vc->time;
228 pthread_mutex_unlock (&cache_lock);
230 } /* int cache_insert */
232 static int cache_update (const data_set_t *ds, const value_list_t *vl)
234 char name[4*DATA_MAX_NAME_LEN];;
238 if (FORMAT_VL (name, sizeof (name), vl, ds) != 0)
241 pthread_mutex_lock (&cache_lock);
243 vc = cache_search (name);
246 return (cache_insert (ds, vl));
248 assert (vc->values_num == ds->ds_num);
249 assert (vc->values_num == vl->values_len);
252 * Update the values. This is possibly a lot more that you'd expect
253 * because we honor min and max values and handle counter overflows here.
255 for (i = 0; i < ds->ds_num; i++)
257 if (ds->ds[i].type == DS_TYPE_COUNTER)
259 if (vl->values[i].counter < vc->counter[i])
261 if (vl->values[i].counter <= 4294967295U)
263 vc->gauge[i] = ((4294967295U - vl->values[i].counter)
264 + vc->counter[i]) / (vl->time - vc->time);
268 vc->gauge[i] = ((18446744073709551615ULL - vl->values[i].counter)
269 + vc->counter[i]) / (vl->time - vc->time);
274 vc->gauge[i] = (vl->values[i].counter - vc->counter[i])
275 / (vl->time - vc->time);
278 vc->counter[i] = vl->values[i].counter;
280 else if (ds->ds[i].type == DS_TYPE_GAUGE)
282 vc->gauge[i] = vl->values[i].gauge;
291 if (isnan (vc->gauge[i])
292 || (!isnan (ds->ds[i].min) && (vc->gauge[i] < ds->ds[i].min))
293 || (!isnan (ds->ds[i].max) && (vc->gauge[i] > ds->ds[i].max)))
295 } /* for i = 0 .. ds->ds_num */
300 if (vc->time < cache_oldest)
301 cache_oldest = vc->time;
303 pthread_mutex_unlock (&cache_lock);
305 } /* int cache_update */
307 static void cache_flush (int max_age)
313 pthread_mutex_lock (&cache_lock);
317 if ((now - cache_oldest) <= max_age)
319 pthread_mutex_unlock (&cache_lock);
330 if ((now - this->time) <= max_age)
332 if (this->time < cache_oldest)
333 cache_oldest = this->time;
341 cache_head = this->next;
343 prev->next = this->next;
346 free (this->counter);
353 } /* while (this != NULL) */
355 pthread_mutex_unlock (&cache_lock);
356 } /* int cache_flush */
358 static int us_open_socket (void)
360 struct sockaddr_un sa;
363 sock_fd = socket (PF_UNIX, SOCK_STREAM, 0);
367 ERROR ("unixsock plugin: socket failed: %s",
368 sstrerror (errno, errbuf, sizeof (errbuf)));
372 memset (&sa, '\0', sizeof (sa));
373 sa.sun_family = AF_UNIX;
374 strncpy (sa.sun_path, (sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
375 sizeof (sa.sun_path) - 1);
376 /* unlink (sa.sun_path); */
378 status = bind (sock_fd, (struct sockaddr *) &sa, sizeof (sa));
382 sstrerror (errno, errbuf, sizeof (errbuf));
383 DEBUG ("bind failed: %s; sa.sun_path = %s", errbuf, sa.sun_path);
384 ERROR ("unixsock plugin: bind failed: %s", errbuf);
390 status = listen (sock_fd, 8);
394 ERROR ("unixsock plugin: listen failed: %s",
395 sstrerror (errno, errbuf, sizeof (errbuf)));
408 grpname = (sock_group != NULL) ? sock_group : COLLECTD_GRP_NAME;
411 status = getgrnam_r (grpname, &sg, grbuf, sizeof (grbuf), &g);
415 WARNING ("unixsock plugin: getgrnam_r (%s) failed: %s", grpname,
416 sstrerror (errno, errbuf, sizeof (errbuf)));
421 WARNING ("unixsock plugin: No such group: `%s'",
426 if (chown ((sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
427 (uid_t) -1, g->gr_gid) != 0)
430 WARNING ("unixsock plugin: chown (%s, -1, %i) failed: %s",
431 (sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
433 sstrerror (errno, errbuf, sizeof (errbuf)));
438 } /* int us_open_socket */
440 static int us_handle_getval (FILE *fh, char **fields, int fields_num)
444 char *plugin_instance;
447 char name[4*DATA_MAX_NAME_LEN];
454 DEBUG ("unixsock plugin: Wrong number of fields: %i", fields_num);
455 fprintf (fh, "-1 Wrong number of fields: Got %i, expected 2.\n",
460 DEBUG ("unixsock plugin: Got query for `%s'", fields[1]);
462 status = parse_identifier (fields[1], &hostname,
463 &plugin, &plugin_instance,
464 &type, &type_instance);
467 DEBUG ("unixsock plugin: Cannot parse `%s'", fields[1]);
468 fprintf (fh, "-1 Cannot parse identifier.\n");
473 status = format_name (name, sizeof (name),
474 hostname, plugin, plugin_instance, type, type_instance);
475 /* FIXME: Send some response */
479 pthread_mutex_lock (&cache_lock);
481 DEBUG ("vc = cache_search (%s)", name);
482 vc = cache_search (name);
486 DEBUG ("Did not find cache entry.");
487 fprintf (fh, "-1 No such value");
491 DEBUG ("Found cache entry.");
492 fprintf (fh, "%i", vc->values_num);
493 for (i = 0; i < vc->values_num; i++)
495 fprintf (fh, " %s=", vc->ds->ds[i].name);
496 if (isnan (vc->gauge[i]))
499 fprintf (fh, "%12e", vc->gauge[i]);
503 /* Free the mutex as soon as possible and definitely before flushing */
504 pthread_mutex_unlock (&cache_lock);
510 } /* int us_handle_getval */
512 static int us_handle_putval (FILE *fh, char **fields, int fields_num)
516 char *plugin_instance;
522 const data_set_t *ds;
523 value_list_t vl = VALUE_LIST_INIT;
529 DEBUG ("unixsock plugin: Wrong number of fields: %i", fields_num);
530 fprintf (fh, "-1 Wrong number of fields: Got %i, expected 3.\n",
536 status = parse_identifier (fields[1], &hostname,
537 &plugin, &plugin_instance,
538 &type, &type_instance);
541 DEBUG ("unixsock plugin: Cannot parse `%s'", fields[1]);
542 fprintf (fh, "-1 Cannot parse identifier.\n");
547 /* FIXME: Send some response */
548 if ((strlen (hostname) > sizeof (vl.host))
549 || (strlen (plugin) > sizeof (vl.plugin))
550 || ((plugin_instance != NULL)
551 && (strlen (plugin_instance) > sizeof (vl.plugin_instance)))
552 || ((type_instance != NULL)
553 && (strlen (type_instance) > sizeof (vl.type_instance))))
556 strcpy (vl.host, hostname);
557 strcpy (vl.plugin, plugin);
558 if (plugin_instance != NULL)
559 strcpy (vl.plugin_instance, plugin_instance);
560 if (type_instance != NULL)
561 strcpy (vl.type_instance, type_instance);
563 { /* parse the time */
565 char *v = strchr (t, ':');
570 vl.time = (time_t) atoi (t);
572 vl.time = time (NULL);
577 ds = plugin_get_ds (type);
581 value_ptr = (char **) calloc (ds->ds_num, sizeof (char *));
582 /* FIXME: Send some response */
583 if (value_ptr == NULL)
587 { /* parse the value-list. It's colon-separated. */
595 while ((ptr = strtok_r (dummy, ":", &saveptr)) != NULL)
610 /* FIXME: Send some response */
613 } /* done parsing the value-list */
615 vl.values_len = ds->ds_num;
616 vl.values = (value_t *) malloc (vl.values_len * sizeof (value_t));
617 if (vl.values == NULL)
622 DEBUG ("value_ptr = 0x%p; vl.values = 0x%p;", (void *) value_ptr, (void *) vl.values);
624 for (i = 0; i < ds->ds_num; i++)
626 if (strcmp (value_ptr[i], "U") == 0)
627 vl.values[i].gauge = NAN;
628 else if (ds->ds[i].type == DS_TYPE_COUNTER)
629 vl.values[i].counter = atoll (value_ptr[i]);
630 else if (ds->ds[i].type == DS_TYPE_GAUGE)
631 vl.values[i].gauge = atof (value_ptr[i]);
632 } /* for (i = 2 .. fields_num) */
634 plugin_dispatch_values (type, &vl);
636 DEBUG ("value_ptr = 0x%p; vl.values = 0x%p;", (void *) value_ptr, (void *) vl.values);
641 fprintf (fh, "0 Success\n");
645 } /* int us_handle_putval */
647 static void *us_handle_client (void *arg)
659 DEBUG ("Reading from fd #%i", fd);
661 fh = fdopen (fd, "r+");
665 ERROR ("unixsock plugin: fdopen failed: %s",
666 sstrerror (errno, errbuf, sizeof (errbuf)));
668 pthread_exit ((void *) 1);
671 while (fgets (buffer, sizeof (buffer), fh) != NULL)
675 len = strlen (buffer);
677 && ((buffer[len - 1] == '\n') || (buffer[len - 1] == '\r')))
678 buffer[--len] = '\0';
683 DEBUG ("fgets -> buffer = %s; len = %i;", buffer, len);
685 fields_num = strsplit (buffer, fields,
686 sizeof (fields) / sizeof (fields[0]));
694 if (strcasecmp (fields[0], "getval") == 0)
696 us_handle_getval (fh, fields, fields_num);
698 else if (strcasecmp (fields[0], "putval") == 0)
700 us_handle_putval (fh, fields, fields_num);
704 fprintf (fh, "Unknown command: %s\n", fields[0]);
707 } /* while (fgets) */
712 pthread_exit ((void *) 0);
713 } /* void *us_handle_client */
715 static void *us_server_thread (void *arg)
720 pthread_attr_t th_attr;
722 if (us_open_socket () != 0)
723 pthread_exit ((void *) 1);
727 DEBUG ("Calling accept..");
728 status = accept (sock_fd, NULL, NULL);
736 ERROR ("unixsock plugin: accept failed: %s",
737 sstrerror (errno, errbuf, sizeof (errbuf)));
740 pthread_exit ((void *) 1);
743 remote_fd = (int *) malloc (sizeof (int));
744 if (remote_fd == NULL)
747 WARNING ("unixsock plugin: malloc failed: %s",
748 sstrerror (errno, errbuf, sizeof (errbuf)));
754 DEBUG ("Spawning child to handle connection on fd #%i", *remote_fd);
756 pthread_attr_init (&th_attr);
757 pthread_attr_setdetachstate (&th_attr, PTHREAD_CREATE_DETACHED);
759 status = pthread_create (&th, &th_attr, us_handle_client, (void *) remote_fd);
763 WARNING ("unixsock plugin: pthread_create failed: %s",
764 sstrerror (errno, errbuf, sizeof (errbuf)));
775 } /* void *us_server_thread */
777 static int us_config (const char *key, const char *val)
779 if (strcasecmp (key, "SocketFile") == 0)
782 sock_file = strdup (val);
784 else if (strcasecmp (key, "SocketGroup") == 0)
787 sock_group = strdup (val);
789 else if (strcasecmp (key, "SocketPerms") == 0)
791 sock_perms = (int) strtol (val, NULL, 8);
799 } /* int us_config */
801 static int us_init (void)
807 status = pthread_create (&listen_thread, NULL, us_server_thread, NULL);
811 ERROR ("unixsock plugin: pthread_create failed: %s",
812 sstrerror (errno, errbuf, sizeof (errbuf)));
819 static int us_shutdown (void)
825 if (listen_thread != (pthread_t) 0)
827 pthread_kill (listen_thread, SIGTERM);
828 pthread_join (listen_thread, &ret);
829 listen_thread = (pthread_t) 0;
832 plugin_unregister_init ("unixsock");
833 plugin_unregister_write ("unixsock");
834 plugin_unregister_shutdown ("unixsock");
837 } /* int us_shutdown */
839 static int us_write (const data_set_t *ds, const value_list_t *vl)
841 cache_update (ds, vl);
842 cache_flush (2 * interval_g);
847 void module_register (void)
849 plugin_register_config ("unixsock", us_config,
850 config_keys, config_keys_num);
851 plugin_register_init ("unixsock", us_init);
852 plugin_register_write ("unixsock", us_write);
853 plugin_register_shutdown ("unixsock", us_shutdown);
854 } /* void module_register (void) */
856 /* vim: set sw=4 ts=4 sts=4 tw=78 : */