Skip to content

Commit

Permalink
Merge pull request #22 from inexio/dev
Browse files Browse the repository at this point in the history
v0.2.3
  • Loading branch information
TheFireMike authored Mar 17, 2021
2 parents fe93620 + 2f25225 commit c257604
Show file tree
Hide file tree
Showing 52 changed files with 2,407 additions and 1,063 deletions.
14 changes: 7 additions & 7 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ on:
- '*'

jobs:
fmt:
name: fmt
golangci:
name: Lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@master
- name: check
uses: grandcolline/golang-github-actions@v1.1.0
- uses: actions/checkout@v2

- name: golangci-lint
uses: golangci/golangci-lint-action@v2
with:
run: fmt
token: ${{ secrets.GITHUB_TOKEN }}
args: --disable unused
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: GoReleaser
name: Release

on:
push:
Expand Down
18 changes: 9 additions & 9 deletions api/request_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ func StartAPI() {
// 200:
// description: Returns the response.
// schema:
// $ref: '#/definitions/CheckMemoryUsageResponse'
// $ref: '#/definitions/CheckResponse'
// 400:
// description: Returns an error with more details in the body.
// schema:
Expand All @@ -273,12 +273,12 @@ func StartAPI() {
// description: Request to process.
// required: true
// schema:
// $ref: '#/definitions/CheckCpuLoadRequest'
// $ref: '#/definitions/CheckCPULoadRequest'
// responses:
// 200:
// description: Returns the response.
// schema:
// $ref: '#/definitions/CheckCpuLoadResponse'
// $ref: '#/definitions/CheckResponse'
// 400:
// description: Returns an error with more details in the body.
// schema:
Expand All @@ -305,7 +305,7 @@ func StartAPI() {
// 200:
// description: Returns the response.
// schema:
// $ref: '#/definitions/CheckSBCResponse'
// $ref: '#/definitions/CheckResponse'
// 400:
// description: Returns an error with more details in the body.
// schema:
Expand All @@ -332,7 +332,7 @@ func StartAPI() {
// 200:
// description: Returns the response.
// schema:
// $ref: '#/definitions/CheckServerResponse'
// $ref: '#/definitions/CheckResponse'
// 400:
// description: Returns an error with more details in the body.
// schema:
Expand All @@ -359,14 +359,14 @@ func StartAPI() {
// 200:
// description: Returns the response.
// schema:
// $ref: '#/definitions/CheckDiskResponse'
// $ref: '#/definitions/CheckResponse'
// 400:
// description: Returns an error with more details in the body.
// schema:
// $ref: '#/definitions/OutputError'
e.POST("/check/disk", checkDisk)

// swagger:operation POST /check/hardware-health check checkSBC
// swagger:operation POST /check/hardware-health check checkHardwareHealth
// ---
// summary: Check an hardware health of an device.
// consumes:
Expand All @@ -386,7 +386,7 @@ func StartAPI() {
// 200:
// description: Returns the response.
// schema:
// $ref: '#/definitions/CheckHardwareHealthResponse'
// $ref: '#/definitions/CheckResponse'
// 400:
// description: Returns an error with more details in the body.
// schema:
Expand Down Expand Up @@ -609,7 +609,7 @@ func StartAPI() {
// $ref: '#/definitions/OutputError'
e.POST("/read/disk", readDisk)

// swagger:operation POST /read/hardware-health read hardware health
// swagger:operation POST /read/hardware-health read hardware-health
// ---
// summary: Reads out hardware health data of a device.
// consumes:
Expand Down
6 changes: 3 additions & 3 deletions api/statistics/statistics.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type statistics struct {
totalResponseTime time.Duration
}

// Statistics includes stats of all requests handled by the api
// Statistics includes stats of all requests handled by the api.
type Statistics struct {
UpSince time.Time
TotalCount int
Expand All @@ -41,7 +41,7 @@ func handlerFunc(h http.Handler) http.Handler {
})
}

// Middleware represents an API middleware
// Middleware represents an API middleware.
func Middleware() echo.MiddlewareFunc {
stats.Do(func() {
stats.init()
Expand All @@ -66,7 +66,7 @@ func (s *statistics) add(startTime time.Time, statusCode int) {
}
}

// GetStatistics returns the current statistics
// GetStatistics returns the current statistics.
func GetStatistics() (Statistics, error) {
stats.RLock()
defer stats.RUnlock()
Expand Down
23 changes: 17 additions & 6 deletions cmd/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"fmt"
"github.com/inexio/go-monitoringplugin"
"github.com/inexio/thola/core/request"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
Expand Down Expand Up @@ -64,35 +65,45 @@ func getCheckRequest() request.CheckRequest {
}
}

func generateCheckThresholds(cmd *cobra.Command, warningMin, warningMax, criticalMin, criticalMax string) request.CheckThresholds {
var thresholds request.CheckThresholds
func generateCheckThresholds(cmd *cobra.Command, warningMin, warningMax, criticalMin, criticalMax string, setMinToZeroIfEmpty bool) monitoringplugin.Thresholds {
var thresholds monitoringplugin.Thresholds
if flagName := warningMin; flagName != "" && cmd.Flags().Changed(flagName) {
v, err := cmd.Flags().GetFloat64(flagName)
if err != nil {
log.Fatal().Err(err).Msgf("flag '%s' is not a float64", flagName)
}
thresholds.WarningMin = &v
thresholds.WarningMin = v
}
if flagName := warningMax; flagName != "" && cmd.Flags().Changed(flagName) {
v, err := cmd.Flags().GetFloat64(flagName)
if err != nil {
log.Fatal().Err(err).Msgf("flag '%s' is not a float64", flagName)
}
thresholds.WarningMax = &v
thresholds.WarningMax = v
}
if flagName := criticalMin; flagName != "" && cmd.Flags().Changed(flagName) {
v, err := cmd.Flags().GetFloat64(flagName)
if err != nil {
log.Fatal().Err(err).Msgf("flag '%s' is not a float64", flagName)
}
thresholds.CriticalMin = &v
thresholds.CriticalMin = v
}
if flagName := criticalMax; flagName != "" && cmd.Flags().Changed(flagName) {
v, err := cmd.Flags().GetFloat64(flagName)
if err != nil {
log.Fatal().Err(err).Msgf("flag '%s' is not a float64", flagName)
}
thresholds.CriticalMax = &v
thresholds.CriticalMax = v
}

if setMinToZeroIfEmpty {
if thresholds.HasWarning() && thresholds.WarningMin == nil {
thresholds.WarningMin = 0
}
if thresholds.HasCritical() && thresholds.CriticalMin == nil {
thresholds.CriticalMin = 0
}
}

return thresholds
}
2 changes: 1 addition & 1 deletion cmd/check_cpu_load.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ var checkCpuLoad = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
r := request.CheckCPULoadRequest{
CheckDeviceRequest: getCheckDeviceRequest(args[0]),
CPULoadThresholds: generateCheckThresholds(cmd, "", "warning", "", "critical"),
CPULoadThresholds: generateCheckThresholds(cmd, "", "warning", "", "critical", true),
}
handleRequest(&r)
},
Expand Down
2 changes: 1 addition & 1 deletion cmd/check_disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ var checkDiskCMD = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
r := request.CheckDiskRequest{
CheckDeviceRequest: getCheckDeviceRequest(args[0]),
DiskThresholds: generateCheckThresholds(cmd, "warning", "", "critical", ""),
DiskThresholds: generateCheckThresholds(cmd, "warning", "", "critical", "", false),
}
handleRequest(&r)
},
Expand Down
2 changes: 1 addition & 1 deletion cmd/check_memory_usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ var checkMemoryUsage = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
r := request.CheckMemoryUsageRequest{
CheckDeviceRequest: getCheckDeviceRequest(args[0]),
MemoryUsageThresholds: generateCheckThresholds(cmd, "", "warning", "", "critical"),
MemoryUsageThresholds: generateCheckThresholds(cmd, "", "warning", "", "critical", true),
}
handleRequest(&r)
},
Expand Down
2 changes: 1 addition & 1 deletion cmd/check_sbc.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var checkSBCCMD = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
r := request.CheckSBCRequest{
CheckDeviceRequest: getCheckDeviceRequest(args[0]),
SystemHealthScoreThresholds: generateCheckThresholds(cmd, "system-health-score-warning", "", "system-health-score-critical", ""),
SystemHealthScoreThresholds: generateCheckThresholds(cmd, "system-health-score-warning", "", "system-health-score-critical", "", false),
}
handleRequest(&r)
},
Expand Down
10 changes: 5 additions & 5 deletions cmd/check_ups.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ var checkUPSCMD = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
r := request.CheckUPSRequest{
CheckDeviceRequest: getCheckDeviceRequest(args[0]),
BatteryCurrentThresholds: generateCheckThresholds(cmd, "batt-current-warning-min", "batt-current-warning-max", "batt-current-critical-min", "batt-current-critical-max"),
BatteryTemperatureThresholds: generateCheckThresholds(cmd, "batt-temperature-warning-min", "batt-temperature-warning-max", "batt-temperature-critical-min", "batt-temperature-critical-max"),
CurrentLoadThresholds: generateCheckThresholds(cmd, "current-load-warning-min", "current-load-warning-max", "current-load-warning-max", "current-load-warning-max"),
RectifierCurrentThresholds: generateCheckThresholds(cmd, "rectifier-current-warning-min", "rectifier-current-warning-max", "rectifier-current-critical-min", "rectifier-current-critical-max"),
SystemVoltageThresholds: generateCheckThresholds(cmd, "system-voltage-warning-min", "system-voltage-warning-max", "system-voltage-critical-min", "system-voltage-critical-max"),
BatteryCurrentThresholds: generateCheckThresholds(cmd, "batt-current-warning-min", "batt-current-warning-max", "batt-current-critical-min", "batt-current-critical-max", false),
BatteryTemperatureThresholds: generateCheckThresholds(cmd, "batt-temperature-warning-min", "batt-temperature-warning-max", "batt-temperature-critical-min", "batt-temperature-critical-max", false),
CurrentLoadThresholds: generateCheckThresholds(cmd, "current-load-warning-min", "current-load-warning-max", "current-load-warning-max", "current-load-warning-max", false),
RectifierCurrentThresholds: generateCheckThresholds(cmd, "rectifier-current-warning-min", "rectifier-current-warning-max", "rectifier-current-critical-min", "rectifier-current-critical-max", false),
SystemVoltageThresholds: generateCheckThresholds(cmd, "system-voltage-warning-min", "system-voltage-warning-max", "system-voltage-critical-min", "system-voltage-critical-max", false),
}
handleRequest(&r)
},
Expand Down
3 changes: 2 additions & 1 deletion core/communicator/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ type baseCommunicator struct {

type relatedNetworkDeviceCommunicators struct {
head NetworkDeviceCommunicator
sub NetworkDeviceCommunicator
//nolint
sub NetworkDeviceCommunicator
}

func (c *baseCommunicator) GetVendor(_ context.Context) (string, error) {
Expand Down
26 changes: 14 additions & 12 deletions core/communicator/ceraos-ip10.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ type ceraosIP10Communicator struct {
baseCommunicator
}

// GetIfTable returns the ifTable of ceraos/ip10 devices.
// These devices need special behavior radio and ethernet interfaces.
func (c *ceraosIP10Communicator) GetIfTable(ctx context.Context) ([]device.Interface, error) {
subInterfaces, err := c.sub.GetIfTable(ctx)
if err != nil {
Expand All @@ -19,27 +21,27 @@ func (c *ceraosIP10Communicator) GetIfTable(ctx context.Context) ([]device.Inter

var targetInterface device.Interface

for i, inter := range subInterfaces {
ok, err := regexp.MatchString("^Ethernet #8$", *inter.IfDescr)
if err != nil {
return nil, errors.Wrap(err, "an unexpected error occurred while trying to match a regexp")
}
regex, err := regexp.Compile("^Ethernet #8$")
if err != nil {
return nil, errors.Wrap(err, "failed to build regex")
}

if ok {
for i, inter := range subInterfaces {
if regex.MatchString(*inter.IfDescr) {
targetInterface = inter
copy(subInterfaces[i:], subInterfaces[i+1:])
subInterfaces = subInterfaces[:len(subInterfaces)-1]
break
}
}

for i := range subInterfaces {
ok, err := regexp.MatchString("^Radio Interface #[0-9]+$", *subInterfaces[i].IfDescr)
if err != nil {
return nil, errors.Wrap(err, "an unexpected error occurred while trying to match a regexp")
}
regex, err = regexp.Compile("^Radio Interface #[0-9]+$")
if err != nil {
return nil, errors.Wrap(err, "failed to build regex")
}

if ok {
for i := range subInterfaces {
if regex.MatchString(*subInterfaces[i].IfDescr) {
subInterfaces[i].IfHCInOctets = targetInterface.IfHCInOctets
subInterfaces[i].IfHCOutOctets = targetInterface.IfHCOutOctets
subInterfaces[i].IfOperStatus = targetInterface.IfOperStatus
Expand Down
4 changes: 2 additions & 2 deletions core/communicator/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func CreateNetworkDeviceCommunicator(ctx context.Context, deviceClassIdentifier
return createCommunicatorByDeviceClass(ctx, deviceClass)
}

// IdentifyNetworkDeviceCommunicator identifies a devices and creates a network device communicator
// IdentifyNetworkDeviceCommunicator identifies a devices and creates a network device communicator.
func IdentifyNetworkDeviceCommunicator(ctx context.Context) (NetworkDeviceCommunicator, error) {
deviceClass, err := identifyDeviceClass(ctx)
if err != nil {
Expand All @@ -32,7 +32,7 @@ func IdentifyNetworkDeviceCommunicator(ctx context.Context) (NetworkDeviceCommun
return com, nil
}

// MatchDeviceClass checks if the device class in the context matches the given identifier
// MatchDeviceClass checks if the device class in the context matches the given identifier.
func MatchDeviceClass(ctx context.Context, identifier string) (bool, error) {
deviceClass, err := getDeviceClass(identifier)
if err != nil {
Expand Down
7 changes: 3 additions & 4 deletions core/communicator/device_class.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ type deviceClass struct {
config deviceClassConfig
identify deviceClassIdentify
components deviceClassComponents
yamlFile string
parentDeviceClass *deviceClass
subDeviceClasses map[string]*deviceClass
tryToMatchLast bool
Expand Down Expand Up @@ -838,15 +837,15 @@ func (d *deviceClassOIDs) validate() error {
}

func conditionContainsUniqueRequest(c condition) bool {
switch c.(type) {
switch c := c.(type) {
case *SnmpCondition:
if c.(*SnmpCondition).Type == "snmpget" {
if c.Type == "snmpget" {
return true
}
case *HTTPCondition:
return true
case *ConditionSet:
for _, con := range c.(*ConditionSet).Conditions {
for _, con := range c.Conditions {
if conditionContainsUniqueRequest(con) {
return true
}
Expand Down
13 changes: 11 additions & 2 deletions core/communicator/ekinops.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type ekinopsCommunicator struct {
baseCommunicator
}

// GetInterfaces returns the interfaces of ekinops devices.
func (c *ekinopsCommunicator) GetInterfaces(ctx context.Context) ([]device.Interface, error) {
interfaces, err := c.GetIfTable(ctx)
if err != nil {
Expand Down Expand Up @@ -69,15 +70,23 @@ func (c *ekinopsCommunicator) GetInterfaces(ctx context.Context) ([]device.Inter
return normalizeEkinopsInterfaces(interfaces)
}

// GetInterfaces returns the interfaces of ekinops devices.
// For ekinops devices, only a few interface values are required.
func (c *ekinopsCommunicator) GetIfTable(ctx context.Context) ([]device.Interface, error) {
if genericDeviceClass.components.interfaces.IfTable == nil {
return nil, errors.New("ifTable information is empty")
}

reader := *genericDeviceClass.components.interfaces.IfTable.(*snmpGroupPropertyReader)
oids := make(deviceClassOIDs)

regex, err := regexp.Compile("(ifIndex|ifDescr|ifType|ifName|ifAdminStatus|ifOperStatus|ifPhysAddress)")
if err != nil {
return nil, errors.Wrap(err, "failed to build regex")
}

for oid, value := range reader.oids {
if ok, err := regexp.MatchString("(ifIndex|ifDescr|ifType|ifName|ifAdminStatus|ifOperStatus|ifPhysAddress)", oid); err == nil && ok {
if regex.MatchString(oid) {
oids[oid] = value
}
}
Expand All @@ -97,7 +106,7 @@ func ekinopsInterfacesIfIdentifierToSliceIndex(interfaces []device.Interface) (m
if interf.IfName == nil {
return nil, fmt.Errorf("no ifName set for interface ifIndex: `%d`", *interf.IfIndex)
}
identifier := strings.Join(strings.Split(*interf.IfName, "/")[2:], "/")
identifier := strings.Split(strings.Join(strings.Split(*interf.IfName, "/")[2:], "/"), "(")[0]

if _, ok := m[identifier]; ok {
return nil, fmt.Errorf("interface identifier `%s` exists multiple times", *interf.IfName)
Expand Down
Loading

0 comments on commit c257604

Please sign in to comment.