2 * Copyright (c) 2013 Mikolaj Golub <trociny@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/queue.h>
33 #include <bsnmp/snmpmod.h>
39 #include "hast_proto.h"
40 #include "hast_tree.h"
45 #define UPDATE_INTERVAL 500 /* update interval in ticks */
47 static struct lmodule *module;
49 static const struct asn_oid oid_hast = OIDX_begemotHast;
51 /* the Object Resource registration index */
52 static u_int hast_index = 0;
55 * Structure that describes single resource.
57 struct hast_snmp_resource {
58 TAILQ_ENTRY(hast_snmp_resource) link;
63 char provname[NAME_MAX];
64 char localpath[PATH_MAX];
67 char remoteaddr[HAST_ADDRSIZE];
68 char sourceaddr[HAST_ADDRSIZE];
76 uint64_t activemap_updates;
78 uint64_t write_errors;
79 uint64_t delete_errors;
80 uint64_t flush_errors;
89 static TAILQ_HEAD(, hast_snmp_resource) resources =
90 TAILQ_HEAD_INITIALIZER(resources);
92 /* Path to configuration file. */
93 static u_char *cfgpath;
94 /* Ticks of the last hast resources update. */
95 static uint64_t last_resources_update;
97 static void free_resources(void);
98 static int hastctl(struct nv *nvin, struct nv **nvout);
99 static int hast_fini(void);
100 static int hast_init(struct lmodule *mod, int argc, char *argv[]);
101 static void hast_start(void);
102 static int set_role(const char *resource, int role);
103 static int str2role(const char *str);
104 static int str2replication(const char *str);
105 static int str2status(const char *str);
106 static int update_resources(void);
108 const struct snmp_module config = {
109 .comment = "This module implements the BEGEMOT MIB for HAST.",
114 .tree_size = hast_CTREE_SIZE,
118 hast_init(struct lmodule *mod, int argc __unused, char *argv[] __unused)
123 pjdlog_init(PJDLOG_MODE_SYSLOG);
126 cfgpath = malloc(sizeof(HAST_CONFIG));
127 if (cfgpath == NULL) {
128 pjdlog_error("Unable to allocate %zu bytes for cfgpath",
129 sizeof(HAST_CONFIG));
132 strcpy(cfgpath, HAST_CONFIG);
139 hast_index = or_register(&oid_hast,
140 "The MIB module for BEGEMOT-HAST-MIB.", module);
147 or_unregister(hast_index);
156 struct hast_snmp_resource *res;
158 while ((res = TAILQ_FIRST(&resources)) != NULL) {
159 TAILQ_REMOVE(&resources, res, link);
165 str2role(const char *str)
168 if (strcmp(str, "init") == 0)
169 return (HAST_ROLE_INIT);
170 if (strcmp(str, "primary") == 0)
171 return (HAST_ROLE_PRIMARY);
172 if (strcmp(str, "secondary") == 0)
173 return (HAST_ROLE_SECONDARY);
174 return (HAST_ROLE_UNDEF);
178 str2replication(const char *str)
181 if (strcmp(str, "fullsync") == 0)
182 return (HAST_REPLICATION_FULLSYNC);
183 if (strcmp(str, "memsync") == 0)
184 return (HAST_REPLICATION_MEMSYNC);
185 if (strcmp(str, "async") == 0)
186 return (HAST_REPLICATION_ASYNC);
191 str2status(const char *str)
194 if (strcmp(str, "complete") == 0)
196 if (strcmp(str, "degraded") == 0)
202 hastctl(struct nv *nvin, struct nv **nvout)
204 struct hastd_config *cfg;
205 struct proto_conn *conn;
209 cfg = yy_config_parse(cfgpath, true);
213 /* Setup control connection... */
214 if (proto_client(NULL, cfg->hc_controladdr, &conn) == -1) {
215 pjdlog_error("Unable to setup control connection to %s",
216 cfg->hc_controladdr);
219 /* ...and connect to hastd. */
220 if (proto_connect(conn, HAST_TIMEOUT) == -1) {
221 pjdlog_error("Unable to connect to hastd via %s",
222 cfg->hc_controladdr);
226 /* Send the command to the server... */
227 if (hast_proto_send(NULL, conn, nvin, NULL, 0) == -1) {
228 pjdlog_error("Unable to send command to hastd via %s",
229 cfg->hc_controladdr);
233 /* ...and receive reply. */
234 if (hast_proto_recv_hdr(conn, &nv) == -1) {
235 pjdlog_error("cannot receive reply from hastd via %s",
236 cfg->hc_controladdr);
241 error = nv_get_int16(nv, "error");
243 pjdlog_error("Error %d received from hastd.", error);
253 set_role(const char *resource, int role)
255 struct nv *nvin, *nvout;
259 nv_add_string(nvin, resource, "resource%d", 0);
260 nv_add_uint8(nvin, HASTCTL_CMD_SETROLE, "cmd");
261 nv_add_uint8(nvin, role, "role");
262 error = hastctl(nvin, &nvout);
267 return (SNMP_ERR_NOERROR);
271 update_resources(void)
273 struct hast_snmp_resource *res;
274 struct nv *nvin, *nvout;
281 if (now - last_resources_update < UPDATE_INTERVAL)
284 last_resources_update = now;
289 nv_add_uint8(nvin, HASTCTL_CMD_STATUS, "cmd");
290 nv_add_string(nvin, "all", "resource%d", 0);
291 error = hastctl(nvin, &nvout);
297 str = nv_get_string(nvout, "resource%u", i);
300 res = calloc(1, sizeof(*res));
302 pjdlog_error("Unable to allocate %zu bytes for "
303 "resource", sizeof(*res));
307 strncpy(res->name, str, sizeof(res->name) - 1);
308 error = nv_get_int16(nvout, "error%u", i);
311 str = nv_get_string(nvout, "role%u", i);
312 res->role = str != NULL ? str2role(str) : HAST_ROLE_UNDEF;
313 str = nv_get_string(nvout, "provname%u", i);
315 strncpy(res->provname, str, sizeof(res->provname) - 1);
316 str = nv_get_string(nvout, "localpath%u", i);
318 strncpy(res->localpath, str,
319 sizeof(res->localpath) - 1);
321 res->extentsize = nv_get_uint32(nvout, "extentsize%u", i);
322 res->keepdirty = nv_get_uint32(nvout, "keepdirty%u", i);
323 str = nv_get_string(nvout, "remoteaddr%u", i);
325 strncpy(res->remoteaddr, str,
326 sizeof(res->remoteaddr) - 1);
328 str = nv_get_string(nvout, "sourceaddr%u", i);
330 strncpy(res->sourceaddr, str,
331 sizeof(res->sourceaddr) - 1);
333 str = nv_get_string(nvout, "replication%u", i);
334 res->replication = str != NULL ? str2replication(str) : -1;
335 str = nv_get_string(nvout, "status%u", i);
336 res->status = str != NULL ? str2status(str) : -1;
337 res->dirty = nv_get_uint64(nvout, "dirty%u", i);
338 res->reads = nv_get_uint64(nvout, "stat_read%u", i);
339 res->writes = nv_get_uint64(nvout, "stat_write%u", i);
340 res->deletes = nv_get_uint64(nvout, "stat_delete%u", i);
341 res->flushes = nv_get_uint64(nvout, "stat_flush%u", i);
342 res->activemap_updates =
343 nv_get_uint64(nvout, "stat_activemap_update%u", i);
345 nv_get_uint64(nvout, "stat_read_error%u", i);
347 nv_get_uint64(nvout, "stat_write_error%u", i);
349 nv_get_uint64(nvout, "stat_delete_error%u", i);
351 nv_get_uint64(nvout, "stat_flush_error%u", i);
352 res->workerpid = nv_get_int32(nvout, "workerpid%u", i);
354 nv_get_uint64(nvout, "local_queue_size%u", i);
356 nv_get_uint64(nvout, "send_queue_size%u", i);
358 nv_get_uint64(nvout, "recv_queue_size%u", i);
360 nv_get_uint64(nvout, "done_queue_size%u", i);
362 nv_get_uint64(nvout, "idle_queue_size%u", i);
363 TAILQ_INSERT_TAIL(&resources, res, link);
370 op_hastConfig(struct snmp_context *context, struct snmp_value *value,
371 u_int sub, u_int iidx __unused, enum snmp_op op)
375 which = value->var.subs[sub - 1];
380 case LEAF_hastConfigFile:
381 return (string_get(value, cfgpath, -1));
383 return (SNMP_ERR_RES_UNAVAIL);
387 case LEAF_hastConfigFile:
388 return (string_save(value, context, -1,
389 (u_char **)&cfgpath));
391 return (SNMP_ERR_RES_UNAVAIL);
393 case SNMP_OP_GETNEXT:
394 case SNMP_OP_ROLLBACK:
396 return (SNMP_ERR_NOERROR);
398 return (SNMP_ERR_RES_UNAVAIL);
403 op_hastResourceTable(struct snmp_context *context __unused,
404 struct snmp_value *value, u_int sub, u_int iidx __unused, enum snmp_op op)
406 struct hast_snmp_resource *res;
410 if (update_resources() == -1)
411 return (SNMP_ERR_RES_UNAVAIL);
413 which = value->var.subs[sub - 1];
416 case SNMP_OP_GETNEXT:
417 res = NEXT_OBJECT_INT(&resources, &value->var, sub);
419 return (SNMP_ERR_NOSUCHNAME);
420 value->var.len = sub + 1;
421 value->var.subs[sub] = res->index;
424 if (value->var.len - sub != 1)
425 return (SNMP_ERR_NOSUCHNAME);
426 res = FIND_OBJECT_INT(&resources, &value->var, sub);
428 return (SNMP_ERR_NOSUCHNAME);
431 res = FIND_OBJECT_INT(&resources, &value->var, sub);
433 return (SNMP_ERR_NOSUCHNAME);
435 case LEAF_hastResourceRole:
436 ret = set_role(res->name, value->v.integer);
437 /* force update on next run */
438 last_resources_update = 0;
441 ret = SNMP_ERR_NOT_WRITEABLE;
445 case SNMP_OP_ROLLBACK:
447 return (SNMP_ERR_NOERROR);
449 return (SNMP_ERR_RES_UNAVAIL);
452 ret = SNMP_ERR_NOERROR;
455 case LEAF_hastResourceIndex:
456 value->v.integer = res->index;
458 case LEAF_hastResourceName:
459 ret = string_get(value, res->name, -1);
461 case LEAF_hastResourceRole:
462 value->v.integer = res->role;
464 case LEAF_hastResourceProvName:
465 ret = string_get(value, res->provname, -1);
467 case LEAF_hastResourceLocalPath:
468 ret = string_get(value, res->localpath, -1);
470 case LEAF_hastResourceExtentSize:
471 value->v.integer = res->extentsize;
473 case LEAF_hastResourceKeepDirty:
474 value->v.integer = res->keepdirty;
476 case LEAF_hastResourceRemoteAddr:
477 ret = string_get(value, res->remoteaddr, -1);
479 case LEAF_hastResourceSourceAddr:
480 ret = string_get(value, res->sourceaddr, -1);
482 case LEAF_hastResourceReplication:
483 value->v.integer = res->replication;
485 case LEAF_hastResourceStatus:
486 value->v.integer = res->status;
488 case LEAF_hastResourceDirty:
489 value->v.counter64 = res->dirty;
491 case LEAF_hastResourceReads:
492 value->v.counter64 = res->reads;
494 case LEAF_hastResourceWrites:
495 value->v.counter64 = res->writes;
497 case LEAF_hastResourceDeletes:
498 value->v.counter64 = res->deletes;
500 case LEAF_hastResourceFlushes:
501 value->v.counter64 = res->flushes;
503 case LEAF_hastResourceActivemapUpdates:
504 value->v.counter64 = res->activemap_updates;
506 case LEAF_hastResourceReadErrors:
507 value->v.counter64 = res->read_errors;
509 case LEAF_hastResourceWriteErrors:
510 value->v.counter64 = res->write_errors;
512 case LEAF_hastResourceDeleteErrors:
513 value->v.counter64 = res->delete_errors;
515 case LEAF_hastResourceFlushErrors:
516 value->v.counter64 = res->flush_errors;
518 case LEAF_hastResourceWorkerPid:
519 value->v.integer = res->workerpid;
521 case LEAF_hastResourceLocalQueue:
522 value->v.uint32 = res->local_queue;
524 case LEAF_hastResourceSendQueue:
525 value->v.uint32 = res->send_queue;
527 case LEAF_hastResourceRecvQueue:
528 value->v.uint32 = res->recv_queue;
530 case LEAF_hastResourceDoneQueue:
531 value->v.uint32 = res->done_queue;
533 case LEAF_hastResourceIdleQueue:
534 value->v.uint32 = res->idle_queue;
537 ret = SNMP_ERR_RES_UNAVAIL;