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"
26 #include "utils_cmd_putval.h"
28 /* Folks without pthread will need to disable this plugin. */
31 #include <sys/socket.h>
37 # define UNIX_PATH_MAX sizeof (((struct sockaddr_un *)0)->sun_path)
40 #define US_DEFAULT_PATH LOCALSTATEDIR"/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 value_cache_t *cache_search (const char *name)
92 for (vc = cache_head; vc != NULL; vc = vc->next)
94 if (strcmp (vc->name, name) == 0)
96 } /* for vc = cache_head .. NULL */
99 } /* value_cache_t *cache_search */
101 static int cache_insert (const data_set_t *ds, const value_list_t *vl)
103 /* We're called from `cache_update' so we don't need to lock the mutex */
107 DEBUG ("unixsock plugin: cache_insert: ds->type = %s; ds->ds_num = %i;"
108 " vl->values_len = %i;",
109 ds->type, ds->ds_num, vl->values_len);
111 assert (ds->ds_num == vl->values_len);
113 if (ds->ds_num != vl->values_len)
115 ERROR ("unixsock plugin: ds->type = %s: (ds->ds_num = %i) != "
116 "(vl->values_len = %i)",
117 ds->type, ds->ds_num, vl->values_len);
122 vc = (value_cache_t *) malloc (sizeof (value_cache_t));
126 pthread_mutex_unlock (&cache_lock);
127 ERROR ("unixsock plugin: malloc failed: %s",
128 sstrerror (errno, errbuf, sizeof (errbuf)));
132 vc->gauge = (gauge_t *) malloc (sizeof (gauge_t) * vl->values_len);
133 if (vc->gauge == NULL)
136 pthread_mutex_unlock (&cache_lock);
137 ERROR ("unixsock plugin: malloc failed: %s",
138 sstrerror (errno, errbuf, sizeof (errbuf)));
143 vc->counter = (counter_t *) malloc (sizeof (counter_t) * vl->values_len);
144 if (vc->counter == NULL)
147 pthread_mutex_unlock (&cache_lock);
148 ERROR ("unixsock plugin: malloc failed: %s",
149 sstrerror (errno, errbuf, sizeof (errbuf)));
155 if (FORMAT_VL (vc->name, sizeof (vc->name), vl, ds))
157 pthread_mutex_unlock (&cache_lock);
158 ERROR ("unixsock plugin: FORMAT_VL failed.");
165 for (i = 0; i < ds->ds_num; i++)
167 if (ds->ds[i].type == DS_TYPE_COUNTER)
170 vc->counter[i] = vl->values[i].counter;
172 else if (ds->ds[i].type == DS_TYPE_GAUGE)
174 vc->gauge[i] = vl->values[i].gauge;
183 vc->values_num = ds->ds_num;
186 vc->next = cache_head;
190 if (vc->time < cache_oldest)
191 cache_oldest = vc->time;
193 pthread_mutex_unlock (&cache_lock);
195 } /* int cache_insert */
197 static int cache_update (const data_set_t *ds, const value_list_t *vl)
199 char name[4*DATA_MAX_NAME_LEN];;
203 if (FORMAT_VL (name, sizeof (name), vl, ds) != 0)
206 pthread_mutex_lock (&cache_lock);
208 vc = cache_search (name);
210 /* pthread_mutex_lock is called by cache_insert. */
212 return (cache_insert (ds, vl));
214 assert (vc->values_num == ds->ds_num);
215 assert (vc->values_num == vl->values_len);
217 /* Avoid floating-point exceptions due to division by zero. */
218 if (vc->time >= vl->time)
220 pthread_mutex_unlock (&cache_lock);
221 ERROR ("unixsock plugin: vc->time >= vl->time. vc->time = %u; "
222 "vl->time = %u; vl = %s;",
223 (unsigned int) vc->time, (unsigned int) vl->time,
226 } /* if (vc->time >= vl->time) */
229 * Update the values. This is possibly a lot more that you'd expect
230 * because we honor min and max values and handle counter overflows here.
232 for (i = 0; i < ds->ds_num; i++)
234 if (ds->ds[i].type == DS_TYPE_COUNTER)
236 if (vl->values[i].counter < vc->counter[i])
238 if (vl->values[i].counter <= 4294967295U)
240 vc->gauge[i] = ((4294967295U - vl->values[i].counter)
241 + vc->counter[i]) / (vl->time - vc->time);
245 vc->gauge[i] = ((18446744073709551615ULL - vl->values[i].counter)
246 + vc->counter[i]) / (vl->time - vc->time);
251 vc->gauge[i] = (vl->values[i].counter - vc->counter[i])
252 / (vl->time - vc->time);
255 vc->counter[i] = vl->values[i].counter;
257 else if (ds->ds[i].type == DS_TYPE_GAUGE)
259 vc->gauge[i] = vl->values[i].gauge;
268 if (isnan (vc->gauge[i])
269 || (!isnan (ds->ds[i].min) && (vc->gauge[i] < ds->ds[i].min))
270 || (!isnan (ds->ds[i].max) && (vc->gauge[i] > ds->ds[i].max)))
272 } /* for i = 0 .. ds->ds_num */
277 if (vc->time < cache_oldest)
278 cache_oldest = vc->time;
280 pthread_mutex_unlock (&cache_lock);
282 } /* int cache_update */
284 static void cache_flush (int max_age)
290 pthread_mutex_lock (&cache_lock);
294 if ((now - cache_oldest) <= max_age)
296 pthread_mutex_unlock (&cache_lock);
307 if ((now - this->time) <= max_age)
309 if (this->time < cache_oldest)
310 cache_oldest = this->time;
318 cache_head = this->next;
320 prev->next = this->next;
323 free (this->counter);
330 } /* while (this != NULL) */
332 pthread_mutex_unlock (&cache_lock);
333 } /* void cache_flush */
335 static int us_open_socket (void)
337 struct sockaddr_un sa;
340 sock_fd = socket (PF_UNIX, SOCK_STREAM, 0);
344 ERROR ("unixsock plugin: socket failed: %s",
345 sstrerror (errno, errbuf, sizeof (errbuf)));
349 memset (&sa, '\0', sizeof (sa));
350 sa.sun_family = AF_UNIX;
351 strncpy (sa.sun_path, (sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
352 sizeof (sa.sun_path) - 1);
353 /* unlink (sa.sun_path); */
355 status = bind (sock_fd, (struct sockaddr *) &sa, sizeof (sa));
359 sstrerror (errno, errbuf, sizeof (errbuf));
360 DEBUG ("bind failed: %s; sa.sun_path = %s", errbuf, sa.sun_path);
361 ERROR ("unixsock plugin: bind failed: %s", errbuf);
367 status = listen (sock_fd, 8);
371 ERROR ("unixsock plugin: listen failed: %s",
372 sstrerror (errno, errbuf, sizeof (errbuf)));
385 grpname = (sock_group != NULL) ? sock_group : COLLECTD_GRP_NAME;
388 status = getgrnam_r (grpname, &sg, grbuf, sizeof (grbuf), &g);
392 WARNING ("unixsock plugin: getgrnam_r (%s) failed: %s", grpname,
393 sstrerror (errno, errbuf, sizeof (errbuf)));
398 WARNING ("unixsock plugin: No such group: `%s'",
403 if (chown ((sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
404 (uid_t) -1, g->gr_gid) != 0)
407 WARNING ("unixsock plugin: chown (%s, -1, %i) failed: %s",
408 (sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
410 sstrerror (errno, errbuf, sizeof (errbuf)));
415 } /* int us_open_socket */
417 static int us_handle_getval (FILE *fh, char **fields, int fields_num)
421 char *plugin_instance;
424 char name[4*DATA_MAX_NAME_LEN];
431 DEBUG ("unixsock plugin: Wrong number of fields: %i", fields_num);
432 fprintf (fh, "-1 Wrong number of fields: Got %i, expected 2.\n",
437 DEBUG ("unixsock plugin: Got query for `%s'", fields[1]);
439 status = parse_identifier (fields[1], &hostname,
440 &plugin, &plugin_instance,
441 &type, &type_instance);
444 DEBUG ("unixsock plugin: Cannot parse `%s'", fields[1]);
445 fprintf (fh, "-1 Cannot parse identifier.\n");
450 status = format_name (name, sizeof (name),
451 hostname, plugin, plugin_instance, type, type_instance);
454 fprintf (fh, "-1 format_name failed.\n");
458 pthread_mutex_lock (&cache_lock);
460 DEBUG ("vc = cache_search (%s)", name);
461 vc = cache_search (name);
465 DEBUG ("Did not find cache entry.");
466 fprintf (fh, "-1 No such value");
470 DEBUG ("Found cache entry.");
471 fprintf (fh, "%i", vc->values_num);
472 for (i = 0; i < vc->values_num; i++)
474 fprintf (fh, " %s=", vc->ds->ds[i].name);
475 if (isnan (vc->gauge[i]))
478 fprintf (fh, "%12e", vc->gauge[i]);
482 /* Free the mutex as soon as possible and definitely before flushing */
483 pthread_mutex_unlock (&cache_lock);
489 } /* int us_handle_getval */
491 static int us_handle_listval (FILE *fh, char **fields, int fields_num)
494 char **value_list = NULL;
495 int value_list_len = 0;
496 value_cache_t *entry;
501 DEBUG ("unixsock plugin: us_handle_listval: "
502 "Wrong number of fields: %i", fields_num);
503 fprintf (fh, "-1 Wrong number of fields: Got %i, expected 1.\n",
509 pthread_mutex_lock (&cache_lock);
511 for (entry = cache_head; entry != NULL; entry = entry->next)
515 snprintf (buffer, sizeof (buffer), "%u %s\n",
516 (unsigned int) entry->time, entry->name);
517 buffer[sizeof (buffer) - 1] = '\0';
519 tmp = realloc (value_list, sizeof (char *) * (value_list_len + 1));
524 value_list[value_list_len] = strdup (buffer);
526 if (value_list[value_list_len] != NULL)
530 pthread_mutex_unlock (&cache_lock);
532 DEBUG ("unixsock plugin: us_handle_listval: value_list_len = %i", value_list_len);
533 fprintf (fh, "%i Values found\n", value_list_len);
534 for (i = 0; i < value_list_len; i++)
535 fputs (value_list[i], fh);
539 } /* int us_handle_listval */
541 static void *us_handle_client (void *arg)
553 DEBUG ("Reading from fd #%i", fd);
555 fh = fdopen (fd, "r+");
559 ERROR ("unixsock plugin: fdopen failed: %s",
560 sstrerror (errno, errbuf, sizeof (errbuf)));
562 pthread_exit ((void *) 1);
565 while (fgets (buffer, sizeof (buffer), fh) != NULL)
569 len = strlen (buffer);
571 && ((buffer[len - 1] == '\n') || (buffer[len - 1] == '\r')))
572 buffer[--len] = '\0';
577 DEBUG ("fgets -> buffer = %s; len = %i;", buffer, len);
579 fields_num = strsplit (buffer, fields,
580 sizeof (fields) / sizeof (fields[0]));
588 if (strcasecmp (fields[0], "getval") == 0)
590 us_handle_getval (fh, fields, fields_num);
592 else if (strcasecmp (fields[0], "putval") == 0)
594 handle_putval (fh, fields, fields_num);
596 else if (strcasecmp (fields[0], "listval") == 0)
598 us_handle_listval (fh, fields, fields_num);
602 fprintf (fh, "-1 Unknown command: %s\n", fields[0]);
605 } /* while (fgets) */
610 pthread_exit ((void *) 0);
611 } /* void *us_handle_client */
613 static void *us_server_thread (void *arg)
618 pthread_attr_t th_attr;
620 if (us_open_socket () != 0)
621 pthread_exit ((void *) 1);
625 DEBUG ("Calling accept..");
626 status = accept (sock_fd, NULL, NULL);
634 ERROR ("unixsock plugin: accept failed: %s",
635 sstrerror (errno, errbuf, sizeof (errbuf)));
638 pthread_exit ((void *) 1);
641 remote_fd = (int *) malloc (sizeof (int));
642 if (remote_fd == NULL)
645 WARNING ("unixsock plugin: malloc failed: %s",
646 sstrerror (errno, errbuf, sizeof (errbuf)));
652 DEBUG ("Spawning child to handle connection on fd #%i", *remote_fd);
654 pthread_attr_init (&th_attr);
655 pthread_attr_setdetachstate (&th_attr, PTHREAD_CREATE_DETACHED);
657 status = pthread_create (&th, &th_attr, us_handle_client, (void *) remote_fd);
661 WARNING ("unixsock plugin: pthread_create failed: %s",
662 sstrerror (errno, errbuf, sizeof (errbuf)));
672 status = unlink ((sock_file != NULL) ? sock_file : US_DEFAULT_PATH);
676 NOTICE ("unixsock plugin: unlink (%s) failed: %s",
677 (sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
678 sstrerror (errno, errbuf, sizeof (errbuf)));
682 } /* void *us_server_thread */
684 static int us_config (const char *key, const char *val)
686 if (strcasecmp (key, "SocketFile") == 0)
689 sock_file = strdup (val);
691 else if (strcasecmp (key, "SocketGroup") == 0)
694 sock_group = strdup (val);
696 else if (strcasecmp (key, "SocketPerms") == 0)
698 sock_perms = (int) strtol (val, NULL, 8);
706 } /* int us_config */
708 static int us_init (void)
714 status = pthread_create (&listen_thread, NULL, us_server_thread, NULL);
718 ERROR ("unixsock plugin: pthread_create failed: %s",
719 sstrerror (errno, errbuf, sizeof (errbuf)));
726 static int us_shutdown (void)
732 if (listen_thread != (pthread_t) 0)
734 pthread_kill (listen_thread, SIGTERM);
735 pthread_join (listen_thread, &ret);
736 listen_thread = (pthread_t) 0;
739 plugin_unregister_init ("unixsock");
740 plugin_unregister_write ("unixsock");
741 plugin_unregister_shutdown ("unixsock");
744 } /* int us_shutdown */
746 static int us_write (const data_set_t *ds, const value_list_t *vl)
748 cache_update (ds, vl);
749 cache_flush (2 * interval_g);
754 void module_register (void)
756 plugin_register_config ("unixsock", us_config,
757 config_keys, config_keys_num);
758 plugin_register_init ("unixsock", us_init);
759 plugin_register_write ("unixsock", us_write);
760 plugin_register_shutdown ("unixsock", us_shutdown);
761 } /* void module_register (void) */
763 /* vim: set sw=4 ts=4 sts=4 tw=78 : */