Skip to content

Commit

Permalink
Merge pull request #71 from inexio/pre-release
Browse files Browse the repository at this point in the history
Pre release
  • Loading branch information
babos77 authored Sep 9, 2021
2 parents 20ad43f + 62881b2 commit 7417f1f
Show file tree
Hide file tree
Showing 29 changed files with 3,498 additions and 2,713 deletions.
8 changes: 0 additions & 8 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,6 @@ jobs:
- name: Check out code into the Go module directory
uses: actions/checkout@v2

- name: Get dependencies
run: |
go get -v -t -d ./...
if [ -f Gopkg.toml ]; then
curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh
dep ensure
fi
- name: Build main
run: go build -v .

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ jobs:
- name: golangci-lint
uses: golangci/golangci-lint-action@v2
with:
args: --disable unused --timeout 5m
args: --disable unused --timeout 5m --skip-files _test.go
28 changes: 26 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ on:

jobs:
goreleaser:
name: Release
name: GitHub Release
runs-on: ubuntu-latest
steps:
- name: Checkout
Expand All @@ -26,4 +26,28 @@ jobs:
version: latest
args: release --rm-dist
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

docker:
name: Docker Image
runs-on: ubuntu-latest
steps:
- name: Get metadata
id: meta
uses: docker/metadata-action@v3
with:
images: ghcr.io/inexio/thola
tags: type=semver,pattern={{version}}

- name: Login to GitHub Container Registry
uses: docker/login-action@v1
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Build and push Docker image
uses: docker/build-push-action@v2
with:
push: true
tags: ${{ steps.meta.outputs.tags }}
13 changes: 4 additions & 9 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,10 @@ jobs:
- name: Check out code into the Go module directory
uses: actions/checkout@v2

- name: Get dependencies
run: |
go get -v -t -d ./...
if [ -f Gopkg.toml ]; then
curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh
dep ensure
fi
- name: Generate mocks
run: go generate ./...

- name: Run tests
run: cd test && go test
run: go test ./... -v
env:
THOLA_TEST_SIMPLEUI: true
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ Thumbs.db

