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_getval.h"
28 #include "utils_cmd_putval.h"
29 #include "utils_cmd_putnotif.h"
31 /* Folks without pthread will need to disable this plugin. */
34 #include <sys/socket.h>
41 # define UNIX_PATH_MAX sizeof (((struct sockaddr_un *)0)->sun_path)
44 #define US_DEFAULT_PATH LOCALSTATEDIR"/run/"PACKAGE_NAME"-unixsock"
47 * Private data structures
49 /* linked list of cached values */
50 typedef struct value_cache_s
52 char name[4*DATA_MAX_NAME_LEN];
58 struct value_cache_s *next;
64 /* valid configuration file keys */
65 static const char *config_keys[] =
72 static int config_keys_num = 3;
76 /* socket configuration */
77 static int sock_fd = -1;
78 static char *sock_file = NULL;
79 static char *sock_group = NULL;
80 static int sock_perms = S_IRWXU | S_IRWXG;
82 static pthread_t listen_thread = (pthread_t) 0;
84 /* Linked list and auxilliary variables for saving values */
85 static value_cache_t *cache_head = NULL;
86 static pthread_mutex_t cache_lock = PTHREAD_MUTEX_INITIALIZER;
87 static time_t cache_oldest = -1;
92 static value_cache_t *cache_search (const char *name)
96 for (vc = cache_head; vc != NULL; vc = vc->next)
98 if (strcmp (vc->name, name) == 0)
100 } /* for vc = cache_head .. NULL */
103 } /* value_cache_t *cache_search */
105 static int cache_insert (const data_set_t *ds, const value_list_t *vl)
107 /* We're called from `cache_update' so we don't need to lock the mutex */
111 DEBUG ("unixsock plugin: cache_insert: ds->type = %s; ds->ds_num = %i;"
112 " vl->values_len = %i;",
113 ds->type, ds->ds_num, vl->values_len);
115 assert (ds->ds_num == vl->values_len);
117 if (ds->ds_num != vl->values_len)
119 ERROR ("unixsock plugin: ds->type = %s: (ds->ds_num = %i) != "
120 "(vl->values_len = %i)",
121 ds->type, ds->ds_num, vl->values_len);
126 vc = (value_cache_t *) malloc (sizeof (value_cache_t));
130 pthread_mutex_unlock (&cache_lock);
131 ERROR ("unixsock plugin: malloc failed: %s",
132 sstrerror (errno, errbuf, sizeof (errbuf)));
136 vc->gauge = (gauge_t *) malloc (sizeof (gauge_t) * vl->values_len);
137 if (vc->gauge == NULL)
140 pthread_mutex_unlock (&cache_lock);
141 ERROR ("unixsock plugin: malloc failed: %s",
142 sstrerror (errno, errbuf, sizeof (errbuf)));
147 vc->counter = (counter_t *) malloc (sizeof (counter_t) * vl->values_len);
148 if (vc->counter == NULL)
151 pthread_mutex_unlock (&cache_lock);
152 ERROR ("unixsock plugin: malloc failed: %s",
153 sstrerror (errno, errbuf, sizeof (errbuf)));
159 if (FORMAT_VL (vc->name, sizeof (vc->name), vl, ds))
161 pthread_mutex_unlock (&cache_lock);
162 ERROR ("unixsock plugin: FORMAT_VL failed.");
169 for (i = 0; i < ds->ds_num; i++)
171 if (ds->ds[i].type == DS_TYPE_COUNTER)
174 vc->counter[i] = vl->values[i].counter;
176 else if (ds->ds[i].type == DS_TYPE_GAUGE)
178 vc->gauge[i] = vl->values[i].gauge;
187 vc->values_num = ds->ds_num;
190 vc->next = cache_head;
194 if ((vc->time < cache_oldest) || (-1 == cache_oldest))
195 cache_oldest = vc->time;
197 pthread_mutex_unlock (&cache_lock);
199 } /* int cache_insert */
201 static int cache_update (const data_set_t *ds, const value_list_t *vl)
203 char name[4*DATA_MAX_NAME_LEN];;
207 if (FORMAT_VL (name, sizeof (name), vl, ds) != 0)
210 pthread_mutex_lock (&cache_lock);
212 vc = cache_search (name);
214 /* pthread_mutex_lock is called by cache_insert. */
216 return (cache_insert (ds, vl));
218 assert (vc->values_num == ds->ds_num);
219 assert (vc->values_num == vl->values_len);
221 /* Avoid floating-point exceptions due to division by zero. */
222 if (vc->time >= vl->time)
224 pthread_mutex_unlock (&cache_lock);
225 ERROR ("unixsock plugin: vc->time >= vl->time. vc->time = %u; "
226 "vl->time = %u; vl = %s;",
227 (unsigned int) vc->time, (unsigned int) vl->time,
230 } /* if (vc->time >= vl->time) */
233 * Update the values. This is possibly a lot more that you'd expect
234 * because we honor min and max values and handle counter overflows here.
236 for (i = 0; i < ds->ds_num; i++)
238 if (ds->ds[i].type == DS_TYPE_COUNTER)
240 if (vl->values[i].counter < vc->counter[i])
242 if (vl->values[i].counter <= 4294967295U)
244 vc->gauge[i] = ((4294967295U - vl->values[i].counter)
245 + vc->counter[i]) / (vl->time - vc->time);
249 vc->gauge[i] = ((18446744073709551615ULL - vl->values[i].counter)
250 + vc->counter[i]) / (vl->time - vc->time);
255 vc->gauge[i] = (vl->values[i].counter - vc->counter[i])
256 / (vl->time - vc->time);
259 vc->counter[i] = vl->values[i].counter;
261 else if (ds->ds[i].type == DS_TYPE_GAUGE)
263 vc->gauge[i] = vl->values[i].gauge;
272 if (isnan (vc->gauge[i])
273 || (!isnan (ds->ds[i].min) && (vc->gauge[i] < ds->ds[i].min))
274 || (!isnan (ds->ds[i].max) && (vc->gauge[i] > ds->ds[i].max)))
276 } /* for i = 0 .. ds->ds_num */
281 if ((vc->time < cache_oldest) || (-1 == cache_oldest))
282 cache_oldest = vc->time;
284 pthread_mutex_unlock (&cache_lock);
286 } /* int cache_update */
288 static void cache_flush (int max_age)
294 pthread_mutex_lock (&cache_lock);
298 if ((now - cache_oldest) <= max_age)
300 pthread_mutex_unlock (&cache_lock);
311 if ((now - this->time) <= max_age)
313 if (this->time < cache_oldest)
314 cache_oldest = this->time;
322 cache_head = this->next;
324 prev->next = this->next;
327 free (this->counter);
334 } /* while (this != NULL) */
336 pthread_mutex_unlock (&cache_lock);
337 } /* void cache_flush */
339 static int us_open_socket (void)
341 struct sockaddr_un sa;
344 sock_fd = socket (PF_UNIX, SOCK_STREAM, 0);
348 ERROR ("unixsock plugin: socket failed: %s",
349 sstrerror (errno, errbuf, sizeof (errbuf)));
353 memset (&sa, '\0', sizeof (sa));
354 sa.sun_family = AF_UNIX;
355 strncpy (sa.sun_path, (sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
356 sizeof (sa.sun_path) - 1);
357 /* unlink (sa.sun_path); */
359 DEBUG ("unixsock plugin: socket path = %s", sa.sun_path);
361 status = bind (sock_fd, (struct sockaddr *) &sa, sizeof (sa));
365 sstrerror (errno, errbuf, sizeof (errbuf));
366 ERROR ("unixsock plugin: bind failed: %s", errbuf);
372 chmod (sa.sun_path, sock_perms);
374 status = listen (sock_fd, 8);
378 ERROR ("unixsock plugin: listen failed: %s",
379 sstrerror (errno, errbuf, sizeof (errbuf)));
392 grpname = (sock_group != NULL) ? sock_group : COLLECTD_GRP_NAME;
395 status = getgrnam_r (grpname, &sg, grbuf, sizeof (grbuf), &g);
399 WARNING ("unixsock plugin: getgrnam_r (%s) failed: %s", grpname,
400 sstrerror (errno, errbuf, sizeof (errbuf)));
405 WARNING ("unixsock plugin: No such group: `%s'",
410 if (chown ((sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
411 (uid_t) -1, g->gr_gid) != 0)
414 WARNING ("unixsock plugin: chown (%s, -1, %i) failed: %s",
415 (sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
417 sstrerror (errno, errbuf, sizeof (errbuf)));
422 } /* int us_open_socket */
424 static int us_handle_listval (FILE *fh, char **fields, int fields_num)
427 char **value_list = NULL;
428 int value_list_len = 0;
429 value_cache_t *entry;
434 DEBUG ("unixsock plugin: us_handle_listval: "
435 "Wrong number of fields: %i", fields_num);
436 fprintf (fh, "-1 Wrong number of fields: Got %i, expected 1.\n",
442 pthread_mutex_lock (&cache_lock);
444 for (entry = cache_head; entry != NULL; entry = entry->next)
448 snprintf (buffer, sizeof (buffer), "%u %s\n",
449 (unsigned int) entry->time, entry->name);
450 buffer[sizeof (buffer) - 1] = '\0';
452 tmp = realloc (value_list, sizeof (char *) * (value_list_len + 1));
457 value_list[value_list_len] = strdup (buffer);
459 if (value_list[value_list_len] != NULL)
463 pthread_mutex_unlock (&cache_lock);
465 DEBUG ("unixsock plugin: us_handle_listval: value_list_len = %i", value_list_len);
466 fprintf (fh, "%i Values found\n", value_list_len);
467 for (i = 0; i < value_list_len; i++)
468 fputs (value_list[i], fh);
472 } /* int us_handle_listval */
474 static void *us_handle_client (void *arg)
486 DEBUG ("Reading from fd #%i", fd);
488 fh = fdopen (fd, "r+");
492 ERROR ("unixsock plugin: fdopen failed: %s",
493 sstrerror (errno, errbuf, sizeof (errbuf)));
495 pthread_exit ((void *) 1);
498 while (fgets (buffer, sizeof (buffer), fh) != NULL)
502 len = strlen (buffer);
504 && ((buffer[len - 1] == '\n') || (buffer[len - 1] == '\r')))
505 buffer[--len] = '\0';
510 DEBUG ("fgets -> buffer = %s; len = %i;", buffer, len);
512 fields_num = strsplit (buffer, fields,
513 sizeof (fields) / sizeof (fields[0]));
521 if (strcasecmp (fields[0], "getval") == 0)
523 handle_getval (fh, fields, fields_num);
525 else if (strcasecmp (fields[0], "putval") == 0)
527 handle_putval (fh, fields, fields_num);
529 else if (strcasecmp (fields[0], "listval") == 0)
531 us_handle_listval (fh, fields, fields_num);
533 else if (strcasecmp (fields[0], "putnotif") == 0)
535 handle_putnotif (fh, fields, fields_num);
539 fprintf (fh, "-1 Unknown command: %s\n", fields[0]);
542 } /* while (fgets) */
547 pthread_exit ((void *) 0);
548 } /* void *us_handle_client */
550 static void *us_server_thread (void *arg)
555 pthread_attr_t th_attr;
557 if (us_open_socket () != 0)
558 pthread_exit ((void *) 1);
562 DEBUG ("unixsock plugin: Calling accept..");
563 status = accept (sock_fd, NULL, NULL);
571 ERROR ("unixsock plugin: accept failed: %s",
572 sstrerror (errno, errbuf, sizeof (errbuf)));
575 pthread_exit ((void *) 1);
578 remote_fd = (int *) malloc (sizeof (int));
579 if (remote_fd == NULL)
582 WARNING ("unixsock plugin: malloc failed: %s",
583 sstrerror (errno, errbuf, sizeof (errbuf)));
589 DEBUG ("Spawning child to handle connection on fd #%i", *remote_fd);
591 pthread_attr_init (&th_attr);
592 pthread_attr_setdetachstate (&th_attr, PTHREAD_CREATE_DETACHED);
594 status = pthread_create (&th, &th_attr, us_handle_client, (void *) remote_fd);
598 WARNING ("unixsock plugin: pthread_create failed: %s",
599 sstrerror (errno, errbuf, sizeof (errbuf)));
609 status = unlink ((sock_file != NULL) ? sock_file : US_DEFAULT_PATH);
613 NOTICE ("unixsock plugin: unlink (%s) failed: %s",
614 (sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
615 sstrerror (errno, errbuf, sizeof (errbuf)));
619 } /* void *us_server_thread */
621 static int us_config (const char *key, const char *val)
623 if (strcasecmp (key, "SocketFile") == 0)
626 sock_file = strdup (val);
628 else if (strcasecmp (key, "SocketGroup") == 0)
631 sock_group = strdup (val);
633 else if (strcasecmp (key, "SocketPerms") == 0)
635 sock_perms = (int) strtol (val, NULL, 8);
643 } /* int us_config */
645 static int us_init (void)
651 status = pthread_create (&listen_thread, NULL, us_server_thread, NULL);
655 ERROR ("unixsock plugin: pthread_create failed: %s",
656 sstrerror (errno, errbuf, sizeof (errbuf)));
663 static int us_shutdown (void)
669 if (listen_thread != (pthread_t) 0)
671 pthread_kill (listen_thread, SIGTERM);
672 pthread_join (listen_thread, &ret);
673 listen_thread = (pthread_t) 0;
676 plugin_unregister_init ("unixsock");
677 plugin_unregister_write ("unixsock");
678 plugin_unregister_shutdown ("unixsock");
681 } /* int us_shutdown */
683 static int us_write (const data_set_t *ds, const value_list_t *vl)
685 cache_update (ds, vl);
686 cache_flush (2 * interval_g);
691 void module_register (void)
693 plugin_register_config ("unixsock", us_config,
694 config_keys, config_keys_num);
695 plugin_register_init ("unixsock", us_init);
696 plugin_register_write ("unixsock", us_write);
697 plugin_register_shutdown ("unixsock", us_shutdown);
698 } /* void module_register (void) */
700 /* vim: set sw=4 ts=4 sts=4 tw=78 : */