2 * RRDTool - src/rrd_client.c
3 * Copyright (C) 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>
23 #include "rrd_client.h"
31 #include <sys/types.h>
32 #include <sys/socket.h>
37 #define ENODATA ENOENT
40 struct rrdc_response_s
47 typedef struct rrdc_response_s rrdc_response_t;
49 static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
51 static FILE *sh = NULL;
52 static char *sd_path = NULL; /* cache the path for sd */
54 /* One must hold `lock' when calling `close_connection'. */
55 static void close_connection (void) /* {{{ */
72 } /* }}} void close_connection */
74 static int buffer_add_string (const char *str, /* {{{ */
75 char **buffer_ret, size_t *buffer_size_ret)
84 buffer_size = *buffer_size_ret;
89 while (buffer_pos < buffer_size)
93 buffer[buffer_pos] = ' ';
98 else if ((str[i] == ' ') || (str[i] == '\\'))
100 if (buffer_pos >= (buffer_size - 1))
102 buffer[buffer_pos] = '\\';
104 buffer[buffer_pos] = str[i];
109 buffer[buffer_pos] = str[i];
113 } /* while (buffer_pos < buffer_size) */
118 *buffer_ret = buffer + buffer_pos;
119 *buffer_size_ret = buffer_size - buffer_pos;
122 } /* }}} int buffer_add_string */
124 static int buffer_add_value (const char *value, /* {{{ */
125 char **buffer_ret, size_t *buffer_size_ret)
129 if (strncmp (value, "N:", 2) == 0)
130 snprintf (temp, sizeof (temp), "%lu:%s",
131 (unsigned long) time (NULL), value + 2);
133 strncpy (temp, value, sizeof (temp));
134 temp[sizeof (temp) - 1] = 0;
136 return (buffer_add_string (temp, buffer_ret, buffer_size_ret));
137 } /* }}} int buffer_add_value */
139 /* Remove trailing newline (NL) and carriage return (CR) characters. Similar to
140 * the Perl function `chomp'. Returns the number of characters that have been
142 static int chomp (char *str) /* {{{ */
152 while ((len > 0) && ((str[len - 1] == '\n') || (str[len - 1] == '\r')))
160 } /* }}} int chomp */
162 static void response_free (rrdc_response_t *res) /* {{{ */
167 if (res->lines != NULL)
171 for (i = 0; i < res->lines_num; i++)
172 if (res->lines[i] != NULL)
173 free (res->lines[i]);
178 } /* }}} void response_free */
180 static int response_read (rrdc_response_t **ret_response) /* {{{ */
182 rrdc_response_t *ret;
192 ret = (rrdc_response_t *) malloc (sizeof (rrdc_response_t));
195 memset (ret, 0, sizeof (*ret));
199 buffer_ptr = fgets (buffer, sizeof (buffer), sh);
200 if (buffer_ptr == NULL)
204 ret->status = strtol (buffer, &ret->message, 0);
205 if (buffer == ret->message)
210 /* Skip leading whitespace of the status message */
211 ret->message += strspn (ret->message, " \t");
213 if (ret->status <= 0)
219 ret->lines = (char **) malloc (sizeof (char *) * ret->status);
220 if (ret->lines == NULL)
225 memset (ret->lines, 0, sizeof (char *) * ret->status);
226 ret->lines_num = (size_t) ret->status;
228 for (i = 0; i < ret->lines_num; i++)
230 buffer_ptr = fgets (buffer, sizeof (buffer), sh);
231 if (buffer_ptr == NULL)
238 ret->lines[i] = strdup (buffer);
239 if (ret->lines[i] == NULL)
248 } /* }}} rrdc_response_t *response_read */
250 static int request (const char *buffer, size_t buffer_size, /* {{{ */
251 rrdc_response_t **ret_response)
254 rrdc_response_t *res;
256 pthread_mutex_lock (&lock);
260 pthread_mutex_unlock (&lock);
264 status = (int) fwrite (buffer, buffer_size, /* nmemb = */ 1, sh);
268 pthread_mutex_unlock (&lock);
274 status = response_read (&res);
276 pthread_mutex_unlock (&lock);
283 } /* }}} int request */
285 /* determine whether we are connected to the specified daemon_addr if
286 * NULL, return whether we are connected at all
288 int rrdc_is_connected(const char *daemon_addr) /* {{{ */
292 else if (daemon_addr == NULL)
294 /* here we have to handle the case i.e.
295 * UPDATE --daemon ...; UPDATEV (no --daemon) ...
296 * In other words: we have a cached connection,
297 * but it is not specified in the current command.
298 * Daemon is only implied in this case if set in ENV
300 if (getenv(ENV_RRDCACHED_ADDRESS) != NULL)
305 else if (strcmp(daemon_addr, sd_path) == 0)
310 } /* }}} int rrdc_is_connected */
312 static int rrdc_connect_unix (const char *path) /* {{{ */
314 struct sockaddr_un sa;
317 assert (path != NULL);
320 sd = socket (PF_UNIX, SOCK_STREAM, /* protocol = */ 0);
327 memset (&sa, 0, sizeof (sa));
328 sa.sun_family = AF_UNIX;
329 strncpy (sa.sun_path, path, sizeof (sa.sun_path) - 1);
331 status = connect (sd, (struct sockaddr *) &sa, sizeof (sa));
339 sh = fdopen (sd, "r+");
348 } /* }}} int rrdc_connect_unix */
350 static int rrdc_connect_network (const char *addr_orig) /* {{{ */
352 struct addrinfo ai_hints;
353 struct addrinfo *ai_res;
354 struct addrinfo *ai_ptr;
355 char addr_copy[NI_MAXHOST];
359 assert (addr_orig != NULL);
362 strncpy(addr_copy, addr_orig, sizeof(addr_copy));
363 addr_copy[sizeof(addr_copy) - 1] = '\0';
367 memset (&ai_hints, 0, sizeof (ai_hints));
368 ai_hints.ai_flags = 0;
370 ai_hints.ai_flags |= AI_ADDRCONFIG;
372 ai_hints.ai_family = AF_UNSPEC;
373 ai_hints.ai_socktype = SOCK_STREAM;
376 if (*addr == '[') /* IPv6+port format */
378 /* `addr' is something like "[2001:780:104:2:211:24ff:feab:26f8]:12345" */
381 port = strchr (addr, ']');
384 rrd_set_error("malformed address: %s", addr_orig);
396 rrd_set_error("garbage after address: %s", port);
399 } /* if (*addr = ']') */
400 else if (strchr (addr, '.') != NULL) /* Hostname or IPv4 */
402 port = rindex(addr, ':');
411 status = getaddrinfo (addr,
412 port == NULL ? RRDCACHED_DEFAULT_PORT : port,
417 for (ai_ptr = ai_res; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
419 sd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
427 status = connect (sd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
435 sh = fdopen (sd, "r+");
443 assert (status == 0);
448 } /* }}} int rrdc_connect_network */
450 int rrdc_connect (const char *addr) /* {{{ */
455 addr = getenv (ENV_RRDCACHED_ADDRESS);
460 pthread_mutex_lock(&lock);
462 if (sd >= 0 && sd_path != NULL && strcmp(addr, sd_path) == 0)
464 /* connection to the same daemon; use cached connection */
465 pthread_mutex_unlock (&lock);
473 if (strncmp ("unix:", addr, strlen ("unix:")) == 0)
474 status = rrdc_connect_unix (addr + strlen ("unix:"));
475 else if (addr[0] == '/')
476 status = rrdc_connect_unix (addr);
478 status = rrdc_connect_network(addr);
480 if (status == 0 && sd >= 0)
481 sd_path = strdup(addr);
483 rrd_set_error("Unable to connect to rrdcached: %s",
486 : rrd_strerror (status));
488 pthread_mutex_unlock (&lock);
490 } /* }}} int rrdc_connect */
492 int rrdc_disconnect (void) /* {{{ */
494 pthread_mutex_lock (&lock);
498 pthread_mutex_unlock (&lock);
501 } /* }}} int rrdc_disconnect */
503 int rrdc_update (const char *filename, int values_num, /* {{{ */
504 const char * const *values)
510 rrdc_response_t *res;
514 memset (buffer, 0, sizeof (buffer));
515 buffer_ptr = &buffer[0];
516 buffer_free = sizeof (buffer);
518 status = buffer_add_string ("update", &buffer_ptr, &buffer_free);
522 status = buffer_add_string (filename, &buffer_ptr, &buffer_free);
526 for (i = 0; i < values_num; i++)
528 status = buffer_add_value (values[i], &buffer_ptr, &buffer_free);
533 assert (buffer_free < sizeof (buffer));
534 buffer_size = sizeof (buffer) - buffer_free;
535 assert (buffer[buffer_size - 1] == ' ');
536 buffer[buffer_size - 1] = '\n';
539 status = request (buffer, buffer_size, &res);
543 status = res->status;
547 } /* }}} int rrdc_update */
549 int rrdc_flush (const char *filename) /* {{{ */
555 rrdc_response_t *res;
558 if (filename == NULL)
561 memset (buffer, 0, sizeof (buffer));
562 buffer_ptr = &buffer[0];
563 buffer_free = sizeof (buffer);
565 status = buffer_add_string ("flush", &buffer_ptr, &buffer_free);
569 status = buffer_add_string (filename, &buffer_ptr, &buffer_free);
573 assert (buffer_free < sizeof (buffer));
574 buffer_size = sizeof (buffer) - buffer_free;
575 assert (buffer[buffer_size - 1] == ' ');
576 buffer[buffer_size - 1] = '\n';
579 status = request (buffer, buffer_size, &res);
583 status = res->status;
587 } /* }}} int rrdc_flush */
590 /* convenience function; if there is a daemon specified, or if we can
591 * detect one from the environment, then flush the file. Otherwise, no-op
593 int rrdc_flush_if_daemon (const char *opt_daemon, const char *filename) /* {{{ */
597 rrdc_connect(opt_daemon);
599 if (rrdc_is_connected(opt_daemon))
601 status = rrdc_flush (filename);
604 rrd_set_error ("rrdc_flush (%s) failed with status %i.",
607 } /* if (daemon_addr) */
610 } /* }}} int rrdc_flush_if_daemon */
613 int rrdc_stats_get (rrdc_stats_t **ret_stats) /* {{{ */
618 rrdc_response_t *res;
623 /* Protocol example: {{{
625 * <- 5 Statistics follow
627 * <- UpdatesWritten: 0
628 * <- DataSetsWritten: 0
629 * <- TreeNodesNumber: 0
634 status = request ("STATS\n", strlen ("STATS\n"), &res);
638 if (res->status <= 0)
646 for (i = 0; i < res->lines_num; i++)
654 value = strchr (key, ':');
660 while ((value[0] == ' ') || (value[0] == '\t'))
663 s = (rrdc_stats_t *) malloc (sizeof (rrdc_stats_t));
666 memset (s, 0, sizeof (*s));
668 s->name = strdup (key);
671 if ((strcmp ("QueueLength", key) == 0)
672 || (strcmp ("TreeDepth", key) == 0)
673 || (strcmp ("TreeNodesNumber", key) == 0))
675 s->type = RRDC_STATS_TYPE_GAUGE;
676 s->value.gauge = strtod (value, &endptr);
678 else if ((strcmp ("DataSetsWritten", key) == 0)
679 || (strcmp ("FlushesReceived", key) == 0)
680 || (strcmp ("JournalBytes", key) == 0)
681 || (strcmp ("JournalRotate", key) == 0)
682 || (strcmp ("UpdatesReceived", key) == 0)
683 || (strcmp ("UpdatesWritten", key) == 0))
685 s->type = RRDC_STATS_TYPE_COUNTER;
686 s->value.counter = (uint64_t) strtoll (value, &endptr, /* base = */ 0);
694 /* Conversion failed */
712 } /* for (i = 0; i < res->lines_num; i++) */
721 } /* }}} int rrdc_stats_get */
723 void rrdc_stats_free (rrdc_stats_t *ret_stats) /* {{{ */
734 if (this->name != NULL)
742 } /* while (this != NULL) */
743 } /* }}} void rrdc_stats_free */
746 * vim: set sw=2 sts=2 ts=8 et fdm=marker :