Code

7285b349b39be9994b92add89167bb3aaaacb174
[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         }
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                 case SDB_ATTRIBUTE:
128                 {
129                         sdb_proto_attribute_t attr;
130                         if (sdb_proto_unmarshal_attribute(buf, len, &attr) < 0) {
131                                 sdb_strbuf_sprintf(conn->errbuf,
132                                                 "STORE: Failed to unmarshal attribute object");
133                                 return -1;
134                         }
135                         return sdb_fe_store_attribute(conn, &attr);
136                 }
137         }
139         sdb_log(SDB_LOG_ERR, "frontend: Invalid object type %d for "
140                         "STORE COMMAND", type);
141         sdb_strbuf_sprintf(conn->errbuf, "STORE: Invalid object type %d", type);
142         return -1;
143 } /* sdb_fe_store */
145 int
146 sdb_fe_store_host(sdb_conn_t *conn, const sdb_proto_host_t *host)
148         if ((! conn) || (! host) || (! host->name))
149                 return -1;
151         return store_reply(conn, SDB_HOST, host->name,
152                         sdb_store_host(host->name, host->last_update));
153 } /* sdb_fe_store_host */
155 int
156 sdb_fe_store_service(sdb_conn_t *conn, const sdb_proto_service_t *svc)
158         char name[svc ? strlen(svc->hostname) + strlen(svc->name) + 2 : 2];
160         if ((! conn) || (! svc) || (! svc->hostname) || (! svc->name))
161                 return -1;
163         snprintf(name, sizeof(name), svc->hostname, svc->name);
164         return store_reply(conn, SDB_SERVICE, name,
165                         sdb_store_service(svc->hostname, svc->name, svc->last_update));
166 } /* sdb_fe_store_service */
168 int
169 sdb_fe_store_metric(sdb_conn_t *conn, const sdb_proto_metric_t *metric)
171         sdb_metric_store_t store;
172         char name[metric ? strlen(metric->hostname) + strlen(metric->name) + 2 : 2];
174         if ((! conn) || (! metric) || (! metric->hostname) || (! metric->name))
175                 return -1;
177         store.type = metric->store_type;
178         store.id = metric->store_id;
179         snprintf(name, sizeof(name), metric->hostname, metric->name);
180         return store_reply(conn, SDB_METRIC, name,
181                         sdb_store_metric(metric->hostname, metric->name,
182                                 &store, metric->last_update));
183 } /* sdb_fe_store_metric */
185 int
186 sdb_fe_store_attribute(sdb_conn_t *conn, const sdb_proto_attribute_t *attr)
188         char name[attr ? strlen(attr->parent) + strlen(attr->key) + 2 : 2];
189         int status;
191         if ((! conn) || (! attr) || (! attr->parent) || (! attr->key))
192                 return -1;
194         if (attr->parent_type == SDB_HOST)
195                 status = sdb_store_attribute(attr->parent,
196                                 attr->key, &attr->value, attr->last_update);
197         else if (attr->parent_type == SDB_SERVICE)
198                 status = sdb_store_service_attr(attr->hostname, attr->parent,
199                                 attr->key, &attr->value, attr->last_update);
200         else if (attr->parent_type == SDB_METRIC)
201                 status = sdb_store_metric_attr(attr->hostname, attr->parent,
202                                 attr->key, &attr->value, attr->last_update);
203         else {
204                 sdb_strbuf_sprintf(conn->errbuf,
205                                 "STORE: Invalid parent object type %d",
206                                 attr->parent_type);
207                 return -1;
208         }
210         snprintf(name, sizeof(name), "%s.%s", attr->parent, attr->key);
211         return store_reply(conn, attr->parent_type | SDB_ATTRIBUTE, name, status);
212 } /* sdb_fe_store_attribute */
214 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */