From a74068d0f3cc366df5296d9ddab26157ec7c632c Mon Sep 17 00:00:00 2001 From: Daniele Sluijters Date: Wed, 23 Nov 2022 19:28:23 +0100 Subject: [PATCH] Retries, timeout, version configurable per job This makes retries, timeout and SNMP version configurable per job. It overrides the module's equivalent WalkParams if they're present as query attributes. Partially addresses: #762 --- main.go | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/main.go b/main.go index c249ffd67..dcbc6dbd7 100644 --- a/main.go +++ b/main.go @@ -19,6 +19,7 @@ import ( _ "net/http/pprof" "os" "os/signal" + "strconv" "sync" "syscall" "time" @@ -84,8 +85,68 @@ func handler(w http.ResponseWriter, r *http.Request, logger log.Logger) { if moduleName == "" { moduleName = "if_mib" } + + timeout := query.Get("timeout") + if len(query["timeout"]) > 1 { + http.Error(w, "'timeout' parameter must only be specified once", 400) + snmpRequestErrors.Inc() + return + } + var ptimeout *time.Duration + if timeout != "" { + p, err := time.ParseDuration(timeout) + if err != nil { + http.Error(w, "'timeout' parameter must be a valid Go duration string", 400) + snmpRequestErrors.Inc() + return + } + ptimeout = &p + } + retries := query.Get("retries") + if len(query["retries"]) > 1 { + http.Error(w, "'retries' parameter must only be specified once", 400) + snmpRequestErrors.Inc() + return + } + var pretries *int + if retries != "" { + i, err := strconv.ParseInt(retries, 0, 64) + if err != nil { + http.Error(w, "'retries' parameter must be an integer", 400) + snmpRequestErrors.Inc() + return + } + ii := int(i) + pretries = &ii + } + version := query.Get("version") + if len(query["version"]) > 1 { + http.Error(w, "'version' parameter must only be specified once", 400) + snmpRequestErrors.Inc() + return + } + var pversion *int + if version != "" { + i, err := strconv.ParseInt(version, 0, 64) + if err != nil { + http.Error(w, "'version' parameter must be an integer", 400) + snmpRequestErrors.Inc() + return + } + ii := int(i) + pversion = &ii + } sc.RLock() module, ok := (*(sc.C))[moduleName] + if ptimeout != nil { + module.WalkParams.Timeout = *ptimeout + } + if pretries != nil { + module.WalkParams.Retries = pretries + } + if pversion != nil { + module.WalkParams.Version = *pversion + } sc.RUnlock() if !ok { http.Error(w, fmt.Sprintf("Unknown module '%s'", moduleName), 400)