From 1ded2732d4c290dbff64d8091def1f70b76f6282 Mon Sep 17 00:00:00 2001 From: Stef Walter Date: Thu, 26 Jan 2006 09:54:31 +0000 Subject: SNMP support coded in. --- src/config.c | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 76 insertions(+), 7 deletions(-) (limited to 'src/config.c') diff --git a/src/config.c b/src/config.c index 066203f..ca64514 100644 --- a/src/config.c +++ b/src/config.c @@ -57,7 +57,8 @@ typedef struct _config_ctx { const char* confname; char* configmem; - uint32_t interval; + uint interval; + uint timeout; rb_item* items; } config_ctx; @@ -77,6 +78,48 @@ config_ctx; * CONFIG LOADING */ +static rb_item* +sort_items_by_host(rb_item *item) +{ + rb_item *sort = NULL; + rb_item *cur; + rb_item *it; + + while(item) + { + cur = item; + item = item->next; + cur->next = NULL; + + /* First item */ + if(!sort) + { + sort = cur; + continue; + } + + /* Before first item */ + else if(cur->host <= sort->host) + { + cur->next = sort; + sort = cur; + continue; + } + + for(it = sort; it->next; it = it->next) + { + if(cur->host <= sort->next->host) + break; + } + + ASSERT(it); + cur->next = it->next; + it->next = cur; + } + + return sort; +} + static void config_done(config_ctx* ctx) { @@ -93,8 +136,12 @@ config_done(config_ctx* ctx) if(ctx->interval == 0) errx(2, "no interval specified in config file: %s", ctx->confname); + if(ctx->timeout == 0) + ctx->timeout = g_state.timeout; + /* And a nice key for lookups */ - snprintf(key, sizeof(key), "%d:%s/%s.rrd", ctx->interval, g_state.rrddir, ctx->confname); + snprintf(key, sizeof(key), "%d-%d:%s/%s.rrd", ctx->timeout, + ctx->interval, g_state.rrddir, ctx->confname); key[sizeof(key) - 1] = 0; /* See if we have one of these pollers already */ @@ -111,7 +158,8 @@ config_done(config_ctx* ctx) ASSERT(t); poll->rrdname = t + 1; - poll->interval = ctx->interval; + poll->interval = ctx->interval * 1000; + poll->timeout = ctx->timeout * 1000; /* Add it to the main lists */ poll->next = g_state.polls; @@ -121,11 +169,13 @@ config_done(config_ctx* ctx) /* Get the last item and add to the list */ for(it = ctx->items; it->next; it = it->next) it->poller = poll; - it->next->poller = poll; + + ASSERT(it); + it->poller = poll; /* Add the items to this poller */ it->next = poll->items; - poll->items = ctx->items; + poll->items = sort_items_by_host(ctx->items); /* * This remains allocated for the life of the program as @@ -203,10 +253,15 @@ parse_item(const char* field, char* uri, config_ctx *ctx) parse_uri(uri, &scheme, &host, &user, &path, ctx); ASSERT(scheme && host && path); + /* TODO: SNMP version support */ + /* Currently we only support SNMP pollers */ if(strcmp(scheme, CONFIG_SNMP) != 0) errx(2, "invalid poll scheme: %s", scheme); + /* TODO: THis code assumes all hosts have the same community + the lookups below won't work wehn host/community is different */ + /* See if we can find an associated host */ rhost = (rb_host*)hsh_get(g_state.host_by_name, host, -1); if(!rhost) @@ -217,7 +272,20 @@ parse_item(const char* field, char* uri, config_ctx *ctx) if(!rhost || !hsh_set(g_state.host_by_name, host, -1, rhost)) errx(1, "out of memory"); + /* TODO: Version support */ + rhost->version = 1; rhost->name = host; + rhost->community = user ? user : "public"; + + /* TODO: Eventually resolving should be in a separate thread, + and done regularly */ + if(sock_any_pton(host, &(rhost->address), + SANY_OPT_DEFPORT(161) | SANY_OPT_DEFLOCAL) == -1) + { + rb_message(LOG_WARNING, "couldn't resolve host address (ignoring): %s", host); + free(rhost); + return; + } /* And add it to the list */ rhost->next = g_state.hosts; @@ -230,12 +298,13 @@ parse_item(const char* field, char* uri, config_ctx *ctx) errx(1, "out of memory"); ritem->rrdfield = field; - ritem->community = user ? user : "public"; ritem->host = rhost; ritem->poller = NULL; /* Set later in config_done */ + ritem->req = NULL; + ritem->value = RB_UNKNOWN; /* And parse the OID */ - if(rb_parse_mib(path, &(ritem->value)) == -1) + if(rb_parse_mib(path, &(ritem->snmpfield)) == -1) errx(2, "invalid OID: %s", path + 1); /* And add it to the list */ -- cgit v1.2.3