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 char *sd_path = NULL; /* cache the path for sd */
52 static void _disconnect(void);
54 static ssize_t sread (void *buffer_void, size_t buffer_size) /* {{{ */
61 buffer = (char *) buffer_void;
63 buffer_free = buffer_size;
65 while (buffer_free > 0)
67 status = read (sd, buffer + buffer_used, buffer_free);
68 if ((status < 0) && ((errno == EAGAIN) || (errno == EINTR)))
81 assert ((0 > status) || (buffer_free >= (size_t) status));
83 buffer_free -= status;
84 buffer_used += status;
86 if (buffer[buffer_used - 1] == '\n')
90 if (buffer[buffer_used - 1] != '\n')
96 buffer[buffer_used - 1] = '\0';
98 } /* }}} ssize_t sread */
100 static ssize_t swrite (const void *buf, size_t count) /* {{{ */
106 ptr = (const char *) buf;
111 status = write (sd, (const void *) ptr, nleft);
113 if ((status < 0) && ((errno == EAGAIN) || (errno == EINTR)))
119 rrd_set_error("lost connection to rrdcached");
128 } /* }}} ssize_t swrite */
130 static int buffer_add_string (const char *str, /* {{{ */
131 char **buffer_ret, size_t *buffer_size_ret)
139 buffer = *buffer_ret;
140 buffer_size = *buffer_size_ret;
145 while (buffer_pos < buffer_size)
149 buffer[buffer_pos] = ' ';
154 else if ((str[i] == ' ') || (str[i] == '\\'))
156 if (buffer_pos >= (buffer_size - 1))
158 buffer[buffer_pos] = '\\';
160 buffer[buffer_pos] = str[i];
165 buffer[buffer_pos] = str[i];
169 } /* while (buffer_pos < buffer_size) */
174 *buffer_ret = buffer + buffer_pos;
175 *buffer_size_ret = buffer_size - buffer_pos;
178 } /* }}} int buffer_add_string */
180 static int buffer_add_value (const char *value, /* {{{ */
181 char **buffer_ret, size_t *buffer_size_ret)
185 if (strncmp (value, "N:", 2) == 0)
186 snprintf (temp, sizeof (temp), "%lu:%s",
187 (unsigned long) time (NULL), value + 2);
189 strncpy (temp, value, sizeof (temp));
190 temp[sizeof (temp) - 1] = 0;
192 return (buffer_add_string (temp, buffer_ret, buffer_size_ret));
193 } /* }}} int buffer_add_value */
195 static int response_parse (char *buffer, size_t buffer_size, /* {{{ */
196 rrdc_response_t **ret_response)
198 rrdc_response_t *ret;
208 if (buffer_size <= 0)
211 if (buffer[buffer_size - 1] != 0)
214 ret = (rrdc_response_t *) malloc (sizeof (rrdc_response_t));
217 memset (ret, 0, sizeof (*ret));
223 while ((line_ptr = strtok_r (dummy, "\r\n", &saveptr)) != NULL)
227 if (ret->message == NULL)
229 ret->status = strtol (buffer, &ret->message, 0);
230 if (buffer == ret->message)
236 /* Skip leading whitespace of the status message */
237 ret->message += strspn (ret->message, " \t");
241 ret->lines = (char **) malloc (sizeof (char *) * ret->status);
242 if (ret->lines == NULL)
247 memset (ret->lines, 0, sizeof (char *) * ret->status);
248 ret->lines_num = (size_t) ret->status;
256 else /* if (ret->message != NULL) */
258 if (line_counter < ret->lines_num)
259 ret->lines[line_counter] = line_ptr;
262 } /* while (strtok_r) */
264 if (ret->lines_num != line_counter)
267 if (ret->lines != NULL)
275 } /* }}} int response_parse */
277 static void response_free (rrdc_response_t *res) /* {{{ */
282 if (res->lines != NULL)
290 } /* }}} void response_free */
293 /* determine whether we are connected to the specified daemon_addr if
294 * NULL, return whether we are connected at all
296 int rrdc_is_connected(const char *daemon_addr) /* {{{ */
300 else if (daemon_addr == NULL)
302 /* here we have to handle the case i.e.
303 * UPDATE --daemon ...; UPDATEV (no --daemon) ...
304 * In other words: we have a cached connection,
305 * but it is not specified in the current command.
306 * Daemon is only implied in this case if set in ENV
308 if (getenv(ENV_RRDCACHED_ADDRESS) != NULL)
313 else if (strcmp(daemon_addr, sd_path) == 0)
318 } /* }}} int rrdc_is_connected */
320 static int rrdc_connect_unix (const char *path) /* {{{ */
322 struct sockaddr_un sa;
325 assert (path != NULL);
328 sd = socket (PF_UNIX, SOCK_STREAM, /* protocol = */ 0);
335 memset (&sa, 0, sizeof (sa));
336 sa.sun_family = AF_UNIX;
337 strncpy (sa.sun_path, path, sizeof (sa.sun_path) - 1);
339 status = connect (sd, (struct sockaddr *) &sa, sizeof (sa));
347 } /* }}} int rrdc_connect_unix */
349 static int rrdc_connect_network (const char *addr) /* {{{ */
351 struct addrinfo ai_hints;
352 struct addrinfo *ai_res;
353 struct addrinfo *ai_ptr;
355 assert (addr != NULL);
359 memset (&ai_hints, 0, sizeof (ai_hints));
360 ai_hints.ai_flags = 0;
362 ai_hints.ai_flags |= AI_ADDRCONFIG;
364 ai_hints.ai_family = AF_UNSPEC;
365 ai_hints.ai_socktype = SOCK_STREAM;
368 status = getaddrinfo (addr, RRDCACHED_DEFAULT_PORT, &ai_hints, &ai_res);
372 for (ai_ptr = ai_res; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
374 sd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
382 status = connect (sd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
390 assert (status == 0);
395 } /* }}} int rrdc_connect_network */
397 int rrdc_connect (const char *addr) /* {{{ */
402 addr = getenv (ENV_RRDCACHED_ADDRESS);
407 pthread_mutex_lock(&lock);
409 if (sd >= 0 && sd_path != NULL && strcmp(addr, sd_path) == 0)
411 /* connection to the same daemon; use cached connection */
412 pthread_mutex_unlock (&lock);
420 if (strncmp ("unix:", addr, strlen ("unix:")) == 0)
421 status = rrdc_connect_unix (addr + strlen ("unix:"));
422 else if (addr[0] == '/')
423 status = rrdc_connect_unix (addr);
425 status = rrdc_connect_network(addr);
427 if (status == 0 && sd >= 0)
428 sd_path = strdup(addr);
430 rrd_set_error("Unable to connect to rrdcached: %s",
433 : rrd_strerror (status));
435 pthread_mutex_unlock (&lock);
437 } /* }}} int rrdc_connect */
439 static void _disconnect(void) /* {{{ */
449 } /* }}} static void _disconnect(void) */
451 int rrdc_disconnect (void) /* {{{ */
453 pthread_mutex_lock (&lock);
457 pthread_mutex_unlock (&lock);
460 } /* }}} int rrdc_disconnect */
462 int rrdc_update (const char *filename, int values_num, /* {{{ */
463 const char * const *values)
472 memset (buffer, 0, sizeof (buffer));
473 buffer_ptr = &buffer[0];
474 buffer_free = sizeof (buffer);
476 status = buffer_add_string ("update", &buffer_ptr, &buffer_free);
480 status = buffer_add_string (filename, &buffer_ptr, &buffer_free);
484 for (i = 0; i < values_num; i++)
486 status = buffer_add_value (values[i], &buffer_ptr, &buffer_free);
491 assert (buffer_free < sizeof (buffer));
492 buffer_size = sizeof (buffer) - buffer_free;
493 assert (buffer[buffer_size - 1] == ' ');
494 buffer[buffer_size - 1] = '\n';
496 pthread_mutex_lock (&lock);
500 pthread_mutex_unlock (&lock);
504 status = swrite (buffer, buffer_size);
507 pthread_mutex_unlock (&lock);
511 status = sread (buffer, sizeof (buffer));
515 pthread_mutex_unlock (&lock);
518 else if (status == 0)
520 pthread_mutex_unlock (&lock);
524 pthread_mutex_unlock (&lock);
526 status = atoi (buffer);
528 } /* }}} int rrdc_update */
530 int rrdc_flush (const char *filename) /* {{{ */
538 if (filename == NULL)
541 memset (buffer, 0, sizeof (buffer));
542 buffer_ptr = &buffer[0];
543 buffer_free = sizeof (buffer);
545 status = buffer_add_string ("flush", &buffer_ptr, &buffer_free);
549 status = buffer_add_string (filename, &buffer_ptr, &buffer_free);
553 assert (buffer_free < sizeof (buffer));
554 buffer_size = sizeof (buffer) - buffer_free;
555 assert (buffer[buffer_size - 1] == ' ');
556 buffer[buffer_size - 1] = '\n';
558 pthread_mutex_lock (&lock);
562 pthread_mutex_unlock (&lock);
566 status = swrite (buffer, buffer_size);
569 pthread_mutex_unlock (&lock);
573 status = sread (buffer, sizeof (buffer));
577 pthread_mutex_unlock (&lock);
580 else if (status == 0)
582 pthread_mutex_unlock (&lock);
586 pthread_mutex_unlock (&lock);
588 status = atoi (buffer);
590 } /* }}} int rrdc_flush */
594 /* convenience function; if there is a daemon specified, or if we can
595 * detect one from the environment, then flush the file. Otherwise, no-op
597 int rrdc_flush_if_daemon (const char *opt_daemon, const char *filename) /* {{{ */
601 rrdc_connect(opt_daemon);
603 if (rrdc_is_connected(opt_daemon))
605 status = rrdc_flush (filename);
608 rrd_set_error ("rrdc_flush (%s) failed with status %i.",
611 } /* if (daemon_addr) */
614 } /* }}} int rrdc_flush_if_daemon */
617 int rrdc_stats_get (rrdc_stats_t **ret_stats) /* {{{ */
622 rrdc_response_t *response;
629 pthread_mutex_lock (&lock);
633 pthread_mutex_unlock (&lock);
637 /* Protocol example: {{{
639 * <- 5 Statistics follow
641 * <- UpdatesWritten: 0
642 * <- DataSetsWritten: 0
643 * <- TreeNodesNumber: 0
646 status = swrite ("STATS\n", strlen ("STATS\n"));
649 pthread_mutex_unlock (&lock);
653 status = sread (buffer, sizeof (buffer));
657 pthread_mutex_unlock (&lock);
660 else if (status == 0)
662 pthread_mutex_unlock (&lock);
666 pthread_mutex_unlock (&lock);
668 /* Assert NULL termination */
669 buffer_size = (size_t) status;
670 if (buffer[buffer_size - 1] != 0)
672 if (buffer_size < sizeof (buffer))
674 buffer[buffer_size] = 0;
683 status = response_parse (buffer, buffer_size, &response);
687 if (response->status <= 0)
689 response_free (response);
695 for (i = 0; i < response->lines_num; i++)
702 key = response->lines[i];
703 value = strchr (key, ':');
709 while ((value[0] == ' ') || (value[0] == '\t'))
712 s = (rrdc_stats_t *) malloc (sizeof (rrdc_stats_t));
715 memset (s, 0, sizeof (*s));
717 s->name = strdup (key);
720 if ((strcmp ("QueueLength", key) == 0)
721 || (strcmp ("TreeNodesNumber", key) == 0)
722 || (strcmp ("TreeDepth", key) == 0))
724 s->type = RRDC_STATS_TYPE_GAUGE;
725 s->value.gauge = strtod (value, &endptr);
727 else if ((strcmp ("UpdatesWritten", key) == 0)
728 || (strcmp ("DataSetsWritten", key) == 0))
730 s->type = RRDC_STATS_TYPE_COUNTER;
731 s->value.counter = (uint64_t) strtoll (value, &endptr, /* base = */ 0);
739 /* Conversion failed */
757 } /* for (i = 0; i < response->lines_num; i++) */
759 response_free (response);
766 } /* }}} int rrdc_stats_get */
768 void rrdc_stats_free (rrdc_stats_t *ret_stats) /* {{{ */
779 if (this->name != NULL)
787 } /* while (this != NULL) */
788 } /* }}} void rrdc_stats_free */
791 * vim: set sw=2 sts=2 ts=8 et fdm=marker :