From d5a6d8579ddbdee84d1aa4b19ff04a5d9cd2b2aa Mon Sep 17 00:00:00 2001 From: babos77 Date: Tue, 2 Feb 2021 13:40:44 +0100 Subject: [PATCH] check hardware-health for oracle-acme --- api/request_handler.go | 78 +++++++++++++++++++ cmd/check_hardware_health.go | 23 ++++++ core/request/check_hardware_health.go | 10 +++ core/request/check_hardware_health_process.go | 58 ++++++++++++++ core/request/client_process.go | 4 + 5 files changed, 173 insertions(+) create mode 100644 cmd/check_hardware_health.go create mode 100644 core/request/check_hardware_health.go create mode 100644 core/request/check_hardware_health_process.go diff --git a/api/request_handler.go b/api/request_handler.go index 4ec47e1..c0466ab 100644 --- a/api/request_handler.go +++ b/api/request_handler.go @@ -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. @@ -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 { diff --git a/cmd/check_hardware_health.go b/cmd/check_hardware_health.go new file mode 100644 index 0000000..76f1d80 --- /dev/null +++ b/cmd/check_hardware_health.go @@ -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) + }, +} diff --git a/core/request/check_hardware_health.go b/core/request/check_hardware_health.go new file mode 100644 index 0000000..b35390d --- /dev/null +++ b/core/request/check_hardware_health.go @@ -0,0 +1,10 @@ +package request + +// CheckHardwareHealthRequest +// +// CheckHardwareHealthRequest is a the request struct for the check sbc request. +// +// swagger:model +type CheckHardwareHealthRequest struct { + CheckDeviceRequest +} diff --git a/core/request/check_hardware_health_process.go b/core/request/check_hardware_health_process.go new file mode 100644 index 0000000..92bce2e --- /dev/null +++ b/core/request/check_hardware_health_process.go @@ -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 +} diff --git a/core/request/client_process.go b/core/request/client_process.go index 3ae08a6..2083bd1 100644 --- a/core/request/client_process.go +++ b/core/request/client_process.go @@ -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)