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 ("unixsock plugin: cache_insert: ds->ds_num = %i;"
154 " vl->values_len = %i;",
155 ds->ds_num, vl->values_len);
156 assert (ds->ds_num == vl->values_len);
158 vc = (value_cache_t *) malloc (sizeof (value_cache_t));
162 pthread_mutex_unlock (&cache_lock);
163 ERROR ("unixsock plugin: malloc failed: %s",
164 sstrerror (errno, errbuf, sizeof (errbuf)));
168 vc->gauge = (gauge_t *) malloc (sizeof (gauge_t) * vl->values_len);
169 if (vc->gauge == NULL)
172 pthread_mutex_unlock (&cache_lock);
173 ERROR ("unixsock plugin: malloc failed: %s",
174 sstrerror (errno, errbuf, sizeof (errbuf)));
179 vc->counter = (counter_t *) malloc (sizeof (counter_t) * vl->values_len);
180 if (vc->counter == NULL)
183 pthread_mutex_unlock (&cache_lock);
184 ERROR ("unixsock plugin: malloc failed: %s",
185 sstrerror (errno, errbuf, sizeof (errbuf)));
191 if (FORMAT_VL (vc->name, sizeof (vc->name), vl, ds))
193 pthread_mutex_unlock (&cache_lock);
194 ERROR ("unixsock plugin: FORMAT_VL failed.");
201 for (i = 0; i < ds->ds_num; i++)
203 if (ds->ds[i].type == DS_TYPE_COUNTER)
206 vc->counter[i] = vl->values[i].counter;
208 else if (ds->ds[i].type == DS_TYPE_GAUGE)
210 vc->gauge[i] = vl->values[i].gauge;
219 vc->values_num = ds->ds_num;
222 vc->next = cache_head;
226 if (vc->time < cache_oldest)
227 cache_oldest = vc->time;
229 pthread_mutex_unlock (&cache_lock);
231 } /* int cache_insert */
233 static int cache_update (const data_set_t *ds, const value_list_t *vl)
235 char name[4*DATA_MAX_NAME_LEN];;
239 if (FORMAT_VL (name, sizeof (name), vl, ds) != 0)
242 pthread_mutex_lock (&cache_lock);
244 vc = cache_search (name);
247 return (cache_insert (ds, vl));
249 assert (vc->values_num == ds->ds_num);
250 assert (vc->values_num == vl->values_len);
253 * Update the values. This is possibly a lot more that you'd expect
254 * because we honor min and max values and handle counter overflows here.
256 for (i = 0; i < ds->ds_num; i++)
258 if (ds->ds[i].type == DS_TYPE_COUNTER)
260 if (vl->values[i].counter < vc->counter[i])
262 if (vl->values[i].counter <= 4294967295U)
264 vc->gauge[i] = ((4294967295U - vl->values[i].counter)
265 + vc->counter[i]) / (vl->time - vc->time);
269 vc->gauge[i] = ((18446744073709551615ULL - vl->values[i].counter)
270 + vc->counter[i]) / (vl->time - vc->time);
275 vc->gauge[i] = (vl->values[i].counter - vc->counter[i])
276 / (vl->time - vc->time);
279 vc->counter[i] = vl->values[i].counter;
281 else if (ds->ds[i].type == DS_TYPE_GAUGE)
283 vc->gauge[i] = vl->values[i].gauge;
292 if (isnan (vc->gauge[i])
293 || (!isnan (ds->ds[i].min) && (vc->gauge[i] < ds->ds[i].min))
294 || (!isnan (ds->ds[i].max) && (vc->gauge[i] > ds->ds[i].max)))
296 } /* for i = 0 .. ds->ds_num */
301 if (vc->time < cache_oldest)
302 cache_oldest = vc->time;
304 pthread_mutex_unlock (&cache_lock);
306 } /* int cache_update */
308 static void cache_flush (int max_age)
314 pthread_mutex_lock (&cache_lock);
318 if ((now - cache_oldest) <= max_age)
320 pthread_mutex_unlock (&cache_lock);
331 if ((now - this->time) <= max_age)
333 if (this->time < cache_oldest)
334 cache_oldest = this->time;
342 cache_head = this->next;
344 prev->next = this->next;
347 free (this->counter);
354 } /* while (this != NULL) */
356 pthread_mutex_unlock (&cache_lock);
357 } /* int cache_flush */
359 static int us_open_socket (void)
361 struct sockaddr_un sa;
364 sock_fd = socket (PF_UNIX, SOCK_STREAM, 0);
368 ERROR ("unixsock plugin: socket failed: %s",
369 sstrerror (errno, errbuf, sizeof (errbuf)));
373 memset (&sa, '\0', sizeof (sa));
374 sa.sun_family = AF_UNIX;
375 strncpy (sa.sun_path, (sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
376 sizeof (sa.sun_path) - 1);
377 /* unlink (sa.sun_path); */
379 status = bind (sock_fd, (struct sockaddr *) &sa, sizeof (sa));
383 sstrerror (errno, errbuf, sizeof (errbuf));
384 DEBUG ("bind failed: %s; sa.sun_path = %s", errbuf, sa.sun_path);
385 ERROR ("unixsock plugin: bind failed: %s", errbuf);
391 status = listen (sock_fd, 8);
395 ERROR ("unixsock plugin: listen failed: %s",
396 sstrerror (errno, errbuf, sizeof (errbuf)));
409 grpname = (sock_group != NULL) ? sock_group : COLLECTD_GRP_NAME;
412 status = getgrnam_r (grpname, &sg, grbuf, sizeof (grbuf), &g);
416 WARNING ("unixsock plugin: getgrnam_r (%s) failed: %s", grpname,
417 sstrerror (errno, errbuf, sizeof (errbuf)));
422 WARNING ("unixsock plugin: No such group: `%s'",
427 if (chown ((sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
428 (uid_t) -1, g->gr_gid) != 0)
431 WARNING ("unixsock plugin: chown (%s, -1, %i) failed: %s",
432 (sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
434 sstrerror (errno, errbuf, sizeof (errbuf)));
439 } /* int us_open_socket */
441 static int us_handle_getval (FILE *fh, char **fields, int fields_num)
445 char *plugin_instance;
448 char name[4*DATA_MAX_NAME_LEN];
455 DEBUG ("unixsock plugin: Wrong number of fields: %i", fields_num);
456 fprintf (fh, "-1 Wrong number of fields: Got %i, expected 2.\n",
461 DEBUG ("unixsock plugin: Got query for `%s'", fields[1]);
463 status = parse_identifier (fields[1], &hostname,
464 &plugin, &plugin_instance,
465 &type, &type_instance);
468 DEBUG ("unixsock plugin: Cannot parse `%s'", fields[1]);
469 fprintf (fh, "-1 Cannot parse identifier.\n");
474 status = format_name (name, sizeof (name),
475 hostname, plugin, plugin_instance, type, type_instance);
476 /* FIXME: Send some response */
480 pthread_mutex_lock (&cache_lock);
482 DEBUG ("vc = cache_search (%s)", name);
483 vc = cache_search (name);
487 DEBUG ("Did not find cache entry.");
488 fprintf (fh, "-1 No such value");
492 DEBUG ("Found cache entry.");
493 fprintf (fh, "%i", vc->values_num);
494 for (i = 0; i < vc->values_num; i++)
496 fprintf (fh, " %s=", vc->ds->ds[i].name);
497 if (isnan (vc->gauge[i]))
500 fprintf (fh, "%12e", vc->gauge[i]);
504 /* Free the mutex as soon as possible and definitely before flushing */
505 pthread_mutex_unlock (&cache_lock);
511 } /* int us_handle_getval */
513 static int us_handle_putval (FILE *fh, char **fields, int fields_num)
517 char *plugin_instance;
523 const data_set_t *ds;
524 value_list_t vl = VALUE_LIST_INIT;
530 DEBUG ("unixsock plugin: Wrong number of fields: %i", fields_num);
531 fprintf (fh, "-1 Wrong number of fields: Got %i, expected 3.\n",
537 status = parse_identifier (fields[1], &hostname,
538 &plugin, &plugin_instance,
539 &type, &type_instance);
542 DEBUG ("unixsock plugin: Cannot parse `%s'", fields[1]);
543 fprintf (fh, "-1 Cannot parse identifier.\n");
548 /* FIXME: Send some response */
549 if ((strlen (hostname) > sizeof (vl.host))
550 || (strlen (plugin) > sizeof (vl.plugin))
551 || ((plugin_instance != NULL)
552 && (strlen (plugin_instance) > sizeof (vl.plugin_instance)))
553 || ((type_instance != NULL)
554 && (strlen (type_instance) > sizeof (vl.type_instance))))
557 strcpy (vl.host, hostname);
558 strcpy (vl.plugin, plugin);
559 if (plugin_instance != NULL)
560 strcpy (vl.plugin_instance, plugin_instance);
561 if (type_instance != NULL)
562 strcpy (vl.type_instance, type_instance);
564 { /* parse the time */
566 char *v = strchr (t, ':');
571 vl.time = (time_t) atoi (t);
573 vl.time = time (NULL);
578 ds = plugin_get_ds (type);
582 value_ptr = (char **) calloc (ds->ds_num, sizeof (char *));
583 /* FIXME: Send some response */
584 if (value_ptr == NULL)
588 { /* parse the value-list. It's colon-separated. */
596 while ((ptr = strtok_r (dummy, ":", &saveptr)) != NULL)
611 /* FIXME: Send some response */
614 } /* done parsing the value-list */
616 vl.values_len = ds->ds_num;
617 vl.values = (value_t *) malloc (vl.values_len * sizeof (value_t));
618 if (vl.values == NULL)
623 DEBUG ("value_ptr = 0x%p; vl.values = 0x%p;", (void *) value_ptr, (void *) vl.values);
625 for (i = 0; i < ds->ds_num; i++)
627 if (strcmp (value_ptr[i], "U") == 0)
628 vl.values[i].gauge = NAN;
629 else if (ds->ds[i].type == DS_TYPE_COUNTER)
630 vl.values[i].counter = atoll (value_ptr[i]);
631 else if (ds->ds[i].type == DS_TYPE_GAUGE)
632 vl.values[i].gauge = atof (value_ptr[i]);
633 } /* for (i = 2 .. fields_num) */
635 plugin_dispatch_values (type, &vl);
637 DEBUG ("value_ptr = 0x%p; vl.values = 0x%p;", (void *) value_ptr, (void *) vl.values);
642 fprintf (fh, "0 Success\n");
646 } /* int us_handle_putval */
648 static void *us_handle_client (void *arg)
660 DEBUG ("Reading from fd #%i", fd);
662 fh = fdopen (fd, "r+");
666 ERROR ("unixsock plugin: fdopen failed: %s",
667 sstrerror (errno, errbuf, sizeof (errbuf)));
669 pthread_exit ((void *) 1);
672 while (fgets (buffer, sizeof (buffer), fh) != NULL)
676 len = strlen (buffer);
678 && ((buffer[len - 1] == '\n') || (buffer[len - 1] == '\r')))
679 buffer[--len] = '\0';
684 DEBUG ("fgets -> buffer = %s; len = %i;", buffer, len);
686 fields_num = strsplit (buffer, fields,
687 sizeof (fields) / sizeof (fields[0]));
695 if (strcasecmp (fields[0], "getval") == 0)
697 us_handle_getval (fh, fields, fields_num);
699 else if (strcasecmp (fields[0], "putval") == 0)
701 us_handle_putval (fh, fields, fields_num);
705 fprintf (fh, "Unknown command: %s\n", fields[0]);
708 } /* while (fgets) */
713 pthread_exit ((void *) 0);
714 } /* void *us_handle_client */
716 static void *us_server_thread (void *arg)
721 pthread_attr_t th_attr;
723 if (us_open_socket () != 0)
724 pthread_exit ((void *) 1);
728 DEBUG ("Calling accept..");
729 status = accept (sock_fd, NULL, NULL);
737 ERROR ("unixsock plugin: accept failed: %s",
738 sstrerror (errno, errbuf, sizeof (errbuf)));
741 pthread_exit ((void *) 1);
744 remote_fd = (int *) malloc (sizeof (int));
745 if (remote_fd == NULL)
748 WARNING ("unixsock plugin: malloc failed: %s",
749 sstrerror (errno, errbuf, sizeof (errbuf)));
755 DEBUG ("Spawning child to handle connection on fd #%i", *remote_fd);
757 pthread_attr_init (&th_attr);
758 pthread_attr_setdetachstate (&th_attr, PTHREAD_CREATE_DETACHED);
760 status = pthread_create (&th, &th_attr, us_handle_client, (void *) remote_fd);
764 WARNING ("unixsock plugin: pthread_create failed: %s",
765 sstrerror (errno, errbuf, sizeof (errbuf)));
776 } /* void *us_server_thread */
778 static int us_config (const char *key, const char *val)
780 if (strcasecmp (key, "SocketFile") == 0)
783 sock_file = strdup (val);
785 else if (strcasecmp (key, "SocketGroup") == 0)
788 sock_group = strdup (val);
790 else if (strcasecmp (key, "SocketPerms") == 0)
792 sock_perms = (int) strtol (val, NULL, 8);
800 } /* int us_config */
802 static int us_init (void)
808 status = pthread_create (&listen_thread, NULL, us_server_thread, NULL);
812 ERROR ("unixsock plugin: pthread_create failed: %s",
813 sstrerror (errno, errbuf, sizeof (errbuf)));
820 static int us_shutdown (void)
826 if (listen_thread != (pthread_t) 0)
828 pthread_kill (listen_thread, SIGTERM);
829 pthread_join (listen_thread, &ret);
830 listen_thread = (pthread_t) 0;
833 plugin_unregister_init ("unixsock");
834 plugin_unregister_write ("unixsock");
835 plugin_unregister_shutdown ("unixsock");
838 } /* int us_shutdown */
840 static int us_write (const data_set_t *ds, const value_list_t *vl)
842 cache_update (ds, vl);
843 cache_flush (2 * interval_g);
848 void module_register (modreg_e load)
850 plugin_register_config ("unixsock", us_config,
851 config_keys, config_keys_num);
852 plugin_register_init ("unixsock", us_init);
853 plugin_register_write ("unixsock", us_write);
854 plugin_register_shutdown ("unixsock", us_shutdown);
855 } /* void module_register (void) */
857 /* vim: set sw=4 ts=4 sts=4 tw=78 : */