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 #include "utils_cmd_putval.h"
28 #include "utils_cmd_putnotif.h"
30 /* Folks without pthread will need to disable this plugin. */
33 #include <sys/socket.h>
40 # define UNIX_PATH_MAX sizeof (((struct sockaddr_un *)0)->sun_path)
43 #define US_DEFAULT_PATH LOCALSTATEDIR"/run/"PACKAGE_NAME"-unixsock"
46 * Private data structures
48 /* linked list of cached values */
49 typedef struct value_cache_s
51 char name[4*DATA_MAX_NAME_LEN];
57 struct value_cache_s *next;
63 /* valid configuration file keys */
64 static const char *config_keys[] =
71 static int config_keys_num = 3;
75 /* socket configuration */
76 static int sock_fd = -1;
77 static char *sock_file = NULL;
78 static char *sock_group = NULL;
79 static int sock_perms = S_IRWXU | S_IRWXG;
81 static pthread_t listen_thread = (pthread_t) 0;
83 /* Linked list and auxilliary variables for saving values */
84 static value_cache_t *cache_head = NULL;
85 static pthread_mutex_t cache_lock = PTHREAD_MUTEX_INITIALIZER;
86 static time_t cache_oldest = -1;
91 static value_cache_t *cache_search (const char *name)
95 for (vc = cache_head; vc != NULL; vc = vc->next)
97 if (strcmp (vc->name, name) == 0)
99 } /* for vc = cache_head .. NULL */
102 } /* value_cache_t *cache_search */
104 static int cache_insert (const data_set_t *ds, const value_list_t *vl)
106 /* We're called from `cache_update' so we don't need to lock the mutex */
110 DEBUG ("unixsock plugin: cache_insert: ds->type = %s; ds->ds_num = %i;"
111 " vl->values_len = %i;",
112 ds->type, ds->ds_num, vl->values_len);
114 assert (ds->ds_num == vl->values_len);
116 if (ds->ds_num != vl->values_len)
118 ERROR ("unixsock plugin: ds->type = %s: (ds->ds_num = %i) != "
119 "(vl->values_len = %i)",
120 ds->type, ds->ds_num, vl->values_len);
125 vc = (value_cache_t *) malloc (sizeof (value_cache_t));
129 pthread_mutex_unlock (&cache_lock);
130 ERROR ("unixsock plugin: malloc failed: %s",
131 sstrerror (errno, errbuf, sizeof (errbuf)));
135 vc->gauge = (gauge_t *) malloc (sizeof (gauge_t) * vl->values_len);
136 if (vc->gauge == NULL)
139 pthread_mutex_unlock (&cache_lock);
140 ERROR ("unixsock plugin: malloc failed: %s",
141 sstrerror (errno, errbuf, sizeof (errbuf)));
146 vc->counter = (counter_t *) malloc (sizeof (counter_t) * vl->values_len);
147 if (vc->counter == NULL)
150 pthread_mutex_unlock (&cache_lock);
151 ERROR ("unixsock plugin: malloc failed: %s",
152 sstrerror (errno, errbuf, sizeof (errbuf)));
158 if (FORMAT_VL (vc->name, sizeof (vc->name), vl, ds))
160 pthread_mutex_unlock (&cache_lock);
161 ERROR ("unixsock plugin: FORMAT_VL failed.");
168 for (i = 0; i < ds->ds_num; i++)
170 if (ds->ds[i].type == DS_TYPE_COUNTER)
173 vc->counter[i] = vl->values[i].counter;
175 else if (ds->ds[i].type == DS_TYPE_GAUGE)
177 vc->gauge[i] = vl->values[i].gauge;
186 vc->values_num = ds->ds_num;
189 vc->next = cache_head;
193 if ((vc->time < cache_oldest) || (-1 == cache_oldest))
194 cache_oldest = vc->time;
196 pthread_mutex_unlock (&cache_lock);
198 } /* int cache_insert */
200 static int cache_update (const data_set_t *ds, const value_list_t *vl)
202 char name[4*DATA_MAX_NAME_LEN];;
206 if (FORMAT_VL (name, sizeof (name), vl, ds) != 0)
209 pthread_mutex_lock (&cache_lock);
211 vc = cache_search (name);
213 /* pthread_mutex_lock is called by cache_insert. */
215 return (cache_insert (ds, vl));
217 assert (vc->values_num == ds->ds_num);
218 assert (vc->values_num == vl->values_len);
220 /* Avoid floating-point exceptions due to division by zero. */
221 if (vc->time >= vl->time)
223 pthread_mutex_unlock (&cache_lock);
224 ERROR ("unixsock plugin: vc->time >= vl->time. vc->time = %u; "
225 "vl->time = %u; vl = %s;",
226 (unsigned int) vc->time, (unsigned int) vl->time,
229 } /* if (vc->time >= vl->time) */
232 * Update the values. This is possibly a lot more that you'd expect
233 * because we honor min and max values and handle counter overflows here.
235 for (i = 0; i < ds->ds_num; i++)
237 if (ds->ds[i].type == DS_TYPE_COUNTER)
239 if (vl->values[i].counter < vc->counter[i])
241 if (vl->values[i].counter <= 4294967295U)
243 vc->gauge[i] = ((4294967295U - vl->values[i].counter)
244 + vc->counter[i]) / (vl->time - vc->time);
248 vc->gauge[i] = ((18446744073709551615ULL - vl->values[i].counter)
249 + vc->counter[i]) / (vl->time - vc->time);
254 vc->gauge[i] = (vl->values[i].counter - vc->counter[i])
255 / (vl->time - vc->time);
258 vc->counter[i] = vl->values[i].counter;
260 else if (ds->ds[i].type == DS_TYPE_GAUGE)
262 vc->gauge[i] = vl->values[i].gauge;
271 if (isnan (vc->gauge[i])
272 || (!isnan (ds->ds[i].min) && (vc->gauge[i] < ds->ds[i].min))
273 || (!isnan (ds->ds[i].max) && (vc->gauge[i] > ds->ds[i].max)))
275 } /* for i = 0 .. ds->ds_num */
280 if ((vc->time < cache_oldest) || (-1 == cache_oldest))
281 cache_oldest = vc->time;
283 pthread_mutex_unlock (&cache_lock);
285 } /* int cache_update */
287 static void cache_flush (int max_age)
293 pthread_mutex_lock (&cache_lock);
297 if ((now - cache_oldest) <= max_age)
299 pthread_mutex_unlock (&cache_lock);
310 if ((now - this->time) <= max_age)
312 if (this->time < cache_oldest)
313 cache_oldest = this->time;
321 cache_head = this->next;
323 prev->next = this->next;
326 free (this->counter);
333 } /* while (this != NULL) */
335 pthread_mutex_unlock (&cache_lock);
336 } /* void cache_flush */
338 static int us_open_socket (void)
340 struct sockaddr_un sa;
343 sock_fd = socket (PF_UNIX, SOCK_STREAM, 0);
347 ERROR ("unixsock plugin: socket failed: %s",
348 sstrerror (errno, errbuf, sizeof (errbuf)));
352 memset (&sa, '\0', sizeof (sa));
353 sa.sun_family = AF_UNIX;
354 strncpy (sa.sun_path, (sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
355 sizeof (sa.sun_path) - 1);
356 /* unlink (sa.sun_path); */
358 DEBUG ("unixsock plugin: socket path = %s", sa.sun_path);
360 status = bind (sock_fd, (struct sockaddr *) &sa, sizeof (sa));
364 sstrerror (errno, errbuf, sizeof (errbuf));
365 ERROR ("unixsock plugin: bind failed: %s", errbuf);
371 chmod (sa.sun_path, sock_perms);
373 status = listen (sock_fd, 8);
377 ERROR ("unixsock plugin: listen failed: %s",
378 sstrerror (errno, errbuf, sizeof (errbuf)));
391 grpname = (sock_group != NULL) ? sock_group : COLLECTD_GRP_NAME;
394 status = getgrnam_r (grpname, &sg, grbuf, sizeof (grbuf), &g);
398 WARNING ("unixsock plugin: getgrnam_r (%s) failed: %s", grpname,
399 sstrerror (errno, errbuf, sizeof (errbuf)));
404 WARNING ("unixsock plugin: No such group: `%s'",
409 if (chown ((sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
410 (uid_t) -1, g->gr_gid) != 0)
413 WARNING ("unixsock plugin: chown (%s, -1, %i) failed: %s",
414 (sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
416 sstrerror (errno, errbuf, sizeof (errbuf)));
421 } /* int us_open_socket */
423 static int us_handle_getval (FILE *fh, char **fields, int fields_num)
427 char *plugin_instance;
430 char name[4*DATA_MAX_NAME_LEN];
437 DEBUG ("unixsock plugin: Wrong number of fields: %i", fields_num);
438 fprintf (fh, "-1 Wrong number of fields: Got %i, expected 2.\n",
443 DEBUG ("unixsock plugin: Got query for `%s'", fields[1]);
445 status = parse_identifier (fields[1], &hostname,
446 &plugin, &plugin_instance,
447 &type, &type_instance);
450 DEBUG ("unixsock plugin: Cannot parse `%s'", fields[1]);
451 fprintf (fh, "-1 Cannot parse identifier.\n");
456 status = format_name (name, sizeof (name),
457 hostname, plugin, plugin_instance, type, type_instance);
460 fprintf (fh, "-1 format_name failed.\n");
464 pthread_mutex_lock (&cache_lock);
466 DEBUG ("vc = cache_search (%s)", name);
467 vc = cache_search (name);
471 DEBUG ("Did not find cache entry.");
472 fprintf (fh, "-1 No such value");
476 DEBUG ("Found cache entry.");
477 fprintf (fh, "%i", vc->values_num);
478 for (i = 0; i < vc->values_num; i++)
480 fprintf (fh, " %s=", vc->ds->ds[i].name);
481 if (isnan (vc->gauge[i]))
484 fprintf (fh, "%12e", vc->gauge[i]);
488 /* Free the mutex as soon as possible and definitely before flushing */
489 pthread_mutex_unlock (&cache_lock);
495 } /* int us_handle_getval */
497 static int us_handle_listval (FILE *fh, char **fields, int fields_num)
500 char **value_list = NULL;
501 int value_list_len = 0;
502 value_cache_t *entry;
507 DEBUG ("unixsock plugin: us_handle_listval: "
508 "Wrong number of fields: %i", fields_num);
509 fprintf (fh, "-1 Wrong number of fields: Got %i, expected 1.\n",
515 pthread_mutex_lock (&cache_lock);
517 for (entry = cache_head; entry != NULL; entry = entry->next)
521 snprintf (buffer, sizeof (buffer), "%u %s\n",
522 (unsigned int) entry->time, entry->name);
523 buffer[sizeof (buffer) - 1] = '\0';
525 tmp = realloc (value_list, sizeof (char *) * (value_list_len + 1));
530 value_list[value_list_len] = strdup (buffer);
532 if (value_list[value_list_len] != NULL)
536 pthread_mutex_unlock (&cache_lock);
538 DEBUG ("unixsock plugin: us_handle_listval: value_list_len = %i", value_list_len);
539 fprintf (fh, "%i Values found\n", value_list_len);
540 for (i = 0; i < value_list_len; i++)
541 fputs (value_list[i], fh);
545 } /* int us_handle_listval */
547 static void *us_handle_client (void *arg)
559 DEBUG ("Reading from fd #%i", fd);
561 fhin = fdopen (fd, "r");
565 ERROR ("unixsock plugin: fdopen failed: %s",
566 sstrerror (errno, errbuf, sizeof (errbuf)));
568 pthread_exit ((void *) 1);
571 fhout = fdopen (fd, "w");
575 ERROR ("unixsock plugin: fdopen failed: %s",
576 sstrerror (errno, errbuf, sizeof (errbuf)));
577 fclose (fhin); /* this closes fd as well */
578 pthread_exit ((void *) 1);
581 while (fgets (buffer, sizeof (buffer), fhin) != NULL)
585 len = strlen (buffer);
587 && ((buffer[len - 1] == '\n') || (buffer[len - 1] == '\r')))
588 buffer[--len] = '\0';
593 DEBUG ("fgets -> buffer = %s; len = %i;", buffer, len);
595 fields_num = strsplit (buffer, fields,
596 sizeof (fields) / sizeof (fields[0]));
604 if (strcasecmp (fields[0], "getval") == 0)
606 us_handle_getval (fhout, fields, fields_num);
608 else if (strcasecmp (fields[0], "putval") == 0)
610 handle_putval (fhout, fields, fields_num);
612 else if (strcasecmp (fields[0], "listval") == 0)
614 us_handle_listval (fhout, fields, fields_num);
616 else if (strcasecmp (fields[0], "putnotif") == 0)
618 handle_putnotif (fhout, fields, fields_num);
622 fprintf (fhout, "-1 Unknown command: %s\n", fields[0]);
625 } /* while (fgets) */
631 pthread_exit ((void *) 0);
633 } /* void *us_handle_client */
635 static void *us_server_thread (void *arg)
640 pthread_attr_t th_attr;
642 if (us_open_socket () != 0)
643 pthread_exit ((void *) 1);
647 DEBUG ("unixsock plugin: Calling accept..");
648 status = accept (sock_fd, NULL, NULL);
656 ERROR ("unixsock plugin: accept failed: %s",
657 sstrerror (errno, errbuf, sizeof (errbuf)));
660 pthread_exit ((void *) 1);
663 remote_fd = (int *) malloc (sizeof (int));
664 if (remote_fd == NULL)
667 WARNING ("unixsock plugin: malloc failed: %s",
668 sstrerror (errno, errbuf, sizeof (errbuf)));
674 DEBUG ("Spawning child to handle connection on fd #%i", *remote_fd);
676 pthread_attr_init (&th_attr);
677 pthread_attr_setdetachstate (&th_attr, PTHREAD_CREATE_DETACHED);
679 status = pthread_create (&th, &th_attr, us_handle_client, (void *) remote_fd);
683 WARNING ("unixsock plugin: pthread_create failed: %s",
684 sstrerror (errno, errbuf, sizeof (errbuf)));
694 status = unlink ((sock_file != NULL) ? sock_file : US_DEFAULT_PATH);
698 NOTICE ("unixsock plugin: unlink (%s) failed: %s",
699 (sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
700 sstrerror (errno, errbuf, sizeof (errbuf)));
704 } /* void *us_server_thread */
706 static int us_config (const char *key, const char *val)
708 if (strcasecmp (key, "SocketFile") == 0)
711 sock_file = strdup (val);
713 else if (strcasecmp (key, "SocketGroup") == 0)
716 sock_group = strdup (val);
718 else if (strcasecmp (key, "SocketPerms") == 0)
720 sock_perms = (int) strtol (val, NULL, 8);
728 } /* int us_config */
730 static int us_init (void)
736 status = pthread_create (&listen_thread, NULL, us_server_thread, NULL);
740 ERROR ("unixsock plugin: pthread_create failed: %s",
741 sstrerror (errno, errbuf, sizeof (errbuf)));
748 static int us_shutdown (void)
754 if (listen_thread != (pthread_t) 0)
756 pthread_kill (listen_thread, SIGTERM);
757 pthread_join (listen_thread, &ret);
758 listen_thread = (pthread_t) 0;
761 plugin_unregister_init ("unixsock");
762 plugin_unregister_shutdown ("unixsock");
765 } /* int us_shutdown */
767 static int us_write (const data_set_t *ds, const value_list_t *vl)
769 cache_update (ds, vl);
770 cache_flush (2 * interval_g);
775 void module_register (void)
777 plugin_register_config ("unixsock", us_config,
778 config_keys, config_keys_num);
779 plugin_register_init ("unixsock", us_init);
780 plugin_register_write ("unixsock", us_write);
781 plugin_register_shutdown ("unixsock", us_shutdown);
782 } /* void module_register (void) */
784 /* vim: set sw=4 ts=4 sts=4 tw=78 : */