diff options
Diffstat (limited to 'daemon')
-rw-r--r-- | daemon/config.c | 32 | ||||
-rw-r--r-- | daemon/rrdbotd.h | 7 | ||||
-rw-r--r-- | daemon/snmp-engine.c | 14 |
3 files changed, 33 insertions, 20 deletions
diff --git a/daemon/config.c b/daemon/config.c index 4245279..34cecfb 100644 --- a/daemon/config.c +++ b/daemon/config.c @@ -160,8 +160,6 @@ config_done(config_ctx* ctx) if(!poll) { poll = (rb_poller*)xcalloc(sizeof(*poll)); - if(!hsh_set(g_state.poll_by_key, key, -1, poll)) - errx(1, "out of memory"); strcpy(poll->key, key); t = strchr(poll->key, ':'); @@ -174,6 +172,10 @@ config_done(config_ctx* ctx) /* Add it to the main lists */ poll->next = g_state.polls; g_state.polls = poll; + + /* And into the hashtable */ + if(!hsh_set(g_state.poll_by_key, poll->key, -1, poll)) + errx(1, "out of memory"); } /* Get the last item and add to the list */ @@ -204,6 +206,7 @@ config_done(config_ctx* ctx) static rb_item* parse_item(const char* field, char* uri, config_ctx *ctx) { + char key[128]; rb_item *ritem; rb_host *rhost; int r; @@ -235,21 +238,24 @@ parse_item(const char* field, char* uri, config_ctx *ctx) else errx(2, "%s: invalid poll scheme: %s", ctx->confname, scheme); - /* TODO: THis code assumes all hosts have the same community - the lookups below won't work wehn host/community is different */ + /* + * Build a lookup key. We can only combine requests for the same + * host when the version and community match. + */ + user = user ? user : "public"; + snprintf(key, sizeof(key), "%d:%s:%s", version, host, user); + key[sizeof(key) - 1] = 0; /* See if we can find an associated host */ - rhost = (rb_host*)hsh_get(g_state.host_by_name, host, -1); + rhost = (rb_host*)hsh_get(g_state.host_by_key, key, -1); if(!rhost) { /* Make a new one if necessary */ rhost = (rb_host*)xcalloc(sizeof(*rhost)); - if(!hsh_set(g_state.host_by_name, host, -1, rhost)) - errx(1, "out of memory"); rhost->version = version; - rhost->name = host; - rhost->community = user ? user : "public"; + rhost->hostname = host; + rhost->community = user; rhost->is_resolved = 1; rhost->resolve_interval = 0; rhost->last_resolved = 0; @@ -276,6 +282,10 @@ parse_item(const char* field, char* uri, config_ctx *ctx) /* And add it to the list */ rhost->next = g_state.hosts; g_state.hosts = rhost; + + /* And into the hash table */ + if(!hsh_set(g_state.host_by_key, rhost->key, -1, rhost)) + errx(1, "out of memory"); } /* Make a new item */ @@ -386,7 +396,7 @@ rb_config_parse() /* Setup the hash tables properly */ g_state.poll_by_key = hsh_create(); - g_state.host_by_name = hsh_create(); + g_state.host_by_key = hsh_create(); memset(&ctx, 0, sizeof(ctx)); @@ -514,7 +524,7 @@ void rb_config_free() { hsh_free(g_state.poll_by_key); - hsh_free(g_state.host_by_name); + hsh_free(g_state.host_by_key); free_hosts(g_state.hosts); diff --git a/daemon/rrdbotd.h b/daemon/rrdbotd.h index 35b3912..70e09fa 100644 --- a/daemon/rrdbotd.h +++ b/daemon/rrdbotd.h @@ -94,7 +94,10 @@ rb_item; typedef struct _rb_host { - const char* name; + /* The hash key is version:hostname:community */ + char key[128]; + + const char* hostname; const char* community; int version; @@ -146,7 +149,7 @@ typedef struct _rb_state /* Quick lookups for responses */ hsh_t* poll_by_key; - hsh_t* host_by_name; + hsh_t* host_by_key; } rb_state; diff --git a/daemon/snmp-engine.c b/daemon/snmp-engine.c index c2afbe9..1d1b64a 100644 --- a/daemon/snmp-engine.c +++ b/daemon/snmp-engine.c @@ -245,7 +245,7 @@ send_req(rb_request* req, mstime when) { if(req->sent <= 1) rb_messagex(LOG_DEBUG, "skipping snmp request: host not resolved: %s", - req->host->name); + req->host->hostname); return; } @@ -259,9 +259,9 @@ send_req(rb_request* req, mstime when) ret = sendto(snmp_socket, snmp_buffer, b.asn_ptr - snmp_buffer, 0, &SANY_ADDR(req->host->address), SANY_LEN(req->host->address)); if(ret == -1) - rb_message(LOG_ERR, "couldn't send snmp packet to: %s", req->host->name); + rb_message(LOG_ERR, "couldn't send snmp packet to: %s", req->host->hostname); else - rb_messagex(LOG_DEBUG, "sent request #%d to: %s", req->id, req->host->name); + rb_messagex(LOG_DEBUG, "sent request #%d to: %s", req->id, req->host->hostname); } } @@ -619,7 +619,7 @@ resolve_cb(int ecode, struct addrinfo* ai, void* arg) if(ecode) { - rb_messagex(LOG_WARNING, "couldn't resolve hostname: %s: %s", host->name, + rb_messagex(LOG_WARNING, "couldn't resolve hostname: %s: %s", host->hostname, gai_strerror(ecode)); return; } @@ -630,7 +630,7 @@ resolve_cb(int ecode, struct addrinfo* ai, void* arg) host->last_resolved = server_get_time(); host->is_resolved = 1; - rb_messagex(LOG_DEBUG, "resolved host: %s", host->name); + rb_messagex(LOG_DEBUG, "resolved host: %s", host->hostname); } static int @@ -648,8 +648,8 @@ resolve_timer(mstime when, void* arg) if(when - host->resolve_interval > host->last_resolve_try) { /* Automatically strips port number */ - rb_messagex(LOG_DEBUG, "resolving host: %s", host->name); - async_resolver_queue(host->name, "161", resolve_cb, host); + rb_messagex(LOG_DEBUG, "resolving host: %s", host->hostname); + async_resolver_queue(host->hostname, "161", resolve_cb, host); host->last_resolve_try = when; } |