Code

frontend: Fixed hostname in error messages in FETCH.
[sysdb.git] / src / frontend / query.c
index 6e94f646f2f9d1c20dc2f12dbd30236283dda21a..aef0ddcc6d7112300f6d1aca90fbd42062b43650 100644 (file)
@@ -69,7 +69,7 @@ sdb_fe_query(sdb_conn_t *conn)
        sdb_conn_node_t *node = NULL;
        int status = 0;
 
-       if ((! conn) || (conn->cmd != CONNECTION_QUERY))
+       if ((! conn) || (conn->cmd != SDB_CONNECTION_QUERY))
                return -1;
 
        parsetree = sdb_fe_parse(sdb_strbuf_string(conn->buf),
@@ -86,7 +86,7 @@ sdb_fe_query(sdb_conn_t *conn)
        switch (sdb_llist_len(parsetree)) {
                case 0:
                        /* skipping empty command; send back an empty reply */
-                       sdb_connection_send(conn, CONNECTION_DATA, 0, NULL);
+                       sdb_connection_send(conn, SDB_CONNECTION_DATA, 0, NULL);
                        break;
                case 1:
                        node = SDB_CONN_NODE(sdb_llist_get(parsetree, 0));
@@ -119,9 +119,9 @@ int
 sdb_fe_fetch(sdb_conn_t *conn)
 {
        char name[conn->cmd_len + 1];
-       int type;
+       uint32_t type;
 
-       if ((! conn) || (conn->cmd != CONNECTION_FETCH))
+       if ((! conn) || (conn->cmd != SDB_CONNECTION_FETCH))
                return -1;
 
        if (conn->cmd_len < sizeof(uint32_t)) {
@@ -132,24 +132,24 @@ sdb_fe_fetch(sdb_conn_t *conn)
                return -1;
        }
 
-       type = sdb_proto_get_int(conn->buf, 0);
+       sdb_proto_unmarshal_int32(SDB_STRBUF_STR(conn->buf), &type);
        strncpy(name, sdb_strbuf_string(conn->buf) + sizeof(uint32_t),
                        conn->cmd_len - sizeof(uint32_t));
        name[sizeof(name) - 1] = '\0';
        /* TODO: support other types besides hosts */
-       return sdb_fe_exec_fetch(conn, type, name, NULL, /* filter = */ NULL);
+       return sdb_fe_exec_fetch(conn, (int)type, name, NULL, /* filter = */ NULL);
 } /* sdb_fe_fetch */
 
 int
 sdb_fe_list(sdb_conn_t *conn)
 {
-       int type = SDB_HOST;
+       uint32_t type = SDB_HOST;
 
-       if ((! conn) || (conn->cmd != CONNECTION_LIST))
+       if ((! conn) || (conn->cmd != SDB_CONNECTION_LIST))
                return -1;
 
        if (conn->cmd_len == sizeof(uint32_t))
-               type = sdb_proto_get_int(conn->buf, 0);
+               sdb_proto_unmarshal_int32(SDB_STRBUF_STR(conn->buf), &type);
        else if (conn->cmd_len) {
                sdb_log(SDB_LOG_ERR, "frontend: Invalid command length %d for "
                                "LIST command", conn->cmd_len);
@@ -157,7 +157,7 @@ sdb_fe_list(sdb_conn_t *conn)
                                conn->cmd_len);
                return -1;
        }
-       return sdb_fe_exec_list(conn, type, /* filter = */ NULL);
+       return sdb_fe_exec_list(conn, (int)type, /* filter = */ NULL);
 } /* sdb_fe_list */
 
 int
@@ -167,18 +167,18 @@ sdb_fe_lookup(sdb_conn_t *conn)
        const char *matcher;
        size_t matcher_len;
 
-       int type;
+       uint32_t type;
        int status;
 
        conn_matcher_t m_node = {
-               { SDB_OBJECT_INIT, CONNECTION_MATCHER }, NULL
+               { SDB_OBJECT_INIT, SDB_CONNECTION_MATCHER }, NULL
        };
        conn_lookup_t node = {
-               { SDB_OBJECT_INIT, CONNECTION_LOOKUP },
+               { SDB_OBJECT_INIT, SDB_CONNECTION_LOOKUP },
                -1, &m_node, NULL
        };
 
-       if ((! conn) || (conn->cmd != CONNECTION_LOOKUP))
+       if ((! conn) || (conn->cmd != SDB_CONNECTION_LOOKUP))
                return -1;
 
        if (conn->cmd_len < sizeof(uint32_t)) {
@@ -188,7 +188,7 @@ sdb_fe_lookup(sdb_conn_t *conn)
                                conn->cmd_len);
                return -1;
        }
