Code

frontend: Let CONNECTION_QUERY support 'TIMESERIES' queries.
authorSebastian Harl <sh@tokkee.org>
Sat, 16 Aug 2014 19:38:21 +0000 (21:38 +0200)
committerSebastian Harl <sh@tokkee.org>
Sat, 16 Aug 2014 19:38:21 +0000 (21:38 +0200)
src/frontend/query.c
src/include/frontend/connection.h
t/integration/simple_query.sh

index a30bee3e4b8f274730ff650d2ca84cc168fee58d..e0b7181161cde74bbf9ba50d96c49022444ec31e 100644 (file)
@@ -187,6 +187,10 @@ sdb_fe_exec(sdb_conn_t *conn, sdb_conn_node_t *node)
                        if (CONN_LOOKUP(node)->filter)
                                filter = CONN_LOOKUP(node)->filter->matcher;
                        return sdb_fe_exec_lookup(conn, m, filter);
+               case CONNECTION_TIMESERIES:
+                       return sdb_fe_exec_timeseries(conn,
+                                       CONN_TS(node)->hostname, CONN_TS(node)->metric,
+                                       &CONN_TS(node)->opts);
 
                default:
                        sdb_log(SDB_LOG_ERR, "frontend: Unknown command %i", node->cmd);
@@ -310,5 +314,36 @@ sdb_fe_exec_lookup(sdb_conn_t *conn, sdb_store_matcher_t *m,
        return 0;
 } /* sdb_fe_exec_lookup */
 
+int
+sdb_fe_exec_timeseries(sdb_conn_t *conn,
+               const char *hostname, const char *metric,
+               sdb_timeseries_opts_t *opts)
+{
+       sdb_strbuf_t *buf;
+
+       buf = sdb_strbuf_create(1024);
+       if (! buf) {
+               char errbuf[1024];
+               sdb_log(SDB_LOG_ERR, "frontend: Failed to create "
+                               "buffer to handle TIMESERIES command: %s",
+                               sdb_strerror(errno, errbuf, sizeof(errbuf)));
+
+               sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
+               return -1;
+       }
+
+       if (sdb_store_fetch_timeseries(hostname, metric, opts, buf)) {
+               sdb_log(SDB_LOG_ERR, "frontend: Failed to fetch time-series");
+               sdb_strbuf_sprintf(conn->errbuf, "Failed to fetch time-series");
+               sdb_strbuf_destroy(buf);
+               return -1;
+       }
+
+       sdb_connection_send(conn, CONNECTION_OK,
+                       (uint32_t)sdb_strbuf_len(buf), sdb_strbuf_string(buf));
+       sdb_strbuf_destroy(buf);
+       return 0;
+} /* sdb_fe_exec_timeseries */
+
 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */
 
index 16ad39a28a51f5c8be014a89ad0714b410806a5c..e963c94e58d0b7f30aa445b38504cdf3a554fbcd 100644 (file)
@@ -225,6 +225,21 @@ int
 sdb_fe_exec_lookup(sdb_conn_t *conn, sdb_store_matcher_t *m,
                sdb_store_matcher_t *filter);
 
+/*
+ * sdb_fe_exec_timeseries:
+ * Execute the 'TIMESERIES' command. Send the time-series for the specified
+ * host's metric, serialized as JSON, to the client. See
+ * sdb_store_fetch_timeseries for details.
+ *
+ * Returns:
+ *  - 0 on success
+ *  - a negative value else
+ */
+int
+sdb_fe_exec_timeseries(sdb_conn_t *conn,
+               const char *hostname, const char *metric,
+               sdb_timeseries_opts_t *opts);
+
 #ifdef __cplusplus
 } /* extern "C" */
 #endif
index 4398b2b9da935be695ed80871cffc7db4690c976..a2df1b933e7d89d316c3fa57c8360cbf486f4c2e 100755 (executable)
@@ -161,5 +161,8 @@ output="$( run_sysdb -H "$SOCKET_FILE" \
        -c "LOOKUP hosts MATCHING attribute.invalid = 'none'" )"
 echo $output | grep -E '^\[\]$'
 
+run_sysdb -H "$SOCKET_FILE" \
+               -c "TIMESERIES 'invalid.host'.'invalid-metric'" && exit 1
+
 stop_sysdbd