Skip to content

Commit

Permalink
Merge pull request #70 from inexio/pre-release
Browse files Browse the repository at this point in the history
Pre release
  • Loading branch information
TheFireMike authored Sep 6, 2021
2 parents b0cfaaf + d974c49 commit 20ad43f
Show file tree
Hide file tree
Showing 28 changed files with 530 additions and 212 deletions.
2 changes: 1 addition & 1 deletion .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ nfpms:
- id: "thola"
homepage: https://thola.io
maintainer: team@thola.io
description: Network Monitoring and Provisioning Tool.
description: Network Monitoring Tool
license: BSD-2-clause
formats:
- deb
Expand Down
20 changes: 13 additions & 7 deletions cmd/check_interface_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ func init() {
checkCMD.AddCommand(checkInterfaceMetricsCMD)

checkInterfaceMetricsCMD.Flags().Bool("print-interfaces", false, "Print interfaces to plugin output")
checkInterfaceMetricsCMD.Flags().Bool("snmp-gets-instead-of-walk", false, "Use SNMP Gets instead of Walks")
checkInterfaceMetricsCMD.Flags().String("ifDescr-regex", "", "Apply a regex on the ifDescr of the interfaces. Use it together with the 'ifDescr-regex-replace' flag")
checkInterfaceMetricsCMD.Flags().String("ifDescr-regex-replace", "", "Apply a regex on the ifDescr of the interfaces. Use it together with the 'ifDescr-regex' flag")
checkInterfaceMetricsCMD.Flags().StringSlice("ifType-filter", []string{}, "Filter out interfaces which ifType equals the given types")
Expand All @@ -28,6 +29,10 @@ var checkInterfaceMetricsCMD = &cobra.Command{
if err != nil {
log.Fatal().Err(err).Msg("print-interfaces needs to be a boolean")
}
snmpGetsInsteadOfWalk, err := cmd.Flags().GetBool("snmp-gets-instead-of-walk")
if err != nil {
log.Fatal().Err(err).Msg("snmp-gets-instead-of-walk needs to be a boolean")
}
ifDescrRegex, err := cmd.Flags().GetString("ifDescr-regex")
if err != nil {
log.Fatal().Err(err).Msg("ifDescr-regex needs to be a string")
Expand All @@ -51,13 +56,14 @@ var checkInterfaceMetricsCMD = &cobra.Command{

var nullString *string
r := request.CheckInterfaceMetricsRequest{
CheckDeviceRequest: getCheckDeviceRequest(args[0]),
PrintInterfaces: printInterfaces,
IfDescrRegex: utility.IfThenElse(cmd.Flags().Changed("ifDescr-regex"), &ifDescrRegex, nullString).(*string),
IfDescrRegexReplace: utility.IfThenElse(cmd.Flags().Changed("ifDescr-regex-replace"), &ifDescrRegexReplace, nullString).(*string),
IfTypeFilter: ifTypeFilter,
IfNameFilter: ifNameFilter,
IfDescrFilter: ifDescrFilter,
CheckDeviceRequest: getCheckDeviceRequest(args[0]),
PrintInterfaces: printInterfaces,
IfDescrRegex: utility.IfThenElse(cmd.Flags().Changed("ifDescr-regex"), &ifDescrRegex, nullString).(*string),
IfDescrRegexReplace: utility.IfThenElse(cmd.Flags().Changed("ifDescr-regex-replace"), &ifDescrRegexReplace, nullString).(*string),
IfTypeFilter: ifTypeFilter,
IfNameFilter: ifNameFilter,
IfDescrFilter: ifDescrFilter,
SNMPGetsInsteadOfWalk: snmpGetsInsteadOfWalk,
}

handleRequest(&r)
Expand Down
5 changes: 3 additions & 2 deletions config/codecommunicator/adva.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package codecommunicator
import (
"context"
"fmt"
"github.com/inexio/thola/internal/communicator/filter"
"github.com/inexio/thola/internal/device"
"github.com/inexio/thola/internal/network"
"github.com/pkg/errors"
Expand All @@ -17,8 +18,8 @@ type advaCommunicator struct {
}

// GetInterfaces returns the interfaces of adva devices.
func (c *advaCommunicator) GetInterfaces(ctx context.Context) ([]device.Interface, error) {
interfaces, err := c.parent.GetInterfaces(ctx)
func (c *advaCommunicator) GetInterfaces(ctx context.Context, filter ...filter.PropertyFilter) ([]device.Interface, error) {
interfaces, err := c.parent.GetInterfaces(ctx, filter...)
if err != nil {
return nil, err
}
Expand Down
5 changes: 3 additions & 2 deletions config/codecommunicator/ceraos-ip10.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package codecommunicator

import (
"context"
"github.com/inexio/thola/internal/communicator/filter"
"github.com/inexio/thola/internal/device"
"github.com/pkg/errors"
"regexp"
Expand All @@ -14,7 +15,7 @@ type ceraosIP10Communicator struct {

// GetInterfaces returns the interfaces of ceraos/ip10 devices.
// These devices need special behavior radio and ethernet interfaces.
func (c *ceraosIP10Communicator) GetInterfaces(ctx context.Context) ([]device.Interface, error) {
func (c *ceraosIP10Communicator) GetInterfaces(ctx context.Context, filter ...filter.PropertyFilter) ([]device.Interface, error) {
subInterfaces, err := c.parent.GetInterfaces(ctx)
if err != nil {
return nil, errors.Wrap(err, "an unexpected error occurred while trying to get ifTable of sub communicator")
Expand Down Expand Up @@ -74,5 +75,5 @@ func (c *ceraosIP10Communicator) GetInterfaces(ctx context.Context) ([]device.In
}
}

return subInterfaces, nil
return filterInterfaces(subInterfaces, filter)
}
69 changes: 68 additions & 1 deletion config/codecommunicator/code_communicator.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import (
"context"
"fmt"
"github.com/inexio/thola/internal/communicator/communicator"
"github.com/inexio/thola/internal/communicator/filter"
"github.com/inexio/thola/internal/device"
"github.com/inexio/thola/internal/tholaerr"
"github.com/pkg/errors"
"regexp"
)

type codeCommunicator struct {
Expand Down Expand Up @@ -69,7 +71,7 @@ func (c *codeCommunicator) GetOSVersion(_ context.Context) (string, error) {
return "", tholaerr.NewNotImplementedError("function is not implemented for this communicator")
}

func (c *codeCommunicator) GetInterfaces(_ context.Context) ([]device.Interface, error) {
func (c *codeCommunicator) GetInterfaces(_ context.Context, _ ...filter.PropertyFilter) ([]device.Interface, error) {
return nil, tholaerr.NewNotImplementedError("function is not implemented for this communicator")
}

Expand Down Expand Up @@ -192,3 +194,68 @@ func (c *codeCommunicator) GetHardwareHealthComponentPowerSupply(_ context.Conte
func (c *codeCommunicator) GetSBCComponentSystemHealthScore(_ context.Context) (int, error) {
return 0, tholaerr.NewNotImplementedError("function is not implemented for this communicator")
}

func filterInterfaces(interfaces []device.Interface, filter []filter.PropertyFilter) ([]device.Interface, error) {
if len(filter) == 0 {
return interfaces, nil
}

var ifDescrFilter, ifNameFilter, ifTypeFilter []*regexp.Regexp

for _, fil := range filter {
switch key := fil.Key; key {
case "ifDescr":
regex, err := regexp.Compile(fil.Regex)
if err != nil {
return nil, errors.Wrap(err, "failed to compile ifDescr regex")
}
ifDescrFilter = append(ifDescrFilter, regex)
case "ifName":
regex, err := regexp.Compile(fil.Regex)
if err != nil {
return nil, errors.Wrap(err, "failed to compile ifName regex")
}
ifNameFilter = append(ifNameFilter, regex)
case "ifType":
regex, err := regexp.Compile(fil.Regex)
if err != nil {
return nil, errors.Wrap(err, "failed to compile ifType regex")
}
ifTypeFilter = append(ifTypeFilter, regex)
default:
return nil, errors.New("unknown filter key: " + key)
}
}

var res []device.Interface
out:
for _, interf := range interfaces {
if interf.IfDescr != nil {
for _, fil := range ifDescrFilter {
if fil.MatchString(*interf.IfDescr) {
continue out
}
}
}

if interf.IfName != nil {
for _, fil := range ifNameFilter {
if fil.MatchString(*interf.IfName) {
continue out
}
}
}

if interf.IfType != nil {
for _, fil := range ifTypeFilter {
if fil.MatchString(*interf.IfType) {
continue out
}
}
}

res = append(res, interf)
}

return res, nil
}
12 changes: 9 additions & 3 deletions config/codecommunicator/ekinops.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package codecommunicator
import (
"context"
"fmt"
"github.com/inexio/thola/internal/communicator/filter"
"github.com/inexio/thola/internal/device"
"github.com/inexio/thola/internal/network"
"github.com/pkg/errors"
Expand All @@ -15,7 +16,7 @@ type ekinopsCommunicator struct {
}

// GetInterfaces returns the interfaces of ekinops devices.
func (c *ekinopsCommunicator) GetInterfaces(ctx context.Context) ([]device.Interface, error) {
func (c *ekinopsCommunicator) GetInterfaces(ctx context.Context, filter ...filter.PropertyFilter) ([]device.Interface, error) {
con, ok := network.DeviceConnectionFromContext(ctx)
if !ok || con.SNMP == nil {
return nil, errors.New("no device connection available")
Expand Down Expand Up @@ -68,7 +69,12 @@ func (c *ekinopsCommunicator) GetInterfaces(ctx context.Context) ([]device.Inter
}
}

return normalizeEkinopsInterfaces(interfaces)
interfaces, err = c.normalizeInterfaces(interfaces)
if err != nil {
return nil, errors.Wrap(err, "failed to normalize interfaces")
}

return filterInterfaces(interfaces, filter)
}

func ekinopsInterfacesIfIdentifierToSliceIndex(interfaces []device.Interface) (map[string]int, error) {
Expand All @@ -88,7 +94,7 @@ func ekinopsInterfacesIfIdentifierToSliceIndex(interfaces []device.Interface) (m
return m, nil
}

func normalizeEkinopsInterfaces(interfaces []device.Interface) ([]device.Interface, error) {
func (c *ekinopsCommunicator) normalizeInterfaces(interfaces []device.Interface) ([]device.Interface, error) {
var res []device.Interface

for _, interf := range interfaces {
Expand Down
19 changes: 10 additions & 9 deletions config/codecommunicator/junos.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package codecommunicator
import (
"context"
"fmt"
"github.com/inexio/thola/internal/communicator/filter"
"github.com/inexio/thola/internal/device"
"github.com/inexio/thola/internal/network"
"github.com/pkg/errors"
Expand All @@ -14,16 +15,16 @@ type junosCommunicator struct {
codeCommunicator
}

func (c *junosCommunicator) GetInterfaces(ctx context.Context) ([]device.Interface, error) {
interfaces, err := c.deviceClass.GetInterfaces(ctx)
func (c *junosCommunicator) GetInterfaces(ctx context.Context, filter ...filter.PropertyFilter) ([]device.Interface, error) {
interfaces, err := c.deviceClass.GetInterfaces(ctx, filter...)
if err != nil {
return nil, err
}

interfacesWithVLANs, err := juniperAddVLANsNonELS(ctx, interfaces)
interfacesWithVLANs, err := c.addVLANsNonELS(ctx, interfaces)
if err != nil {
log.Ctx(ctx).Debug().Err(err).Msg("getting juniper VLANs for non ELS devices failed, trying for ELS devices")
interfacesWithVLANs, err = juniperAddVLANsELS(ctx, interfaces)
interfacesWithVLANs, err = c.addVLANsELS(ctx, interfaces)
if err != nil {
log.Ctx(ctx).Debug().Err(err).Msg("getting juniper VLANs for ELS devices failed, skipping VLANs")
interfacesWithVLANs = interfaces
Expand All @@ -33,7 +34,7 @@ func (c *junosCommunicator) GetInterfaces(ctx context.Context) ([]device.Interfa
return interfacesWithVLANs, nil
}

func juniperAddVLANsELS(ctx context.Context, interfaces []device.Interface) ([]device.Interface, error) {
func (c *junosCommunicator) addVLANsELS(ctx context.Context, interfaces []device.Interface) ([]device.Interface, error) {
con, ok := network.DeviceConnectionFromContext(ctx)
if !ok || con.SNMP == nil {
return nil, errors.New("snmp client is empty")
Expand Down Expand Up @@ -80,7 +81,7 @@ func juniperAddVLANsELS(ctx context.Context, interfaces []device.Interface) ([]d
}
}

portIfIndex, err := juniperGetPortIfIndexMapping(ctx)
portIfIndex, err := c.getPortIfIndexMapping(ctx)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -130,7 +131,7 @@ out:
return interfaces, nil
}

func juniperAddVLANsNonELS(ctx context.Context, interfaces []device.Interface) ([]device.Interface, error) {
func (c *junosCommunicator) addVLANsNonELS(ctx context.Context, interfaces []device.Interface) ([]device.Interface, error) {
con, ok := network.DeviceConnectionFromContext(ctx)
if !ok || con.SNMP == nil {
return nil, errors.New("snmp client is empty")
Expand All @@ -142,7 +143,7 @@ func juniperAddVLANsNonELS(ctx context.Context, interfaces []device.Interface) (
return nil, errors.Wrap(err, "failed to get jnxExVlanPortStatus")
}

portIfIndex, err := juniperGetPortIfIndexMapping(ctx)
portIfIndex, err := c.getPortIfIndexMapping(ctx)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -204,7 +205,7 @@ func juniperAddVLANsNonELS(ctx context.Context, interfaces []device.Interface) (
return interfaces, nil
}

func juniperGetPortIfIndexMapping(ctx context.Context) (map[string]string, error) {
func (c *junosCommunicator) getPortIfIndexMapping(ctx context.Context) (map[string]string, error) {
con, ok := network.DeviceConnectionFromContext(ctx)
if !ok || con.SNMP == nil {
return nil, errors.New("snmp client is empty")
Expand Down
7 changes: 4 additions & 3 deletions config/codecommunicator/timos-sas.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package codecommunicator

import (
"context"
"github.com/inexio/thola/internal/communicator/filter"
"github.com/inexio/thola/internal/device"
"github.com/inexio/thola/internal/network"
"github.com/pkg/errors"
Expand All @@ -14,7 +15,7 @@ type timosSASCommunicator struct {
}

// GetInterfaces returns the interfaces of a Nokia SAS-T device.
func (c *timosSASCommunicator) GetInterfaces(ctx context.Context) ([]device.Interface, error) {
func (c *timosSASCommunicator) GetInterfaces(ctx context.Context, filter ...filter.PropertyFilter) ([]device.Interface, error) {
interfaces, err := c.parent.GetInterfaces(ctx)
if err != nil {
return nil, err
Expand Down Expand Up @@ -69,14 +70,14 @@ func (c *timosSASCommunicator) GetInterfaces(ctx context.Context) ([]device.Inte
}
}

return interfaces, nil
return filterInterfaces(interfaces, filter)
}

// getInterfaceBySubIndex returns the index of the interface that has the given index.
// The returned index is the index of the array, not the IfIndex.
func getInterfaceBySubIndex(subIndex uint64, interfaces []device.Interface) (int, error) {
for index, iface := range interfaces {
if *iface.IfIndex == subIndex {
if iface.IfIndex != nil && *iface.IfIndex == subIndex {
return index, nil
}
}
Expand Down
5 changes: 3 additions & 2 deletions config/codecommunicator/timos.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package codecommunicator

import (
"context"
"github.com/inexio/thola/internal/communicator/filter"
"github.com/inexio/thola/internal/device"
"github.com/inexio/thola/internal/network"
"github.com/pkg/errors"
Expand All @@ -15,7 +16,7 @@ type timosCommunicator struct {
}

// GetInterfaces returns the interfaces of Nokia devices.
func (c *timosCommunicator) GetInterfaces(ctx context.Context) ([]device.Interface, error) {
func (c *timosCommunicator) GetInterfaces(ctx context.Context, filter ...filter.PropertyFilter) ([]device.Interface, error) {
interfaces, err := c.parent.GetInterfaces(ctx)
if err != nil {
return nil, err
Expand Down Expand Up @@ -89,7 +90,7 @@ func (c *timosCommunicator) GetInterfaces(ctx context.Context) ([]device.Interfa
})
}

return interfaces, nil
return filterInterfaces(interfaces, filter)
}

// getPhysPortDescriptions returns a mapping from every ifIndex to a description.
Expand Down
2 changes: 2 additions & 0 deletions config/device-classes/generic.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ config:
interfaces: true
snmp:
max_repetitions: 20
max_oids: 60

components:
interfaces:
count: 1.3.6.1.2.1.2.1.0
properties:
detection: snmpwalk
index: 1.3.6.1.2.1.2.2.1.1
values:
ifIndex:
oid: 1.3.6.1.2.1.2.2.1.1
Expand Down
4 changes: 4 additions & 0 deletions config/device-classes/generic/ceraos.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ match:
- .1.3.6.1.4.1.2281.
logical_operator: OR

config:
snmp:
max_oids: 1

identify:
properties:
vendor:
Expand Down
3 changes: 2 additions & 1 deletion internal/communicator/communicator/communicator.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package communicator
import (
"context"
"github.com/inexio/thola/internal/communicator/component"
"github.com/inexio/thola/internal/communicator/filter"
"github.com/inexio/thola/internal/device"
)

Expand Down Expand Up @@ -67,7 +68,7 @@ type Functions interface {
GetOSVersion(ctx context.Context) (string, error)

// GetInterfaces returns the interfaces of a device.
GetInterfaces(ctx context.Context) ([]device.Interface, error)
GetInterfaces(ctx context.Context, filter ...filter.PropertyFilter) ([]device.Interface, error)

// GetCountInterfaces returns the count of interfaces of a device.
GetCountInterfaces(ctx context.Context) (int, error)
Expand Down
Loading

0 comments on commit 20ad43f

Please sign in to comment.