# Thola specific files #
########################
filesystem-vfsdata.go
**/mocks
mock_*
6 changes: 6 additions & 0 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("print-interfaces-csv", false, "Print interfaces to plugin output as CSV")
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")
Expand All @@ -29,6 +30,10 @@ var checkInterfaceMetricsCMD = &cobra.Command{
if err != nil {
log.Fatal().Err(err).Msg("print-interfaces needs to be a boolean")
}
printInterfacesCSV, err := cmd.Flags().GetBool("print-interfaces-csv")
if err != nil {
log.Fatal().Err(err).Msg("print-interfaces-csv 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")
Expand Down Expand Up @@ -58,6 +63,7 @@ var checkInterfaceMetricsCMD = &cobra.Command{
r := request.CheckInterfaceMetricsRequest{
CheckDeviceRequest: getCheckDeviceRequest(args[0]),
PrintInterfaces: printInterfaces,
PrintInterfacesCSV: printInterfacesCSV,
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,
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.5"
const Version = "v0.4.1"
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ require (
github.com/dgraph-io/badger/v2 v2.2007.2
github.com/go-resty/resty/v2 v2.3.0
github.com/go-sql-driver/mysql v1.5.0
github.com/gocarina/gocsv v0.0.0-20210516172204-ca9e8a8ddea8
github.com/gomodule/redigo v1.8.4
github.com/google/go-cmp v0.5.4
github.com/gosnmp/gosnmp v1.30.0
github.com/inexio/go-monitoringplugin v1.0.7
github.com/inexio/go-monitoringplugin v1.0.8
github.com/jmoiron/sqlx v1.2.0
github.com/labstack/echo/v4 v4.2.1
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db
Expand Down
7 changes: 5 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG
github.com/go-sql-driver/mysql v1.5.0 h1:ozyZYNQW3x3HtqT1jira07DN2PArx2v7/mN66gGcHOs=
github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/gocarina/gocsv v0.0.0-20210516172204-ca9e8a8ddea8 h1:hp1oqdzmv37vPLYFGjuM/RmUgUMfD9vQfMszc54l55Y=
github.com/gocarina/gocsv v0.0.0-20210516172204-ca9e8a8ddea8/go.mod h1:5YoVOkjYAQumqlV356Hj3xeYh4BdZuLE0/nRkf2NKkI=
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zVXpSg4=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
Expand Down Expand Up @@ -139,8 +141,8 @@ github.com/hashicorp/serf v0.8.2/go.mod h1:6hOLApaqBFA1NXqRQAsxw9QxuDEvNxSQRwA/J
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/inexio/go-monitoringplugin v1.0.7 h1:Aj47aNEykRJkaH/zEVOxFRF7/ekGDjyy7JofFi9SvK4=
github.com/inexio/go-monitoringplugin v1.0.7/go.mod h1:kzHRJGZ2iE/0IElB4NYI38h3h0HM5wqTTH7KyUCjkM8=
github.com/inexio/go-monitoringplugin v1.0.8 h1:MpfLgxrXzQNz6uYYph+I/voatFQ0g4VBDeO/6ghZtDs=
github.com/inexio/go-monitoringplugin v1.0.8/go.mod h1:kzHRJGZ2iE/0IElB4NYI38h3h0HM5wqTTH7KyUCjkM8=
github.com/jmoiron/sqlx v1.2.0 h1:41Ip0zITnmWNR/vHV+S4m+VoUivnWY5E4OJfLZjCJMA=
github.com/jmoiron/sqlx v1.2.0/go.mod h1:1FEQNm3xlJgrMD+FBdI9+xvCksHtbpVBBw5dYhBSsks=
github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
Expand Down Expand Up @@ -269,6 +271,7 @@ github.com/spf13/viper v1.4.0/go.mod h1:PTJ7Z/lr49W6bUbkmS1V3by4uWynFiR9p7+dSq/y
github.com/spf13/viper v1.7.1 h1:pM5oEahlgWv/WnHXpgbKz7iLIxRf65tye2Ci+XFK5sk=
github.com/spf13/viper v1.7.1/go.mod h1:8WkrPz2fc9jxqZNCJI/76HCieCp4Q8HaLFoCha5qpdg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1 h1:2vfRuCMp5sSVIDSqO8oNnWJq7mPa6KVP3iPIwFBuy8A=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
Expand Down
2 changes: 1 addition & 1 deletion internal/communicator/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func identifyDeviceRecursive(ctx context.Context, children map[string]hierarchy.
}

// return generic device class
if (con.SNMP == nil || len(con.SNMP.SnmpClient.GetSuccessfulCachedRequests()) == 0) && (con.HTTP == nil || len(con.HTTP.HTTPClient.GetSuccessfulCachedRequests()) == 0) {
if (con.SNMP == nil || !con.SNMP.SnmpClient.HasSuccessfulCachedRequest()) && (con.HTTP == nil || !con.HTTP.HTTPClient.HasSuccessfulCachedRequest()) {
return nil, errors.New("no network requests to device succeeded")
}
return nil, tholaerr.NewNotFoundError("no device class matched")
Expand Down
40 changes: 20 additions & 20 deletions internal/communicator/deviceclass/device_class.go
Original file line number Diff line number Diff line change
Expand Up @@ -558,38 +558,30 @@ func (y *yamlComponentsInterfaces) convert(parentComponentsInterfaces *deviceCla
}

func (y *yamlComponentsOID) convert() (deviceClassOID, error) {
var idxMappings *deviceClassOID
res := deviceClassOID{
SNMPGetConfiguration: network.SNMPGetConfiguration{
OID: y.OID,
UseRawResult: y.UseRawResult,
},
}

if y.IndicesMapping != nil {
mappings, err := y.IndicesMapping.convert()
if err != nil {
return deviceClassOID{}, errors.New("failed to convert indices mappings")
}
idxMappings = &mappings
res.indicesMapping = &mappings
}

if y.Operators != nil {
operators, err := interfaceSlice2propertyOperators(y.Operators, propertyDefault)
if err != nil {
return deviceClassOID{}, errors.Wrap(err, "failed to read yaml oids operators")
}
return deviceClassOID{
SNMPGetConfiguration: network.SNMPGetConfiguration{
OID: y.OID,
UseRawResult: y.UseRawResult,
},
operators: operators,
indicesMapping: idxMappings,
}, nil
res.operators = operators
}

return deviceClassOID{
SNMPGetConfiguration: network.SNMPGetConfiguration{
OID: y.OID,
UseRawResult: y.UseRawResult,
},
operators: nil,
indicesMapping: idxMappings,
}, nil
return res, nil
}

func (y *yamlComponentsOID) validate() error {
Expand Down Expand Up @@ -1604,15 +1596,23 @@ func interface2GroupPropertyReader(i interface{}, parentGroupPropertyReader grou
return nil, errors.New("can't merge SNMP group property reader with property reader of different type")
}

devClassOIDsMerged := parentSNMPGroupPropertyReader.oids.merge(*devClassOIDs)
parentSNMPGroupPropertyReaderOIDs, ok := parentSNMPGroupPropertyReader.oids.(*deviceClassOIDs)
if !ok {
return nil, errors.New("parent SNMP group property reader oids is not of type 'deviceClassOIDs'")
}

devClassOIDsMerged := parentSNMPGroupPropertyReaderOIDs.merge(*devClassOIDs)
devClassOIDs = &devClassOIDsMerged

if index == nil {
index = parentSNMPGroupPropertyReader.index
}
}

return &snmpGroupPropertyReader{index, *devClassOIDs}, nil
return &snmpGroupPropertyReader{
index: index,
oids: devClassOIDs,
}, nil
default:
return nil, fmt.Errorf("unknown detection type '%s'", stringDetection)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,7 @@ func (o *deviceClassCommunicator) GetInterfaces(ctx context.Context, filter ...f

var interfaces []device.Interface

err = interfacesRaw.Decode(&interfaces)
err = interfacesRaw.decode(&interfaces)
if err != nil {
return nil, errors.Wrap(err, "failed to decode raw interfaces into interface structs")
}
Expand Down
38 changes: 24 additions & 14 deletions internal/communicator/deviceclass/group_properties.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ import (
"strings"
)

//go:generate go run github.com/vektra/mockery/v2 --name=oidReader --inpackage

type propertyGroup map[string]interface{}

type propertyGroups []propertyGroup

func (g *propertyGroups) Decode(destination interface{}) error {
func (g *propertyGroups) decode(destination interface{}) error {
return mapstructure.WeakDecode(g, destination)
}

Expand All @@ -29,7 +31,7 @@ type groupPropertyReader interface {

type snmpGroupPropertyReader struct {
index oidReader
oids deviceClassOIDs
oids oidReader
}

func (s *snmpGroupPropertyReader) getProperty(ctx context.Context, filter ...filter.PropertyFilter) (propertyGroups, []value.Value, error) {
Expand Down Expand Up @@ -110,7 +112,7 @@ func (s *snmpGroupPropertyReader) getFilteredIndices(ctx context.Context, filter

// find filter oid
attrs := strings.Split(f.Key, "/")
reader := oidReader(&s.oids)
reader := s.oids
for _, attr := range attrs {
// check if current oid reader contains multiple OIDs
multipleReader, ok := reader.(*deviceClassOIDs)
Expand Down Expand Up @@ -219,7 +221,7 @@ func (d *deviceClassOIDs) merge(overwrite deviceClassOIDs) deviceClassOIDs {
type deviceClassOID struct {
network.SNMPGetConfiguration
operators propertyOperators
indicesMapping *deviceClassOID
indicesMapping oidReader
}

func (d *deviceClassOID) readOID(ctx context.Context, indices []value.Value, skipEmpty bool) (map[int]interface{}, error) {
Expand Down Expand Up @@ -248,10 +250,15 @@ func (d *deviceClassOID) readOID(ctx context.Context, indices []value.Value, ski

ifIndexRelIndex := make(map[string]value.Value)
for relIndex, ifIndex := range mappingIndices {
if idx, ok := ifIndexRelIndex[ifIndex.(value.Value).String()]; ok {
ifIndexValue, ok := ifIndex.(value.Value)
if !ok {
return nil, errors.New("index mapping oid didn't return a result of type 'value'")
}
ifIndexString := ifIndexValue.String()
if idx, ok := ifIndexRelIndex[ifIndexString]; ok {
return nil, fmt.Errorf("index mapping resulted in duplicate ifIndex mapping on '%s'", idx.String())
}
ifIndexRelIndex[ifIndex.(value.Value).String()] = value.New(relIndex)
ifIndexRelIndex[ifIndexString] = value.New(relIndex)
}

var newIndices []value.Value
Expand Down Expand Up @@ -321,14 +328,17 @@ func (d *deviceClassOID) readOID(ctx context.Context, indices []value.Value, ski
mappedResult := make(map[int]interface{})

for k, v := range result {
var idx int
if _, ok := mappingIndices[k]; ok {
idx, err = mappingIndices[k].(value.Value).Int()
if err != nil {
return nil, errors.Wrap(err, "failed to convert Value to int")
}
} else {
idx = k
mappedIdx, ok := mappingIndices[k]
if !ok {
continue
}
idxValue, ok := mappedIdx.(value.Value)
if !ok {
return nil, errors.New("index mapping oid didn't return a result of type 'value'")
}
idx, err := idxValue.Int()
if err != nil {
return nil, errors.Wrap(err, "failed to convert Value to int")
}

if _, ok := mappedResult[idx]; ok {
Expand Down
Loading

0 comments on commit 7417f1f

Please sign in to comment.