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

cisco_telemetry_mdt enhancement #8661

Merged
merged 15 commits into from
Mar 16, 2021
Merged

cisco_telemetry_mdt enhancement #8661

merged 15 commits into from
Mar 16, 2021

Conversation

dsx1123
Copy link
Contributor

@dsx1123 dsx1123 commented Jan 7, 2021

Required for all PRs:

  • Signed CLA.
  • Associated README.md updated.
  • Has appropriate unit tests.

Summary of Changes

  1. Fixed NXAPI to handle more complicated output
  2. DME add support for Events and addd support for Class based query
  3. Property transformation logicSummary of Changes

@dsx1123 dsx1123 mentioned this pull request Jan 7, 2021
3 tasks
@lgtm-com
Copy link

lgtm-com bot commented Jan 7, 2021

This pull request introduces 1 alert when merging d17e725 into 910b726 - view on LGTM.com

new alerts:

  • 1 for Useless assignment to local variable

@srebhan srebhan self-assigned this Jan 18, 2021
@helenosheaa helenosheaa added the feat Improvement on an existing feature such as adding a new setting/mode to an existing plugin label Feb 4, 2021
@dsx1123
Copy link
Contributor Author

dsx1123 commented Feb 9, 2021

@jagularr do you mind take a look at the change?

Comment on lines 193 to 196
v1 := c.propMap["auto-prop-xfrom"](field, value)
if v1 != nil {
return v1
}
Copy link
Member

Choose a reason for hiding this comment

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

What do you return if v1 is nil? If I interpret the logic correctly you will return nil don't you? If so, you can simplify to

Suggested change
v1 := c.propMap["auto-prop-xfrom"](field, value)
if v1 != nil {
return v1
}
return c.propMap["auto-prop-xfrom"](field, value)

and in turn remove the else below.

} else {
//Now check path based conversion.
//If mapping is found then do the required transformation.
if c.nxpathMap[path] != nil {
Copy link
Member

Choose a reason for hiding this comment

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

Again invert the logic

Suggested change
if c.nxpathMap[path] != nil {
if c.nxpathMap[path] == nil {
return nil
}

and save some indention level.

Comment on lines 210 to 212
return int(value.(uint32))
case *telemetry.TelemetryField_Uint64Value:
return int64(value.(uint64))
Copy link
Member

@srebhan srebhan Feb 10, 2021

Choose a reason for hiding this comment

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

Please check the type assertions before using them as otherwise telegraf will panic in case the assertion doesn't hold. You maybe should even rely on val instead of value!?

} //switch
return nil
case "string":
return (xformValueString(field))
Copy link
Member

Choose a reason for hiding this comment

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

Please remove extra brackets.

Comment on lines 336 to 340
key := "show processes memory physical"
c.nxpathMap[key] = make(map[string]string, 1)
c.nxpathMap[key]["processname"] = "string"

return nil
Copy link
Member

Choose a reason for hiding this comment

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

How about

Suggested change
key := "show processes memory physical"
c.nxpathMap[key] = make(map[string]string, 1)
c.nxpathMap[key]["processname"] = "string"
return nil
c.nxpathMap["show processes memory physical"] = map[string]string{"processname": "string"}
return nil

Copy link
Member

Choose a reason for hiding this comment

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

Btw. If there cannot be an error, please remove the return values!

return nil
}

func (c *CiscoTelemetryMDT) InitMemPhys() error {
Copy link
Member

@srebhan srebhan Feb 10, 2021

Choose a reason for hiding this comment

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

Please make this function private. Same for all other functions only used within this plugin.

Choose a reason for hiding this comment

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

All these functions are updating nxpathMap that's why they are defined with
func (c *CiscoTelemetryMDT) InitMemPhys()
c.nxpathMap[key]["processname"] = "string"

Choose a reason for hiding this comment

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

Did you mean following changes to make function private
-func (c *CiscoTelemetryMDT) InitProc() error {
+func InitProc(c *CiscoTelemetryMDT) {

Copy link
Member

@srebhan srebhan Feb 15, 2021

Choose a reason for hiding this comment

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

No I want to make it func (c *CiscoTelemetryMDT) initMemPhys() error do it will definitively stay inside of this package. In case a function starts with lower-case it will not be visible from outside the package.

Comment on lines 343 to 349
func (c *CiscoTelemetryMDT) InitBgpV4() error {
key := "show bgp ipv4 unicast"
c.nxpathMap[key] = make(map[string]string, 1)
c.nxpathMap[key]["aspath"] = "string"

return nil
}
Copy link
Member

Choose a reason for hiding this comment

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

See above.

Comment on lines 987 to 1018
c.InitPower()
c.InitMemPhys()
c.InitBgpV4()
c.InitCpu()
c.InitResources()
c.InitPtpCorrection()
c.InitTrans()
c.InitIgmp()
c.InitVrfAll()
c.InitIgmpSnoop()
c.InitIgmpSnoopGroups()
c.InitIgmpSnoopGroupDetails()
c.InitIgmpSnoopGroupsSumm()
c.InitMrouter()
c.InitSnoopStats()
c.InitPimInterface()
c.InitPimNeigh()
c.InitPimRoute()
c.InitPimRp()
c.InitPimStats()
c.InitIntfBrief()
c.InitPimVrf()
c.InitIpMroute()
c.InitIpv6Mroute()
c.InitVpc()
c.InitBgp()
c.InitCh()
c.InitIntf()
c.InitProcsys()
c.InitProc()
c.InitBfd()
c.InitLldp()
Copy link
Member

Choose a reason for hiding this comment

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

As seemingly those functions cannot error remove the error from the function signature of all those functions to indicate this fact.

c.InitBfd()
c.InitLldp()

return nil
Copy link
Member

Choose a reason for hiding this comment

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

Same here. No error so remove the error return value.

@lgtm-com
Copy link

lgtm-com bot commented Feb 16, 2021

This pull request introduces 1 alert when merging 94d1e94 into a790529 - view on LGTM.com

new alerts:

  • 1 for Useless assignment to local variable

Comment on lines 1348 to 1355
case "address", "vrfName":
key := "nextHop/" + ff.Name
tags[key] = decodeTag(ff)
}
if value := decodeValue(ff); value != nil {
name := "nextHop/" + ff.Name
grouper.Add(measurement, tags, timestamp, name, value)
}
Copy link
Member

Choose a reason for hiding this comment

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

Do you mean

Suggested change
case "address", "vrfName":
key := "nextHop/" + ff.Name
tags[key] = decodeTag(ff)
}
if value := decodeValue(ff); value != nil {
name := "nextHop/" + ff.Name
grouper.Add(measurement, tags, timestamp, name, value)
}
case "address", "vrfName":
key := "nextHop/" + ff.Name
tags[key] = decodeTag(ff)
continue
}
if value := decodeValue(ff); value != nil {
name := "nextHop/" + ff.Name
grouper.Add(measurement, tags, timestamp, name, value)
}

as otherwise you process ff twice in case of address and vrfName...

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.

Can you please move all nxosValueXform* functions to a separate file (e.g. cisco_telemetry_nxos.go) that also belongs to package cisco_telemetry_mdt! This way the main file is not so cluttered with the conversion and init functions for the new feature.
Please also check the issue brought up by LGTM!

@lgtm-com
Copy link

lgtm-com bot commented Feb 16, 2021

This pull request introduces 1 alert when merging 2da8907 into f888136 - view on LGTM.com

new alerts:

  • 1 for Useless assignment to local variable

return nil
}
if _, ok := c.propMap[field.Name]; ok {
return (c.propMap[field.Name](field, value))
Copy link
Member

Choose a reason for hiding this comment

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

Please remove the extra parentheses

Suggested change
return (c.propMap[field.Name](field, value))
return c.propMap[field.Name](field, value)

}
if _, ok := c.propMap[field.Name]; ok {
return (c.propMap[field.Name](field, value))
} else {
Copy link
Member

Choose a reason for hiding this comment

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

You don't require an else here as you return in the if path. Please remove the else and save another indention level.

@srebhan
Copy link
Member

srebhan commented Feb 17, 2021

@dsx1123 very good, almost there. Can you please move all new functions starting with nx or init to an own file!? This way the main file is not so cluttered and it's easier to find the functions you are looking for.

@srebhan
Copy link
Member

srebhan commented Feb 18, 2021

@dsx1123 please fix the formatting errors using make fmt. Please also run make check locally to detect such errors before pushing...

@@ -50,13 +52,48 @@ type CiscoTelemetryMDT struct {

// Internal state
aliases map[string]string
dmes 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.

Can you please rename this (e.g. to dmesFuncs or similar) or Dmes above to avoid confusion between the two.

Comment on lines 73 to 95
//xform Field to string
func xformValueString(field *telemetry.TelemetryField) string {
var str string
switch val := field.ValueByType.(type) {
case *telemetry.TelemetryField_StringValue:
if len(val.StringValue) > 0 {
return val.StringValue
}
case *telemetry.TelemetryField_Uint32Value:
str = strconv.FormatUint(uint64(val.Uint32Value), 10)
return str
case *telemetry.TelemetryField_Uint64Value:
str = strconv.FormatUint(val.Uint64Value, 10)
return str
case *telemetry.TelemetryField_Sint32Value:
str = strconv.FormatInt(int64(val.Sint32Value), 10)
return str
case *telemetry.TelemetryField_Sint64Value:
str = strconv.FormatInt(val.Sint64Value, 10)
return str
}
return ""
}
Copy link
Member

Choose a reason for hiding this comment

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

Should this also move to the _util.go file?

Comment on lines 123 to 148
if path == "uint64 to int" {
c.propMap[dme] = nxosValueXformUint64Toint64
} else if path == "uint64 to string" {
c.propMap[dme] = nxosValueXformUint64ToString
} else if path == "string to float64" {
c.propMap[dme] = nxosValueXformStringTofloat
} else if path == "string to uint64" {
c.propMap[dme] = nxosValueXformStringToUint64
} else if path == "string to int64" {
c.propMap[dme] = nxosValueXformStringToInt64
} else if path == "auto-float-xfrom" {
c.propMap[dme] = nxosValueAutoXformFloatProp
} else if dme[0:6] == "dnpath" { //path based property map
js := []byte(path)
var jsStruct NxPayloadXfromStructure

err := json.Unmarshal(js, &jsStruct)
if err == nil {
//Build 2 level Hash nxpathMap Key = jsStruct.Name, Value = map of jsStruct.Prop
//It will override the default of code if same path is provided in configuration.
c.nxpathMap[jsStruct.Name] = make(map[string]string, len(jsStruct.Prop))
for _, prop := range jsStruct.Prop {
c.nxpathMap[jsStruct.Name][prop.Key] = prop.Value
}
}
}
Copy link
Member

Choose a reason for hiding this comment

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

We should make this a switch statement

Suggested change
if path == "uint64 to int" {
c.propMap[dme] = nxosValueXformUint64Toint64
} else if path == "uint64 to string" {
c.propMap[dme] = nxosValueXformUint64ToString
} else if path == "string to float64" {
c.propMap[dme] = nxosValueXformStringTofloat
} else if path == "string to uint64" {
c.propMap[dme] = nxosValueXformStringToUint64
} else if path == "string to int64" {
c.propMap[dme] = nxosValueXformStringToInt64
} else if path == "auto-float-xfrom" {
c.propMap[dme] = nxosValueAutoXformFloatProp
} else if dme[0:6] == "dnpath" { //path based property map
js := []byte(path)
var jsStruct NxPayloadXfromStructure
err := json.Unmarshal(js, &jsStruct)
if err == nil {
//Build 2 level Hash nxpathMap Key = jsStruct.Name, Value = map of jsStruct.Prop
//It will override the default of code if same path is provided in configuration.
c.nxpathMap[jsStruct.Name] = make(map[string]string, len(jsStruct.Prop))
for _, prop := range jsStruct.Prop {
c.nxpathMap[jsStruct.Name][prop.Key] = prop.Value
}
}
}
switch path {
case "uint64 to int":
c.propMap[dme] = nxosValueXformUint64Toint64
case "uint64 to string":
c.propMap[dme] = nxosValueXformUint64ToString
case "string to float64":
c.propMap[dme] = nxosValueXformStringTofloat
case "string to uint64":
c.propMap[dme] = nxosValueXformStringToUint64
case "string to int64":
c.propMap[dme] = nxosValueXformStringToInt64
case "auto-float-xfrom"
c.propMap[dme] = nxosValueAutoXformFloatProp
default:
if ! strings.HasPrefix(dme, "dnpath") { // not path based property map
continue
}
var jsStruct NxPayloadXfromStructure
if err := json.Unmarshal([]byte(path), &jsStruct); if err != nil {
continue
}
// Build 2 level Hash nxpathMap Key = jsStruct.Name, Value = map of jsStruct.Prop
// It will override the default of code if same path is provided in configuration.
c.nxpathMap[jsStruct.Name] = make(map[string]string, len(jsStruct.Prop))
for _, prop := range jsStruct.Prop {
c.nxpathMap[jsStruct.Name][prop.Key] = prop.Value
}
}

Comment on lines 374 to 376
if msg.GetSubscriptionIdStr() != "" {
tags["subscription"] = msg.GetSubscriptionIdStr()
}
Copy link
Member

Choose a reason for hiding this comment

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

We can save a function call here

Suggested change
if msg.GetSubscriptionIdStr() != "" {
tags["subscription"] = msg.GetSubscriptionIdStr()
}
if msgID := msg.GetSubscriptionIdStr(); msgID != "" {
tags["subscription"] = msgID
}

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.

The (almost) final pass brought up two or three more minor requests. Can you please fix them and I think then we are good to go.

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.

Looks good to me.

@srebhan srebhan added the ready for final review This pull request has been reviewed and/or tested by multiple users and is ready for a final review. label Mar 1, 2021
Comment on lines 1508 to 1517
if !isEVENT {
nxChildren = subfield
} else {
sub := subfield.Fields
if sub[0] != nil && sub[0].Fields[0].Name == "subscriptionId" && len(sub[0].Fields) >= 2 {
nxAttributes = sub[0].Fields[1].Fields[0].Fields[0].Fields[0].Fields[0].Fields[0]
}
}
//if nxAttributes == NULL then class based query.
if nxAttributes == nil {
Copy link
Contributor

@ssoroka ssoroka Mar 2, 2021

Choose a reason for hiding this comment

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

It looks like nxChildren can sometimes not be set?

@srebhan srebhan removed the ready for final review This pull request has been reviewed and/or tested by multiple users and is ready for a final review. label Mar 3, 2021
@sspaink
Copy link
Contributor

sspaink commented Mar 11, 2021

@ssoroka I looked over your requested changes and looks like @dsx1123 has addressed them and I marked the conversations as resolved. I don't see anything else, but do you have anything else you would like to add before merging this? Thanks!

@helenosheaa helenosheaa added the waiting for response waiting for response from contributor label Mar 16, 2021
@helenosheaa helenosheaa merged commit f555294 into influxdata:master Mar 16, 2021
jblesener pushed a commit to jblesener/telegraf that referenced this pull request Apr 18, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/cisco feat Improvement on an existing feature such as adding a new setting/mode to an existing plugin waiting for response waiting for response from contributor
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants