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"
27 /* Folks without pthread will need to disable this plugin. */
30 #include <sys/socket.h>
37 # define UNIX_PATH_MAX sizeof (((struct sockaddr_un *)0)->sun_path)
40 #define US_DEFAULT_PATH PREFIX"/var/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;
70 /* socket configuration */
71 static int sock_fd = -1;
72 static char *sock_file = NULL;
73 static char *sock_group = NULL;
74 static int sock_perms = S_IRWXU | S_IRWXG;
76 static pthread_t listen_thread = (pthread_t) 0;
78 /* Linked list and auxilliary variables for saving values */
79 static value_cache_t *cache_head = NULL;
80 static pthread_mutex_t cache_lock = PTHREAD_MUTEX_INITIALIZER;
81 static unsigned int cache_oldest = UINT_MAX;
86 static value_cache_t *cache_search (const char *name)
90 for (vc = cache_head; vc != NULL; vc = vc->next)
92 if (strcmp (vc->name, name) == 0)
94 } /* for vc = cache_head .. NULL */
97 } /* value_cache_t *cache_search */
99 static int cache_alloc_name (char *ret, int ret_len,
100 const char *hostname,
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/%s",
113 hostname, plugin, type);
115 status = snprintf (ret, ret_len, "%s/%s/%s-%s",
116 hostname, plugin, type, type_instance);
120 if ((type_instance == NULL) || (strlen (type_instance) == 0))
121 status = snprintf (ret, ret_len, "%s/%s-%s/%s",
122 hostname, plugin, plugin_instance, type);
124 status = snprintf (ret, ret_len, "%s/%s-%s/%s-%s",
125 hostname, plugin, plugin_instance, type, type_instance);
128 if ((status < 1) || (status >= ret_len))
131 } /* int cache_alloc_name */
133 static int cache_insert (const data_set_t *ds, const value_list_t *vl)
135 /* We're called from `cache_update' so we don't need to lock the mutex */
139 DEBUG ("ds->ds_num = %i; vl->values_len = %i;",
140 ds->ds_num, vl->values_len);
141 assert (ds->ds_num == vl->values_len);
143 vc = (value_cache_t *) malloc (sizeof (value_cache_t));
146 pthread_mutex_unlock (&cache_lock);
147 ERROR ("unixsock plugin: malloc failed: %s",
152 vc->gauge = (gauge_t *) malloc (sizeof (gauge_t) * vl->values_len);
153 if (vc->gauge == NULL)
155 pthread_mutex_unlock (&cache_lock);
156 ERROR ("unixsock plugin: malloc failed: %s",
162 vc->counter = (counter_t *) malloc (sizeof (counter_t) * vl->values_len);
163 if (vc->counter == NULL)
165 pthread_mutex_unlock (&cache_lock);
166 ERROR ("unixsock plugin: malloc failed: %s",
173 if (cache_alloc_name (vc->name, sizeof (vc->name),
174 vl->host, vl->plugin, vl->plugin_instance,
175 ds->type, vl->type_instance) != 0)
177 pthread_mutex_unlock (&cache_lock);
178 ERROR ("unixsock plugin: cache_alloc_name failed.");
185 for (i = 0; i < ds->ds_num; i++)
187 if (ds->ds[i].type == DS_TYPE_COUNTER)
190 vc->counter[i] = vl->values[i].counter;
192 else if (ds->ds[i].type == DS_TYPE_GAUGE)
194 vc->gauge[i] = vl->values[i].gauge;
203 vc->values_num = ds->ds_num;
206 vc->next = cache_head;
210 if (vc->time < cache_oldest)
211 cache_oldest = vc->time;
213 pthread_mutex_unlock (&cache_lock);
215 } /* int cache_insert */
217 static int cache_update (const data_set_t *ds, const value_list_t *vl)
219 char name[4*DATA_MAX_NAME_LEN];;
223 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 vc->counter[i] = vl->values[i].counter;
268 else if (ds->ds[i].type == DS_TYPE_GAUGE)
270 vc->gauge[i] = vl->values[i].gauge;
279 if (isnan (vc->gauge[i])
280 || (!isnan (ds->ds[i].min) && (vc->gauge[i] < ds->ds[i].min))
281 || (!isnan (ds->ds[i].max) && (vc->gauge[i] > ds->ds[i].max)))
283 } /* for i = 0 .. ds->ds_num */
288 if (vc->time < cache_oldest)
289 cache_oldest = vc->time;
291 pthread_mutex_unlock (&cache_lock);
293 } /* int cache_update */
295 static void cache_flush (int max_age)
301 pthread_mutex_lock (&cache_lock);
305 if ((now - cache_oldest) <= max_age)
307 pthread_mutex_unlock (&cache_lock);
318 if ((now - this->time) <= max_age)
320 if (this->time < cache_oldest)
321 cache_oldest = this->time;
329 cache_head = this->next;
331 prev->next = this->next;
334 free (this->counter);
341 } /* while (this != NULL) */
343 pthread_mutex_unlock (&cache_lock);
344 } /* int cache_flush */
346 static int us_open_socket (void)
348 struct sockaddr_un sa;
351 sock_fd = socket (PF_UNIX, SOCK_STREAM, 0);
354 ERROR ("unixsock plugin: socket failed: %s",
359 memset (&sa, '\0', sizeof (sa));
360 sa.sun_family = AF_UNIX;
361 strncpy (sa.sun_path, (sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
362 sizeof (sa.sun_path) - 1);
363 /* unlink (sa.sun_path); */
365 status = bind (sock_fd, (struct sockaddr *) &sa, sizeof (sa));
368 DEBUG ("bind failed: %s; sa.sun_path = %s",
369 strerror (errno), sa.sun_path);
370 ERROR ("unixsock plugin: bind failed: %s",
377 status = listen (sock_fd, 8);
380 ERROR ("unixsock plugin: listen failed: %s",
394 grpname = (sock_group != NULL) ? sock_group : COLLECTD_GRP_NAME;
397 status = getgrnam_r (grpname, &sg, grbuf, sizeof (grbuf), &g);
400 WARNING ("unixsock plugin: getgrnam_r (%s) failed: %s",
401 grpname, strerror (status));
406 WARNING ("unixsock plugin: No such group: `%s'",
411 if (chown ((sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
412 (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,
422 } /* int us_open_socket */
424 static int us_handle_getval (FILE *fh, char **fields, int fields_num)
426 char *hostname = fields[1];
428 char *plugin_instance;
431 char name[4*DATA_MAX_NAME_LEN];
439 plugin = strchr (hostname, '/');
442 *plugin = '\0'; plugin++;
444 type = strchr (plugin, '/');
447 *type = '\0'; type++;
449 plugin_instance = strchr (plugin, '-');
450 if (plugin_instance != NULL)
452 *plugin_instance = '\0';
456 type_instance = strchr (type, '-');
457 if (type_instance != NULL)
459 *type_instance = '\0';
463 status = cache_alloc_name (name, sizeof (name),
464 hostname, plugin, plugin_instance, type, type_instance);
468 pthread_mutex_lock (&cache_lock);
470 DEBUG ("vc = cache_search (%s)", name);
471 vc = cache_search (name);
475 DEBUG ("Did not find cache entry.");
476 fprintf (fh, "-1 No such value");
480 DEBUG ("Found cache entry.");
481 fprintf (fh, "%i", vc->values_num);
482 for (i = 0; i < vc->values_num; i++)
484 fprintf (fh, " %s=", vc->ds->ds[i].name);
485 if (isnan (vc->gauge[i]))
488 fprintf (fh, "%12e", vc->gauge[i]);
492 /* Free the mutex as soon as possible and definitely before flushing */
493 pthread_mutex_unlock (&cache_lock);
499 } /* int us_handle_getval */
501 static void *us_handle_client (void *arg)
513 DEBUG ("Reading from fd #%i", fd);
515 fh = fdopen (fd, "r+");
518 ERROR ("unixsock plugin: fdopen failed: %s",
521 pthread_exit ((void *) 1);
524 while (fgets (buffer, sizeof (buffer), fh) != NULL)
528 len = strlen (buffer);
530 && ((buffer[len - 1] == '\n') || (buffer[len - 1] == '\r')))
531 buffer[--len] = '\0';
536 DEBUG ("fgets -> buffer = %s; len = %i;", buffer, len);
538 fields_num = strsplit (buffer, fields,
539 sizeof (fields) / sizeof (fields[0]));
547 if (strcasecmp (fields[0], "getval") == 0)
549 us_handle_getval (fh, fields, fields_num);
553 fprintf (fh, "Unknown command: %s\n", fields[0]);
556 } /* while (fgets) */
561 pthread_exit ((void *) 0);
562 } /* void *us_handle_client */
564 static void *us_server_thread (void *arg)
569 pthread_attr_t th_attr;
571 if (us_open_socket () != 0)
572 pthread_exit ((void *) 1);
576 DEBUG ("Calling accept..");
577 status = accept (sock_fd, NULL, NULL);
583 ERROR ("unixsock plugin: accept failed: %s",
587 pthread_exit ((void *) 1);
590 remote_fd = (int *) malloc (sizeof (int));
591 if (remote_fd == NULL)
593 WARNING ("unixsock plugin: malloc failed: %s",
600 DEBUG ("Spawning child to handle connection on fd #%i", *remote_fd);
602 pthread_attr_init (&th_attr);
603 pthread_attr_setdetachstate (&th_attr, PTHREAD_CREATE_DETACHED);
605 status = pthread_create (&th, &th_attr, us_handle_client, (void *) remote_fd);
608 WARNING ("unixsock plugin: pthread_create failed: %s",
617 } /* void *us_server_thread */
619 static int us_config (const char *key, const char *val)
621 if (strcasecmp (key, "SocketFile") == 0)
624 sock_file = strdup (val);
626 else if (strcasecmp (key, "SocketGroup") == 0)
629 sock_group = strdup (val);
631 else if (strcasecmp (key, "SocketPerms") == 0)
633 sock_perms = (int) strtol (val, NULL, 8);
641 } /* int us_config */
643 static int us_init (void)
647 status = pthread_create (&listen_thread, NULL, us_server_thread, NULL);
650 ERROR ("unixsock plugin: pthread_create failed: %s",
658 static int us_shutdown (void)
662 if (listen_thread != (pthread_t) 0)
664 pthread_kill (listen_thread, SIGTERM);
665 pthread_join (listen_thread, &ret);
666 listen_thread = (pthread_t) 0;
669 plugin_unregister_init ("unixsock");
670 plugin_unregister_write ("unixsock");
671 plugin_unregister_shutdown ("unixsock");
674 } /* int us_shutdown */
676 static int us_write (const data_set_t *ds, const value_list_t *vl)
678 cache_update (ds, vl);
679 cache_flush (2 * atoi (COLLECTD_STEP));
684 void module_register (void)
686 plugin_register_config ("unixsock", us_config,
687 config_keys, config_keys_num);
688 plugin_register_init ("unixsock", us_init);
689 plugin_register_write ("unixsock", us_write);
690 plugin_register_shutdown ("unixsock", us_shutdown);
691 } /* void module_register (void) */
693 /* vim: set sw=4 ts=4 sts=4 tw=78 : */