+
+
+/* convenience function; if there is a daemon specified, or if we can
+ * detect one from the environment, then flush the file. Otherwise, no-op
+ */
+int rrdc_flush_if_daemon (const char *opt_daemon, const char *filename) /* {{{ */
+{
+ int status = 0;
+
+ rrdc_connect(opt_daemon);
+
+ if (rrdc_is_connected(opt_daemon))
+ {
+ status = rrdc_flush (filename);
+ if (status != 0)
+ {
+ rrd_set_error ("rrdc_flush (%s) failed with status %i.",
+ filename, status);
+ }
+ } /* if (daemon_addr) */
+
+ return status;
+} /* }}} int rrdc_flush_if_daemon */
+
+
+int rrdc_stats_get (rrdc_stats_t **ret_stats) /* {{{ */
+{
+ rrdc_stats_t *head;
+ rrdc_stats_t *tail;
+
+ rrdc_response_t *response;
+
+ char buffer[4096];
+ size_t buffer_size;
+ int status;
+ size_t i;
+
+ pthread_mutex_lock (&lock);
+
+ if (sd < 0)
+ {
+ pthread_mutex_unlock (&lock);
+ return (ENOTCONN);
+ }
+
+ /* Protocol example: {{{
+ * -> STATS
+ * <- 5 Statistics follow
+ * <- QueueLength: 0
+ * <- UpdatesWritten: 0
+ * <- DataSetsWritten: 0
+ * <- TreeNodesNumber: 0
+ * <- TreeDepth: 0
+ * }}} */
+ status = swrite ("STATS\n", strlen ("STATS\n"));
+ if (status != 0)
+ {
+ pthread_mutex_unlock (&lock);
+ return (status);
+ }
+
+ status = sread (buffer, sizeof (buffer));
+ if (status < 0)
+ {
+ status = errno;
+ pthread_mutex_unlock (&lock);
+ return (status);
+ }
+ else if (status == 0)
+ {
+ pthread_mutex_unlock (&lock);
+ return (ENODATA);
+ }
+
+ pthread_mutex_unlock (&lock);
+
+ /* Assert NULL termination */
+ buffer_size = (size_t) status;
+ if (buffer[buffer_size - 1] != 0)
+ {
+ if (buffer_size < sizeof (buffer))
+ {
+ buffer[buffer_size] = 0;
+ buffer_size++;
+ }
+ else
+ {
+ return (ENOBUFS);
+ }
+ }
+
+ status = response_parse (buffer, buffer_size, &response);
+ if (status != 0)
+ return (status);
+
+ if (response->status <= 0)
+ {
+ response_free (response);
+ return (EIO);
+ }
+
+ head = NULL;
+ tail = NULL;
+ for (i = 0; i < response->lines_num; i++)
+ {
+ char *key;
+ char *value;
+ char *endptr;
+ rrdc_stats_t *s;
+
+ key = response->lines[i];
+ value = strchr (key, ':');
+ if (value == NULL)
+ continue;
+ *value = 0;
+ value++;
+
+ while ((value[0] == ' ') || (value[0] == '\t'))
+ value++;
+
+ s = (rrdc_stats_t *) malloc (sizeof (rrdc_stats_t));
+ if (s == NULL)
+ continue;
+ memset (s, 0, sizeof (*s));
+
+ s->name = strdup (key);
+
+ endptr = NULL;
+ if ((strcmp ("QueueLength", key) == 0)
+ || (strcmp ("TreeNodesNumber", key) == 0)
+ || (strcmp ("TreeDepth", key) == 0))
+ {
+ s->type = RRDC_STATS_TYPE_GAUGE;
+ s->value.gauge = strtod (value, &endptr);
+ }
+ else if ((strcmp ("UpdatesWritten", key) == 0)
+ || (strcmp ("DataSetsWritten", key) == 0))
+ {
+ s->type = RRDC_STATS_TYPE_COUNTER;
+ s->value.counter = (uint64_t) strtoll (value, &endptr, /* base = */ 0);
+ }
+ else
+ {
+ free (s);
+ continue;
+ }
+
+ /* Conversion failed */
+ if (endptr == value)
+ {
+ free (s);
+ continue;
+ }
+
+ if (head == NULL)
+ {
+ head = s;
+ tail = s;
+ s->next = NULL;
+ }
+ else
+ {
+ tail->next = s;
+ tail = s;
+ }
+ } /* for (i = 0; i < response->lines_num; i++) */
+
+ response_free (response);
+
+ if (head == NULL)
+ return (EPROTO);
+
+ *ret_stats = head;
+ return (0);
+} /* }}} int rrdc_stats_get */
+
+void rrdc_stats_free (rrdc_stats_t *ret_stats) /* {{{ */
+{
+ rrdc_stats_t *this;
+
+ this = ret_stats;
+ while (this != NULL)
+ {
+ rrdc_stats_t *next;
+
+ next = this->next;
+
+ if (this->name != NULL)
+ {
+ free (this->name);
+ this->name = NULL;
+ }
+ free (this);
+
+ this = next;
+ } /* while (this != NULL) */
+} /* }}} void rrdc_stats_free */
+