Code

frontend: Add support for SDB_CONNECTION_STORE.
[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 int
77 sdb_fe_store(sdb_conn_t *conn)
78 {
79         uint32_t type;
81         const char *buf = sdb_strbuf_string(conn->buf);
82         size_t len = conn->cmd_len;
83         ssize_t n;
85         if ((! conn) || (conn->cmd != SDB_CONNECTION_STORE))
86                 return -1;
88         if ((n = sdb_proto_unmarshal_int32(buf, len, &type)) < 0) {
89                 sdb_log(SDB_LOG_ERR, "frontend: Invalid command length %zu for "
90                                 "STORE command", len);
91                 sdb_strbuf_sprintf(conn->errbuf,
92                                 "STORE: Invalid command length %zu", len);
93                 return -1;
94         }
95         buf += n; len -= n;
97         switch (type) {
98                 case SDB_HOST:
99                 {
100                         sdb_proto_host_t host;
101                         if (sdb_proto_unmarshal_host(buf, len, &host) < 0) {
102                                 sdb_strbuf_sprintf(conn->errbuf,
103                                                 "STORE: Failed to unmarshal host object");
104                                 return -1;
105                         }
106                         return sdb_fe_store_host(conn, &host);
107                 }
108                 case SDB_SERVICE:
109                 {
110                         sdb_proto_service_t svc;
111                         if (sdb_proto_unmarshal_service(buf, len, &svc) < 0) {
112                                 sdb_strbuf_sprintf(conn->errbuf,
113                                                 "STORE: Failed to unmarshal service object");
114                                 return -1;
115                         }
116                         return sdb_fe_store_service(conn, &svc);
117                 }
118                 case SDB_METRIC:
119                 {
120                         sdb_proto_metric_t metric;
121                         if (sdb_proto_unmarshal_metric(buf, len, &metric) < 0) {
122                                 sdb_strbuf_sprintf(conn->errbuf,
123                                                 "STORE: Failed to unmarshal metric object");
124                                 return -1;
125                         }
126                         return sdb_fe_store_metric(conn, &metric);
127                 }
128                 case SDB_ATTRIBUTE:
129                 {
130                         sdb_proto_attribute_t attr;
131                         if (sdb_proto_unmarshal_attribute(buf, len, &attr) < 0) {
132                                 sdb_strbuf_sprintf(conn->errbuf,
133                                                 "STORE: Failed to unmarshal attribute object");
134                                 return -1;
135                         }
136                         return sdb_fe_store_attribute(conn, &attr);
137                 }
138         }
140         sdb_log(SDB_LOG_ERR, "frontend: Invalid object type %d for "
141                         "STORE COMMAND", type);
142         sdb_strbuf_sprintf(conn->errbuf, "STORE: Invalid object type %d", type);
143         return -1;
144 } /* sdb_fe_store */
146 int
147 sdb_fe_store_host(sdb_conn_t *conn, const sdb_proto_host_t *host)
149         if ((! conn) || (! host) || (! host->name))
150                 return -1;
152         return store_reply(conn, SDB_HOST, host->name,
153                         sdb_store_host(host->name, host->last_update));
154 } /* sdb_fe_store_host */
156 int
157 sdb_fe_store_service(sdb_conn_t *conn, const sdb_proto_service_t *svc)
159         char name[svc ? strlen(svc->hostname) + strlen(svc->name) + 2 : 2];
161         if ((! conn) || (! svc) || (! svc->hostname) || (! svc->name))
162                 return -1;
164         snprintf(name, sizeof(name), svc->hostname, svc->name);
165         return store_reply(conn, SDB_SERVICE, name,
166                         sdb_store_service(svc->hostname, svc->name, svc->last_update));
167 } /* sdb_fe_store_service */
169 int
170 sdb_fe_store_metric(sdb_conn_t *conn, const sdb_proto_metric_t *metric)
172         sdb_metric_store_t store;
173         char name[metric ? strlen(metric->hostname) + strlen(metric->name) + 2 : 2];
175         if ((! conn) || (! metric) || (! metric->hostname) || (! metric->name))
176                 return -1;
178         store.type = metric->store_type;
179         store.id = metric->store_id;
180         snprintf(name, sizeof(name), metric->hostname, metric->name);
181         return store_reply(conn, SDB_METRIC, name,
182                         sdb_store_metric(metric->hostname, metric->name,
183                                 &store, metric->last_update));
184 } /* sdb_fe_store_metric */
186 int
187 sdb_fe_store_attribute(sdb_conn_t *conn, const sdb_proto_attribute_t *attr)
189         char name[attr ? strlen(attr->parent) + strlen(attr->key) + 2 : 2];
190         int status;
192         if ((! conn) || (! attr) || (! attr->parent) || (! attr->key))
193                 return -1;
195         if (attr->parent_type == SDB_HOST)
196                 status = sdb_store_attribute(attr->parent,
197                                 attr->key, &attr->value, attr->last_update);
198         else if (attr->parent_type == SDB_SERVICE)
199                 status = sdb_store_service_attr(attr->hostname, attr->parent,
200                                 attr->key, &attr->value, attr->last_update);
201         else if (attr->parent_type == SDB_METRIC)
202                 status = sdb_store_metric_attr(attr->hostname, attr->parent,
203                                 attr->key, &attr->value, attr->last_update);
204         else {
205                 sdb_strbuf_sprintf(conn->errbuf,
206                                 "STORE: Invalid parent object type %d",
207                                 attr->parent_type);
208                 return -1;
209         }
211         snprintf(name, sizeof(name), "%s.%s", attr->parent, attr->key);
212         return store_reply(conn, attr->parent_type | SDB_ATTRIBUTE, name, status);
213 } /* sdb_fe_store_attribute */
215 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */