Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup snmp plugins #9055

Closed
wants to merge 3 commits into from
Closed

Cleanup snmp plugins #9055

wants to merge 3 commits into from

Conversation

Hipska
Copy link
Contributor

@Hipska Hipska commented Mar 26, 2021

Required for all PRs:

  • Updated associated README.md.
  • Wrote appropriate unit tests.

Are there any more tests or parts of the plugin that could need cleanup?

@Hipska Hipska added discussion Topics for discussion feat Improvement on an existing feature such as adding a new setting/mode to an existing plugin area/snmp plugin/input 1. Request for new input plugins 2. Issues/PRs that are related to input plugins labels Mar 26, 2021
Copy link
Contributor

@telegraf-tiger telegraf-tiger bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤝 ✅ CLA has been signed. Thank you!

@Hipska Hipska marked this pull request as draft March 26, 2021 12:50
@Hipska Hipska changed the title Cleanup snmp input Cleanup snmp plugin Mar 26, 2021
Copy link
Contributor

@telegraf-tiger telegraf-tiger bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ssoroka
Copy link
Contributor

ssoroka commented Apr 13, 2021

Looks okay, I'm not sure that the regular expression is any clearer, but it's fine. Looks like we don't have test coverage of this conditional branch? Are you able to add a test for this?

@Hipska Hipska marked this pull request as ready for review April 20, 2021 06:52
@Hipska
Copy link
Contributor Author

Hipska commented Apr 20, 2021

@ssoroka Tests that use this part of the code:

  • TestFieldInit
  • TestTableInit
  • TestSnmpInit
  • TestSnmpTranslateCache_miss (but that one is not testing the value on correctness)

@Hipska Hipska requested review from reimda and ssoroka April 20, 2021 08:53
@Hipska Hipska requested a review from srebhan May 5, 2021 10:26
@Hipska Hipska removed the discussion Topics for discussion label May 11, 2021
@Hipska
Copy link
Contributor Author

Hipska commented May 11, 2021

@ssoroka @reimda What about moving SnmpTranslate and related methods into internal/snmp?

@reimda
Copy link
Contributor

reimda commented May 11, 2021

@ssoroka @reimda What about moving SnmpTranslate and related methods into internal/snmp?

That's not a bad idea as long as snmp and snmp_trap end up using the same translate code. I don't know exactly why I didn't do it when I wrote snmp_trap.

@Hipska Hipska marked this pull request as draft May 11, 2021 14:44
@Hipska Hipska removed the request for review from srebhan May 11, 2021 14:44
@Hipska
Copy link
Contributor Author

Hipska commented May 11, 2021

Great, will try to do that. It will also be good as preparation to switch to using gosmi.

@Hipska Hipska changed the title Cleanup snmp plugin Cleanup snmp plugins May 11, 2021
Copy link
Member

@srebhan srebhan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure I'm of help here. Code change looks sensible, but I've not been much into SNMP. :-)

@reimda
Copy link
Contributor

reimda commented May 25, 2021

Hi @Hipska is this meant to be a draft still? Maybe you're keeping it as a draft until you can move things to internal/snmp?

@Hipska
Copy link
Contributor Author

Hipska commented May 26, 2021

Yes indeed, but don’t have much time for it now.

@Hipska Hipska marked this pull request as ready for review July 1, 2021 08:32
@Hipska
Copy link
Contributor Author

Hipska commented Jul 1, 2021

As I understood @MyaLongmire will work on implementing gosmi, and so also move SnmpTranslate and other related methods to internal/snmp namespace. So I can make this PR as ready to review. Please do and merge.

@Hipska Hipska requested review from reimda and removed request for reimda and ssoroka July 1, 2021 08:32
plugins/inputs/snmp/snmp.go Show resolved Hide resolved
Comment on lines -915 to +914
objs := strings.TrimPrefix(line, "::= { ")
objs = strings.TrimSuffix(objs, " }")
re := regexp.MustCompile(`(?:\w+\()?(\d+)\)?`)

for _, obj := range strings.Split(objs, " ") {
if len(obj) == 0 {
continue
}
if i := strings.Index(obj, "("); i != -1 {
obj = obj[i+1:]
oidNum += "." + obj[:strings.Index(obj, ")")]
} else {
oidNum += "." + obj
}
for _, match := range re.FindAllStringSubmatch(line, -1) {
oidNum += "." + match[1]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes the code shorter but I'm not sure it's more maintainable to use regexp instead of manually splitting the string. Do we have unit tests that cover this? I'm not sure it does the exact same thing as before.

plugins/inputs/snmp/snmp_test.go Show resolved Hide resolved
@Hipska
Copy link
Contributor Author

Hipska commented Oct 14, 2021

Closing since it will mostly be irrelevant when #9518 is merged

@Hipska Hipska closed this Oct 14, 2021
@Hipska
Copy link
Contributor Author

Hipska commented Nov 8, 2021

Okay, I will reopen since that PR is now creating a new snmp plugin, so my improvements here are again relevant. @reimda can you finally merge please?

@Hipska Hipska reopened this Nov 8, 2021
@Hipska Hipska requested a review from reimda November 8, 2021 09:27
@@ -9,6 +9,7 @@ import (
"math"
"net"
"os/exec"
"regexp"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Hipska maybe in this cleanup you can include fixing linters findings?

plugins/inputs/snmp/snmp.go:1:9                  revive       import-shadowing: The name 'snmp' shadows an import name
plugins/inputs/snmp/snmp.go:8:2                  revive       imports-blacklist: should not use the following blacklisted import: "log"
plugins/inputs/snmp/snmp.go:376:27               revive       flag-parameter: parameter 'walk' seems to be a control flag, avoid control coupling
plugins/inputs/snmp/snmp.go:406:21               revive       flag-parameter: parameter 'walk' seems to be a control flag, avoid control coupling
plugins/inputs/snmp/snmp.go:594:15               unconvert    unnecessary conversion
plugins/inputs/snmp/snmp.go:692:10               revive       early-return: if c {...} else {... return } can be simplified to if !c { ... return } ...
plugins/inputs/snmp/snmp.go:748:1                revive       function-result-limit: maximum number of return results per function exceeded; max 3 but got 5
plugins/inputs/snmp/snmp.go:765:1                revive       function-result-limit: maximum number of return results per function exceeded; max 3 but got 5
plugins/inputs/snmp/snmp.go:836:1                revive       function-result-limit: maximum number of return results per function exceeded; max 3 but got 5
plugins/inputs/snmp/snmp.go:862:1                revive       function-result-limit: maximum number of return results per function exceeded; max 3 but got 5
plugins/inputs/snmp/snmp_mocks_test.go:47:3      revive       unhandled-error: Unhandled error in call to function fmt.Fprintf
plugins/inputs/snmp/snmp_mocks_test.go:48:3      revive       deep-exit: calls to os.Exit only in main() or init() functions
plugins/inputs/snmp/snmp_mocks_test.go:50:2      revive       unhandled-error: Unhandled error in call to function fmt.Printf
plugins/inputs/snmp/snmp_mocks_test.go:51:2      revive       unhandled-error: Unhandled error in call to function fmt.Fprintf
plugins/inputs/snmp/snmp_mocks_test.go:53:3      revive       deep-exit: calls to os.Exit only in main() or init() functions
plugins/inputs/snmp/snmp_mocks_test.go:55:2      revive       deep-exit: calls to os.Exit only in main() or init() functions
plugins/inputs/snmp/snmp_test.go:2:9             revive       import-shadowing: The name 'snmp' shadows an import name
plugins/inputs/snmp/snmp_test.go:15:2            revive       duplicated-imports: Package "github.com/influxdata/telegraf/internal/snmp" already imported
plugins/inputs/snmp/snmp_test.go:476:2           staticcheck  SA5001: should check returned error before deferring srvr.Close()
plugins/inputs/snmp/snmp_test.go:495:16          errcheck     Error return value of `srvr.WriteTo` is not checked
plugins/inputs/snmp/snmp_test.go:515:2           revive       unhandled-error: Unhandled error in call to function srvr.Close
plugins/inputs/snmp/snmp_test.go:526:2           staticcheck  SA5001: should check returned error before deferring srvr.Close()
plugins/inputs/snmp/snmp_test.go:545:16          errcheck     Error return value of `srvr.WriteTo` is not checked
plugins/inputs/snmp/snmp_test.go:565:2           revive       unhandled-error: Unhandled error in call to function srvr.Close
plugins/inputs/snmp/snmp_test.go:748:10          errcheck     Error return value of `s.Gather` is not checked
plugins/inputs/snmp/snmp_test.go:795:10          errcheck     Error return value of `s.Gather` is not checked

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ouch, I think I'm afraid I'm not able to do that. I could give it a try if really wanted.

@Hipska
Copy link
Contributor Author

Hipska commented Dec 8, 2021

Closing again since #9518 got merged and did a lot of cleanup as well

@Hipska Hipska closed this Dec 8, 2021
@Hipska Hipska deleted the snmp/cleanup branch December 8, 2021 09:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/snmp feat Improvement on an existing feature such as adding a new setting/mode to an existing plugin plugin/input 1. Request for new input plugins 2. Issues/PRs that are related to input plugins
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants