summaryrefslogtreecommitdiff
path: root/src/bsnmp-regex.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/bsnmp-regex.c')
-rw-r--r--src/bsnmp-regex.c171
1 files changed, 171 insertions, 0 deletions
diff --git a/src/bsnmp-regex.c b/src/bsnmp-regex.c
new file mode 100644
index 0000000..a3a7385
--- /dev/null
+++ b/src/bsnmp-regex.c
@@ -0,0 +1,171 @@
+
+#include "usuals.h"
+
+#include <sys/queue.h>
+#include <sys/select.h>
+#include <syslog.h>
+#include <unistd.h>
+
+#include <bsnmp/snmpmod.h>
+
+#include "regex_tree.h"
+#include "regex_oid.h"
+
+/* our module handle */
+static struct lmodule *module;
+
+/* OIDs */
+static const struct asn_oid oid_regex = OIDX_regexData;
+
+/* the Object Resource registration index */
+static u_int reg_index;
+
+/* last time we've fetch the peer list */
+static uint64_t peers_tick;
+
+/* request sequence number generator */
+static uint16_t seqno;
+
+struct data_entry {
+ uint32_t index;
+ TAILQ_ENTRY(data_entry) link;
+
+ char *descr;
+ uint64_t last_update;
+ uint64_t value;
+};
+
+TAILQ_HEAD(data_entry_list, data_entry);
+
+/* list of peers */
+static struct data_entry_list entries = TAILQ_HEAD_INITIALIZER(entries);
+static uint32_t entry_count = 0;
+
+/* the initialisation function */
+static int
+module_init(struct lmodule *mod, int argc, char *argv[])
+{
+ module = mod;
+
+ if (argc != 0) {
+ syslog(LOG_ERR, "bad number of arguments for %s", __func__);
+ return (EINVAL);
+ }
+
+ return 0;
+}
+
+/* Module is started */
+static void
+module_start(void)
+{
+ reg_index = or_register(&oid_regex, "The MIB for regex data.", module);
+}
+
+/* Called, when the module is to be unloaded after it was successfully loaded */
+static int
+module_fini(void)
+{
+ or_unregister(reg_index);
+ return 0;
+}
+
+const struct snmp_module config = {
+ .comment = "This module implements SNMP listing of data from regular expressions",
+ .init = module_init,
+ .start = module_start,
+ .fini = module_fini,
+ .tree = regex_ctree,
+ .tree_size = regex_CTREE_SIZE,
+};
+
+/* System variables - read-only scalars only. */
+int
+op_data (struct snmp_context *ctx, struct snmp_value *value,
+ u_int sub, u_int iidx, enum snmp_op op)
+{
+ asn_subid_t which = value->var.subs[sub - 1];
+
+ switch (op) {
+ case SNMP_OP_GET:
+ break;
+
+ case SNMP_OP_SET:
+ return SNMP_ERR_NOT_WRITEABLE;
+
+ default:
+ ASSERT(0);
+ break;
+ }
+
+ switch (which) {
+ case LEAF_dataCount:
+ value->v.integer = entry_count;
+ break;
+
+ default:
+ ASSERT(0);
+ break;
+ }
+
+ return SNMP_ERR_NOERROR;
+}
+
+int
+op_dataentry (struct snmp_context *ctx, struct snmp_value *value,
+ u_int sub, u_int iidx, enum snmp_op op)
+{
+ asn_subid_t which = value->var.subs[sub - 1];
+ struct data_entry *data;
+
+ switch (op) {
+ case SNMP_OP_GETNEXT:
+ data = NEXT_OBJECT_INT (&entries, &value->var, sub);
+ if (data == NULL)
+ return SNMP_ERR_NOSUCHNAME;
+ value->var.len = sub + 1;
+ value->var.subs[sub] = data->index;
+ break;
+
+ case SNMP_OP_GET:
+ data = FIND_OBJECT_INT (&entries, &value->var, sub);
+ if (data == NULL)
+ return SNMP_ERR_NOSUCHNAME;
+ break;
+
+ case SNMP_OP_SET:
+ if (index_decode (&value->var, sub, iidx, &data))
+ return SNMP_ERR_NO_CREATION;
+ data = FIND_OBJECT_INT (&entries, &value->var, sub);
+ if (data != NULL)
+ return SNMP_ERR_NOT_WRITEABLE;
+ return SNMP_ERR_NO_CREATION;
+
+ default:
+ ASSERT(0);
+ break;
+ }
+
+ switch (which) {
+ case LEAF_dataIndex:
+ value->v.integer = data->index;
+ break;
+
+ case LEAF_dataDescr:
+ return (string_get (value, data->descr, -1));
+
+ case LEAF_dataLast:
+ value->v.uint32 = data->last_update;
+ break;
+
+ case LEAF_dataValue:
+ value->v.uint32 = data->value;
+ break;
+
+ default:
+ ASSERT(0);
+ break;
+ };
+
+ return SNMP_ERR_NOERROR;
+}