|
|
@ -5,36 +5,45 @@ import (
|
|
|
|
dbModels "github.com/analogj/scrutiny/webapp/backend/pkg/models/db"
|
|
|
|
dbModels "github.com/analogj/scrutiny/webapp/backend/pkg/models/db"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/jinzhu/gorm"
|
|
|
|
"github.com/jinzhu/gorm"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"net/http"
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
func UploadDeviceMetrics(c *gin.Context) {
|
|
|
|
func UploadDeviceMetrics(c *gin.Context) {
|
|
|
|
db := c.MustGet("DB").(*gorm.DB)
|
|
|
|
db := c.MustGet("DB").(*gorm.DB)
|
|
|
|
|
|
|
|
logger := c.MustGet("LOGGER").(logrus.FieldLogger)
|
|
|
|
|
|
|
|
|
|
|
|
var collectorSmartData collector.SmartInfo
|
|
|
|
var collectorSmartData collector.SmartInfo
|
|
|
|
err := c.BindJSON(&collectorSmartData)
|
|
|
|
err := c.BindJSON(&collectorSmartData)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
//TODO: cannot parse smart data
|
|
|
|
logger.Errorln("Cannot parse SMART data", err)
|
|
|
|
log.Error("Cannot parse SMART data")
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"success": false})
|
|
|
|
c.JSON(http.StatusOK, gin.H{"success": false})
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//update the device information if necessary
|
|
|
|
//update the device information if necessary
|
|
|
|
var device dbModels.Device
|
|
|
|
var device dbModels.Device
|
|
|
|
db.Where("wwn = ?", c.Param("wwn")).First(&device)
|
|
|
|
db.Where("wwn = ?", c.Param("wwn")).First(&device)
|
|
|
|
device.UpdateFromCollectorSmartInfo(collectorSmartData)
|
|
|
|
device.UpdateFromCollectorSmartInfo(collectorSmartData)
|
|
|
|
db.Model(&device).Updates(device)
|
|
|
|
if err := db.Model(&device).Updates(device).Error; err != nil {
|
|
|
|
|
|
|
|
logger.Errorln("An error occurred while updating device data from smartctl metrics", err)
|
|
|
|
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"success": false})
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// insert smart info
|
|
|
|
// insert smart info
|
|
|
|
deviceSmartData := dbModels.Smart{}
|
|
|
|
deviceSmartData := dbModels.Smart{}
|
|
|
|
err = deviceSmartData.FromCollectorSmartInfo(c.Param("wwn"), collectorSmartData)
|
|
|
|
err = deviceSmartData.FromCollectorSmartInfo(c.Param("wwn"), collectorSmartData)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
c.JSON(http.StatusOK, gin.H{"success": false})
|
|
|
|
logger.Errorln("Could not process SMART metrics", err)
|
|
|
|
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"success": false})
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := db.Create(&deviceSmartData).Error; err != nil {
|
|
|
|
|
|
|
|
logger.Errorln("An error occurred while saving smartctl metrics", err)
|
|
|
|
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"success": false})
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
db.Create(&deviceSmartData)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{"success": true})
|
|
|
|
c.JSON(http.StatusOK, gin.H{"success": true})
|
|
|
|
}
|
|
|
|
}
|
|
|
|