Skip to content

Backend will add invalid data to the database if smartctl returns a json-formatted error #944

Description

@kaysond

smartctl will return a non-zero exit code under a number of conditions, defined in their source here.

If the collector gets a non-zero exit code for a smartctl --xall, it will still send the data to the backend:

result, err := mc.shell.Command(mc.logger, mc.config.GetString("commands.metrics_smartctl_bin"), args, "", os.Environ())
resultBytes := []byte(result)
if err != nil {
if exitError, ok := err.(*exec.ExitError); ok {
// smartctl command exited with an error, we should still push the data to the API server
mc.logger.Errorf("smartctl returned an error code (%d) while processing %s\n", exitError.ExitCode(), deviceName)
mc.LogSmartctlExitCode(exitError.ExitCode())
mc.Publish(deviceWWN, resultBytes)

which will happily unmarshall it into our model and push it (with a bunch of uninitialized struct fields) into the database:

// insert smart info
smartData, err := deviceRepo.SaveSmartAttributes(c, c.Param("wwn"), collectorSmartData)
if err != nil {
logger.Errorln("An error occurred while saving smartctl metrics", err)
c.JSON(http.StatusInternalServerError, gin.H{"success": false})
return
}

We should update the exit_status field in our model to match the bitmask from the smartctl source:

package collector
type SmartInfo struct {
JSONFormatVersion []int `json:"json_format_version"`
Smartctl struct {
Version []int `json:"version"`
SvnRevision string `json:"svn_revision"`
PlatformInfo string `json:"platform_info"`
BuildInfo string `json:"build_info"`
Argv []string `json:"argv"`
ExitStatus int `json:"exit_status"`

and in the backend handler shown above, skip pushing data if any of bits 2-0 are set.

Note that some error codes are shared! Smartctl exits 2 for a variety of reasons including a drive being in standby.

Metadata

Metadata

Assignees

No one assigned

    Labels

    contributor friendlyA well-scoped, approachable issue for someone looking to contribute.goIssues that require updating Go code

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions