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 }
96 switch (type) {
97 case SDB_HOST:
98 {
99 sdb_proto_host_t host;
100 if (sdb_proto_unmarshal_host(buf, len, &host) < 0) {
101 sdb_strbuf_sprintf(conn->errbuf,
102 "STORE: Failed to unmarshal host object");
103 return -1;
104 }
105 return sdb_fe_store_host(conn, &host);
106 }
107 case SDB_SERVICE:
108 {
109 sdb_proto_service_t svc;
110 if (sdb_proto_unmarshal_service(buf, len, &svc) < 0) {
111 sdb_strbuf_sprintf(conn->errbuf,
112 "STORE: Failed to unmarshal service object");
113 return -1;
114 }
115 return sdb_fe_store_service(conn, &svc);
116 }
117 case SDB_METRIC:
118 {
119 sdb_proto_metric_t metric;
120 if (sdb_proto_unmarshal_metric(buf, len, &metric) < 0) {
121 sdb_strbuf_sprintf(conn->errbuf,
122 "STORE: Failed to unmarshal metric object");
123 return -1;
124 }
125 return sdb_fe_store_metric(conn, &metric);
126 }
127 }
128 if (type & SDB_ATTRIBUTE) {
129 sdb_proto_attribute_t attr;
130 int status;
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 status = sdb_fe_store_attribute(conn, &attr);
137 sdb_data_free_datum(&attr.value);
138 return status;
139 }
141 sdb_log(SDB_LOG_ERR, "frontend: Invalid object type %d for "
142 "STORE COMMAND", type);
143 sdb_strbuf_sprintf(conn->errbuf, "STORE: Invalid object type %d", type);
144 return -1;
145 } /* sdb_fe_store */
147 int
148 sdb_fe_store_host(sdb_conn_t *conn, const sdb_proto_host_t *host)
149 {
150 if ((! conn) || (! host) || (! host->name))
151 return -1;
153 return store_reply(conn, SDB_HOST, host->name,
154 sdb_store_host(host->name, host->last_update));
155 } /* sdb_fe_store_host */
157 int
158 sdb_fe_store_service(sdb_conn_t *conn, const sdb_proto_service_t *svc)
159 {
160 char name[svc ? strlen(svc->hostname) + strlen(svc->name) + 2 : 2];
162 if ((! conn) || (! svc) || (! svc->hostname) || (! svc->name))
163 return -1;
165 snprintf(name, sizeof(name), svc->hostname, svc->name);
166 return store_reply(conn, SDB_SERVICE, name,
167 sdb_store_service(svc->hostname, svc->name, svc->last_update));
168 } /* sdb_fe_store_service */
170 int
171 sdb_fe_store_metric(sdb_conn_t *conn, const sdb_proto_metric_t *metric)
172 {
173 sdb_metric_store_t store;
174 char name[metric ? strlen(metric->hostname) + strlen(metric->name) + 2 : 2];
176 if ((! conn) || (! metric) || (! metric->hostname) || (! metric->name))
177 return -1;
179 store.type = metric->store_type;
180 store.id = metric->store_id;
181 snprintf(name, sizeof(name), metric->hostname, metric->name);
182 return store_reply(conn, SDB_METRIC, name,
183 sdb_store_metric(metric->hostname, metric->name,
184 &store, metric->last_update));
185 } /* sdb_fe_store_metric */
187 int
188 sdb_fe_store_attribute(sdb_conn_t *conn, const sdb_proto_attribute_t *attr)
189 {
190 char name[attr ? strlen(attr->parent) + strlen(attr->key) + 2 : 2];
191 int status;
193 if ((! conn) || (! attr) || (! attr->parent) || (! attr->key))
194 return -1;
196 if (attr->parent_type == SDB_HOST)
197 status = sdb_store_attribute(attr->parent,
198 attr->key, &attr->value, attr->last_update);
199 else if (attr->parent_type == SDB_SERVICE)
200 status = sdb_store_service_attr(attr->hostname, attr->parent,
201 attr->key, &attr->value, attr->last_update);
202 else if (attr->parent_type == SDB_METRIC)
203 status = sdb_store_metric_attr(attr->hostname, attr->parent,
204 attr->key, &attr->value, attr->last_update);
205 else {
206 sdb_strbuf_sprintf(conn->errbuf,
207 "STORE: Invalid parent object type %d",
208 attr->parent_type);
209 return -1;
210 }
212 snprintf(name, sizeof(name), "%s.%s", attr->parent, attr->key);
213 return store_reply(conn, attr->parent_type | SDB_ATTRIBUTE, name, status);
214 } /* sdb_fe_store_attribute */
216 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */