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

feat(inputs.mikrotik): Add plugin #16081

Closed
wants to merge 1 commit into from

Conversation

s-r-engineer
Copy link

@s-r-engineer s-r-engineer commented Oct 27, 2024

Summary

The idea is to be able to connect and gather metrics from the Mikrotik's RouterOS

Checklist

  • No AI generated code was used in this PR

Related issues

resolves #16080

@telegraf-tiger
Copy link
Contributor

Thanks so much for the pull request!
🤝 ✒️ Just a reminder that the CLA has not yet been signed, and we'll need it before merging. Please sign the CLA when you get a chance, then post a comment here saying !signed-cla

@s-r-engineer s-r-engineer force-pushed the master branch 3 times, most recently from e557986 to b9c5115 Compare October 27, 2024 14:12
@s-r-engineer s-r-engineer changed the title Mikrotik plugin added feat: Mikrotik plugin Oct 27, 2024
@telegraf-tiger telegraf-tiger bot added the feat Improvement on an existing feature such as adding a new setting/mode to an existing plugin label Oct 27, 2024
@s-r-engineer s-r-engineer marked this pull request as draft October 27, 2024 14:42
@s-r-engineer
Copy link
Author

This three issues I am having:

  1. https://github.com/influxdata/telegraf/actions/runs/11541885708/job/32124423506?pr=16081 I am highly confused. I ran super-linter locally and only issues I got are MD013 which are according to .markdownlint.yml shall be ignored.
  2. https://app.circleci.com/pipelines/github/influxdata/telegraf/23202/workflows/acca2f7b-af70-4cc2-a827-736c98f95542/jobs/362160 This one looks like something not working properly. The script make_docs.sh when running locally formatting telegraf/plugins/outputs/wavefront/README.md so I believe it is the reason for the failure
  3. https://app.circleci.com/pipelines/github/influxdata/telegraf/23202/workflows/acca2f7b-af70-4cc2-a827-736c98f95542/jobs/362162 not seems to be related at all

@DStrand1
Copy link
Member

This three issues I am having:

  1. https://github.com/influxdata/telegraf/actions/runs/11541885708/job/32124423506?pr=16081 I am highly confused. I ran super-linter locally and only issues I got are MD013 which are according to .markdownlint.yml shall be ignored.
  2. https://app.circleci.com/pipelines/github/influxdata/telegraf/23202/workflows/acca2f7b-af70-4cc2-a827-736c98f95542/jobs/362160 This one looks like something not working properly. The script make_docs.sh when running locally formatting telegraf/plugins/outputs/wavefront/README.md so I believe it is the reason for the failure
  3. https://app.circleci.com/pipelines/github/influxdata/telegraf/23202/workflows/acca2f7b-af70-4cc2-a827-736c98f95542/jobs/362162 not seems to be related at all

#16087 I believe should fix the first two, and the 3rd is an unrelated flaky test, so nothing needed here (other than a rebase/merge after the linked PR merges)

@s-r-engineer s-r-engineer marked this pull request as ready for review October 29, 2024 10:04
@s-r-engineer s-r-engineer reopened this Oct 29, 2024
@srebhan srebhan changed the title feat: Mikrotik plugin feat(inputs.mikrotik): Add plugin Oct 29, 2024
@telegraf-tiger telegraf-tiger bot added the plugin/input 1. Request for new input plugins 2. Issues/PRs that are related to input plugins label Oct 29, 2024
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.

Thanks @s-r-engineer for your contribution! I want to encourage you to simplify your code! Why not defining known tag and field names of responses and decode the JSON responses to map[string]string? Then you can iterate over the map and decode the value accordingly? This would get rid of the reflection code and in general would simplify the code...

plugins/inputs/mikrotik/README.md Outdated Show resolved Hide resolved
plugins/inputs/mikrotik/README.md Outdated Show resolved Hide resolved
plugins/inputs/mikrotik/README.md Outdated Show resolved Hide resolved
plugins/inputs/mikrotik/sample.conf Outdated Show resolved Hide resolved
plugins/inputs/mikrotik/sample.conf Outdated Show resolved Hide resolved
plugins/inputs/mikrotik/parsers.go Outdated Show resolved Hide resolved
plugins/inputs/mikrotik/parsers.go Outdated Show resolved Hide resolved
plugins/inputs/mikrotik/parsers.go Outdated Show resolved Hide resolved
plugins/inputs/mikrotik/tools.go Outdated Show resolved Hide resolved
plugins/inputs/mikrotik/tools.go Outdated Show resolved Hide resolved
@s-r-engineer s-r-engineer requested a review from srebhan October 29, 2024 12:24
@s-r-engineer s-r-engineer marked this pull request as draft October 29, 2024 12:50
@srebhan srebhan self-assigned this Oct 30, 2024
@srebhan
Copy link
Member

srebhan commented Oct 30, 2024

@s-r-engineer my review comments are that, comments about the code. They are not meant to criticize or insult you as a programmer or anyone else. If you understood it this way, please accept my apologies.

@s-r-engineer
Copy link
Author

s-r-engineer commented Oct 30, 2024

@s-r-engineer my review comments are that, comments about the code. They are not meant to criticize or insult you as a programmer or anyone else. If you understood it this way, please accept my apologies.

@srebhan I never was insulted whatsoever. I am sorry if I made you feel like that.

@s-r-engineer s-r-engineer force-pushed the master branch 2 times, most recently from 0edceab to d1f2e40 Compare October 31, 2024 08:34
@s-r-engineer s-r-engineer marked this pull request as ready for review October 31, 2024 18:11
@s-r-engineer
Copy link
Author

@srebhan what to do with this? I am not sure why this is an issue. https://app.circleci.com/jobs/github/influxdata/telegraf/363717

@telegraf-tiger
Copy link
Contributor

telegraf-tiger bot commented Nov 1, 2024

@s-r-engineer
Copy link
Author

@srebhan shall I fix something else?

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.

@s-r-engineer thanks for the update! We are coming closer... I still do have some comments especially around redirections in your code. Please do understand that we need to maintain this potentially "forever" and therefore need to enforce a certain style in the code otherwise debugging or extending this later will be much harder for us. We do have these examples in our codebase already and cannot extend that zoo infinitively.

plugins/inputs/mikrotik/mikrotik.go Outdated Show resolved Hide resolved
plugins/inputs/mikrotik/mikrotik.go Outdated Show resolved Hide resolved
plugins/inputs/mikrotik/mikrotik.go Show resolved Hide resolved
plugins/inputs/mikrotik/mikrotik.go Outdated Show resolved Hide resolved
plugins/inputs/mikrotik/mikrotik.go Outdated Show resolved Hide resolved
plugins/inputs/mikrotik/tools.go Outdated Show resolved Hide resolved
Comment on lines +3 to +6
type common []commonData

type commonData map[string]string

Copy link
Member

Choose a reason for hiding this comment

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

Please get rid of this indirection!

Copy link
Author

Choose a reason for hiding this comment

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

  1. Why?
  2. I need both because some endpoints are returning not arrays. Or are you suggesting to not to have commonData named at all?

}
}

points = append(points, parsedPoint{Tags: tags, Fields: fields})
Copy link
Member

Choose a reason for hiding this comment

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

You could create the metric here, can't you? This way you can get rid of the extra struct and/or directly add them to the accumulator.

Copy link
Author

Choose a reason for hiding this comment

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

I need then pass system tags here or redo parset to be a method of the type. I can redo that into yield if that is ok

plugins/inputs/mikrotik/parsers.go Outdated Show resolved Hide resolved
)

func parse(data common) (points []parsedPoint, err error) {
var errorList []string
Copy link
Member

Choose a reason for hiding this comment

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

Why not keep the errors as errors and maybe directly log them or if you need to join them in the end using errors.Join?

Copy link
Author

Choose a reason for hiding this comment

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

Ok. What structure do you prefer? To have a struct with everything being a part of it? Or to have whatever possible not a part of it?

@s-r-engineer
Copy link
Author

#16081 (comment)
@srebhan I see your point here and will fix it. But also I will be continuing to support this module (and couple more in development) and never even considered to leave it for you, guys, to support 100%.

@srebhan
Copy link
Member

srebhan commented Dec 11, 2024

@s-r-engineer even though I do believe you that this is your intention, I've seen the opposite in the past, e.g. because people moved away from the hardware or due to other personal reasons.... Any way, I'm very happy with you maintaining that plugin!

Let me know as soon as you are ready for the next review round...

@s-r-engineer
Copy link
Author

s-r-engineer commented Dec 11, 2024

@s-r-engineer even though I do believe you that this is your intention, I've seen the opposite in the past, e.g. because people moved away from the hardware or due to other personal reasons.... Any way, I'm very happy with you maintaining that plugin!

Let me know as soon as you are ready for the next review round...

Hi, @srebhan
Please tell me what to do with this. It is rising every single time I am making username secret by your request.
https://app.circleci.com/jobs/github/influxdata/telegraf/365770

Comment on lines +163 to +174
parsedData, err := parse(result)
if err != nil {
return fmt.Errorf("gatherURL -> %w", err)
}

for _, point := range parsedData {
point.Tags["source-module"] = endpoint.name
for n, v := range h.tags {
point.Tags[n] = v
}
acc.AddFields("mikrotik", point.Fields, point.Tags, timestamp)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Looks like this parse method could already return a telegraf metric instead of something that almost looks like a metric (only Tags and Fields).

Suggested change
parsedData, err := parse(result)
if err != nil {
return fmt.Errorf("gatherURL -> %w", err)
}
for _, point := range parsedData {
point.Tags["source-module"] = endpoint.name
for n, v := range h.tags {
point.Tags[n] = v
}
acc.AddFields("mikrotik", point.Fields, point.Tags, timestamp)
}
metrics, err := parse(result, endpoint.name)
if err != nil {
return fmt.Errorf("could not parse result: %w", err)
}
for _, metric := range metrics {
acc.AddMetric(metric)
}

It also looks like the name for the metric should be endpoint.name. Currently, You are only putting that into a tag, meaning there will be a lot of metrics with the same name but with totally different tags- and fields-set.

Comment on lines +33 to +38
## Optional TLS Config
# tls_ca = "/etc/telegraf/ca.pem"
# tls_cert = "/etc/telegraf/cert.pem"
# tls_key = "/etc/telegraf/key.pem"
## Use TLS but skip chain & host verification
# insecure_skip_verify = false
Copy link
Contributor

Choose a reason for hiding this comment

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

Use config_includer/generator, see http input as example.

Username config.Secret `toml:"username"`
Password config.Secret `toml:"password"`
Log telegraf.Logger `toml:"-"`
common_tls.ClientConfig
Copy link
Contributor

Choose a reason for hiding this comment

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

Probably want to use client config from plugins/common/http?

Suggested change
common_tls.ClientConfig
http.HTTPClientConfig

var sampleConfig string

type Mikrotik struct {
Address string `toml:"address"`
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it an idea to support an array of URLs?


func (h *Mikrotik) Init() error {
if h.Username.Empty() {
return errors.New("mikrotik init -> username must be present")
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
return errors.New("mikrotik init -> username must be present")
return errors.New("username is required")


for _, selectedModule := range h.IncludeModules {
if _, ok := modules[selectedModule]; !ok {
return fmt.Errorf("mikrotik init -> module %s does not exist or has a typo. Correct modules are: %s", selectedModule, getModuleNames())
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
return fmt.Errorf("mikrotik init -> module %s does not exist or has a typo. Correct modules are: %s", selectedModule, getModuleNames())
return fmt.Errorf("module %s does not exist or has a typo. Correct modules are: %s", selectedModule, getModuleNames())

These error prefixes are not needed, just make sure the rest of the message is specific enough.

@srebhan
Copy link
Member

srebhan commented Dec 11, 2024

Hi, @srebhan Please tell me what to do with this. It is rising every single time I am making username secret by your request. https://app.circleci.com/jobs/github/influxdata/telegraf/365770

Do you maybe not destroy the secret-buffer?

@s-r-engineer
Copy link
Author

Hi, @srebhan Please tell me what to do with this. It is rising every single time I am making username secret by your request. https://app.circleci.com/jobs/github/influxdata/telegraf/365770

Do you maybe not destroy the secret-buffer?

You mean I shall destroy that or shall not but desroying anyway?

@srebhan
Copy link
Member

srebhan commented Dec 11, 2024

@s-r-engineer did you mean to close this PR? I will need to take a look into the "secret" issue.

@s-r-engineer
Copy link
Author

Yes I meant to close this. I am tired of this and kinda feel that until all telegraf maintainers would not review my code it will not be published. Since I am willing to suport my code in the future I do not see any actual reason to merge it to telegraf in this circumstances. I will publish my module as a standalone daemon that could be running with execd.

@srebhan
Copy link
Member

srebhan commented Dec 13, 2024

@s-r-engineer would you be OK if I work on the code to get it to a mergable state? You are so close and there are only small things left to do... I would do it on your branch if you provide write access or in a separate branch if you are ok with it...

@s-r-engineer
Copy link
Author

@srebhan no I'm not ok with it. I deleted my clone of the repo already and moved the code to standalone binary that will not be covered with MIT from now on.

But your repo is covered by MIT license and I signed CLA. Therefore ultimately you can do whatever you want with the blocks I already submitted.

@srebhan
Copy link
Member

srebhan commented Dec 16, 2024

We will respect your choice of course and not work on the plugin. If you decide you want to add the plugin upstream one day, please do not hesitate to reopen the PR or add a new one.

I'm sad we seem to have scared you away from adding this plugin and I would be interested in learning what we did wrong...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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.

Mikrotik input plugin
4 participants