2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2013 Mikolaj Golub <trociny@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/queue.h>
35 #include <bsnmp/snmpmod.h>
41 #include "hast_proto.h"
42 #include "hast_tree.h"
47 #define UPDATE_INTERVAL 500 /* update interval in ticks */
49 static struct lmodule *module;
51 static const struct asn_oid oid_hast = OIDX_begemotHast;
53 /* the Object Resource registration index */
54 static u_int hast_index = 0;
57 * Structure that describes single resource.
59 struct hast_snmp_resource {
60 TAILQ_ENTRY(hast_snmp_resource) link;
65 char provname[NAME_MAX];
66 char localpath[PATH_MAX];
69 char remoteaddr[HAST_ADDRSIZE];
70 char sourceaddr[HAST_ADDRSIZE];
78 uint64_t activemap_updates;
80 uint64_t write_errors;
81 uint64_t delete_errors;
82 uint64_t flush_errors;
91 static TAILQ_HEAD(, hast_snmp_resource) resources =
92 TAILQ_HEAD_INITIALIZER(resources);
94 /* Path to configuration file. */
95 static u_char *cfgpath;
96 /* Ticks of the last hast resources update. */
97 static uint64_t last_resources_update;
99 static void free_resources(void);
100 static int hastctl(struct nv *nvin, struct nv **nvout);
101 static int hast_fini(void);
102 static int hast_init(struct lmodule *mod, int argc, char *argv[]);
103 static void hast_start(void);
104 static int set_role(const char *resource, int role);
105 static int str2role(const char *str);
106 static int str2replication(const char *str);
107 static int str2status(const char *str);
108 static int update_resources(void);
110 const struct snmp_module config = {
111 .comment = "This module implements the BEGEMOT MIB for HAST.",
116 .tree_size = hast_CTREE_SIZE,
120 hast_init(struct lmodule *mod, int argc __unused, char *argv[] __unused)
125 pjdlog_init(PJDLOG_MODE_SYSLOG);
128 cfgpath = malloc(sizeof(HAST_CONFIG));
129 if (cfgpath == NULL) {
130 pjdlog_error("Unable to allocate %zu bytes for cfgpath",
131 sizeof(HAST_CONFIG));
134 strcpy(cfgpath, HAST_CONFIG);
141 hast_index = or_register(&oid_hast,
142 "The MIB module for BEGEMOT-HAST-MIB.", module);
149 or_unregister(hast_index);
158 struct hast_snmp_resource *res;
160 while ((res = TAILQ_FIRST(&resources)) != NULL) {
161 TAILQ_REMOVE(&resources, res, link);
167 str2role(const char *str)
170 if (strcmp(str, "init") == 0)
171 return (HAST_ROLE_INIT);
172 if (strcmp(str, "primary") == 0)
173 return (HAST_ROLE_PRIMARY);
174 if (strcmp(str, "secondary") == 0)
175 return (HAST_ROLE_SECONDARY);
176 return (HAST_ROLE_UNDEF);
180 str2replication(const char *str)
183 if (strcmp(str, "fullsync") == 0)
184 return (HAST_REPLICATION_FULLSYNC);
185 if (strcmp(str, "memsync") == 0)
186 return (HAST_REPLICATION_MEMSYNC);
187 if (strcmp(str, "async") == 0)
188 return (HAST_REPLICATION_ASYNC);
193 str2status(const char *str)
196 if (strcmp(str, "complete") == 0)
198 if (strcmp(str, "degraded") == 0)
204 hastctl(struct nv *nvin, struct nv **nvout)
206 struct hastd_config *cfg;
207 struct proto_conn *conn;
211 cfg = yy_config_parse(cfgpath, true);
215 /* Setup control connection... */
216 if (proto_client(NULL, cfg->hc_controladdr, &conn) == -1) {
217 pjdlog_error("Unable to setup control connection to %s",
218 cfg->hc_controladdr);
221 /* ...and connect to hastd. */
222 if (proto_connect(conn, HAST_TIMEOUT) == -1) {
223 pjdlog_error("Unable to connect to hastd via %s",
224 cfg->hc_controladdr);
228 /* Send the command to the server... */
229 if (hast_proto_send(NULL, conn, nvin, NULL, 0) == -1) {
230 pjdlog_error("Unable to send command to hastd via %s",
231 cfg->hc_controladdr);
235 /* ...and receive reply. */
236 if (hast_proto_recv_hdr(conn, &nv) == -1) {
237 pjdlog_error("cannot receive reply from hastd via %s",
238 cfg->hc_controladdr);
243 error = nv_get_int16(nv, "error");
245 pjdlog_error("Error %d received from hastd.", error);
255 set_role(const char *resource, int role)
257 struct nv *nvin, *nvout;
261 nv_add_string(nvin, resource, "resource%d", 0);
262 nv_add_uint8(nvin, HASTCTL_CMD_SETROLE, "cmd");
263 nv_add_uint8(nvin, role, "role");
264 error = hastctl(nvin, &nvout);
269 return (SNMP_ERR_NOERROR);
273 update_resources(void)
275 struct hast_snmp_resource *res;
276 struct nv *nvin, *nvout;
283 if (now - last_resources_update < UPDATE_INTERVAL)
286 last_resources_update = now;
291 nv_add_uint8(nvin, HASTCTL_CMD_STATUS, "cmd");
292 nv_add_string(nvin, "all", "resource%d", 0);
293 error = hastctl(nvin, &nvout);
299 str = nv_get_string(nvout, "resource%u", i);
302 res = calloc(1, sizeof(*res));
304 pjdlog_error("Unable to allocate %zu bytes for "
305 "resource", sizeof(*res));
309 strncpy(res->name, str, sizeof(res->name) - 1);
310 error = nv_get_int16(nvout, "error%u", i);
313 str = nv_get_string(nvout, "role%u", i);
314 res->role = str != NULL ? str2role(str) : HAST_ROLE_UNDEF;
315 str = nv_get_string(nvout, "provname%u", i);
317 strncpy(res->provname, str, sizeof(res->provname) - 1);
318 str = nv_get_string(nvout, "localpath%u", i);
320 strncpy(res->localpath, str,
321 sizeof(res->localpath) - 1);
323 res->extentsize = nv_get_uint32(nvout, "extentsize%u", i);
324 res->keepdirty = nv_get_uint32(nvout, "keepdirty%u", i);
325 str = nv_get_string(nvout, "remoteaddr%u", i);
327 strncpy(res->remoteaddr, str,
328 sizeof(res->remoteaddr) - 1);
330 str = nv_get_string(nvout, "sourceaddr%u", i);
332 strncpy(res->sourceaddr, str,
333 sizeof(res->sourceaddr) - 1);
335 str = nv_get_string(nvout, "replication%u", i);
336 res->replication = str != NULL ? str2replication(str) : -1;
337 str = nv_get_string(nvout, "status%u", i);
338 res->status = str != NULL ? str2status(str) : -1;
339 res->dirty = nv_get_uint64(nvout, "dirty%u", i);
340 res->reads = nv_get_uint64(nvout, "stat_read%u", i);
341 res->writes = nv_get_uint64(nvout, "stat_write%u", i);
342 res->deletes = nv_get_uint64(nvout, "stat_delete%u", i);
343 res->flushes = nv_get_uint64(nvout, "stat_flush%u", i);
344 res->activemap_updates =
345 nv_get_uint64(nvout, "stat_activemap_update%u", i);
347 nv_get_uint64(nvout, "stat_read_error%u", i);
349 nv_get_uint64(nvout, "stat_write_error%u", i);
351 nv_get_uint64(nvout, "stat_delete_error%u", i);
353 nv_get_uint64(nvout, "stat_flush_error%u", i);
354 res->workerpid = nv_get_int32(nvout, "workerpid%u", i);
356 nv_get_uint64(nvout, "local_queue_size%u", i);
358 nv_get_uint64(nvout, "send_queue_size%u", i);
360 nv_get_uint64(nvout, "recv_queue_size%u", i);
362 nv_get_uint64(nvout, "done_queue_size%u", i);
364 nv_get_uint64(nvout, "idle_queue_size%u", i);
365 TAILQ_INSERT_TAIL(&resources, res, link);
372 op_hastConfig(struct snmp_context *context, struct snmp_value *value,
373 u_int sub, u_int iidx __unused, enum snmp_op op)
377 which = value->var.subs[sub - 1];
382 case LEAF_hastConfigFile:
383 return (string_get(value, cfgpath, -1));
385 return (SNMP_ERR_RES_UNAVAIL);
389 case LEAF_hastConfigFile:
390 return (string_save(value, context, -1,
391 (u_char **)&cfgpath));
393 return (SNMP_ERR_RES_UNAVAIL);
395 case SNMP_OP_GETNEXT:
396 case SNMP_OP_ROLLBACK:
398 return (SNMP_ERR_NOERROR);
400 return (SNMP_ERR_RES_UNAVAIL);
405 op_hastResourceTable(struct snmp_context *context __unused,
406 struct snmp_value *value, u_int sub, u_int iidx __unused, enum snmp_op op)
408 struct hast_snmp_resource *res;
412 if (update_resources() == -1)
413 return (SNMP_ERR_RES_UNAVAIL);
415 which = value->var.subs[sub - 1];
418 case SNMP_OP_GETNEXT:
419 res = NEXT_OBJECT_INT(&resources, &value->var, sub);
421 return (SNMP_ERR_NOSUCHNAME);
422 value->var.len = sub + 1;
423 value->var.subs[sub] = res->index;
426 if (value->var.len - sub != 1)
427 return (SNMP_ERR_NOSUCHNAME);
428 res = FIND_OBJECT_INT(&resources, &value->var, sub);
430 return (SNMP_ERR_NOSUCHNAME);
433 res = FIND_OBJECT_INT(&resources, &value->var, sub);
435 return (SNMP_ERR_NOSUCHNAME);
437 case LEAF_hastResourceRole:
438 ret = set_role(res->name, value->v.integer);
439 /* force update on next run */
440 last_resources_update = 0;
443 ret = SNMP_ERR_NOT_WRITEABLE;
447 case SNMP_OP_ROLLBACK:
449 return (SNMP_ERR_NOERROR);
451 return (SNMP_ERR_RES_UNAVAIL);
454 ret = SNMP_ERR_NOERROR;
457 case LEAF_hastResourceIndex:
458 value->v.integer = res->index;
460 case LEAF_hastResourceName:
461 ret = string_get(value, res->name, -1);
463 case LEAF_hastResourceRole:
464 value->v.integer = res->role;
466 case LEAF_hastResourceProvName:
467 ret = string_get(value, res->provname, -1);
469 case LEAF_hastResourceLocalPath:
470 ret = string_get(value, res->localpath, -1);
472 case LEAF_hastResourceExtentSize:
473 value->v.integer = res->extentsize;
475 case LEAF_hastResourceKeepDirty:
476 value->v.integer = res->keepdirty;
478 case LEAF_hastResourceRemoteAddr:
479 ret = string_get(value, res->remoteaddr, -1);
481 case LEAF_hastResourceSourceAddr:
482 ret = string_get(value, res->sourceaddr, -1);
484 case LEAF_hastResourceReplication:
485 value->v.integer = res->replication;
487 case LEAF_hastResourceStatus:
488 value->v.integer = res->status;
490 case LEAF_hastResourceDirty:
491 value->v.counter64 = res->dirty;
493 case LEAF_hastResourceReads:
494 value->v.counter64 = res->reads;
496 case LEAF_hastResourceWrites:
497 value->v.counter64 = res->writes;
499 case LEAF_hastResourceDeletes:
500 value->v.counter64 = res->deletes;
502 case LEAF_hastResourceFlushes:
503 value->v.counter64 = res->flushes;
505 case LEAF_hastResourceActivemapUpdates:
506 value->v.counter64 = res->activemap_updates;
508 case LEAF_hastResourceReadErrors:
509 value->v.counter64 = res->read_errors;
511 case LEAF_hastResourceWriteErrors:
512 value->v.counter64 = res->write_errors;
514 case LEAF_hastResourceDeleteErrors:
515 value->v.counter64 = res->delete_errors;
517 case LEAF_hastResourceFlushErrors:
518 value->v.counter64 = res->flush_errors;
520 case LEAF_hastResourceWorkerPid:
521 value->v.integer = res->workerpid;
523 case LEAF_hastResourceLocalQueue:
524 value->v.uint32 = res->local_queue;
526 case LEAF_hastResourceSendQueue:
527 value->v.uint32 = res->send_queue;
529 case LEAF_hastResourceRecvQueue:
530 value->v.uint32 = res->recv_queue;
532 case LEAF_hastResourceDoneQueue:
533 value->v.uint32 = res->done_queue;
535 case LEAF_hastResourceIdleQueue:
536 value->v.uint32 = res->idle_queue;
539 ret = SNMP_ERR_RES_UNAVAIL;