From d8b12900629ed73a78b27535f08c4f0a721a93be Mon Sep 17 00:00:00 2001 From: Jeff Gehlbach Date: Wed, 30 Jul 2014 17:51:55 -0400 Subject: [PATCH] CHANGES: snmpd: Add -cacheTime and -execType flags to "extend" config directive Add the following two flags to the "extend" config directive: -cacheTime specifies the cache timeout. -execType specifies the execution type (sh or exec). This enables non-volatile configuration of two aspects that so far have been configurable only temporarily via SETs. See also https://sourceforge.net/p/net-snmp/patches/1290/. --- agent/mibgroup/agent/extend.c | 27 +++++++++++++++++++++++++-- man/snmpd.conf.5.def | 10 +++++++++- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/agent/mibgroup/agent/extend.c b/agent/mibgroup/agent/extend.c index 48379a2fde..1f047be768 100644 --- a/agent/mibgroup/agent/extend.c +++ b/agent/mibgroup/agent/extend.c @@ -522,8 +522,28 @@ extend_parse_config(const char *token, char *cptr) size_t oid_len; extend_registration_block *eptr; int flags; + int cache_timeout = 0; + int exec_type = NS_EXTEND_ETYPE_EXEC; - cptr = copy_nword(cptr, exec_name, sizeof(exec_name)); + cptr = copy_nword(cptr, exec_name, sizeof(exec_name)); + if (strcmp(exec_name, "-cacheTime") == 0) { + char cache_timeout_str[32]; + + cptr = copy_nword(cptr, cache_timeout_str, sizeof(cache_timeout_str)); + /* If atoi can't do the conversion, it returns 0 */ + cache_timeout = atoi(cache_timeout_str); + cptr = copy_nword(cptr, exec_name, sizeof(exec_name)); + } + if (strcmp(exec_name, "-execType") == 0) { + char exec_type_str[16]; + + cptr = copy_nword(cptr, exec_type_str, sizeof(exec_type_str)); + if (strcmp(exec_type_str, "sh") == 0) + exec_type = NS_EXTEND_ETYPE_SHELL; + else + exec_type = NS_EXTEND_ETYPE_EXEC; + cptr = copy_nword(cptr, exec_name, sizeof(exec_name)); + } if ( *exec_name == '.' ) { oid_len = MAX_OID_LEN - 2; if (0 == read_objid( exec_name, oid_buf, &oid_len )) { @@ -545,7 +565,8 @@ extend_parse_config(const char *token, char *cptr) flags = (NS_EXTEND_FLAGS_ACTIVE | NS_EXTEND_FLAGS_CONFIG); if (!strcmp( token, "sh" ) || !strcmp( token, "extend-sh" ) || - !strcmp( token, "sh2" )) + !strcmp( token, "sh2") || + exec_type == NS_EXTEND_ETYPE_SHELL) flags |= NS_EXTEND_FLAGS_SHELL; if (!strcmp( token, "execFix" ) || !strcmp( token, "extendfix" ) || @@ -566,6 +587,8 @@ extend_parse_config(const char *token, char *cptr) extension->command = strdup( exec_command ); if (cptr) extension->args = strdup( cptr ); + if (cache_timeout != 0) + extension->cache->timeout = cache_timeout; } else { snmp_log(LOG_ERR, "Failed to register extend entry '%s' - possibly duplicate name.\n", exec_name ); return; diff --git a/man/snmpd.conf.5.def b/man/snmpd.conf.5.def index f643c329f9..5c4fb68094 100644 --- a/man/snmpd.conf.5.def +++ b/man/snmpd.conf.5.def @@ -1284,7 +1284,7 @@ Attempting to define an unaccompanied \fIexecfix\fR directive will fail. .PP \fIexec\fR and \fIsh\fR extensions can only be configured via the snmpd.conf file. They cannot be set up via SNMP SET requests. -.IP "extend [MIBOID] NAME PROG ARGS" +.IP "extend [-cacheTime TIME] [-execType TYPE] [MIBOID] NAME PROG ARGS" works in a similar manner to the \fIexec\fR directive, but with a number of improvements. The MIB tables (\fInsExtendConfigTable\fR etc) are indexed by the NAME token, so are unaffected by the order in @@ -1294,6 +1294,14 @@ containing the exit status, the first line and full output (as a single string) for each \fIextend\fR entry, and the other (\fInsExtendOutput2Table\fR) containing the complete output as a series of separate lines. .IP +If -cacheTime is specified, then its argument is used as the cache timeout +(in whole seconds) for this \fIextend\fR entry. This mechanism provides a +non-volatile way to specify the cache timeout. +.IP +If -execType is specified and has a value of \fIsh\fR, then this \fIextend\fR +entry will be run in a shell. Otherwise it will be run in the default \fIexec\fR +fashion. This mechanism provides a non-volatile way to specify the exec type. +.IP If MIBOID is specified, then the configuration and result tables will be rooted at this point in the OID tree, but are otherwise structured in exactly the same way. This means that several separate \fIextend\fR