From 53dfd21bec9056a8b47a16225e0e62ba0638f957 Mon Sep 17 00:00:00 2001 From: babos77 Date: Fri, 16 Jul 2021 12:38:39 +0200 Subject: [PATCH 1/4] fix routeros condition --- config/device-classes/generic/routeros.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/device-classes/generic/routeros.yaml b/config/device-classes/generic/routeros.yaml index d36b82c..ec8e628 100644 --- a/config/device-classes/generic/routeros.yaml +++ b/config/device-classes/generic/routeros.yaml @@ -6,7 +6,7 @@ match: - type: SysObjectID match_mode: startsWith values: - - .1.3.6.1.4.1.14988.1 + - .1.3.6.1.4.1.14988. - type: HttpGetBody uri: "/" match_mode: contains From 14811868ba0ef81001d1c2dd777189d71f34386c Mon Sep 17 00:00:00 2001 From: mikameyer Date: Thu, 29 Jul 2021 09:51:56 +0200 Subject: [PATCH 2/4] fixed snmp maxrepititions not set --- internal/communicator/communicator/communicator.go | 3 +++ .../communicator/network_device_communicator.go | 4 ++++ internal/communicator/deviceclass/device_class.go | 8 -------- .../deviceclass/device_class_communicator.go | 9 +++++++++ internal/network/snmp_client.go | 8 +++++--- internal/request/get_communicator.go | 6 ++++++ 6 files changed, 27 insertions(+), 11 deletions(-) diff --git a/internal/communicator/communicator/communicator.go b/internal/communicator/communicator/communicator.go index 72d3e73..1a5c55e 100644 --- a/internal/communicator/communicator/communicator.go +++ b/internal/communicator/communicator/communicator.go @@ -21,6 +21,9 @@ type Communicator interface { // Match checks if the device matches the device class Match(ctx context.Context) (bool, error) + // UpdateConnection updates the device connection with class specific values + UpdateConnection(ctx context.Context) error + // GetIdentifyProperties returns the identify properties of a device like vendor, model... GetIdentifyProperties(ctx context.Context) (device.Properties, error) diff --git a/internal/communicator/communicator/network_device_communicator.go b/internal/communicator/communicator/network_device_communicator.go index f4cdae8..d915937 100644 --- a/internal/communicator/communicator/network_device_communicator.go +++ b/internal/communicator/communicator/network_device_communicator.go @@ -39,6 +39,10 @@ func (c *networkDeviceCommunicator) Match(ctx context.Context) (bool, error) { return c.deviceClassCommunicator.Match(ctx) } +func (c *networkDeviceCommunicator) UpdateConnection(ctx context.Context) error { + return c.deviceClassCommunicator.UpdateConnection(ctx) +} + func (c *networkDeviceCommunicator) GetIdentifyProperties(ctx context.Context) (device.Properties, error) { dev := device.Device{ Class: c.GetIdentifier(), diff --git a/internal/communicator/deviceclass/device_class.go b/internal/communicator/deviceclass/device_class.go index 53d55de..1a2b55b 100644 --- a/internal/communicator/deviceclass/device_class.go +++ b/internal/communicator/deviceclass/device_class.go @@ -382,14 +382,6 @@ func (d *deviceClass) matchDevice(ctx context.Context) (bool, error) { return d.match.check(ctx) } -// getSNMPMaxRepetitions returns the maximum snmp repetitions. -func (d *deviceClass) getSNMPMaxRepetitions() (uint32, error) { - if d.config.snmp.MaxRepetitions != 0 { - return d.config.snmp.MaxRepetitions, nil - } - return 0, tholaerr.NewNotFoundError("max_repetitions not found") -} - // getAvailableComponents returns the available components. func (d *deviceClass) getAvailableComponents() map[component.Component]bool { return d.config.components diff --git a/internal/communicator/deviceclass/device_class_communicator.go b/internal/communicator/deviceclass/device_class_communicator.go index 4fb0b34..574ecc8 100644 --- a/internal/communicator/deviceclass/device_class_communicator.go +++ b/internal/communicator/deviceclass/device_class_communicator.go @@ -49,6 +49,15 @@ func (o *deviceClassCommunicator) Match(ctx context.Context) (bool, error) { return o.matchDevice(ctx) } +func (o *deviceClassCommunicator) UpdateConnection(ctx context.Context) error { + if conn, ok := network.DeviceConnectionFromContext(ctx); ok { + if conn.SNMP != nil && conn.SNMP.SnmpClient != nil { + conn.SNMP.SnmpClient.SetMaxRepetitions(o.deviceClass.config.snmp.MaxRepetitions) + } + } + return nil +} + func (o *deviceClassCommunicator) GetIdentifyProperties(ctx context.Context) (device.Properties, error) { dev := device.Device{ Class: o.GetIdentifier(), diff --git a/internal/network/snmp_client.go b/internal/network/snmp_client.go index 49ac85b..087cd0f 100644 --- a/internal/network/snmp_client.go +++ b/internal/network/snmp_client.go @@ -7,7 +7,6 @@ import ( "fmt" "github.com/gosnmp/gosnmp" "github.com/inexio/thola/internal/tholaerr" - "github.com/inexio/thola/internal/utility" "github.com/pkg/errors" "github.com/rs/zerolog/log" "golang.org/x/text/encoding/charmap" @@ -527,8 +526,11 @@ func (s *SNMPClient) GetVersion() string { } // GetMaxRepetitions returns the max repetitions. -func (s *SNMPClient) GetMaxRepetitions() uint8 { - return utility.IfThenElse(s.client.MaxRepetitions == 0, gosnmp.Default.MaxRepetitions, s.client.MaxRepetitions).(uint8) +func (s *SNMPClient) GetMaxRepetitions() uint32 { + if s.client.MaxRepetitions == 0 { + return gosnmp.Default.MaxRepetitions + } + return s.client.MaxRepetitions } // SetMaxRepetitions sets the maximum repetitions. diff --git a/internal/request/get_communicator.go b/internal/request/get_communicator.go index 025c394..e1ef603 100644 --- a/internal/request/get_communicator.go +++ b/internal/request/get_communicator.go @@ -54,5 +54,11 @@ func GetCommunicator(ctx context.Context, baseRequest BaseRequest) (communicator if err != nil { return nil, errors.Wrapf(err, "failed to get communicator for os '%s'", deviceProperties.Class) } + + err = com.UpdateConnection(ctx) + if err != nil { + return nil, errors.Wrap(err, "failed to update connection") + } + return com, nil } From ac8e59b6e0eb87262d527ed3af85ce1bca8afab5 Mon Sep 17 00:00:00 2001 From: mikameyer Date: Thu, 29 Jul 2021 10:15:02 +0200 Subject: [PATCH 3/4] added ifDescr filter --- cmd/check_interface_metrics.go | 6 ++++++ internal/request/check_interface_metrics_request.go | 1 + .../check_interface_metrics_request_process.go | 12 ++++++++++++ 3 files changed, 19 insertions(+) diff --git a/cmd/check_interface_metrics.go b/cmd/check_interface_metrics.go index b8396cf..e9b9ae2 100644 --- a/cmd/check_interface_metrics.go +++ b/cmd/check_interface_metrics.go @@ -13,6 +13,7 @@ func init() { checkInterfaceMetricsCMD.Flags().Bool("print-interfaces", false, "Print interfaces to plugin output") checkInterfaceMetricsCMD.Flags().StringSlice("ifType-filter", []string{}, "Filter out interfaces which ifType equals the given types") checkInterfaceMetricsCMD.Flags().StringSlice("ifName-filter", []string{}, "Filter out interfaces which ifName matches the given regex") + checkInterfaceMetricsCMD.Flags().StringSlice("ifDescr-filter", []string{}, "Filter out interfaces which ifDescription matches the given regex") } var checkInterfaceMetricsCMD = &cobra.Command{ @@ -32,11 +33,16 @@ var checkInterfaceMetricsCMD = &cobra.Command{ if err != nil { log.Fatal().Err(err).Msg("ifName-filter needs to be a string") } + ifDescrFilter, err := cmd.Flags().GetStringSlice("ifDescr-filter") + if err != nil { + log.Fatal().Err(err).Msg("ifDescr-filter needs to be a string") + } r := request.CheckInterfaceMetricsRequest{ CheckDeviceRequest: getCheckDeviceRequest(args[0]), PrintInterfaces: printInterfaces, IfTypeFilter: ifTypeFilter, IfNameFilter: ifNameFilter, + IfDescrFilter: ifDescrFilter, } handleRequest(&r) }, diff --git a/internal/request/check_interface_metrics_request.go b/internal/request/check_interface_metrics_request.go index de6bd2a..d667ab8 100644 --- a/internal/request/check_interface_metrics_request.go +++ b/internal/request/check_interface_metrics_request.go @@ -9,5 +9,6 @@ type CheckInterfaceMetricsRequest struct { PrintInterfaces bool `yaml:"print_interfaces" json:"print_interfaces" xml:"print_interfaces"` IfTypeFilter []string `yaml:"ifType_filter" json:"ifType_filter" xml:"ifType_filter"` IfNameFilter []string `yaml:"ifName_filter" json:"ifName_filter" xml:"ifName_filter"` + IfDescrFilter []string `yaml:"ifDescr_filter" json:"ifDescr_filter" xml:"ifDescr_filter"` CheckDeviceRequest } diff --git a/internal/request/check_interface_metrics_request_process.go b/internal/request/check_interface_metrics_request_process.go index 9c9c880..fc9ea74 100644 --- a/internal/request/check_interface_metrics_request_process.go +++ b/internal/request/check_interface_metrics_request_process.go @@ -103,6 +103,18 @@ out: } } } + for _, filter := range r.IfDescrFilter { + if interf.IfDescr != nil { + matched, err := regexp.MatchString(filter, *interf.IfDescr) + if err != nil { + return nil, errors.Wrap(err, "ifDescr filter regex match failed") + } + if matched { + filterIndices = append(filterIndices, i) + continue out + } + } + } } readInterfacesResponse.Interfaces = filterInterfaces(readInterfacesResponse.Interfaces, filterIndices, 0) From 0f5c678073e637e1a8ddbab83b0c837275cb10bc Mon Sep 17 00:00:00 2001 From: mikameyer Date: Fri, 30 Jul 2021 09:15:43 +0200 Subject: [PATCH 4/4] version bump --- doc/doc.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/doc.go b/doc/doc.go index 1be40e1..65e17a1 100644 --- a/doc/doc.go +++ b/doc/doc.go @@ -28,4 +28,4 @@ package doc // Version specifies the current version. -const Version = "v0.3.3" +const Version = "v0.3.4"