b44ef5cb9c
/scrutiny/config/collector.yaml Adding ability to specify host identifier (label), that is updated on every collector run. Can be specified by `host-id` CLI or `COLLECTOR_HOST_ID` env var. Created a config class, interface and associated tests. Created a "TransformDetectedDrives" function, that will allow users to insert drives not detected by Smarctl --scan, ignore drives that they dont want, and override smartctl device type. Added Upsert functionality when registering devices. Replaced "github.com/jinzhu/gorm" with "gorm.io/gorm" (ORM location moved, was using incorrect lib url) Removed machineid library.
54 lines
1.5 KiB
Go
54 lines
1.5 KiB
Go
package handler
|
|
|
|
import (
|
|
dbModels "github.com/analogj/scrutiny/webapp/backend/pkg/models/db"
|
|
"github.com/gin-gonic/gin"
|
|
"gorm.io/gorm"
|
|
"gorm.io/gorm/clause"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
"net/http"
|
|
)
|
|
|
|
// register devices that are detected by various collectors.
|
|
// This function is run everytime a collector is about to start a run. It can be used to update device data.
|
|
func RegisterDevices(c *gin.Context) {
|
|
db := c.MustGet("DB").(*gorm.DB)
|
|
logger := c.MustGet("LOGGER").(logrus.FieldLogger)
|
|
|
|
var collectorDeviceWrapper dbModels.DeviceWrapper
|
|
err := c.BindJSON(&collectorDeviceWrapper)
|
|
if err != nil {
|
|
logger.Errorln("Cannot parse detected devices", err)
|
|
c.JSON(http.StatusInternalServerError, gin.H{"success": false})
|
|
return
|
|
}
|
|
|
|
errs := []error{}
|
|
for _, dev := range collectorDeviceWrapper.Data {
|
|
//insert devices into DB (and update specified columns if device is already registered)
|
|
// update device fields that may change: (DeviceType, HostID)
|
|
if err := db.Clauses(clause.OnConflict{
|
|
Columns: []clause.Column{{Name: "wwn"}},
|
|
DoUpdates: clause.AssignmentColumns([]string{"host_id", "device_name"}),
|
|
}).Create(&dev).Error; err != nil {
|
|
|
|
errs = append(errs, err)
|
|
}
|
|
}
|
|
|
|
if len(errs) > 0 {
|
|
logger.Errorln("An error occurred while registering devices", errs)
|
|
c.JSON(http.StatusInternalServerError, gin.H{
|
|
"success": false,
|
|
})
|
|
return
|
|
} else {
|
|
c.JSON(http.StatusOK, dbModels.DeviceWrapper{
|
|
Success: true,
|
|
Data: collectorDeviceWrapper.Data,
|
|
})
|
|
return
|
|
}
|
|
}
|