Skip to content

Commit

Permalink
Merge pull request #61 from inexio/pre-release
Browse files Browse the repository at this point in the history
Pre release
  • Loading branch information
TheFireMike authored Jul 30, 2021
2 parents dd6573e + c95a447 commit 961991f
Show file tree
Hide file tree
Showing 11 changed files with 48 additions and 13 deletions.
6 changes: 6 additions & 0 deletions cmd/check_interface_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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)
},
Expand Down
2 changes: 1 addition & 1 deletion config/device-classes/generic/routeros.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion doc/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@
package doc

// Version specifies the current version.
const Version = "v0.3.3"
const Version = "v0.3.4"
3 changes: 3 additions & 0 deletions internal/communicator/communicator/communicator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
8 changes: 0 additions & 8 deletions internal/communicator/deviceclass/device_class.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
8 changes: 5 additions & 3 deletions internal/network/snmp_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions internal/request/check_interface_metrics_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
12 changes: 12 additions & 0 deletions internal/request/check_interface_metrics_request_process.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions internal/request/get_communicator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 961991f

Please sign in to comment.