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>
38 # define UNIX_PATH_MAX sizeof (((struct sockaddr_un *)0)->sun_path)
41 #define US_DEFAULT_PATH LOCALSTATEDIR"/run/"PACKAGE_NAME"-unixsock"
44 * Private data structures
46 /* linked list of cached values */
47 typedef struct value_cache_s
49 char name[4*DATA_MAX_NAME_LEN];
55 struct value_cache_s *next;
61 /* valid configuration file keys */
62 static const char *config_keys[] =
69 static int config_keys_num = 3;
73 /* socket configuration */
74 static int sock_fd = -1;
75 static char *sock_file = NULL;
76 static char *sock_group = NULL;
77 static int sock_perms = S_IRWXU | S_IRWXG;
79 static pthread_t listen_thread = (pthread_t) 0;
81 /* Linked list and auxilliary variables for saving values */
82 static value_cache_t *cache_head = NULL;
83 static pthread_mutex_t cache_lock = PTHREAD_MUTEX_INITIALIZER;
84 static unsigned int cache_oldest = UINT_MAX;
89 static value_cache_t *cache_search (const char *name)
93 for (vc = cache_head; vc != NULL; vc = vc->next)
95 if (strcmp (vc->name, name) == 0)
97 } /* for vc = cache_head .. NULL */
100 } /* value_cache_t *cache_search */
102 static int cache_insert (const data_set_t *ds, const value_list_t *vl)
104 /* We're called from `cache_update' so we don't need to lock the mutex */
108 DEBUG ("unixsock plugin: cache_insert: ds->type = %s; ds->ds_num = %i;"
109 " vl->values_len = %i;",
110 ds->type, ds->ds_num, vl->values_len);
112 assert (ds->ds_num == vl->values_len);
114 if (ds->ds_num != vl->values_len)
116 ERROR ("unixsock plugin: ds->type = %s: (ds->ds_num = %i) != "
117 "(vl->values_len = %i)",
118 ds->type, ds->ds_num, vl->values_len);
123 vc = (value_cache_t *) malloc (sizeof (value_cache_t));
127 pthread_mutex_unlock (&cache_lock);
128 ERROR ("unixsock plugin: malloc failed: %s",
129 sstrerror (errno, errbuf, sizeof (errbuf)));
133 vc->gauge = (gauge_t *) malloc (sizeof (gauge_t) * vl->values_len);
134 if (vc->gauge == NULL)
137 pthread_mutex_unlock (&cache_lock);
138 ERROR ("unixsock plugin: malloc failed: %s",
139 sstrerror (errno, errbuf, sizeof (errbuf)));
144 vc->counter = (counter_t *) malloc (sizeof (counter_t) * vl->values_len);
145 if (vc->counter == NULL)
148 pthread_mutex_unlock (&cache_lock);
149 ERROR ("unixsock plugin: malloc failed: %s",
150 sstrerror (errno, errbuf, sizeof (errbuf)));
156 if (FORMAT_VL (vc->name, sizeof (vc->name), vl, ds))
158 pthread_mutex_unlock (&cache_lock);
159 ERROR ("unixsock plugin: FORMAT_VL failed.");
166 for (i = 0; i < ds->ds_num; i++)
168 if (ds->ds[i].type == DS_TYPE_COUNTER)
171 vc->counter[i] = vl->values[i].counter;
173 else if (ds->ds[i].type == DS_TYPE_GAUGE)
175 vc->gauge[i] = vl->values[i].gauge;
184 vc->values_num = ds->ds_num;
187 vc->next = cache_head;
191 if (vc->time < cache_oldest)
192 cache_oldest = vc->time;
194 pthread_mutex_unlock (&cache_lock);
196 } /* int cache_insert */
198 static int cache_update (const data_set_t *ds, const value_list_t *vl)
200 char name[4*DATA_MAX_NAME_LEN];;
204 if (FORMAT_VL (name, sizeof (name), vl, ds) != 0)
207 pthread_mutex_lock (&cache_lock);
209 vc = cache_search (name);
211 /* pthread_mutex_lock is called by cache_insert. */
213 return (cache_insert (ds, vl));
215 assert (vc->values_num == ds->ds_num);
216 assert (vc->values_num == vl->values_len);
218 /* Avoid floating-point exceptions due to division by zero. */
219 if (vc->time >= vl->time)
221 pthread_mutex_unlock (&cache_lock);
222 ERROR ("unixsock plugin: vc->time >= vl->time. vc->time = %u; "
223 "vl->time = %u; vl = %s;",
224 (unsigned int) vc->time, (unsigned int) vl->time,
227 } /* if (vc->time >= vl->time) */
230 * Update the values. This is possibly a lot more that you'd expect
231 * because we honor min and max values and handle counter overflows here.
233 for (i = 0; i < ds->ds_num; i++)
235 if (ds->ds[i].type == DS_TYPE_COUNTER)
237 if (vl->values[i].counter < vc->counter[i])
239 if (vl->values[i].counter <= 4294967295U)
241 vc->gauge[i] = ((4294967295U - vl->values[i].counter)
242 + vc->counter[i]) / (vl->time - vc->time);
246 vc->gauge[i] = ((18446744073709551615ULL - vl->values[i].counter)
247 + vc->counter[i]) / (vl->time - vc->time);
252 vc->gauge[i] = (vl->values[i].counter - vc->counter[i])
253 / (vl->time - vc->time);
256 vc->counter[i] = vl->values[i].counter;
258 else if (ds->ds[i].type == DS_TYPE_GAUGE)
260 vc->gauge[i] = vl->values[i].gauge;
269 if (isnan (vc->gauge[i])
270 || (!isnan (ds->ds[i].min) && (vc->gauge[i] < ds->ds[i].min))
271 || (!isnan (ds->ds[i].max) && (vc->gauge[i] > ds->ds[i].max)))
273 } /* for i = 0 .. ds->ds_num */
278 if (vc->time < cache_oldest)
279 cache_oldest = vc->time;
281 pthread_mutex_unlock (&cache_lock);
283 } /* int cache_update */
285 static void cache_flush (int max_age)
291 pthread_mutex_lock (&cache_lock);
295 if ((now - cache_oldest) <= max_age)
297 pthread_mutex_unlock (&cache_lock);
308 if ((now - this->time) <= max_age)
310 if (this->time < cache_oldest)
311 cache_oldest = this->time;
319 cache_head = this->next;
321 prev->next = this->next;
324 free (this->counter);
331 } /* while (this != NULL) */
333 pthread_mutex_unlock (&cache_lock);
334 } /* void cache_flush */
336 static int us_open_socket (void)
338 struct sockaddr_un sa;
341 sock_fd = socket (PF_UNIX, SOCK_STREAM, 0);
345 ERROR ("unixsock plugin: socket failed: %s",
346 sstrerror (errno, errbuf, sizeof (errbuf)));
350 memset (&sa, '\0', sizeof (sa));
351 sa.sun_family = AF_UNIX;
352 strncpy (sa.sun_path, (sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
353 sizeof (sa.sun_path) - 1);
354 /* unlink (sa.sun_path); */
356 DEBUG ("unixsock plugin: socket path = %s", sa.sun_path);
358 status = bind (sock_fd, (struct sockaddr *) &sa, sizeof (sa));
362 sstrerror (errno, errbuf, sizeof (errbuf));
363 ERROR ("unixsock plugin: bind failed: %s", errbuf);
369 chmod (sa.sun_path, sock_perms);
371 status = listen (sock_fd, 8);
375 ERROR ("unixsock plugin: listen failed: %s",
376 sstrerror (errno, errbuf, sizeof (errbuf)));
389 grpname = (sock_group != NULL) ? sock_group : COLLECTD_GRP_NAME;
392 status = getgrnam_r (grpname, &sg, grbuf, sizeof (grbuf), &g);
396 WARNING ("unixsock plugin: getgrnam_r (%s) failed: %s", grpname,
397 sstrerror (errno, errbuf, sizeof (errbuf)));
402 WARNING ("unixsock plugin: No such group: `%s'",
407 if (chown ((sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
408 (uid_t) -1, g->gr_gid) != 0)
411 WARNING ("unixsock plugin: chown (%s, -1, %i) failed: %s",
412 (sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
414 sstrerror (errno, errbuf, sizeof (errbuf)));
419 } /* int us_open_socket */
421 static int us_handle_getval (FILE *fh, char **fields, int fields_num)
425 char *plugin_instance;
428 char name[4*DATA_MAX_NAME_LEN];
435 DEBUG ("unixsock plugin: Wrong number of fields: %i", fields_num);
436 fprintf (fh, "-1 Wrong number of fields: Got %i, expected 2.\n",
441 DEBUG ("unixsock plugin: Got query for `%s'", fields[1]);
443 status = parse_identifier (fields[1], &hostname,
444 &plugin, &plugin_instance,
445 &type, &type_instance);
448 DEBUG ("unixsock plugin: Cannot parse `%s'", fields[1]);
449 fprintf (fh, "-1 Cannot parse identifier.\n");
454 status = format_name (name, sizeof (name),
455 hostname, plugin, plugin_instance, type, type_instance);
458 fprintf (fh, "-1 format_name failed.\n");
462 pthread_mutex_lock (&cache_lock);
464 DEBUG ("vc = cache_search (%s)", name);
465 vc = cache_search (name);
469 DEBUG ("Did not find cache entry.");
470 fprintf (fh, "-1 No such value");
474 DEBUG ("Found cache entry.");
475 fprintf (fh, "%i", vc->values_num);
476 for (i = 0; i < vc->values_num; i++)
478 fprintf (fh, " %s=", vc->ds->ds[i].name);
479 if (isnan (vc->gauge[i]))
482 fprintf (fh, "%12e", vc->gauge[i]);
486 /* Free the mutex as soon as possible and definitely before flushing */
487 pthread_mutex_unlock (&cache_lock);
493 } /* int us_handle_getval */
495 static int us_handle_listval (FILE *fh, char **fields, int fields_num)
498 char **value_list = NULL;
499 int value_list_len = 0;
500 value_cache_t *entry;
505 DEBUG ("unixsock plugin: us_handle_listval: "
506 "Wrong number of fields: %i", fields_num);
507 fprintf (fh, "-1 Wrong number of fields: Got %i, expected 1.\n",
513 pthread_mutex_lock (&cache_lock);
515 for (entry = cache_head; entry != NULL; entry = entry->next)
519 snprintf (buffer, sizeof (buffer), "%u %s\n",
520 (unsigned int) entry->time, entry->name);
521 buffer[sizeof (buffer) - 1] = '\0';
523 tmp = realloc (value_list, sizeof (char *) * (value_list_len + 1));
528 value_list[value_list_len] = strdup (buffer);
530 if (value_list[value_list_len] != NULL)
534 pthread_mutex_unlock (&cache_lock);
536 DEBUG ("unixsock plugin: us_handle_listval: value_list_len = %i", value_list_len);
537 fprintf (fh, "%i Values found\n", value_list_len);
538 for (i = 0; i < value_list_len; i++)
539 fputs (value_list[i], fh);
543 } /* int us_handle_listval */
545 static void *us_handle_client (void *arg)
557 DEBUG ("Reading from fd #%i", fd);
559 fh = fdopen (fd, "r+");
563 ERROR ("unixsock plugin: fdopen failed: %s",
564 sstrerror (errno, errbuf, sizeof (errbuf)));
566 pthread_exit ((void *) 1);
569 while (fgets (buffer, sizeof (buffer), fh) != NULL)
573 len = strlen (buffer);
575 && ((buffer[len - 1] == '\n') || (buffer[len - 1] == '\r')))
576 buffer[--len] = '\0';
581 DEBUG ("fgets -> buffer = %s; len = %i;", buffer, len);
583 fields_num = strsplit (buffer, fields,
584 sizeof (fields) / sizeof (fields[0]));
592 if (strcasecmp (fields[0], "getval") == 0)
594 us_handle_getval (fh, fields, fields_num);
596 else if (strcasecmp (fields[0], "putval") == 0)
598 handle_putval (fh, fields, fields_num);
600 else if (strcasecmp (fields[0], "listval") == 0)
602 us_handle_listval (fh, fields, fields_num);
606 fprintf (fh, "-1 Unknown command: %s\n", fields[0]);
609 } /* while (fgets) */
614 pthread_exit ((void *) 0);
615 } /* void *us_handle_client */
617 static void *us_server_thread (void *arg)
622 pthread_attr_t th_attr;
624 if (us_open_socket () != 0)
625 pthread_exit ((void *) 1);
629 DEBUG ("unixsock plugin: Calling accept..");
630 status = accept (sock_fd, NULL, NULL);
638 ERROR ("unixsock plugin: accept failed: %s",
639 sstrerror (errno, errbuf, sizeof (errbuf)));
642 pthread_exit ((void *) 1);
645 remote_fd = (int *) malloc (sizeof (int));
646 if (remote_fd == NULL)
649 WARNING ("unixsock plugin: malloc failed: %s",
650 sstrerror (errno, errbuf, sizeof (errbuf)));
656 DEBUG ("Spawning child to handle connection on fd #%i", *remote_fd);
658 pthread_attr_init (&th_attr);
659 pthread_attr_setdetachstate (&th_attr, PTHREAD_CREATE_DETACHED);
661 status = pthread_create (&th, &th_attr, us_handle_client, (void *) remote_fd);
665 WARNING ("unixsock plugin: pthread_create failed: %s",
666 sstrerror (errno, errbuf, sizeof (errbuf)));
676 status = unlink ((sock_file != NULL) ? sock_file : US_DEFAULT_PATH);
680 NOTICE ("unixsock plugin: unlink (%s) failed: %s",
681 (sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
682 sstrerror (errno, errbuf, sizeof (errbuf)));
686 } /* void *us_server_thread */
688 static int us_config (const char *key, const char *val)
690 if (strcasecmp (key, "SocketFile") == 0)
693 sock_file = strdup (val);
695 else if (strcasecmp (key, "SocketGroup") == 0)
698 sock_group = strdup (val);
700 else if (strcasecmp (key, "SocketPerms") == 0)
702 sock_perms = (int) strtol (val, NULL, 8);
710 } /* int us_config */
712 static int us_init (void)
718 status = pthread_create (&listen_thread, NULL, us_server_thread, NULL);
722 ERROR ("unixsock plugin: pthread_create failed: %s",
723 sstrerror (errno, errbuf, sizeof (errbuf)));
730 static int us_shutdown (void)
736 if (listen_thread != (pthread_t) 0)
738 pthread_kill (listen_thread, SIGTERM);
739 pthread_join (listen_thread, &ret);
740 listen_thread = (pthread_t) 0;
743 plugin_unregister_init ("unixsock");
744 plugin_unregister_write ("unixsock");
745 plugin_unregister_shutdown ("unixsock");
748 } /* int us_shutdown */
750 static int us_write (const data_set_t *ds, const value_list_t *vl)
752 cache_update (ds, vl);
753 cache_flush (2 * interval_g);
758 void module_register (void)
760 plugin_register_config ("unixsock", us_config,
761 config_keys, config_keys_num);
762 plugin_register_init ("unixsock", us_init);
763 plugin_register_write ("unixsock", us_write);
764 plugin_register_shutdown ("unixsock", us_shutdown);
765 } /* void module_register (void) */
767 /* vim: set sw=4 ts=4 sts=4 tw=78 : */