1 /**
2 * RRDTool - src/rrd_client.c
3 * Copyright (C) 2008 Florian octo Forster
4 *
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.
8 *
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.
13 *
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
17 *
18 * Authors:
19 * Florian octo Forster <octo at verplant.org>
20 **/
22 #include "rrd.h"
23 #include "rrd_client.h"
24 #include "rrd_tool.h"
26 #include <stdlib.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <assert.h>
30 #include <pthread.h>
31 #include <sys/types.h>
32 #include <sys/socket.h>
33 #include <sys/un.h>
34 #include <netdb.h>
36 #ifndef ENODATA
37 #define ENODATA ENOENT
38 #endif
40 struct rrdc_response_s
41 {
42 int status;
43 char *message;
44 char **lines;
45 size_t lines_num;
46 };
47 typedef struct rrdc_response_s rrdc_response_t;
49 static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
50 static int sd = -1;
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) /* {{{ */
55 {
56 char *buffer;
57 size_t buffer_used;
58 size_t buffer_free;
59 ssize_t status;
61 buffer = (char *) buffer_void;
62 buffer_used = 0;
63 buffer_free = buffer_size;
65 while (buffer_free > 0)
66 {
67 status = read (sd, buffer + buffer_used, buffer_free);
68 if ((status < 0) && ((errno == EAGAIN) || (errno == EINTR)))
69 continue;
71 if (status < 0)
72 return (-1);
74 if (status == 0)
75 {
76 _disconnect();
77 errno = EPROTO;
78 return (-1);
79 }
81 assert ((0 > status) || (buffer_free >= (size_t) status));
83 buffer_free -= status;
84 buffer_used += status;
86 if (buffer[buffer_used - 1] == '\n')
87 break;
88 }
90 if (buffer[buffer_used - 1] != '\n')
91 {
92 errno = ENOBUFS;
93 return (-1);
94 }
96 buffer[buffer_used - 1] = '\0';
97 return (buffer_used);
98 } /* }}} ssize_t sread */
100 static ssize_t swrite (const void *buf, size_t count) /* {{{ */
101 {
102 const char *ptr;
103 size_t nleft;
104 ssize_t status;
106 ptr = (const char *) buf;
107 nleft = count;
109 while (nleft > 0)
110 {
111 status = write (sd, (const void *) ptr, nleft);
113 if ((status < 0) && ((errno == EAGAIN) || (errno == EINTR)))
114 continue;
116 if (status < 0)
117 {
118 _disconnect();
119 rrd_set_error("lost connection to rrdcached");
120 return (status);
121 }
123 nleft -= status;
124 ptr += status;
125 }
127 return (0);
128 } /* }}} ssize_t swrite */
130 static int buffer_add_string (const char *str, /* {{{ */
131 char **buffer_ret, size_t *buffer_size_ret)
132 {
133 char *buffer;
134 size_t buffer_size;
135 size_t buffer_pos;
136 size_t i;
137 int status;
139 buffer = *buffer_ret;
140 buffer_size = *buffer_size_ret;
141 buffer_pos = 0;
143 i = 0;
144 status = -1;
145 while (buffer_pos < buffer_size)
146 {
147 if (str[i] == 0)
148 {
149 buffer[buffer_pos] = ' ';
150 buffer_pos++;
151 status = 0;
152 break;
153 }
154 else if ((str[i] == ' ') || (str[i] == '\\'))
155 {
156 if (buffer_pos >= (buffer_size - 1))
157 break;
158 buffer[buffer_pos] = '\\';
159 buffer_pos++;
160 buffer[buffer_pos] = str[i];
161 buffer_pos++;
162 }
163 else
164 {
165 buffer[buffer_pos] = str[i];
166 buffer_pos++;
167 }
168 i++;
169 } /* while (buffer_pos < buffer_size) */
171 if (status != 0)
172 return (-1);
174 *buffer_ret = buffer + buffer_pos;
175 *buffer_size_ret = buffer_size - buffer_pos;
177 return (0);
178 } /* }}} int buffer_add_string */
180 static int buffer_add_value (const char *value, /* {{{ */
181 char **buffer_ret, size_t *buffer_size_ret)
182 {
183 char temp[4096];
185 if (strncmp (value, "N:", 2) == 0)
186 snprintf (temp, sizeof (temp), "%lu:%s",
187 (unsigned long) time (NULL), value + 2);
188 else
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)
197 {
198 rrdc_response_t *ret;
200 char *dummy;
201 char *saveptr;
203 char *line_ptr;
204 size_t line_counter;
206 if (buffer == NULL)
207 return (EINVAL);
208 if (buffer_size <= 0)
209 return (EINVAL);
211 if (buffer[buffer_size - 1] != 0)
212 return (-1);
214 ret = (rrdc_response_t *) malloc (sizeof (rrdc_response_t));
215 if (ret == NULL)
216 return (ENOMEM);
217 memset (ret, 0, sizeof (*ret));
219 line_counter = 0;
221 dummy = buffer;
222 saveptr = NULL;
223 while ((line_ptr = strtok_r (dummy, "\r\n", &saveptr)) != NULL)
224 {
225 dummy = NULL;
227 if (ret->message == NULL)
228 {
229 ret->status = strtol (buffer, &ret->message, 0);
230 if (buffer == ret->message)
231 {
232 free (ret);
233 return (EPROTO);
234 }
236 /* Skip leading whitespace of the status message */
237 ret->message += strspn (ret->message, " \t");
239 if (ret->status > 0)
240 {
241 ret->lines = (char **) malloc (sizeof (char *) * ret->status);
242 if (ret->lines == NULL)
243 {
244 free (ret);
245 return (ENOMEM);
246 }
247 memset (ret->lines, 0, sizeof (char *) * ret->status);
248 ret->lines_num = (size_t) ret->status;
249 }
250 else
251 {
252 ret->lines = NULL;
253 ret->lines_num = 0;
254 }
255 }
256 else /* if (ret->message != NULL) */
257 {
258 if (line_counter < ret->lines_num)
259 ret->lines[line_counter] = line_ptr;
260 line_counter++;
261 }
262 } /* while (strtok_r) */
264 if (ret->lines_num != line_counter)
265 {
266 errno = EPROTO;
267 if (ret->lines != NULL)
268 free (ret->lines);
269 free (ret);
270 return (-1);
271 }
273 *ret_response = ret;
274 return (0);
275 } /* }}} int response_parse */
277 static void response_free (rrdc_response_t *res) /* {{{ */
278 {
279 if (res == NULL)
280 return;
282 if (res->lines != NULL)
283 {
284 res->lines_num = 0;
285 free (res->lines);
286 res->lines = NULL;
287 }
289 free (res);
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
295 */
296 int rrdc_is_connected(const char *daemon_addr) /* {{{ */
297 {
298 if (sd < 0)
299 return 0;
300 else if (daemon_addr == NULL)
301 {
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
307 */
308 if (getenv(ENV_RRDCACHED_ADDRESS) != NULL)
309 return 1;
310 else
311 return 0;
312 }
313 else if (strcmp(daemon_addr, sd_path) == 0)
314 return 1;
315 else
316 return 0;
318 } /* }}} int rrdc_is_connected */
320 static int rrdc_connect_unix (const char *path) /* {{{ */
321 {
322 struct sockaddr_un sa;
323 int status;
325 assert (path != NULL);
326 assert (sd == -1);
328 sd = socket (PF_UNIX, SOCK_STREAM, /* protocol = */ 0);
329 if (sd < 0)
330 {
331 status = errno;
332 return (status);
333 }
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));
340 if (status != 0)
341 {
342 status = errno;
343 return (status);
344 }
346 return (0);
347 } /* }}} int rrdc_connect_unix */
349 static int rrdc_connect_network (const char *addr) /* {{{ */
350 {
351 struct addrinfo ai_hints;
352 struct addrinfo *ai_res;
353 struct addrinfo *ai_ptr;
355 assert (addr != NULL);
356 assert (sd == -1);
358 int status;
359 memset (&ai_hints, 0, sizeof (ai_hints));
360 ai_hints.ai_flags = 0;
361 #ifdef AI_ADDRCONFIG
362 ai_hints.ai_flags |= AI_ADDRCONFIG;
363 #endif
364 ai_hints.ai_family = AF_UNSPEC;
365 ai_hints.ai_socktype = SOCK_STREAM;
367 ai_res = NULL;
368 status = getaddrinfo (addr, RRDCACHED_DEFAULT_PORT, &ai_hints, &ai_res);
369 if (status != 0)
370 return (status);
372 for (ai_ptr = ai_res; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
373 {
374 sd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
375 if (sd < 0)
376 {
377 status = errno;
378 sd = -1;
379 continue;
380 }
382 status = connect (sd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
383 if (status != 0)
384 {
385 status = errno;
386 _disconnect();
387 continue;
388 }
390 assert (status == 0);
391 break;
392 } /* for (ai_ptr) */
394 return (status);
395 } /* }}} int rrdc_connect_network */
397 int rrdc_connect (const char *addr) /* {{{ */
398 {
399 int status = 0;
401 if (addr == NULL)
402 addr = getenv (ENV_RRDCACHED_ADDRESS);
404 if (addr == NULL)
405 return 0;
407 pthread_mutex_lock(&lock);
409 if (sd >= 0 && sd_path != NULL && strcmp(addr, sd_path) == 0)
410 {
411 /* connection to the same daemon; use cached connection */
412 pthread_mutex_unlock (&lock);
413 return (0);
414 }
415 else
416 {
417 _disconnect();
418 }
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);
424 else
425 status = rrdc_connect_network(addr);
427 if (status == 0 && sd >= 0)
428 sd_path = strdup(addr);
429 else
430 rrd_set_error("Unable to connect to rrdcached: %s",
431 (status < 0)
432 ? "Internal error"
433 : rrd_strerror (status));
435 pthread_mutex_unlock (&lock);
436 return (status);
437 } /* }}} int rrdc_connect */
439 static void _disconnect(void) /* {{{ */
440 {
441 if (sd >= 0)
442 close(sd);
444 if (sd_path != NULL)
445 free(sd_path);
447 sd = -1;
448 sd_path = NULL;
449 } /* }}} static void _disconnect(void) */
451 int rrdc_disconnect (void) /* {{{ */
452 {
453 pthread_mutex_lock (&lock);
455 _disconnect();
457 pthread_mutex_unlock (&lock);
459 return (0);
460 } /* }}} int rrdc_disconnect */
462 int rrdc_update (const char *filename, int values_num, /* {{{ */
463 const char * const *values)
464 {
465 char buffer[4096];
466 char *buffer_ptr;
467 size_t buffer_free;
468 size_t buffer_size;
469 int status;
470 int i;
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);
477 if (status != 0)
478 return (ENOBUFS);
480 status = buffer_add_string (filename, &buffer_ptr, &buffer_free);
481 if (status != 0)
482 return (ENOBUFS);
484 for (i = 0; i < values_num; i++)
485 {
486 status = buffer_add_value (values[i], &buffer_ptr, &buffer_free);
487 if (status != 0)
488 return (ENOBUFS);
489 }
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);
498 if (sd < 0)
499 {
500 pthread_mutex_unlock (&lock);
501 return (ENOTCONN);
502 }
504 status = swrite (buffer, buffer_size);
505 if (status != 0)
506 {
507 pthread_mutex_unlock (&lock);
508 return (status);
509 }
511 status = sread (buffer, sizeof (buffer));
512 if (status < 0)
513 {
514 status = errno;
515 pthread_mutex_unlock (&lock);
516 return (status);
517 }
518 else if (status == 0)
519 {
520 pthread_mutex_unlock (&lock);
521 return (ENODATA);
522 }
524 pthread_mutex_unlock (&lock);
526 status = atoi (buffer);
527 return (status);
528 } /* }}} int rrdc_update */
530 int rrdc_flush (const char *filename) /* {{{ */
531 {
532 char buffer[4096];
533 char *buffer_ptr;
534 size_t buffer_free;
535 size_t buffer_size;
536 int status;
538 if (filename == NULL)
539 return (-1);
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);
546 if (status != 0)
547 return (ENOBUFS);
549 status = buffer_add_string (filename, &buffer_ptr, &buffer_free);
550 if (status != 0)
551 return (ENOBUFS);
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);
560 if (sd < 0)
561 {
562 pthread_mutex_unlock (&lock);
563 return (ENOTCONN);
564 }
566 status = swrite (buffer, buffer_size);
567 if (status != 0)
568 {
569 pthread_mutex_unlock (&lock);
570 return (status);
571 }
573 status = sread (buffer, sizeof (buffer));
574 if (status < 0)
575 {
576 status = errno;
577 pthread_mutex_unlock (&lock);
578 return (status);
579 }
580 else if (status == 0)
581 {
582 pthread_mutex_unlock (&lock);
583 return (ENODATA);
584 }
586 pthread_mutex_unlock (&lock);
588 status = atoi (buffer);
589 return (status);
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
596 */
597 int rrdc_flush_if_daemon (const char *opt_daemon, const char *filename) /* {{{ */
598 {
599 int status = 0;
601 rrdc_connect(opt_daemon);
603 if (rrdc_is_connected(opt_daemon))
604 {
605 status = rrdc_flush (filename);
606 if (status != 0)
607 {
608 rrd_set_error ("rrdc_flush (%s) failed with status %i.",
609 filename, status);
610 }
611 } /* if (daemon_addr) */
613 return status;
614 } /* }}} int rrdc_flush_if_daemon */
617 int rrdc_stats_get (rrdc_stats_t **ret_stats) /* {{{ */
618 {
619 rrdc_stats_t *head;
620 rrdc_stats_t *tail;
622 rrdc_response_t *response;
624 char buffer[4096];
625 size_t buffer_size;
626 int status;
627 size_t i;
629 pthread_mutex_lock (&lock);
631 if (sd < 0)
632 {
633 pthread_mutex_unlock (&lock);
634 return (ENOTCONN);
635 }
637 /* Protocol example: {{{
638 * -> STATS
639 * <- 5 Statistics follow
640 * <- QueueLength: 0
641 * <- UpdatesWritten: 0
642 * <- DataSetsWritten: 0
643 * <- TreeNodesNumber: 0
644 * <- TreeDepth: 0
645 * }}} */
646 status = swrite ("STATS\n", strlen ("STATS\n"));
647 if (status != 0)
648 {
649 pthread_mutex_unlock (&lock);
650 return (status);
651 }
653 status = sread (buffer, sizeof (buffer));
654 if (status < 0)
655 {
656 status = errno;
657 pthread_mutex_unlock (&lock);
658 return (status);
659 }
660 else if (status == 0)
661 {
662 pthread_mutex_unlock (&lock);
663 return (ENODATA);
664 }
666 pthread_mutex_unlock (&lock);
668 /* Assert NULL termination */
669 buffer_size = (size_t) status;
670 if (buffer[buffer_size - 1] != 0)
671 {
672 if (buffer_size < sizeof (buffer))
673 {
674 buffer[buffer_size] = 0;
675 buffer_size++;
676 }
677 else
678 {
679 return (ENOBUFS);
680 }
681 }
683 status = response_parse (buffer, buffer_size, &response);
684 if (status != 0)
685 return (status);
687 if (response->status <= 0)
688 {
689 response_free (response);
690 return (EIO);
691 }
693 head = NULL;
694 tail = NULL;
695 for (i = 0; i < response->lines_num; i++)
696 {
697 char *key;
698 char *value;
699 char *endptr;
700 rrdc_stats_t *s;
702 key = response->lines[i];
703 value = strchr (key, ':');
704 if (value == NULL)
705 continue;
706 *value = 0;
707 value++;
709 while ((value[0] == ' ') || (value[0] == '\t'))
710 value++;
712 s = (rrdc_stats_t *) malloc (sizeof (rrdc_stats_t));
713 if (s == NULL)
714 continue;
715 memset (s, 0, sizeof (*s));
717 s->name = strdup (key);
719 endptr = NULL;
720 if ((strcmp ("QueueLength", key) == 0)
721 || (strcmp ("TreeNodesNumber", key) == 0)
722 || (strcmp ("TreeDepth", key) == 0))
723 {
724 s->type = RRDC_STATS_TYPE_GAUGE;
725 s->value.gauge = strtod (value, &endptr);
726 }
727 else if ((strcmp ("UpdatesWritten", key) == 0)
728 || (strcmp ("DataSetsWritten", key) == 0))
729 {
730 s->type = RRDC_STATS_TYPE_COUNTER;
731 s->value.counter = (uint64_t) strtoll (value, &endptr, /* base = */ 0);
732 }
733 else
734 {
735 free (s);
736 continue;
737 }
739 /* Conversion failed */
740 if (endptr == value)
741 {
742 free (s);
743 continue;
744 }
746 if (head == NULL)
747 {
748 head = s;
749 tail = s;
750 s->next = NULL;
751 }
752 else
753 {
754 tail->next = s;
755 tail = s;
756 }
757 } /* for (i = 0; i < response->lines_num; i++) */
759 response_free (response);
761 if (head == NULL)
762 return (EPROTO);
764 *ret_stats = head;
765 return (0);
766 } /* }}} int rrdc_stats_get */
768 void rrdc_stats_free (rrdc_stats_t *ret_stats) /* {{{ */
769 {
770 rrdc_stats_t *this;
772 this = ret_stats;
773 while (this != NULL)
774 {
775 rrdc_stats_t *next;
777 next = this->next;
779 if (this->name != NULL)
780 {
781 free (this->name);
782 this->name = NULL;
783 }
784 free (this);
786 this = next;
787 } /* while (this != NULL) */
788 } /* }}} void rrdc_stats_free */
790 /*
791 * vim: set sw=2 sts=2 ts=8 et fdm=marker :
792 */