Skip to content

Commit

Permalink
Merge branch 'main' into feature-redigo
Browse files Browse the repository at this point in the history
  • Loading branch information
TheFireMike committed May 26, 2021
2 parents 0bd5a58 + 40af228 commit e1c4c14
Show file tree
Hide file tree
Showing 6 changed files with 217 additions and 15 deletions.
10 changes: 5 additions & 5 deletions api/request_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func StartAPI() {
// 200:
// description: Returns the response.
// schema:
// $ref: '#/definitions/CheckResponse'
// $ref: '#/definitions/CheckIdentifyResponse'
// 400:
// description: Returns an error with more details in the body.
// schema:
Expand All @@ -143,7 +143,7 @@ func StartAPI() {
// 200:
// description: Returns the response.
// schema:
// $ref: '#/definitions/CheckResponse'
// $ref: '#/definitions/CheckSNMPResponse'
// 400:
// description: Returns an error with more details in the body.
// schema:
Expand Down Expand Up @@ -258,7 +258,7 @@ func StartAPI() {
// $ref: '#/definitions/OutputError'
e.POST("/check/memory-usage", checkMemoryUsage)

// swagger:operation POST /check/cpu-load check checkCpuLoad
// swagger:operation POST /check/cpu-load check checkCPULoad
// ---
// summary: Check the cpu load of a device.
// consumes:
Expand All @@ -283,7 +283,7 @@ func StartAPI() {
// description: Returns an error with more details in the body.
// schema:
// $ref: '#/definitions/OutputError'
e.POST("/check/cpu-load", checkCpuLoad)
e.POST("/check/cpu-load", checkCPULoad)

// swagger:operation POST /check/sbc check checkSBC
// ---
Expand Down Expand Up @@ -783,7 +783,7 @@ func checkMemoryUsage(ctx echo.Context) error {
return returnInFormat(ctx, http.StatusOK, resp)
}

func checkCpuLoad(ctx echo.Context) error {
func checkCPULoad(ctx echo.Context) error {
r := request.CheckCPULoadRequest{}
if err := ctx.Bind(&r); err != nil {
return err
Expand Down
35 changes: 29 additions & 6 deletions core/network/snmp_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ import (

// SNMPClient is used to communicate via snmp.
type SNMPClient struct {
client *gosnmp.GoSNMP
useCache bool
getCache requestCache
client *gosnmp.GoSNMP
useCache bool
getCache requestCache
walkCache requestCache
}

type snmpClientCreation struct {
Expand Down Expand Up @@ -269,9 +270,10 @@ func newSNMPClientTestConnection(client *gosnmp.GoSNMP) (*SNMPClient, error) {
client.ExponentialTimeout = true

return &SNMPClient{
client: client,
useCache: true,
getCache: newRequestCache(),
client: client,
useCache: true,
getCache: newRequestCache(),
walkCache: newRequestCache(),
}, nil
}

Expand Down Expand Up @@ -413,6 +415,17 @@ func (s *SNMPClient) SNMPGet(ctx context.Context, oid ...string) ([]SNMPResponse

// SNMPWalk sends a snmpwalk request to the specified oid.
func (s *SNMPClient) SNMPWalk(ctx context.Context, oid string) ([]SNMPResponse, error) {
if s.useCache {
x, err := s.walkCache.get(oid)
if err == nil {
res, ok := x.res.([]SNMPResponse)
if !ok {
return nil, errors.New("cached snmp result is not a snmp response")
}
return res, nil
}
}

s.client.Context = ctx

var response []gosnmp.SnmpPDU
Expand All @@ -428,11 +441,17 @@ func (s *SNMPClient) SNMPWalk(ctx context.Context, oid string) ([]SNMPResponse,
}
if err != nil {
err = errors.Wrap(err, "snmpwalk failed")
if s.useCache {
s.walkCache.add(oid, nil, err)
}
return nil, err
}

if response == nil {
err = tholaerr.NewNotFoundError("No Such Object available on this agent at this OID")
if s.useCache {
s.walkCache.add(oid, nil, err)
}
return nil, err
}

Expand All @@ -454,6 +473,10 @@ func (s *SNMPClient) SNMPWalk(ctx context.Context, oid string) ([]SNMPResponse,
}
}

if s.useCache {
s.walkCache.add(oid, res, nil)
}

return res, nil
}

Expand Down
4 changes: 4 additions & 0 deletions core/request/check_identify_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ type CheckIdentifyRequest struct {
SerialNumberDiffWarning bool `yaml:"serial_number_diff_warning" json:"serial_number_diff_warning" xml:"serial_number_diff_warning"`
}

// CheckIdentifyResponse
//
// CheckIdentifyResponse is a response struct for the check identify request.
//
// swagger:model
type CheckIdentifyResponse struct {
CheckResponse
IdentifyResult *device.Device `yaml:"identify_result" json:"identify_result" xml:"identify_result"`
Expand Down
4 changes: 4 additions & 0 deletions core/request/check_snmp_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ func (r *CheckSNMPRequest) setupConnection(_ context.Context) (*network.RequestD
return &network.RequestDeviceConnection{}, nil
}

// CheckSNMPResponse
//
// CheckSNMPResponse is a response struct for the check snmp request.
//
// swagger:model
type CheckSNMPResponse struct {
CheckResponse
SuccessfulSnmpCredentials *network.SNMPCredentials `yaml:"successful_snmp_credentials" json:"successful_snmp_credentials" xml:"successful_snmp_credentials"`
Expand Down
177 changes: 174 additions & 3 deletions doc/api_doc.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"check"
],
"summary": "Check the cpu load of a device.",
"operationId": "checkCpuLoad",
"operationId": "checkCPULoad",
"parameters": [
{
"description": "Request to process.",
Expand Down Expand Up @@ -179,7 +179,7 @@
"200": {
"description": "Returns the response.",
"schema": {
"$ref": "#/definitions/CheckResponse"
"$ref": "#/definitions/CheckIdentifyResponse"
}
},
"400": {
Expand Down Expand Up @@ -389,7 +389,7 @@
"200": {
"description": "Returns the response.",
"schema": {
"$ref": "#/definitions/CheckResponse"
"$ref": "#/definitions/CheckSNMPResponse"
}
},
"400": {
Expand Down Expand Up @@ -1153,6 +1153,47 @@
},
"x-go-package": "github.com/inexio/thola/core/request"
},
"CheckIdentifyResponse": {
"description": "CheckIdentifyResponse is a response struct for the check identify request.",
"type": "object",
"title": "CheckIdentifyResponse",
"properties": {
"failed_expectations": {
"type": "object",
"additionalProperties": {
"$ref": "#/definitions/IdentifyExpectationResult"
},
"x-go-name": "FailedExpectations"
},
"identify_result": {
"$ref": "#/definitions/Device"
},
"messages": {
"type": "array",
"items": {
"$ref": "#/definitions/OutputMessage"
},
"x-go-name": "Messages"
},
"performance_data": {
"type": "array",
"items": {
"$ref": "#/definitions/PerformanceDataPoint"
},
"x-go-name": "PerformanceData"
},
"raw_output": {
"type": "string",
"x-go-name": "RawOutput"
},
"status_code": {
"type": "integer",
"format": "int64",
"x-go-name": "StatusCode"
}
},
"x-go-package": "github.com/inexio/thola/core/request"
},
"CheckInterfaceMetricsRequest": {
"description": "CheckInterfaceRequest is a the request struct for the check interface metrics request.",
"type": "object",
Expand Down Expand Up @@ -1324,6 +1365,40 @@
},
"x-go-package": "github.com/inexio/thola/core/request"
},
"CheckSNMPResponse": {
"description": "CheckSNMPResponse is a response struct for the check snmp request.",
"type": "object",
"title": "CheckSNMPResponse",
"properties": {
"messages": {
"type": "array",
"items": {
"$ref": "#/definitions/OutputMessage"
},
"x-go-name": "Messages"
},
"performance_data": {
"type": "array",
"items": {
"$ref": "#/definitions/PerformanceDataPoint"
},
"x-go-name": "PerformanceData"
},
"raw_output": {
"type": "string",
"x-go-name": "RawOutput"
},
"status_code": {
"type": "integer",
"format": "int64",
"x-go-name": "StatusCode"
},
"successful_snmp_credentials": {
"$ref": "#/definitions/SNMPCredentials"
}
},
"x-go-package": "github.com/inexio/thola/core/request"
},
"CheckServerRequest": {
"description": "CheckServerRequest is a the request struct for the check server request.",
"type": "object",
Expand Down Expand Up @@ -1727,6 +1802,21 @@
},
"x-go-package": "github.com/inexio/thola/core/device"
},
"IdentifyExpectationResult": {
"type": "object",
"title": "IdentifyExpectationResult is a response struct for the check identify request.",
"properties": {
"expected": {
"type": "string",
"x-go-name": "Expected"
},
"got": {
"type": "string",
"x-go-name": "Got"
}
},
"x-go-package": "github.com/inexio/thola/core/request"
},
"IdentifyRequest": {
"description": "IdentifyRequest is a the request struct for the identify request.",
"type": "object",
Expand Down Expand Up @@ -2806,6 +2896,9 @@
161
]
},
"v3_data": {
"$ref": "#/definitions/SNMPv3ConnectionData"
},
"versions": {
"description": "The snmp version(s) of the device.",
"type": "array",
Expand All @@ -2820,6 +2913,84 @@
},
"x-go-package": "github.com/inexio/thola/core/network"
},
"SNMPCredentials": {
"type": "object",
"title": "SNMPCredentials includes all credential information of the snmp connection.",
"properties": {
"community": {
"type": "string",
"x-go-name": "Community"
},
"port": {
"type": "integer",
"format": "int64",
"x-go-name": "Port"
},
"v3ContextName": {
"type": "string",
"x-go-name": "V3ContextName"
},
"v3Level": {
"type": "string",
"x-go-name": "V3Level"
},
"version": {
"type": "string",
"x-go-name": "Version"
}
},
"x-go-package": "github.com/inexio/thola/core/network"
},
"SNMPv3ConnectionData": {
"description": "SNMPv3ConnectionData includes all snmp v3 specific connection data.",
"type": "object",
"title": "SNMPv3ConnectionData",
"properties": {
"auth_key": {
"description": "The authentication protocol passphrase of the SNMP connection.",
"type": "string",
"x-go-name": "AuthKey",
"example": "passphrase"
},
"auth_protocol": {
"description": "The authentication protocol of the SNMP connection.",
"type": "string",
"x-go-name": "AuthProtocol",
"example": "MD5"
},
"context_name": {
"description": "The context name of the SNMP connection.",
"type": "string",
"x-go-name": "ContextName",
"example": "bridge1"
},
"level": {
"description": "The security level of the SNMP connection.",
"type": "string",
"x-go-name": "Level",
"example": "authPriv"
},
"priv_key": {
"description": "The privacy protocol passphrase of the SNMP connection.",
"type": "string",
"x-go-name": "PrivKey",
"example": "passphrase"
},
"priv_protocol": {
"description": "The privacy protocol of the SNMP connection.",
"type": "string",
"x-go-name": "PrivProtocol",
"example": "DES"
},
"user": {
"description": "The user of the SNMP connection.",
"type": "string",
"x-go-name": "User",
"example": "user"
}
},
"x-go-package": "github.com/inexio/thola/core/network"
},
"ServerComponent": {
"description": "ServerComponent represents a server component.",
"type": "object",
Expand Down
2 changes: 1 addition & 1 deletion doc/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@
// swagger:meta
package doc

const Version = "v0.2.3"
const Version = "v0.2.4"

0 comments on commit e1c4c14

Please sign in to comment.