From 1e735c038c86294df2ecfbd6a39abcfab4b3e8c3 Mon Sep 17 00:00:00 2001 From: Stef Walter Date: Tue, 4 Apr 2006 21:07:18 +0000 Subject: Move functionality for parsing MIBs, SNMP into common files. --- daemon/Makefile.am | 4 ++-- daemon/config.c | 65 +++++++++++----------------------------------------- daemon/rrdbotd.c | 12 ++++------ daemon/rrdbotd.h | 19 --------------- daemon/snmp-engine.c | 53 +++++++----------------------------------- 5 files changed, 27 insertions(+), 126 deletions(-) (limited to 'daemon') diff --git a/daemon/Makefile.am b/daemon/Makefile.am index 466353b..63f5169 100644 --- a/daemon/Makefile.am +++ b/daemon/Makefile.am @@ -2,13 +2,13 @@ sbin_PROGRAMS = rrdbotd rrdbotd_SOURCES = rrdbotd.c rrdbotd.h config.c \ - snmp-help.c snmp-engine.c rrd-update.c \ + snmp-engine.c rrd-update.c \ ../common/server-mainloop.c ../common/server-mainloop.h \ ../common/sock-any.h ../common/sock-any.c \ ../common/compat.h ../common/compat.c \ ../common/hash.h ../common/hash.c \ ../common/config-parser.h ../common/config-parser.c \ - ../mib/parse.c + ../mib/mib-parser.h ../mib/mib-parser.c rrdbotd_CFLAGS = -I${top_srcdir}/common/ -I${top_srcdir}/bsnmp/ -I${top_srcdir} \ -DCONF_PREFIX=\"$(sysconfdir)\" -DDATA_PREFIX=\"$(datadir)\" rrdbotd_LDADD = $(top_builddir)/bsnmp/libbsnmp-custom.a diff --git a/daemon/config.c b/daemon/config.c index 9a5320b..d95de8a 100644 --- a/daemon/config.c +++ b/daemon/config.c @@ -44,6 +44,8 @@ #include #include +#include + #include "rrdbotd.h" #include "config-parser.h" @@ -189,68 +191,27 @@ config_done(config_ctx* ctx) ctx->timeout = 0; } -static void -parse_uri(char *uri, char** scheme, char** host, - char** user, char** path, config_ctx* ctx) -{ - /* Copy only for error messages as we mess with original */ - char* copy = strdup(uri); - char* t; - - *host = NULL; - *path = NULL; - *user = NULL; - - *scheme = strsep(&uri, ":"); - if(uri == NULL) - errx(2, "%s: invalid poll uri (scheme invalid): %s", ctx->confname, copy); - - if((uri[0] != '/' && uri[1] != '/')) - errx(2, "%s: invalid poll uri (scheme invalid): %s", ctx->confname, copy); - - uri += 2; - *host = strsep(&uri, "/"); - if(*host[0]) - { - /* Parse the user name out from the host */ - t = strchr(*host, '@'); - if(t) - { - *t = 0; - *user = *host; - *host = t + 1; - } - } - - if(!*host[0]) - errx(2, "%s: invalid poll uri (no hostname found): %s", ctx->confname, copy); - - if(!uri || !uri[0] || !uri[1]) - errx(2, "%s: invalid poll uri (no pathname found): %s", ctx->confname, copy); - - *path = uri; - - while((*path)[0] == '/') - (*path)++; - - /* This copy only for error messages */ - free(copy); -} - static rb_item* parse_item(const char* field, char* uri, config_ctx *ctx) { rb_item *ritem; rb_host *rhost; + const char *msg; + char* copy; + char* scheme; char* host; char* user; - char* scheme; char* path; /* Parse the SNMP URI */ - parse_uri(uri, &scheme, &host, &user, &path, ctx); - ASSERT(scheme && host && path); + copy = strdup(uri); + msg = cfg_parse_uri(uri, &scheme, &host, &user, &path); + if(msg) + errx(2, "%s: %s: %s", ctx->confname, msg, copy); + free(copy); + + ASSERT(host && path); /* TODO: SNMP version support */ @@ -299,7 +260,7 @@ parse_item(const char* field, char* uri, config_ctx *ctx) ritem->vtype = VALUE_UNSET; /* And parse the OID */ - if(rb_snmp_parse_mib(path, &(ritem->snmpfield)) == -1) + if(mib_parse(path, &(ritem->snmpfield)) == -1) errx(2, "%s: invalid MIB: %s", ctx->confname, path); rb_messagex(LOG_DEBUG, "parsed MIB into oid: %s -> %s", path, diff --git a/daemon/rrdbotd.c b/daemon/rrdbotd.c index 261955d..e2b0248 100644 --- a/daemon/rrdbotd.c +++ b/daemon/rrdbotd.c @@ -46,6 +46,7 @@ #include #include +#include #include "rrdbotd.h" #include "server-mainloop.h" @@ -53,7 +54,6 @@ /* The default command line options */ #define DEFAULT_CONFIG CONF_PREFIX "/rrdbot" #define DEFAULT_WORK "/var/db/rrdbot" -#define DEFAULT_MIB DATA_PREFIX "/mib" #define DEFAULT_RETRIES 3 #define DEFAULT_TIMEOUT 5 @@ -64,10 +64,6 @@ /* The one main state object */ rb_state g_state; -/* Whether we print warnings when loading MIBs or not */ -const char* g_mib_directory = DEFAULT_MIB; -int g_mib_warnings = 0; - /* Some logging flags */ static int daemonized = 0; static int debug_level = LOG_ERR; @@ -251,12 +247,12 @@ main(int argc, char* argv[]) /* mib directory */ case 'm': - g_mib_directory = optarg; + mib_directory = optarg; break; /* MIB load warnings */ case 'M': - g_mib_warnings = 1; + mib_warnings = 1; break; /* Write out a pid file */ @@ -309,7 +305,7 @@ main(int argc, char* argv[]) rb_config_parse(); /* As an optimization we unload the MIB processing data here */ - rb_mib_uninit(); + mib_uninit(); /* Rev up the main engine */ rb_snmp_engine_init(); diff --git a/daemon/rrdbotd.h b/daemon/rrdbotd.h index 0b7b0e1..061beb2 100644 --- a/daemon/rrdbotd.h +++ b/daemon/rrdbotd.h @@ -166,12 +166,6 @@ void rb_vmessage(int level, int err, const char* msg, va_list ap); void rb_config_parse(); void rb_config_free(); -/* ----------------------------------------------------------------------------- - * SNMP HELPERS (snmp-help.c) - */ - -int rb_snmp_parse_mib(const char* oid, struct snmp_value* value); - /* ----------------------------------------------------------------------------- * SNMP ENGINE (snmp-engine.c) */ @@ -185,17 +179,4 @@ void rb_snmp_engine_uninit(); void rb_rrd_update(rb_poller *poll); -/* ----------------------------------------------------------------------------- - * MIB PARSING - */ - -typedef void* mib_node; - -void rb_mib_init(const char* dir, int warnings); -mib_node rb_mib_lookup(const char* match); -int rb_mib_subid(mib_node n, const char* name); -void rb_mib_oid(mib_node n, struct asn_oid* oid); -mib_node rb_mib_node(struct asn_oid* oid); -void rb_mib_uninit(); - #endif /* __RRDBOTD_H__ */ diff --git a/daemon/snmp-engine.c b/daemon/snmp-engine.c index a1f773c..e4bf9b7 100644 --- a/daemon/snmp-engine.c +++ b/daemon/snmp-engine.c @@ -57,37 +57,6 @@ static uint32_t snmp_request = 100000; /* Since we only deal with one packet at a time, global buffer */ static unsigned char snmp_buffer[0x1000]; -/* ----------------------------------------------------------------------------- - * CONSTANTS - */ - -/* All SNMP error messages */ -static struct -{ - int code; - const char* msg; -} snmp_errmsgs[] = { - { SNMP_ERR_NOERROR, "Success" }, - { SNMP_ERR_TOOBIG, "SNMP packet or response too big" }, - { SNMP_ERR_NOSUCHNAME, "Unknown variable name" }, - { SNMP_ERR_BADVALUE, "Incorrect syntax or value when modifing a variable" }, - { SNMP_ERR_READONLY, "Value is readonly" }, - { SNMP_ERR_GENERR, "Unspecified general error" }, - { SNMP_ERR_NO_ACCESS, "Access was denied to the object" }, - { SNMP_ERR_WRONG_TYPE, "The object type is incorrect for the object" }, - { SNMP_ERR_WRONG_LENGTH, "Incorrect specified for the object." }, - { SNMP_ERR_WRONG_ENCODING, "Incorrect encoding specified for the object." }, - { SNMP_ERR_WRONG_VALUE, "Not possible to set this value for the object." }, - { SNMP_ERR_NO_CREATION, "The value cannot be created." }, - { SNMP_ERR_INCONS_VALUE, "Not possible to set the value at this time." }, - { SNMP_ERR_RES_UNAVAIL, "The resource is not availeble" }, - { SNMP_ERR_COMMIT_FAILED, "Commit failed" }, - { SNMP_ERR_UNDO_FAILED, "Undo failed" }, - { SNMP_ERR_AUTH_ERR, "A problem occured during authentication" }, - { SNMP_ERR_NOT_WRITEABLE, "The variable cannot be written or created." }, - { SNMP_ERR_INCONS_NAME, "The variable does not exist." } -}; - /* ----------------------------------------------------------------------------- * REQUESTS */ @@ -526,7 +495,7 @@ receive_resp(int fd, int type, void* arg) struct asn_buf b; rb_request* req; const char* msg; - int len, ret, i; + int len, ret; int32_t ip; ASSERT(snmp_socket == fd); @@ -570,19 +539,13 @@ receive_resp(int fd, int type, void* arg) /* Check for errors */ if(pdu.error_status != SNMP_ERR_NOERROR) { - msg = NULL; - for(i = 0; i < countof(snmp_errmsgs); i++) - { - if(pdu.error_status == snmp_errmsgs[i].code) - { - rb_message(LOG_ERR, "snmp error from host '%s': %s", - hostname, snmp_errmsgs[i].msg); - return; - } - } - - rb_message(LOG_ERR, "unknown snmp error from host '%s': %d", - hostname, pdu.error_status); + msg = snmp_get_errmsg (pdu.error_status); + if(msg) + rb_message(LOG_ERR, "snmp error from host '%s': %s", + hostname, msg); + else + rb_message(LOG_ERR, "unknown snmp error from host '%s': %d", + hostname, pdu.error_status); return; } -- cgit v1.2.3