Skip to content

Commit

Permalink
added comments & fixed lint
Browse files Browse the repository at this point in the history
  • Loading branch information
TheFireMike committed May 26, 2021
1 parent af61647 commit 69c6e70
Show file tree
Hide file tree
Showing 18 changed files with 32 additions and 128 deletions.
8 changes: 4 additions & 4 deletions api/statistics/statistics.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,11 @@ func GetStatistics() (Statistics, error) {

if s.TotalCount == 0 {
return s, nil
} else {
avgNs := int64(stats.totalResponseTime) / int64(s.TotalCount)
avgSec := float64(avgNs) / float64(time.Second)
s.AverageResponseTime = math.Floor(avgSec*1000) / 1000
}

avgNs := int64(stats.totalResponseTime) / int64(s.TotalCount)
avgSec := float64(avgNs) / float64(time.Second)
s.AverageResponseTime = math.Floor(avgSec*1000) / 1000

return s, nil
}
2 changes: 1 addition & 1 deletion core/communicator/codecommunicator/code_communicator.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type codeCommunicator struct {
}

// GetCodeCommunicator returns the code communicator for the given device class
func GetCodeCommunicator(deviceClass communicator.Communicator, parentNetworkDeviceCommunicator communicator.Communicator) (communicator.AvailableCommunicatorFunctions, error) {
func GetCodeCommunicator(deviceClass communicator.Communicator, parentNetworkDeviceCommunicator communicator.Communicator) (communicator.Functions, error) {
if deviceClass == nil {
return nil, errors.New("device class is nil")
}
Expand Down
6 changes: 2 additions & 4 deletions core/communicator/codecommunicator/ekinops_module_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,8 @@ func ekinopsPowerTransform10Log10XDivideBy10000(f float64) float64 {
func ekionopsPowerTransformShiftDivideBy100(f float64) float64 {
if f < 32768 {
return f / 100
} else {
return (f - 65536) / 100
}
return (f - 65536) / 100
}

func ekinopsPowerTransformMinus32768MultiplyByPoint005(f float64) float64 {
Expand All @@ -242,9 +241,8 @@ func ekinopsPowerTransformMinus32768MultiplyByPoint005(f float64) float64 {
func ekinopsPowerTransformOPM8(f float64) float64 {
if f < 32768 {
return f / 256
} else {
return f/256 - 256
}
return f/256 - 256
}

func ekinopsPowerTransformCheckInfinity(f float64) float64 {
Expand Down
5 changes: 3 additions & 2 deletions core/communicator/communicator/communicator.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,11 @@ type Communicator interface {
// GetHardwareHealthComponent returns the hardware health component of a device if available.
GetHardwareHealthComponent(ctx context.Context) (device.HardwareHealthComponent, error)

AvailableCommunicatorFunctions
Functions
}

type AvailableCommunicatorFunctions interface {
// Functions represents all overrideable functions which can be used to communicate with a device.
type Functions interface {

// GetVendor returns the vendor of a device.
GetVendor(ctx context.Context) (string, error)
Expand Down
4 changes: 2 additions & 2 deletions core/communicator/communicator/network_device_communicator.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

// CreateNetworkDeviceCommunicator creates a network device communicator which combines a device class communicator and code communicator
func CreateNetworkDeviceCommunicator(deviceClassCommunicator Communicator, codeCommunicator AvailableCommunicatorFunctions) Communicator {
func CreateNetworkDeviceCommunicator(deviceClassCommunicator Communicator, codeCommunicator Functions) Communicator {
return &networkDeviceCommunicator{
deviceClassCommunicator: deviceClassCommunicator,
codeCommunicator: codeCommunicator,
Expand All @@ -18,7 +18,7 @@ func CreateNetworkDeviceCommunicator(deviceClassCommunicator Communicator, codeC

type networkDeviceCommunicator struct {
deviceClassCommunicator Communicator
codeCommunicator AvailableCommunicatorFunctions
codeCommunicator Functions
}

func (c *networkDeviceCommunicator) GetIdentifier() string {
Expand Down
2 changes: 2 additions & 0 deletions core/communicator/component/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const (
HardwareHealth
)

// CreateComponent creates a component.
func CreateComponent(component string) (Component, error) {
switch component {
case "interfaces":
Expand All @@ -43,6 +44,7 @@ func CreateComponent(component string) (Component, error) {
}
}

// ToString returns the component as a string.
func (d *Component) ToString() (string, error) {
if d == nil {
return "", errors.New("component is empty")
Expand Down
13 changes: 7 additions & 6 deletions core/communicator/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,14 @@ func GetNetworkDeviceCommunicator(ctx context.Context, identifier string) (commu

if configIdentifiers[0] == "generic" {
return genericHierarchy.NetworkDeviceCommunicator, nil
} else {
currentIdentifier = configIdentifiers[0]
hier, ok = genericHierarchy.Children[currentIdentifier]
if !ok {
return nil, errors.New("hierarchy does not exist")
}
}

currentIdentifier = configIdentifiers[0]
hier, ok = genericHierarchy.Children[currentIdentifier]
if !ok {
return nil, errors.New("hierarchy does not exist")
}

for i, ident := range configIdentifiers {
if i == 0 {
continue
Expand Down
2 changes: 1 addition & 1 deletion core/communicator/deviceclass/device_class.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// Package deviceclass contains the logic for interacting with device classes.
// It contains methods that read out the .yaml files representing device classes.
// On top of that, code communicators that extend .yaml files can be added here.
package deviceclass

import (
Expand Down Expand Up @@ -264,6 +263,7 @@ type yamlComponentsOID struct {
IndicesMapping *yamlComponentsOID `yaml:"indices_mapping" mapstructure:"indices_mapping"`
}

// GetHierarchy returns the hierarchy of device classes merged with their corresponding code communicator.
func GetHierarchy() (hierarchy.Hierarchy, error) {
genericDeviceClassDir := "device-classes"
genericDeviceClassFile, err := config.FileSystem.Open(filepath.Join(genericDeviceClassDir, "generic.yaml"))
Expand Down
3 changes: 1 addition & 2 deletions core/database/badgerDB.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,8 @@ func (d *badgerDatabase) GetConnectionData(_ context.Context, ip string) (networ
func (d *badgerDatabase) CheckConnection(_ context.Context) error {
if d.db.IsClosed() {
return errors.New("badger db is closed")
} else {
return nil
}
return nil
}

func (d *badgerDatabase) CloseConnection(ctx context.Context) error {
Expand Down
2 changes: 2 additions & 0 deletions core/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ var db struct {

var cacheExpiration time.Duration

// Database represents a database.
type Database interface {
SetDeviceProperties(ctx context.Context, ip string, data device.Device) error
GetDeviceProperties(ctx context.Context, ip string) (device.Device, error)
Expand Down Expand Up @@ -124,6 +125,7 @@ func initDB(ctx context.Context) error {
return nil
}

// GetDB returns the current DB.
func GetDB(ctx context.Context) (Database, error) {
var err error
db.Do(func() {
Expand Down
1 change: 1 addition & 0 deletions core/network/snmp_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,7 @@ func (s *SNMPResponse) GetSNMPType() gosnmp.Asn1BER {
return s.snmpType
}

// SNMPGetConfiguration represents the configuration needed to get a value.
type SNMPGetConfiguration struct {
OID OID `yaml:"oid" mapstructure:"oid"`
UseRawResult bool `yaml:"use_raw_result" mapstructure:"use_raw_result"`
Expand Down
3 changes: 1 addition & 2 deletions core/parser/human_readable_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,7 @@ func toHumanReadable(value reflect.Value, insertion int) string {
default:
if !value.IsValid() {
return ""
} else {
return fmt.Sprint(value.Interface())
}
return fmt.Sprint(value.Interface())
}
}
2 changes: 1 addition & 1 deletion core/parser/human_readable_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestToHumanReadablePointer1(t *testing.T) {
}

func TestToHumanReadablePointer2(t *testing.T) {
var str []string = nil
var str []string
output, err := ToHumanReadable(&str)
assert.Nil(t, err)
assert.Equal(t, "", string(output))
Expand Down
57 changes: 0 additions & 57 deletions core/request/check_request.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package request

import (
"errors"
"github.com/inexio/go-monitoringplugin"
"github.com/inexio/thola/core/value"
)

// CheckRequest
Expand Down Expand Up @@ -47,58 +45,3 @@ func (c *CheckResponse) ToCheckPluginOutput() ([]byte, error) {
func (c *CheckResponse) GetExitCode() int {
return c.ResponseInfo.StatusCode
}

type CheckThresholds struct {
WarningMin *float64 `json:"warningMin" xml:"warningMin"`
WarningMax *float64 `json:"warningMax" xml:"warningMax"`
CriticalMin *float64 `json:"criticalMin" xml:"criticalMin"`
CriticalMax *float64 `json:"criticalMax" xml:"criticalMax"`
}

func (c *CheckThresholds) validate() error {
if c.WarningMin != nil && c.WarningMax != nil && *c.WarningMin >= *c.WarningMax {
return errors.New("warning min and max are invalid")
}

if c.CriticalMin != nil && c.CriticalMax != nil && *c.CriticalMin >= *c.CriticalMax {
return errors.New("critical min and max are invalid")
}

if c.CriticalMin != nil && c.WarningMin != nil && *c.CriticalMin >= *c.WarningMin {
return errors.New("critical and warning min are invalid")
}

if c.WarningMax != nil && c.CriticalMax != nil && *c.WarningMax >= *c.CriticalMax {
return errors.New("critical and warning max are invalid")
}

return nil
}

func (c *CheckThresholds) isEmpty() bool {
return c.WarningMin == nil && c.WarningMax == nil && c.CriticalMin == nil && c.CriticalMax == nil
}

func (c *CheckThresholds) checkValue(v value.Value) int {
if c.CriticalMin != nil {
if res, err := value.New(*c.CriticalMin).Cmp(v); err != nil || res != -1 {
return monitoringplugin.CRITICAL
}
}
if c.CriticalMax != nil {
if res, err := value.New(*c.CriticalMax).Cmp(v); err != nil || res != 1 {
return monitoringplugin.CRITICAL
}
}
if c.WarningMin != nil {
if res, err := value.New(*c.WarningMin).Cmp(v); err != nil || res != -1 {
return monitoringplugin.WARNING
}
}
if c.WarningMax != nil {
if res, err := value.New(*c.WarningMax).Cmp(v); err != nil || res != 1 {
return monitoringplugin.WARNING
}
}
return monitoringplugin.OK
}
46 changes: 0 additions & 46 deletions core/request/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,52 +6,6 @@ import (
"testing"
)

func TestCheckThresholds(t *testing.T) {
th1 := CheckThresholds{
WarningMin: getPointer(5),
WarningMax: getPointer(10),
CriticalMin: getPointer(3),
CriticalMax: getPointer(12),
}
assert.NoError(t, th1.validate())

th2 := CheckThresholds{}
assert.NoError(t, th2.validate())

th3 := CheckThresholds{
WarningMax: getPointer(3),
}
assert.NoError(t, th3.validate())

th4 := CheckThresholds{
WarningMin: getPointer(2),
WarningMax: getPointer(1),
}
assert.Error(t, th4.validate())

th5 := CheckThresholds{
CriticalMin: getPointer(2),
CriticalMax: getPointer(1),
}
assert.Error(t, th5.validate())

th6 := CheckThresholds{
WarningMin: getPointer(1),
CriticalMin: getPointer(2),
}
assert.Error(t, th6.validate())

th7 := CheckThresholds{
WarningMax: getPointer(2),
CriticalMax: getPointer(1),
}
assert.Error(t, th7.validate())
}

func getPointer(f float64) *float64 {
return &f
}

func TestRemoveInterfaces(t *testing.T) {
for i := 1; i < 1000; i++ {
var interfaces []device.Interface
Expand Down
2 changes: 2 additions & 0 deletions core/utility/utility.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func IfThenElseString(condition bool, t string, e string) string {
return e
}

// SliceUniqueString removes duplicates from a string slice.
func SliceUniqueString(a []string) []string {
l := len(a)
seen := make(map[string]struct{}, l)
Expand All @@ -42,6 +43,7 @@ func SliceUniqueString(a []string) []string {
return a[0:k]
}

// SliceUniqueInt removes duplicates from an int slice.
func SliceUniqueInt(a []int) []int {
l := len(a)
seen := make(map[int]struct{}, l)
Expand Down
1 change: 1 addition & 0 deletions core/value/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strconv"
)

// Value represents a value of any type.
type Value interface {
String() string
Float64() (float64, error)
Expand Down
1 change: 1 addition & 0 deletions doc/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@
// swagger:meta
package doc

// Version specifies the current version.
const Version = "v0.2.3"

0 comments on commit 69c6e70

Please sign in to comment.