-       type = sdb_proto_get_int(conn->buf, 0);
+       sdb_proto_unmarshal_int32(SDB_STRBUF_STR(conn->buf), &type);
 
        matcher = sdb_strbuf_string(conn->buf) + sizeof(uint32_t);
        matcher_len = conn->cmd_len - sizeof(uint32_t);
@@ -203,7 +203,7 @@ sdb_fe_lookup(sdb_conn_t *conn)
                return -1;
        }
 
-       node.type = type;
+       node.type = (int)type;
        m_node.matcher = m;
 
        /* run analyzer separately; parse_matcher is missing
@@ -220,7 +220,7 @@ sdb_fe_lookup(sdb_conn_t *conn)
                status = -1;
        }
        else
-               status = sdb_fe_exec_lookup(conn, type, m, /* filter = */ NULL);
+               status = sdb_fe_exec_lookup(conn, (int)type, m, /* filter = */ NULL);
        sdb_object_deref(SDB_OBJ(m));
        return status;
 } /* sdb_fe_lookup */
@@ -234,23 +234,53 @@ sdb_fe_exec(sdb_conn_t *conn, sdb_conn_node_t *node)
                return -1;
 
        switch (node->cmd) {
-               case CONNECTION_FETCH:
+               case SDB_CONNECTION_FETCH:
                        if (CONN_FETCH(node)->filter)
                                filter = CONN_FETCH(node)->filter->matcher;
                        return sdb_fe_exec_fetch(conn, CONN_FETCH(node)->type,
                                        CONN_FETCH(node)->host, CONN_FETCH(node)->name, filter);
-               case CONNECTION_LIST:
+               case SDB_CONNECTION_LIST:
                        if (CONN_LIST(node)->filter)
                                filter = CONN_LIST(node)->filter->matcher;
                        return sdb_fe_exec_list(conn, CONN_LIST(node)->type, filter);
-               case CONNECTION_LOOKUP:
+               case SDB_CONNECTION_LOOKUP:
                        if (CONN_LOOKUP(node)->matcher)
                                m = CONN_LOOKUP(node)->matcher->matcher;
                        if (CONN_LOOKUP(node)->filter)
                                filter = CONN_LOOKUP(node)->filter->matcher;
                        return sdb_fe_exec_lookup(conn,
                                        CONN_LOOKUP(node)->type, m, filter);
-               case CONNECTION_TIMESERIES:
+               case SDB_CONNECTION_STORE_HOST:
+               {
+                       conn_store_host_t *n = CONN_STORE_HOST(node);
+                       sdb_proto_host_t host = { n->last_update, n->name };
+                       return sdb_fe_store_host(conn, &host);
+               }
+               case SDB_CONNECTION_STORE_SERVICE:
+               {
+                       conn_store_svc_t *n = CONN_STORE_SVC(node);
+                       sdb_proto_service_t svc = { n->last_update, n->hostname, n->name };
+                       return sdb_fe_store_service(conn, &svc);
+               }
+               case SDB_CONNECTION_STORE_METRIC:
+               {
+                       conn_store_metric_t *n = CONN_STORE_METRIC(node);
+                       sdb_proto_metric_t metric = {
+                               n->last_update, n->hostname, n->name,
+                               n->store_type, n->store_id
+                       };
+                       return sdb_fe_store_metric(conn, &metric);
+               }
+               case SDB_CONNECTION_STORE_ATTRIBUTE:
+               {
+                       conn_store_attr_t *n = CONN_STORE_ATTR(node);
+                       sdb_proto_attribute_t attr = {
+                               n->last_update, n->parent_type, n->hostname, n->parent,
+                               n->key, n->value
+                       };
+                       return sdb_fe_store_attribute(conn, &attr);
+               }
+               case SDB_CONNECTION_TIMESERIES:
                        return sdb_fe_exec_timeseries(conn,
                                        CONN_TS(node)->hostname, CONN_TS(node)->metric,
                                        &CONN_TS(node)->opts);
@@ -266,7 +296,7 @@ int
 sdb_fe_exec_fetch(sdb_conn_t *conn, int type,
                const char *hostname, const char *name, sdb_store_matcher_t *filter)
 {
-       uint32_t res_type = htonl(CONNECTION_FETCH);
+       uint32_t res_type = htonl(SDB_CONNECTION_FETCH);
 
        sdb_store_obj_t *host;
        sdb_store_obj_t *obj;
@@ -282,6 +312,8 @@ sdb_fe_exec_fetch(sdb_conn_t *conn, int type,
                                SDB_STORE_TYPE_TO_NAME(type), hostname, name);
                return -1;
        }
+       if (type == SDB_HOST)
+               name = hostname;
 
        host = sdb_store_get_host(hostname);
        if ((! host) || (filter
@@ -301,10 +333,14 @@ sdb_fe_exec_fetch(sdb_conn_t *conn, int type,
                        sdb_strbuf_sprintf(conn->errbuf, "Failed to fetch %s %s.%s: "
                                        "%s not found", SDB_STORE_TYPE_TO_NAME(type),
                                        hostname, name, name);
+                       if (obj)
+                               sdb_object_deref(SDB_OBJ(obj));
+                       sdb_object_deref(SDB_OBJ(host));
                        return -1;
                }
                sdb_object_deref(SDB_OBJ(host));
        }
+       host = NULL;
 
        buf = sdb_strbuf_create(1024);
        if (! buf) {
@@ -344,7 +380,7 @@ sdb_fe_exec_fetch(sdb_conn_t *conn, int type,
        }
        sdb_store_json_finish(f);
 
-       sdb_connection_send(conn, CONNECTION_DATA,
+       sdb_connection_send(conn, SDB_CONNECTION_DATA,
                        (uint32_t)sdb_strbuf_len(buf), sdb_strbuf_string(buf));
        sdb_strbuf_destroy(buf);
        free(f);
@@ -355,7 +391,7 @@ sdb_fe_exec_fetch(sdb_conn_t *conn, int type,
 int
 sdb_fe_exec_list(sdb_conn_t *conn, int type, sdb_store_matcher_t *filter)
 {
-       uint32_t res_type = htonl(CONNECTION_LIST);
+       uint32_t res_type = htonl(SDB_CONNECTION_LIST);
 
        sdb_store_json_formatter_t *f;
        sdb_strbuf_t *buf;
@@ -394,7 +430,7 @@ sdb_fe_exec_list(sdb_conn_t *conn, int type, sdb_store_matcher_t *filter)
        }
        sdb_store_json_finish(f);
 
-       sdb_connection_send(conn, CONNECTION_DATA,
+       sdb_connection_send(conn, SDB_CONNECTION_DATA,
                        (uint32_t)sdb_strbuf_len(buf), sdb_strbuf_string(buf));
        sdb_strbuf_destroy(buf);
        free(f);
@@ -405,7 +441,7 @@ int
 sdb_fe_exec_lookup(sdb_conn_t *conn, int type,
                sdb_store_matcher_t *m, sdb_store_matcher_t *filter)
 {
-       uint32_t res_type = htonl(CONNECTION_LOOKUP);
+       uint32_t res_type = htonl(SDB_CONNECTION_LOOKUP);
 
        sdb_store_json_formatter_t *f;
        sdb_strbuf_t *buf;
@@ -445,7 +481,7 @@ sdb_fe_exec_lookup(sdb_conn_t *conn, int type,
        }
        sdb_store_json_finish(f);
 
-       sdb_connection_send(conn, CONNECTION_DATA,
+       sdb_connection_send(conn, SDB_CONNECTION_DATA,
                        (uint32_t)sdb_strbuf_len(buf), sdb_strbuf_string(buf));
        sdb_strbuf_destroy(buf);
        free(f);
@@ -458,7 +494,7 @@ sdb_fe_exec_timeseries(sdb_conn_t *conn,
                sdb_timeseries_opts_t *opts)
 {
        sdb_strbuf_t *buf;
-       uint32_t res_type = htonl(CONNECTION_TIMESERIES);
+       uint32_t res_type = htonl(SDB_CONNECTION_TIMESERIES);
 
        buf = sdb_strbuf_create(1024);
        if (! buf) {
@@ -479,7 +515,7 @@ sdb_fe_exec_timeseries(sdb_conn_t *conn,
                return -1;
        }
 
-       sdb_connection_send(conn, CONNECTION_DATA,
+       sdb_connection_send(conn, SDB_CONNECTION_DATA,
                        (uint32_t)sdb_strbuf_len(buf), sdb_strbuf_string(buf));
        sdb_strbuf_destroy(buf);
        return 0;