Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/dev' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
TheFireMike committed Feb 3, 2021
2 parents 8ddf624 + d5a6d85 commit 158b6bf
Show file tree
Hide file tree
Showing 5 changed files with 173 additions and 0 deletions.
78 changes: 78 additions & 0 deletions api/request_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,60 @@ func StartAPI() {
// $ref: '#/definitions/OutputError'
e.POST("/check/cpu-load", checkCpuLoad)

// swagger:operation POST /check/sbc check checkSBC
// ---
// summary: Check an sbc device.
// consumes:
// - application/json
// - application/xml
// produces:
// - application/json
// - application/xml
// parameters:
// - name: body
// in: body
// description: Request to process.
// required: true
// schema:
// $ref: '#/definitions/CheckSBCRequest'
// responses:
// 200:
// description: Returns the response.
// schema:
// $ref: '#/definitions/CheckSBCResponse'
// 400:
// description: Returns an error with more details in the body.
// schema:
// $ref: '#/definitions/OutputError'
e.POST("/check/sbc", checkSBC)

// swagger:operation POST /check/hardware-health check checkSBC
// ---
// summary: Check an hardware health of an device.
// consumes:
// - application/json
// - application/xml
// produces:
// - application/json
// - application/xml
// parameters:
// - name: body
// in: body
// description: Request to process.
// required: true
// schema:
// $ref: '#/definitions/CheckHardwareHealthRequest'
// responses:
// 200:
// description: Returns the response.
// schema:
// $ref: '#/definitions/CheckHardwareHealthResponse'
// 400:
// description: Returns an error with more details in the body.
// schema:
// $ref: '#/definitions/OutputError'
e.POST("/check/hardware-health", checkHardwareHealth)

// swagger:operation POST /read/interfaces read readInterfaces
// ---
// summary: Reads out data of the interfaces of a device.
Expand Down Expand Up @@ -633,6 +687,30 @@ func checkCpuLoad(ctx echo.Context) error {
return returnInFormat(ctx, http.StatusOK, resp)
}

func checkSBC(ctx echo.Context) error {
r := request.CheckSBCRequest{}
if err := ctx.Bind(&r); err != nil {
return err
}
resp, err := handleAPIRequest(ctx, &r, &r.BaseRequest.DeviceData.IPAddress)
if err != nil {
return handleError(ctx, err)
}
return returnInFormat(ctx, http.StatusOK, resp)
}

func checkHardwareHealth(ctx echo.Context) error {
r := request.CheckHardwareHealthRequest{}
if err := ctx.Bind(&r); err != nil {
return err
}
resp, err := handleAPIRequest(ctx, &r, &r.BaseRequest.DeviceData.IPAddress)
if err != nil {
return handleError(ctx, err)
}
return returnInFormat(ctx, http.StatusOK, resp)
}

func readInterfaces(ctx echo.Context) error {
r := request.ReadInterfacesRequest{}
if err := ctx.Bind(&r); err != nil {
Expand Down
23 changes: 23 additions & 0 deletions cmd/check_hardware_health.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package cmd

import (
"github.com/inexio/thola/core/request"
"github.com/spf13/cobra"
)

func init() {
addDeviceFlags(checkHardwareHealthCMD)
checkCMD.AddCommand(checkHardwareHealthCMD)
}

var checkHardwareHealthCMD = &cobra.Command{
Use: "hardware-health",
Short: "Check hardware-health of a device.",
Long: "Check hardware-health of a device and return various performance data.",
Run: func(cmd *cobra.Command, args []string) {
r := request.CheckHardwareHealthRequest{
CheckDeviceRequest: getCheckDeviceRequest(),
}
handleRequest(&r)
},
}
10 changes: 10 additions & 0 deletions core/request/check_hardware_health.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package request

// CheckHardwareHealthRequest
//
// CheckHardwareHealthRequest is a the request struct for the check sbc request.
//
// swagger:model
type CheckHardwareHealthRequest struct {
CheckDeviceRequest
}
58 changes: 58 additions & 0 deletions core/request/check_hardware_health_process.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// +build !client

package request

import (
"context"
"github.com/inexio/go-monitoringplugin"
)

func (r *CheckHardwareHealthRequest) process(ctx context.Context) (Response, error) {
r.init()

hhRequest := ReadHardwareHealthRequest{ReadRequest{r.BaseRequest}}
response, err := hhRequest.process(ctx)
if r.mon.UpdateStatusOnError(err, monitoringplugin.UNKNOWN, "error while processing read sbc request", true) {
return &CheckResponse{r.mon.GetInfo()}, nil
}
res := response.(*ReadHardwareHealthResponse)

if res.EnvironmentMonitorState != nil {
err = r.mon.AddPerformanceDataPoint(monitoringplugin.NewPerformanceDataPoint("environment_monitor_state", *res.EnvironmentMonitorState, ""))
if r.mon.UpdateStatusOnError(err, monitoringplugin.UNKNOWN, "error while adding performance data point", true) {
r.mon.PrintPerformanceData(false)
return &CheckResponse{r.mon.GetInfo()}, nil
}

// state 2 only works for oracle-acme sbs, this needs to be generalized once check hardware health is made for all device classes
r.mon.UpdateStatusIf(*res.EnvironmentMonitorState != 2, monitoringplugin.CRITICAL, "environment monitor state is critical")
}

for _, fan := range res.Fans {
if r.mon.UpdateStatusIf(fan.State == nil || fan.Description == nil, monitoringplugin.UNKNOWN, "description or state is missing for fan") {
r.mon.PrintPerformanceData(false)
return &CheckResponse{r.mon.GetInfo()}, nil
}
p := monitoringplugin.NewPerformanceDataPoint("fan_state", *fan.State, "").SetLabel(*fan.Description)
err = r.mon.AddPerformanceDataPoint(p)
if r.mon.UpdateStatusOnError(err, monitoringplugin.UNKNOWN, "error while adding performance data point", true) {
r.mon.PrintPerformanceData(false)
return &CheckResponse{r.mon.GetInfo()}, nil
}
}

for _, powerSupply := range res.PowerSupply {
if r.mon.UpdateStatusIf(powerSupply.State == nil || powerSupply.Description == nil, monitoringplugin.UNKNOWN, "description or state is missing for power supply") {
r.mon.PrintPerformanceData(false)
return &CheckResponse{r.mon.GetInfo()}, nil
}
p := monitoringplugin.NewPerformanceDataPoint("power_supply_state", *powerSupply.State, "").SetLabel(*powerSupply.Description)
err = r.mon.AddPerformanceDataPoint(p)
if r.mon.UpdateStatusOnError(err, monitoringplugin.UNKNOWN, "error while adding performance data point", true) {
r.mon.PrintPerformanceData(false)
return &CheckResponse{r.mon.GetInfo()}, nil
}
}

return &CheckResponse{r.mon.GetInfo()}, nil
}
4 changes: 4 additions & 0 deletions core/request/client_process.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ func (r *CheckCPULoadRequest) process(ctx context.Context) (Response, error) {
return checkProcess(ctx, r, "check/cpu-load"), nil
}

func (r *CheckHardwareHealthRequest) process(ctx context.Context) (Response, error) {
return checkProcess(ctx, r, "check/hardware-health"), nil
}

func (r *ReadInterfacesRequest) process(ctx context.Context) (Response, error) {
apiFormat := viper.GetString("target-api-format")
responseBody, err := sendToAPI(ctx, r, "read/interfaces", apiFormat)
Expand Down

0 comments on commit 158b6bf

Please sign in to comment.