2 * collectd - src/unixsock.c
3 * Copyright (C) 2007,2008 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 /* FIXME: Replace this with "utils_cmd_getval.h" asap */
28 #include "utils_cache.h"
30 #include "utils_cmd_putval.h"
31 #include "utils_cmd_putnotif.h"
33 /* Folks without pthread will need to disable this plugin. */
36 #include <sys/socket.h>
43 # define UNIX_PATH_MAX sizeof (((struct sockaddr_un *)0)->sun_path)
46 #define US_DEFAULT_PATH LOCALSTATEDIR"/run/"PACKAGE_NAME"-unixsock"
49 * Private data structures
51 /* linked list of cached values */
52 typedef struct value_cache_s
54 char name[4*DATA_MAX_NAME_LEN];
60 struct value_cache_s *next;
66 /* valid configuration file keys */
67 static const char *config_keys[] =
74 static int config_keys_num = 3;
78 /* socket configuration */
79 static int sock_fd = -1;
80 static char *sock_file = NULL;
81 static char *sock_group = NULL;
82 static int sock_perms = S_IRWXU | S_IRWXG;
84 static pthread_t listen_thread = (pthread_t) 0;
86 /* Linked list and auxilliary variables for saving values */
87 static value_cache_t *cache_head = NULL;
88 static pthread_mutex_t cache_lock = PTHREAD_MUTEX_INITIALIZER;
89 static time_t cache_oldest = -1;
94 static value_cache_t *cache_search (const char *name)
98 for (vc = cache_head; vc != NULL; vc = vc->next)
100 if (strcmp (vc->name, name) == 0)
102 } /* for vc = cache_head .. NULL */
105 } /* value_cache_t *cache_search */
107 static int cache_insert (const data_set_t *ds, const value_list_t *vl)
109 /* We're called from `cache_update' so we don't need to lock the mutex */
113 DEBUG ("unixsock plugin: cache_insert: ds->type = %s; ds->ds_num = %i;"
114 " vl->values_len = %i;",
115 ds->type, ds->ds_num, vl->values_len);
117 assert (ds->ds_num == vl->values_len);
119 if (ds->ds_num != vl->values_len)
121 ERROR ("unixsock plugin: ds->type = %s: (ds->ds_num = %i) != "
122 "(vl->values_len = %i)",
123 ds->type, ds->ds_num, vl->values_len);
128 vc = (value_cache_t *) malloc (sizeof (value_cache_t));
132 pthread_mutex_unlock (&cache_lock);
133 ERROR ("unixsock plugin: malloc failed: %s",
134 sstrerror (errno, errbuf, sizeof (errbuf)));
138 vc->gauge = (gauge_t *) malloc (sizeof (gauge_t) * vl->values_len);
139 if (vc->gauge == NULL)
142 pthread_mutex_unlock (&cache_lock);
143 ERROR ("unixsock plugin: malloc failed: %s",
144 sstrerror (errno, errbuf, sizeof (errbuf)));
149 vc->counter = (counter_t *) malloc (sizeof (counter_t) * vl->values_len);
150 if (vc->counter == NULL)
153 pthread_mutex_unlock (&cache_lock);
154 ERROR ("unixsock plugin: malloc failed: %s",
155 sstrerror (errno, errbuf, sizeof (errbuf)));
161 if (FORMAT_VL (vc->name, sizeof (vc->name), vl, ds))
163 pthread_mutex_unlock (&cache_lock);
164 ERROR ("unixsock plugin: FORMAT_VL failed.");
171 for (i = 0; i < ds->ds_num; i++)
173 if (ds->ds[i].type == DS_TYPE_COUNTER)
176 vc->counter[i] = vl->values[i].counter;
178 else if (ds->ds[i].type == DS_TYPE_GAUGE)
180 vc->gauge[i] = vl->values[i].gauge;
189 vc->values_num = ds->ds_num;
192 vc->next = cache_head;
196 if ((vc->time < cache_oldest) || (-1 == cache_oldest))
197 cache_oldest = vc->time;
199 pthread_mutex_unlock (&cache_lock);
201 } /* int cache_insert */
203 static int cache_update (const data_set_t *ds, const value_list_t *vl)
205 char name[4*DATA_MAX_NAME_LEN];;
209 if (FORMAT_VL (name, sizeof (name), vl, ds) != 0)
212 pthread_mutex_lock (&cache_lock);
214 vc = cache_search (name);
216 /* pthread_mutex_lock is called by cache_insert. */
218 return (cache_insert (ds, vl));
220 assert (vc->values_num == ds->ds_num);
221 assert (vc->values_num == vl->values_len);
223 /* Avoid floating-point exceptions due to division by zero. */
224 if (vc->time >= vl->time)
226 pthread_mutex_unlock (&cache_lock);
227 ERROR ("unixsock plugin: vc->time >= vl->time. vc->time = %u; "
228 "vl->time = %u; vl = %s;",
229 (unsigned int) vc->time, (unsigned int) vl->time,
232 } /* if (vc->time >= vl->time) */
235 * Update the values. This is possibly a lot more that you'd expect
236 * because we honor min and max values and handle counter overflows here.
238 for (i = 0; i < ds->ds_num; i++)
240 if (ds->ds[i].type == DS_TYPE_COUNTER)
242 if (vl->values[i].counter < vc->counter[i])
244 if (vl->values[i].counter <= 4294967295U)
246 vc->gauge[i] = ((4294967295U - vl->values[i].counter)
247 + vc->counter[i]) / (vl->time - vc->time);
251 vc->gauge[i] = ((18446744073709551615ULL - vl->values[i].counter)
252 + vc->counter[i]) / (vl->time - vc->time);
257 vc->gauge[i] = (vl->values[i].counter - vc->counter[i])
258 / (vl->time - vc->time);
261 vc->counter[i] = vl->values[i].counter;
263 else if (ds->ds[i].type == DS_TYPE_GAUGE)
265 vc->gauge[i] = vl->values[i].gauge;
274 if (isnan (vc->gauge[i])
275 || (!isnan (ds->ds[i].min) && (vc->gauge[i] < ds->ds[i].min))
276 || (!isnan (ds->ds[i].max) && (vc->gauge[i] > ds->ds[i].max)))
278 } /* for i = 0 .. ds->ds_num */
283 if ((vc->time < cache_oldest) || (-1 == cache_oldest))
284 cache_oldest = vc->time;
286 pthread_mutex_unlock (&cache_lock);
288 } /* int cache_update */
290 static void cache_flush (int max_age)
296 pthread_mutex_lock (&cache_lock);
300 if ((now - cache_oldest) <= max_age)
302 pthread_mutex_unlock (&cache_lock);
313 if ((now - this->time) <= max_age)
315 if (this->time < cache_oldest)
316 cache_oldest = this->time;
324 cache_head = this->next;
326 prev->next = this->next;
329 free (this->counter);
336 } /* while (this != NULL) */
338 pthread_mutex_unlock (&cache_lock);
339 } /* void cache_flush */
341 static int us_open_socket (void)
343 struct sockaddr_un sa;
346 sock_fd = socket (PF_UNIX, SOCK_STREAM, 0);
350 ERROR ("unixsock plugin: socket failed: %s",
351 sstrerror (errno, errbuf, sizeof (errbuf)));
355 memset (&sa, '\0', sizeof (sa));
356 sa.sun_family = AF_UNIX;
357 strncpy (sa.sun_path, (sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
358 sizeof (sa.sun_path) - 1);
359 /* unlink (sa.sun_path); */
361 DEBUG ("unixsock plugin: socket path = %s", sa.sun_path);
363 status = bind (sock_fd, (struct sockaddr *) &sa, sizeof (sa));
367 sstrerror (errno, errbuf, sizeof (errbuf));
368 ERROR ("unixsock plugin: bind failed: %s", errbuf);
374 chmod (sa.sun_path, sock_perms);
376 status = listen (sock_fd, 8);
380 ERROR ("unixsock plugin: listen failed: %s",
381 sstrerror (errno, errbuf, sizeof (errbuf)));
394 grpname = (sock_group != NULL) ? sock_group : COLLECTD_GRP_NAME;
397 status = getgrnam_r (grpname, &sg, grbuf, sizeof (grbuf), &g);
401 WARNING ("unixsock plugin: getgrnam_r (%s) failed: %s", grpname,
402 sstrerror (errno, errbuf, sizeof (errbuf)));
407 WARNING ("unixsock plugin: No such group: `%s'",
412 if (chown ((sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
413 (uid_t) -1, g->gr_gid) != 0)
416 WARNING ("unixsock plugin: chown (%s, -1, %i) failed: %s",
417 (sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
419 sstrerror (errno, errbuf, sizeof (errbuf)));
424 } /* int us_open_socket */
426 static int us_handle_getval (FILE *fh, char **fields, int fields_num)
430 char *plugin_instance;
436 const data_set_t *ds;
443 DEBUG ("unixsock plugin: Wrong number of fields: %i", fields_num);
444 fprintf (fh, "-1 Wrong number of fields: Got %i, expected 2.\n",
449 DEBUG ("unixsock plugin: Got query for `%s'", fields[1]);
451 if (strlen (fields[1]) < strlen ("h/p/t"))
453 fprintf (fh, "-1 Invalied identifier, %s", fields[1]);
458 status = parse_identifier (fields[1], &hostname,
459 &plugin, &plugin_instance,
460 &type, &type_instance);
463 DEBUG ("unixsock plugin: Cannot parse `%s'", fields[1]);
464 fprintf (fh, "-1 Cannot parse identifier.\n");
469 ds = plugin_get_ds (type);
472 DEBUG ("unixsock plugin: plugin_get_ds (%s) == NULL;", type);
473 fprintf (fh, "-1 Type `%s' is unknown.\n", type);
480 status = uc_get_rate_by_name (fields[1], &values, &values_num);
483 fprintf (fh, "-1 No such value");
488 if (ds->ds_num != values_num)
490 ERROR ("ds[%s]->ds_num = %i, "
491 "but uc_get_rate_by_name returned %i values.",
492 ds->type, ds->ds_num, values_num);
493 fprintf (fh, "-1 Error reading value from cache.\n");
499 fprintf (fh, "%u", (unsigned int) values_num);
500 for (i = 0; i < values_num; i++)
502 fprintf (fh, " %s=", ds->ds[i].name);
503 if (isnan (values[i]))
506 fprintf (fh, "%12e", values[i]);
515 } /* int us_handle_getval */
517 static int us_handle_listval (FILE *fh, char **fields, int fields_num)
520 char **value_list = NULL;
521 int value_list_len = 0;
522 value_cache_t *entry;
527 DEBUG ("unixsock plugin: us_handle_listval: "
528 "Wrong number of fields: %i", fields_num);
529 fprintf (fh, "-1 Wrong number of fields: Got %i, expected 1.\n",
535 pthread_mutex_lock (&cache_lock);
537 for (entry = cache_head; entry != NULL; entry = entry->next)
541 snprintf (buffer, sizeof (buffer), "%u %s\n",
542 (unsigned int) entry->time, entry->name);
543 buffer[sizeof (buffer) - 1] = '\0';
545 tmp = realloc (value_list, sizeof (char *) * (value_list_len + 1));
550 value_list[value_list_len] = strdup (buffer);
552 if (value_list[value_list_len] != NULL)
556 pthread_mutex_unlock (&cache_lock);
558 DEBUG ("unixsock plugin: us_handle_listval: value_list_len = %i", value_list_len);
559 fprintf (fh, "%i Values found\n", value_list_len);
560 for (i = 0; i < value_list_len; i++)
561 fputs (value_list[i], fh);
565 } /* int us_handle_listval */
567 static void *us_handle_client (void *arg)
579 DEBUG ("Reading from fd #%i", fd);
581 fh = fdopen (fd, "r+");
585 ERROR ("unixsock plugin: fdopen failed: %s",
586 sstrerror (errno, errbuf, sizeof (errbuf)));
588 pthread_exit ((void *) 1);
591 while (fgets (buffer, sizeof (buffer), fh) != NULL)
595 len = strlen (buffer);
597 && ((buffer[len - 1] == '\n') || (buffer[len - 1] == '\r')))
598 buffer[--len] = '\0';
603 DEBUG ("fgets -> buffer = %s; len = %i;", buffer, len);
605 fields_num = strsplit (buffer, fields,
606 sizeof (fields) / sizeof (fields[0]));
614 if (strcasecmp (fields[0], "getval") == 0)
616 us_handle_getval (fh, fields, fields_num);
618 else if (strcasecmp (fields[0], "putval") == 0)
620 handle_putval (fh, fields, fields_num);
622 else if (strcasecmp (fields[0], "listval") == 0)
624 us_handle_listval (fh, fields, fields_num);
626 else if (strcasecmp (fields[0], "putnotif") == 0)
628 handle_putnotif (fh, fields, fields_num);
632 fprintf (fh, "-1 Unknown command: %s\n", fields[0]);
635 } /* while (fgets) */
640 pthread_exit ((void *) 0);
641 } /* void *us_handle_client */
643 static void *us_server_thread (void *arg)
648 pthread_attr_t th_attr;
650 if (us_open_socket () != 0)
651 pthread_exit ((void *) 1);
655 DEBUG ("unixsock plugin: Calling accept..");
656 status = accept (sock_fd, NULL, NULL);
664 ERROR ("unixsock plugin: accept failed: %s",
665 sstrerror (errno, errbuf, sizeof (errbuf)));
668 pthread_exit ((void *) 1);
671 remote_fd = (int *) malloc (sizeof (int));
672 if (remote_fd == NULL)
675 WARNING ("unixsock plugin: malloc failed: %s",
676 sstrerror (errno, errbuf, sizeof (errbuf)));
682 DEBUG ("Spawning child to handle connection on fd #%i", *remote_fd);
684 pthread_attr_init (&th_attr);
685 pthread_attr_setdetachstate (&th_attr, PTHREAD_CREATE_DETACHED);
687 status = pthread_create (&th, &th_attr, us_handle_client, (void *) remote_fd);
691 WARNING ("unixsock plugin: pthread_create failed: %s",
692 sstrerror (errno, errbuf, sizeof (errbuf)));
702 status = unlink ((sock_file != NULL) ? sock_file : US_DEFAULT_PATH);
706 NOTICE ("unixsock plugin: unlink (%s) failed: %s",
707 (sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
708 sstrerror (errno, errbuf, sizeof (errbuf)));
712 } /* void *us_server_thread */
714 static int us_config (const char *key, const char *val)
716 if (strcasecmp (key, "SocketFile") == 0)
719 sock_file = strdup (val);
721 else if (strcasecmp (key, "SocketGroup") == 0)
724 sock_group = strdup (val);
726 else if (strcasecmp (key, "SocketPerms") == 0)
728 sock_perms = (int) strtol (val, NULL, 8);
736 } /* int us_config */
738 static int us_init (void)
744 status = pthread_create (&listen_thread, NULL, us_server_thread, NULL);
748 ERROR ("unixsock plugin: pthread_create failed: %s",
749 sstrerror (errno, errbuf, sizeof (errbuf)));
756 static int us_shutdown (void)
762 if (listen_thread != (pthread_t) 0)
764 pthread_kill (listen_thread, SIGTERM);
765 pthread_join (listen_thread, &ret);
766 listen_thread = (pthread_t) 0;
769 plugin_unregister_init ("unixsock");
770 plugin_unregister_write ("unixsock");
771 plugin_unregister_shutdown ("unixsock");
774 } /* int us_shutdown */
776 static int us_write (const data_set_t *ds, const value_list_t *vl)
778 cache_update (ds, vl);
779 cache_flush (2 * interval_g);
784 void module_register (void)
786 plugin_register_config ("unixsock", us_config,
787 config_keys, config_keys_num);
788 plugin_register_init ("unixsock", us_init);
789 plugin_register_write ("unixsock", us_write);
790 plugin_register_shutdown ("unixsock", us_shutdown);
791 } /* void module_register (void) */
793 /* vim: set sw=4 ts=4 sts=4 tw=78 : */