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>
20 * Sebastian tokkee Harl <sh at tokkee.org>
24 #include "rrd_client.h"
32 #include <sys/types.h>
33 #include <sys/socket.h>
38 #define ENODATA ENOENT
41 struct rrdc_response_s
48 typedef struct rrdc_response_s rrdc_response_t;
50 static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
52 static FILE *sh = NULL;
53 static char *sd_path = NULL; /* cache the path for sd */
55 /* get_path: Return a path name appropriate to be sent to the daemon.
57 * When talking to a local daemon (thru a UNIX socket), relative path names
58 * are resolved to absolute path names to allow for transparent integration
59 * into existing solutions (as requested by Tobi). Else, absolute path names
60 * are not allowed, since path name translation is done by the server.
62 * One must hold `lock' when calling this function. */
63 static const char *get_path (const char *path, char *resolved_path) /* {{{ */
65 const char *ret = path;
69 || (strncmp ("unix:", sd_path, strlen ("unix:")) == 0))
72 if (*path == '/') /* absolute path */
76 rrd_set_error ("absolute path names not allowed when talking "
77 "to a remote daemon");
80 /* else: nothing to do */
82 else /* relative path */
86 realpath (path, resolved_path);
89 /* else: nothing to do */
92 } /* }}} char *get_path */
94 /* One must hold `lock' when calling `close_connection'. */
95 static void close_connection (void) /* {{{ */
112 } /* }}} void close_connection */
114 static int buffer_add_string (const char *str, /* {{{ */
115 char **buffer_ret, size_t *buffer_size_ret)
123 buffer = *buffer_ret;
124 buffer_size = *buffer_size_ret;
129 while (buffer_pos < buffer_size)
133 buffer[buffer_pos] = ' ';
138 else if ((str[i] == ' ') || (str[i] == '\\'))
140 if (buffer_pos >= (buffer_size - 1))
142 buffer[buffer_pos] = '\\';
144 buffer[buffer_pos] = str[i];
149 buffer[buffer_pos] = str[i];
153 } /* while (buffer_pos < buffer_size) */
158 *buffer_ret = buffer + buffer_pos;
159 *buffer_size_ret = buffer_size - buffer_pos;
162 } /* }}} int buffer_add_string */
164 static int buffer_add_value (const char *value, /* {{{ */
165 char **buffer_ret, size_t *buffer_size_ret)
169 if (strncmp (value, "N:", 2) == 0)
170 snprintf (temp, sizeof (temp), "%lu:%s",
171 (unsigned long) time (NULL), value + 2);
173 strncpy (temp, value, sizeof (temp));
174 temp[sizeof (temp) - 1] = 0;
176 return (buffer_add_string (temp, buffer_ret, buffer_size_ret));
177 } /* }}} int buffer_add_value */
179 /* Remove trailing newline (NL) and carriage return (CR) characters. Similar to
180 * the Perl function `chomp'. Returns the number of characters that have been
182 static int chomp (char *str) /* {{{ */
192 while ((len > 0) && ((str[len - 1] == '\n') || (str[len - 1] == '\r')))
200 } /* }}} int chomp */
202 static void response_free (rrdc_response_t *res) /* {{{ */
207 if (res->lines != NULL)
211 for (i = 0; i < res->lines_num; i++)
212 if (res->lines[i] != NULL)
213 free (res->lines[i]);
218 } /* }}} void response_free */
220 static int response_read (rrdc_response_t **ret_response) /* {{{ */
222 rrdc_response_t *ret;
232 ret = (rrdc_response_t *) malloc (sizeof (rrdc_response_t));
235 memset (ret, 0, sizeof (*ret));
239 buffer_ptr = fgets (buffer, sizeof (buffer), sh);
240 if (buffer_ptr == NULL)
244 ret->status = strtol (buffer, &ret->message, 0);
245 if (buffer == ret->message)
250 /* Skip leading whitespace of the status message */
251 ret->message += strspn (ret->message, " \t");
253 if (ret->status <= 0)
256 rrd_set_error("rrdcached: %s", ret->message);
261 ret->lines = (char **) malloc (sizeof (char *) * ret->status);
262 if (ret->lines == NULL)
267 memset (ret->lines, 0, sizeof (char *) * ret->status);
268 ret->lines_num = (size_t) ret->status;
270 for (i = 0; i < ret->lines_num; i++)
272 buffer_ptr = fgets (buffer, sizeof (buffer), sh);
273 if (buffer_ptr == NULL)
280 ret->lines[i] = strdup (buffer);
281 if (ret->lines[i] == NULL)
290 } /* }}} rrdc_response_t *response_read */
292 static int request (const char *buffer, size_t buffer_size, /* {{{ */
293 rrdc_response_t **ret_response)
296 rrdc_response_t *res;
301 status = (int) fwrite (buffer, buffer_size, /* nmemb = */ 1, sh);
305 rrd_set_error("request: socket error (%d) while talking to rrdcached",
312 status = response_read (&res);
317 rrd_set_error("request: internal error while talking to rrdcached");
323 } /* }}} int request */
325 /* determine whether we are connected to the specified daemon_addr if
326 * NULL, return whether we are connected at all
328 int rrdc_is_connected(const char *daemon_addr) /* {{{ */
332 else if (daemon_addr == NULL)
334 /* here we have to handle the case i.e.
335 * UPDATE --daemon ...; UPDATEV (no --daemon) ...
336 * In other words: we have a cached connection,
337 * but it is not specified in the current command.
338 * Daemon is only implied in this case if set in ENV
340 if (getenv(ENV_RRDCACHED_ADDRESS) != NULL)
345 else if (strcmp(daemon_addr, sd_path) == 0)
350 } /* }}} int rrdc_is_connected */
352 static int rrdc_connect_unix (const char *path) /* {{{ */
354 struct sockaddr_un sa;
357 assert (path != NULL);
360 sd = socket (PF_UNIX, SOCK_STREAM, /* protocol = */ 0);
367 memset (&sa, 0, sizeof (sa));
368 sa.sun_family = AF_UNIX;
369 strncpy (sa.sun_path, path, sizeof (sa.sun_path) - 1);
371 status = connect (sd, (struct sockaddr *) &sa, sizeof (sa));
379 sh = fdopen (sd, "r+");
388 } /* }}} int rrdc_connect_unix */
390 static int rrdc_connect_network (const char *addr_orig) /* {{{ */
392 struct addrinfo ai_hints;
393 struct addrinfo *ai_res;
394 struct addrinfo *ai_ptr;
395 char addr_copy[NI_MAXHOST];
399 assert (addr_orig != NULL);
402 strncpy(addr_copy, addr_orig, sizeof(addr_copy));
403 addr_copy[sizeof(addr_copy) - 1] = '\0';
407 memset (&ai_hints, 0, sizeof (ai_hints));
408 ai_hints.ai_flags = 0;
410 ai_hints.ai_flags |= AI_ADDRCONFIG;
412 ai_hints.ai_family = AF_UNSPEC;
413 ai_hints.ai_socktype = SOCK_STREAM;
416 if (*addr == '[') /* IPv6+port format */
418 /* `addr' is something like "[2001:780:104:2:211:24ff:feab:26f8]:12345" */
421 port = strchr (addr, ']');
424 rrd_set_error("malformed address: %s", addr_orig);
436 rrd_set_error("garbage after address: %s", port);
439 } /* if (*addr = ']') */
440 else if (strchr (addr, '.') != NULL) /* Hostname or IPv4 */
442 port = rindex(addr, ':');
451 status = getaddrinfo (addr,
452 port == NULL ? RRDCACHED_DEFAULT_PORT : port,
457 for (ai_ptr = ai_res; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
459 sd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
467 status = connect (sd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
475 sh = fdopen (sd, "r+");
483 assert (status == 0);
488 } /* }}} int rrdc_connect_network */
490 int rrdc_connect (const char *addr) /* {{{ */
495 addr = getenv (ENV_RRDCACHED_ADDRESS);
500 pthread_mutex_lock(&lock);
502 if (sd >= 0 && sd_path != NULL && strcmp(addr, sd_path) == 0)
504 /* connection to the same daemon; use cached connection */
505 pthread_mutex_unlock (&lock);
513 if (strncmp ("unix:", addr, strlen ("unix:")) == 0)
514 status = rrdc_connect_unix (addr + strlen ("unix:"));
515 else if (addr[0] == '/')
516 status = rrdc_connect_unix (addr);
518 status = rrdc_connect_network(addr);
520 if (status == 0 && sd >= 0)
521 sd_path = strdup(addr);
523 rrd_set_error("Unable to connect to rrdcached: %s",
526 : rrd_strerror (status));
528 pthread_mutex_unlock (&lock);
530 } /* }}} int rrdc_connect */
532 int rrdc_disconnect (void) /* {{{ */
534 pthread_mutex_lock (&lock);
538 pthread_mutex_unlock (&lock);
541 } /* }}} int rrdc_disconnect */
543 int rrdc_update (const char *filename, int values_num, /* {{{ */
544 const char * const *values)
550 rrdc_response_t *res;
553 char file_path[PATH_MAX];
555 memset (buffer, 0, sizeof (buffer));
556 buffer_ptr = &buffer[0];
557 buffer_free = sizeof (buffer);
559 status = buffer_add_string ("update", &buffer_ptr, &buffer_free);
563 pthread_mutex_lock (&lock);
564 filename = get_path (filename, file_path);
565 if (filename == NULL)
567 pthread_mutex_unlock (&lock);
571 status = buffer_add_string (filename, &buffer_ptr, &buffer_free);
574 pthread_mutex_unlock (&lock);
578 for (i = 0; i < values_num; i++)
580 status = buffer_add_value (values[i], &buffer_ptr, &buffer_free);
583 pthread_mutex_unlock (&lock);
588 assert (buffer_free < sizeof (buffer));
589 buffer_size = sizeof (buffer) - buffer_free;
590 assert (buffer[buffer_size - 1] == ' ');
591 buffer[buffer_size - 1] = '\n';
594 status = request (buffer, buffer_size, &res);
595 pthread_mutex_unlock (&lock);
600 status = res->status;
604 } /* }}} int rrdc_update */
606 int rrdc_flush (const char *filename) /* {{{ */
612 rrdc_response_t *res;
614 char file_path[PATH_MAX];
616 if (filename == NULL)
619 memset (buffer, 0, sizeof (buffer));
620 buffer_ptr = &buffer[0];
621 buffer_free = sizeof (buffer);
623 status = buffer_add_string ("flush", &buffer_ptr, &buffer_free);
627 pthread_mutex_lock (&lock);
628 filename = get_path (filename, file_path);
629 if (filename == NULL)
631 pthread_mutex_unlock (&lock);
635 status = buffer_add_string (filename, &buffer_ptr, &buffer_free);
638 pthread_mutex_unlock (&lock);
642 assert (buffer_free < sizeof (buffer));
643 buffer_size = sizeof (buffer) - buffer_free;
644 assert (buffer[buffer_size - 1] == ' ');
645 buffer[buffer_size - 1] = '\n';
648 status = request (buffer, buffer_size, &res);
649 pthread_mutex_unlock (&lock);
654 status = res->status;
658 } /* }}} int rrdc_flush */
661 /* convenience function; if there is a daemon specified, or if we can
662 * detect one from the environment, then flush the file. Otherwise, no-op
664 int rrdc_flush_if_daemon (const char *opt_daemon, const char *filename) /* {{{ */
668 rrdc_connect(opt_daemon);
670 if (rrdc_is_connected(opt_daemon))
673 status = rrdc_flush (filename);
675 if (status != 0 && !rrd_test_error())
679 rrd_set_error("rrdc_flush (%s) failed: %s",
680 filename, rrd_strerror(status));
684 rrd_set_error("rrdc_flush (%s) failed with status %i.",
688 } /* if (rrdc_is_connected(..)) */
691 } /* }}} int rrdc_flush_if_daemon */
694 int rrdc_stats_get (rrdc_stats_t **ret_stats) /* {{{ */
699 rrdc_response_t *res;
704 /* Protocol example: {{{
706 * <- 5 Statistics follow
708 * <- UpdatesWritten: 0
709 * <- DataSetsWritten: 0
710 * <- TreeNodesNumber: 0
715 pthread_mutex_lock (&lock);
716 status = request ("STATS\n", strlen ("STATS\n"), &res);
717 pthread_mutex_unlock (&lock);
722 if (res->status <= 0)
730 for (i = 0; i < res->lines_num; i++)
738 value = strchr (key, ':');
744 while ((value[0] == ' ') || (value[0] == '\t'))
747 s = (rrdc_stats_t *) malloc (sizeof (rrdc_stats_t));
750 memset (s, 0, sizeof (*s));
752 s->name = strdup (key);
755 if ((strcmp ("QueueLength", key) == 0)
756 || (strcmp ("TreeDepth", key) == 0)
757 || (strcmp ("TreeNodesNumber", key) == 0))
759 s->type = RRDC_STATS_TYPE_GAUGE;
760 s->value.gauge = strtod (value, &endptr);
762 else if ((strcmp ("DataSetsWritten", key) == 0)
763 || (strcmp ("FlushesReceived", key) == 0)
764 || (strcmp ("JournalBytes", key) == 0)
765 || (strcmp ("JournalRotate", key) == 0)
766 || (strcmp ("UpdatesReceived", key) == 0)
767 || (strcmp ("UpdatesWritten", key) == 0))
769 s->type = RRDC_STATS_TYPE_COUNTER;
770 s->value.counter = (uint64_t) strtoll (value, &endptr, /* base = */ 0);
778 /* Conversion failed */
796 } /* for (i = 0; i < res->lines_num; i++) */
805 } /* }}} int rrdc_stats_get */
807 void rrdc_stats_free (rrdc_stats_t *ret_stats) /* {{{ */
818 if (this->name != NULL)
820 free ((char *)this->name);
826 } /* while (this != NULL) */
827 } /* }}} void rrdc_stats_free */
830 * vim: set sw=2 sts=2 ts=8 et fdm=marker :