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_debug.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 PREFIX"/var/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;
71 /* socket configuration */
72 static int sock_fd = -1;
73 static char *sock_file = NULL;
74 static char *sock_group = NULL;
75 static int sock_perms = S_IRWXU | S_IRWXG;
77 static pthread_t listen_thread = -1;
79 /* Linked list and auxilliary variables for saving values */
80 static value_cache_t *cache_head = NULL;
81 static pthread_mutex_t cache_lock = PTHREAD_MUTEX_INITIALIZER;
82 static unsigned int cache_oldest = UINT_MAX;
87 static value_cache_t *cache_search (const char *name)
91 for (vc = cache_head; vc != NULL; vc = vc->next)
93 if (strcmp (vc->name, name) == 0)
95 } /* for vc = cache_head .. NULL */
98 } /* value_cache_t *cache_search */
100 static int cache_alloc_name (char *ret, int ret_len,
101 const char *plugin, const char *plugin_instance,
102 const char *type, const char *type_instance)
106 assert (plugin != NULL);
107 assert (type != NULL);
109 if ((plugin_instance == NULL) || (strlen (plugin_instance) == 0))
111 if ((type_instance == NULL) || (strlen (type_instance) == 0))
112 status = snprintf (ret, ret_len, "%s/%s",
115 status = snprintf (ret, ret_len, "%s/%s-%s",
116 plugin, type, type_instance);
120 if ((type_instance == NULL) || (strlen (type_instance) == 0))
121 status = snprintf (ret, ret_len, "%s-%s/%s",
122 plugin, plugin_instance, type);
124 status = snprintf (ret, ret_len, "%s-%s/%s-%s",
125 plugin, plugin_instance,
126 type, type_instance);
129 if ((status < 1) || (status >= ret_len))
132 } /* int cache_alloc_name */
134 static int cache_insert (const data_set_t *ds, const value_list_t *vl)
136 /* We're called from `cache_update' so we don't need to lock the mutex */
140 DBG ("ds->ds_num = %i; vl->values_len = %i;",
141 ds->ds_num, vl->values_len);
142 assert (ds->ds_num == vl->values_len);
144 vc = (value_cache_t *) malloc (sizeof (value_cache_t));
147 pthread_mutex_unlock (&cache_lock);
148 syslog (LOG_ERR, "unixsock plugin: malloc failed: %s",
153 vc->gauge = (gauge_t *) malloc (sizeof (gauge_t) * vl->values_len);
154 if (vc->gauge == NULL)
156 pthread_mutex_unlock (&cache_lock);
157 syslog (LOG_ERR, "unixsock plugin: malloc failed: %s",
163 vc->counter = (counter_t *) malloc (sizeof (counter_t) * vl->values_len);
164 if (vc->counter == NULL)
166 pthread_mutex_unlock (&cache_lock);
167 syslog (LOG_ERR, "unixsock plugin: malloc failed: %s",
174 if (cache_alloc_name (vc->name, sizeof (vc->name),
175 vl->plugin, vl->plugin_instance,
176 ds->type, vl->type_instance) != 0)
178 pthread_mutex_unlock (&cache_lock);
179 syslog (LOG_ERR, "unixsock plugin: cache_alloc_name failed.");
186 for (i = 0; i < ds->ds_num; i++)
188 if (ds->ds[i].type == DS_TYPE_COUNTER)
191 vc->counter[i] = vl->values[i].counter;
193 else if (ds->ds[i].type == DS_TYPE_GAUGE)
195 vc->gauge[i] = vl->values[i].gauge;
204 vc->values_num = ds->ds_num;
207 vc->next = cache_head;
211 if (vc->time < cache_oldest)
212 cache_oldest = vc->time;
214 pthread_mutex_unlock (&cache_lock);
216 } /* int cache_insert */
218 static int cache_update (const data_set_t *ds, const value_list_t *vl)
220 char name[4*DATA_MAX_NAME_LEN];;
224 if (cache_alloc_name (name, sizeof (name),
225 vl->plugin, vl->plugin_instance,
226 ds->type, vl->type_instance) != 0)
229 pthread_mutex_lock (&cache_lock);
231 vc = cache_search (name);
234 return (cache_insert (ds, vl));
236 assert (vc->values_num == ds->ds_num);
237 assert (vc->values_num == vl->values_len);
240 * Update the values. This is possibly a lot more that you'd expect
241 * because we honor min and max values and handle counter overflows here.
243 for (i = 0; i < ds->ds_num; i++)
245 if (ds->ds[i].type == DS_TYPE_COUNTER)
247 if (vl->values[i].counter < vc->counter[i])
249 if (vl->values[i].counter <= 4294967295U)
251 vc->gauge[i] = ((4294967295U - vl->values[i].counter)
252 + vc->counter[i]) / (vl->time - vc->time);
256 vc->gauge[i] = ((18446744073709551615ULL - vl->values[i].counter)
257 + vc->counter[i]) / (vl->time - vc->time);
262 vc->gauge[i] = (vl->values[i].counter - vc->counter[i])
263 / (vl->time - vc->time);
266 DBG ("name = %s; old counter: %llu; new counter: %llu; rate: %lf;",
268 vc->counter[i], vl->values[i].counter,
271 vc->counter[i] = vl->values[i].counter;
273 else if (ds->ds[i].type == DS_TYPE_GAUGE)
275 vc->gauge[i] = vl->values[i].gauge;
278 DBG ("name, %s; gauge: %lf;",
287 if ((vc->gauge[i] == NAN)
288 || ((ds->ds[i].min != NAN) && (vc->gauge[i] < ds->ds[i].min))
289 || ((ds->ds[i].max != NAN) && (vc->gauge[i] > ds->ds[i].max)))
291 } /* for i = 0 .. ds->ds_num */
296 if (vc->time < cache_oldest)
297 cache_oldest = vc->time;
299 pthread_mutex_unlock (&cache_lock);
301 } /* int cache_update */
303 static void cache_flush (int max_age)
309 pthread_mutex_lock (&cache_lock);
313 if ((now - cache_oldest) <= max_age)
315 pthread_mutex_unlock (&cache_lock);
326 if ((now - this->time) <= max_age)
328 if (this->time < cache_oldest)
329 cache_oldest = this->time;
337 cache_head = this->next;
339 prev->next = this->next;
342 free (this->counter);
349 } /* while (this != NULL) */
351 pthread_mutex_unlock (&cache_lock);
352 } /* int cache_flush */
354 static int us_open_socket (void)
356 struct sockaddr_un sa;
359 sock_fd = socket (PF_UNIX, SOCK_STREAM, 0);
362 syslog (LOG_ERR, "unixsock plugin: socket failed: %s",
367 memset (&sa, '\0', sizeof (sa));
368 sa.sun_family = AF_UNIX;
369 strncpy (sa.sun_path, (sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
370 sizeof (sa.sun_path) - 1);
371 /* unlink (sa.sun_path); */
373 status = bind (sock_fd, (struct sockaddr *) &sa, sizeof (sa));
376 DBG ("bind failed: %s; sa.sun_path = %s",
377 strerror (errno), sa.sun_path);
378 syslog (LOG_ERR, "unixsock plugin: bind failed: %s",
385 status = listen (sock_fd, 8);
388 syslog (LOG_ERR, "unixsock plugin: listen failed: %s",
400 g = getgrnam ((sock_group != NULL) ? sock_group : COLLECTD_GRP_NAME);
404 syslog (LOG_WARNING, "unixsock plugin: getgrnam (%s) failed: %s",
405 (sock_group != NULL) ? sock_group : COLLECTD_GRP_NAME,
413 if (chown ((sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
414 (uid_t) -1, g->gr_gid) != 0)
416 syslog (LOG_WARNING, "unixsock plugin: chown (%s, -1, %i) failed: %s",
417 (sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
424 } /* int us_open_socket */
426 static int us_handle_getval (FILE *fh, char **fields, int fields_num)
428 char *plugin = fields[1];
429 char *plugin_instance;
432 char name[4*DATA_MAX_NAME_LEN];
440 type = strchr (plugin, '/');
443 *type = '\0'; type++;
445 plugin_instance = strchr (plugin, '-');
446 if (plugin_instance != NULL)
448 *plugin_instance = '\0';
452 type_instance = strchr (type, '-');
453 if (type_instance != NULL)
455 *type_instance = '\0';
459 status = cache_alloc_name (name, sizeof (name),
460 plugin, plugin_instance, type, type_instance);
464 pthread_mutex_lock (&cache_lock);
466 DBG ("vc = cache_search (%s)", name);
467 vc = cache_search (name);
471 DBG ("Did not find cache entry.");
472 fprintf (fh, "-1 No such value");
476 DBG ("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 (vc->gauge[i] == NAN)
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 void *us_handle_client (void *arg)
509 DBG ("Reading from fd #%i", fd);
511 fh = fdopen (fd, "r+");
514 syslog (LOG_ERR, "unixsock plugin: fdopen failed: %s",
517 pthread_exit ((void *) 1);
520 while (fgets (buffer, sizeof (buffer), fh) != NULL)
524 len = strlen (buffer);
526 && ((buffer[len - 1] == '\n') || (buffer[len - 1] == '\r')))
527 buffer[--len] = '\0';
532 DBG ("fgets -> buffer = %s; len = %i;", buffer, len);
534 fields_num = strsplit (buffer, fields,
535 sizeof (fields) / sizeof (fields[0]));
543 if (strcasecmp (fields[0], "getval") == 0)
545 us_handle_getval (fh, fields, fields_num);
549 fprintf (fh, "Unknown command: %s\n", fields[0]);
552 } /* while (fgets) */
557 pthread_exit ((void *) 0);
558 } /* void *us_handle_client */
560 static void *us_server_thread (void *arg)
566 if (us_open_socket () != 0)
567 pthread_exit ((void *) 1);
571 DBG ("Calling accept..");
572 status = accept (sock_fd, NULL, NULL);
578 syslog (LOG_ERR, "unixsock plugin: accept failed: %s",
582 pthread_exit ((void *) 1);
585 remote_fd = (int *) malloc (sizeof (int));
586 if (remote_fd == NULL)
588 syslog (LOG_WARNING, "unixsock plugin: malloc failed: %s",
595 DBG ("Spawning child to handle connection on fd #%i", *remote_fd);
597 status = pthread_create (&th, NULL, us_handle_client, (void *) remote_fd);
600 syslog (LOG_WARNING, "unixsock plugin: pthread_create failed: %s",
609 } /* void *us_server_thread */
611 static int us_config (const char *key, const char *val)
613 if (strcasecmp (key, "SocketFile") == 0)
616 sock_file = strdup (val);
618 else if (strcasecmp (key, "SocketGroup") == 0)
621 sock_group = strdup (val);
623 else if (strcasecmp (key, "SocketPerms") == 0)
625 sock_perms = (int) strtol (val, NULL, 8);
633 } /* int us_config */
635 static int us_init (void)
639 status = pthread_create (&listen_thread, NULL, us_server_thread, NULL);
642 syslog (LOG_ERR, "unixsock plugin: pthread_create failed: %s",
650 static int us_shutdown (void)
654 if (listen_thread >= 0)
656 pthread_kill (listen_thread, SIGTERM);
657 pthread_join (listen_thread, &ret);
661 } /* int us_shutdown */
663 static int us_write (const data_set_t *ds, const value_list_t *vl)
665 cache_update (ds, vl);
666 cache_flush (2 * atoi (COLLECTD_STEP));
671 void module_register (void)
673 plugin_register_config ("unixsock", us_config,
674 config_keys, config_keys_num);
675 plugin_register_init ("unixsock", us_init);
676 plugin_register_write ("unixsock", us_write);
677 plugin_register_shutdown ("unixsock", us_shutdown);
678 } /* void module_register (void) */
680 /* vim: set sw=4 ts=4 sts=4 tw=78 : */