Code

3d1a847b00b61f236c2d9d8ae914d7fa847ff3b1
[sysdb.git] / src / frontend / store.c
1 /*
2  * SysDB - src/frontend/store.c
3  * Copyright (C) 2015 Sebastian 'tokkee' Harl <sh@tokkee.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
19  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
28 #include "sysdb.h"
30 #include "core/store.h"
31 #include "frontend/connection-private.h"
32 #include "utils/error.h"
33 #include "utils/proto.h"
34 #include "utils/strbuf.h"
36 #include <ctype.h>
37 #include <string.h>
39 /*
40  * private helper functions
41  */
43 static int
44 store_reply(sdb_conn_t *conn, int type, const char *name, int status)
45 {
46         if (status < 0) {
47                 sdb_strbuf_sprintf(conn->errbuf, "STORE: Failed to store %s object",
48                                 SDB_STORE_TYPE_TO_NAME(type));
49                 return -1;
50         }
52         if (! status) {
53                 char msg[strlen(name) + 64];
54                 snprintf(msg, sizeof(msg), "Successfully stored %s %s",
55                                 SDB_STORE_TYPE_TO_NAME(type), name);
56                 msg[sizeof(msg) - 1] = '\0';
57                 sdb_connection_send(conn, SDB_CONNECTION_OK,
58                                 (uint32_t)strlen(msg), msg);
59         }
60         else {
61                 char msg[strlen(name) + 64];
62                 snprintf(msg, sizeof(msg), "%s %s already up to date",
63                                 SDB_STORE_TYPE_TO_NAME(type), name);
64                 msg[0] = (char)toupper((int)msg[0]);
65                 msg[sizeof(msg) - 1] = '\0';
66                 sdb_connection_send(conn, SDB_CONNECTION_OK,
67                                 (uint32_t)strlen(msg), msg);
68         }
69         return 0;
70 } /* store_reply */
72 /*
73  * public API
74  */
76 /*
77  * TODO: move sdb_fe_store to frontend/query.c and let it build an AST
78  */
80 int
81 sdb_fe_store(sdb_conn_t *conn)
82 {
83         uint32_t type;
85         const char *buf = sdb_strbuf_string(conn->buf);
86         size_t len = conn->cmd_len;
87         ssize_t n;
89         if ((! conn) || (conn->cmd != SDB_CONNECTION_STORE))
90                 return -1;
92         if ((n = sdb_proto_unmarshal_int32(buf, len, &type)) < 0) {
93                 sdb_log(SDB_LOG_ERR, "frontend: Invalid command length %zu for "
94                                 "STORE command", len);
95                 sdb_strbuf_sprintf(conn->errbuf,
96                                 "STORE: Invalid command length %zu", len);
97                 return -1;
98         }
100         switch (type) {
101                 case SDB_HOST:
102                 {
103                         sdb_proto_host_t host;
104                         if (sdb_proto_unmarshal_host(buf, len, &host) < 0) {
105                                 sdb_strbuf_sprintf(conn->errbuf,
106                                                 "STORE: Failed to unmarshal host object");
107                                 return -1;
108                         }
109                         return sdb_fe_store_host(conn, &host);
110                 }
111                 case SDB_SERVICE:
112                 {
113                         sdb_proto_service_t svc;
114                         if (sdb_proto_unmarshal_service(buf, len, &svc) < 0) {
115                                 sdb_strbuf_sprintf(conn->errbuf,
116                                                 "STORE: Failed to unmarshal service object");
117                                 return -1;
118                         }
119                         return sdb_fe_store_service(conn, &svc);
120                 }
121                 case SDB_METRIC:
122                 {
123                         sdb_proto_metric_t metric;
124                         if (sdb_proto_unmarshal_metric(buf, len, &metric) < 0) {
125                                 sdb_strbuf_sprintf(conn->errbuf,
126                                                 "STORE: Failed to unmarshal metric object");
127                                 return -1;
128                         }
129                         return sdb_fe_store_metric(conn, &metric);
130                 }
131         }
132         if (type & SDB_ATTRIBUTE) {
133                 sdb_proto_attribute_t attr;
134                 int status;
135                 if (sdb_proto_unmarshal_attribute(buf, len, &attr) < 0) {
136                         sdb_strbuf_sprintf(conn->errbuf,
137                                         "STORE: Failed to unmarshal attribute object");
138                         return -1;
139                 }
140                 status = sdb_fe_store_attribute(conn, &attr);
141                 sdb_data_free_datum(&attr.value);
142                 return status;
143         }
145         sdb_log(SDB_LOG_ERR, "frontend: Invalid object type %d for "
146                         "STORE COMMAND", type);
147         sdb_strbuf_sprintf(conn->errbuf, "STORE: Invalid object type %d", type);
148         return -1;
149 } /* sdb_fe_store */
151 int
152 sdb_fe_store_host(sdb_conn_t *conn, const sdb_proto_host_t *host)
154         if ((! conn) || (! host) || (! host->name))
155                 return -1;
157         return store_reply(conn, SDB_HOST, host->name,
158                         sdb_store_host(host->name, host->last_update));
159 } /* sdb_fe_store_host */
161 int
162 sdb_fe_store_service(sdb_conn_t *conn, const sdb_proto_service_t *svc)
164         char name[svc ? strlen(svc->hostname) + strlen(svc->name) + 2 : 2];
166         if ((! conn) || (! svc) || (! svc->hostname) || (! svc->name))
167                 return -1;
169         snprintf(name, sizeof(name), "%s.%s", svc->hostname, svc->name);
170         return store_reply(conn, SDB_SERVICE, name,
171                         sdb_store_service(svc->hostname, svc->name, svc->last_update));
172 } /* sdb_fe_store_service */
174 int
175 sdb_fe_store_metric(sdb_conn_t *conn, const sdb_proto_metric_t *metric)
177         sdb_metric_store_t store;
178         char name[metric ? strlen(metric->hostname) + strlen(metric->name) + 2 : 2];
180         if ((! conn) || (! metric) || (! metric->hostname) || (! metric->name))
181                 return -1;
183         store.type = metric->store_type;
184         store.id = metric->store_id;
185         snprintf(name, sizeof(name), "%s.%s", metric->hostname, metric->name);
186         return store_reply(conn, SDB_METRIC, name,
187                         sdb_store_metric(metric->hostname, metric->name,
188                                 &store, metric->last_update));
189 } /* sdb_fe_store_metric */
191 int
192 sdb_fe_store_attribute(sdb_conn_t *conn, const sdb_proto_attribute_t *attr)
194         char name[attr ? strlen(attr->parent) + strlen(attr->key) + 2 : 2];
195         int status;
197         if ((! conn) || (! attr) || (! attr->parent) || (! attr->key))
198                 return -1;
200         if (attr->parent_type == SDB_HOST)
201                 status = sdb_store_attribute(attr->parent,
202                                 attr->key, &attr->value, attr->last_update);
203         else if (attr->parent_type == SDB_SERVICE)
204                 status = sdb_store_service_attr(attr->hostname, attr->parent,
205                                 attr->key, &attr->value, attr->last_update);
206         else if (attr->parent_type == SDB_METRIC)
207                 status = sdb_store_metric_attr(attr->hostname, attr->parent,
208                                 attr->key, &attr->value, attr->last_update);
209         else {
210                 sdb_strbuf_sprintf(conn->errbuf,
211                                 "STORE: Invalid parent object type %d",
212                                 attr->parent_type);
213                 return -1;
214         }
216         snprintf(name, sizeof(name), "%s.%s", attr->parent, attr->key);
217         return store_reply(conn, attr->parent_type | SDB_ATTRIBUTE, name, status);
218 } /* sdb_fe_store_attribute */
220 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */