src/rrd_daemon.c: Impemented the `stats' command.
authorFlorian Forster <octo@verplant.org>
Thu, 3 Jul 2008 08:30:33 +0000 (10:30 +0200)
committerFlorian Forster <octo@verplant.org>
Thu, 3 Jul 2008 08:30:33 +0000 (10:30 +0200)
It currently returns five statistics:
- Number of nodes and depth of the tree.
- Number of nodes in the update queue.
- Total number of updates and values written to disk since startup.

src/rrd_daemon.c

index 4b725c2..024738b 100644 (file)
@@ -66,6 +66,8 @@
 #include <stdio.h>
 #include <unistd.h>
 #include <string.h>
+#include <stdint.h>
+#include <inttypes.h>
 
 #include <sys/types.h>
 #include <sys/stat.h>
@@ -595,6 +597,67 @@ static int flush_file (const char *filename) /* {{{ */
   return (0);
 } /* }}} int flush_file */
 
+static int handle_request_stats (int fd, /* {{{ */
+    char *buffer __attribute__((unused)),
+    size_t buffer_size __attribute__((unused)))
+{
+  int status;
+  char outbuf[4096];
+
+  uint64_t copy_queue_length;
+  uint64_t copy_updates_total;
+  uint64_t copy_values_total;
+
+  uint64_t tree_nodes;
+  uint64_t tree_depth;
+
+  pthread_mutex_lock (&stats_lock);
+  copy_queue_length  = stats_queue_length;
+  copy_updates_total = stats_updates_total;
+  copy_values_total  = stats_values_total;
+  pthread_mutex_unlock (&stats_lock);
+
+  pthread_mutex_lock (&cache_lock);
+  tree_nodes = (uint64_t) g_tree_nnodes (cache_tree);
+  tree_depth = (uint64_t) g_tree_height (cache_tree);
+  pthread_mutex_unlock (&cache_lock);
+
+#define RRDD_STATS_SEND \
+  outbuf[sizeof (outbuf) - 1] = 0; \
+  status = write (fd, outbuf, strlen (outbuf)); \
+  if (status < 0) \
+  { \
+    status = errno; \
+    RRDD_LOG (LOG_INFO, "handle_request_stats: write(2) returned an error."); \
+    return (status); \
+  }
+
+  strncpy (outbuf, "5 Statistics follow\n", sizeof (outbuf));
+  RRDD_STATS_SEND;
+
+  snprintf (outbuf, sizeof (outbuf),
+      "QueueLength: %"PRIu64"\n", copy_queue_length);
+  RRDD_STATS_SEND;
+
+  snprintf (outbuf, sizeof (outbuf),
+      "UpdatesWritten: %"PRIu64"\n", copy_updates_total);
+  RRDD_STATS_SEND;
+
+  snprintf (outbuf, sizeof (outbuf),
+      "ValuesWritten: %"PRIu64"\n", copy_values_total);
+  RRDD_STATS_SEND;
+
+  snprintf (outbuf, sizeof (outbuf),
+      "TreeNodesNumber: %"PRIu64"\n", tree_nodes);
+  RRDD_STATS_SEND;
+
+  snprintf (outbuf, sizeof (outbuf),
+      "TreeDepth: %"PRIu64"\n", tree_depth);
+  RRDD_STATS_SEND;
+
+  return (0);
+} /* }}} int handle_request_stats */
+
 static int handle_request_flush (int fd, /* {{{ */
     char *buffer, size_t buffer_size)
 {
@@ -798,6 +861,10 @@ static int handle_request (int fd) /* {{{ */
   {
     return (handle_request_flush (fd, buffer_ptr, buffer_size));
   }
+  else if (strcmp (command, "stats") == 0)
+  {
+    return (handle_request_stats (fd, buffer_ptr, buffer_size));
+  }
   else
   {
     RRDD_LOG (LOG_INFO, "handle_request: unknown command: %s.", buffer);