From fd002987fa76eb84f2d92ac5c08908b885509f96 Mon Sep 17 00:00:00 2001 From: Paulo Truta Date: Sun, 6 Jun 2021 12:29:46 +0000 Subject: [PATCH] Added storage module, base structs, getDevices() func, taskGetStorageDevices (ticks every 30) --- internal/storage/storage.go | 70 +++++++++++++++++++++++++++++++++++++ internal/tasks/tasks.go | 33 +++++++++++++++++ 2 files changed, 103 insertions(+) create mode 100644 internal/storage/storage.go diff --git a/internal/storage/storage.go b/internal/storage/storage.go new file mode 100644 index 0000000..2faef76 --- /dev/null +++ b/internal/storage/storage.go @@ -0,0 +1,70 @@ +package storage + +import ( + "bufio" + "strings" + + "github.com/edgebox-iot/edgeboxctl/internal/utils" +) + +// Device : Struct representing a storage device in the system +type Device struct { + ID string `json:"id"` + Name string `json:"name"` + Size string `json:"size"` + MainDevice bool `json:"main_device"` + MAJ string `json:"maj"` + MIN string `json:"min"` + RM string `json:"rm"` + RO string `json:"ro"` + Status DeviceStatus `json:"status"` +} + +// DeviceStatus : Struct representing possible storage device statuses (code + description) +type DeviceStatus struct { + ID int `json:"id"` + Description string `json:"description"` +} + +// GetDevices : Returns a list of all available sotrage devices in structs filled with information +func GetDevices() []Device { + + var devices []Device + + cmdArgs := []string{"--raw", "--nodeps", "--noheadings"} + cmdOutput := utils.Exec("lsblk", cmdArgs) + cmdOutputReader := strings.NewReader(cmdOutput) + scanner := bufio.NewScanner(cmdOutputReader) + scanner.Split(bufio.ScanLines) + + mainDevice := true + + for scanner.Scan() { + // 1 Device is represented here. Extract words in order for filling a Device struct + // Example deviceRawInfo: "mmcblk0 179:0 0 29.7G 0 disk" + + deviceRawInfo := strings.Fields(scanner.Text()) + majMin := strings.SplitN(deviceRawInfo[1], ":", 2) + + device := Device{ + ID: deviceRawInfo[0], + Name: deviceRawInfo[0], + Size: deviceRawInfo[3], + MainDevice: mainDevice, + MAJ: majMin[0], + MIN: majMin[1], + RO: deviceRawInfo[4], + RM: deviceRawInfo[2], + Status: DeviceStatus{ID: 1, Description: "healthy"}, + } + + // Once the first device is found, set to false. + if mainDevice == true { + mainDevice = false + } + + devices = append(devices, device) + } + + return devices +} diff --git a/internal/tasks/tasks.go b/internal/tasks/tasks.go index 064ffa2..adc46c6 100644 --- a/internal/tasks/tasks.go +++ b/internal/tasks/tasks.go @@ -10,6 +10,7 @@ import ( "github.com/edgebox-iot/edgeboxctl/internal/diagnostics" "github.com/edgebox-iot/edgeboxctl/internal/edgeapps" + "github.com/edgebox-iot/edgeboxctl/internal/storage" "github.com/edgebox-iot/edgeboxctl/internal/system" "github.com/edgebox-iot/edgeboxctl/internal/utils" _ "github.com/go-sql-driver/mysql" // Mysql Driver @@ -253,12 +254,14 @@ func ExecuteSchedules(tick int) { uptime := taskGetSystemUptime() log.Println("Uptime is " + uptime + " seconds (" + system.GetUptimeFormatted() + ")") + log.Println(taskGetStorageDevices()) log.Println(taskGetEdgeApps()) } if tick%30 == 0 { // Executing every 30 ticks + log.Println(taskGetStorageDevices()) log.Println(taskGetEdgeApps()) } @@ -435,3 +438,33 @@ func taskGetSystemUptime() string { return uptime } + +func taskGetStorageDevices() string { + fmt.Println("Executing taskGetStorageDevices") + + devices := storage.GetDevices() + devicesJSON, _ := json.Marshal(devices) + + db, err := sql.Open("sqlite3", utils.GetSQLiteDbConnectionDetails()) + + if err != nil { + log.Fatal(err.Error()) + } + + statement, err := db.Prepare("REPLACE into option (name, value, created, updated) VALUES (?, ?, ?, ?);") // Prepare SQL Statement + if err != nil { + log.Fatal(err.Error()) + } + + formatedDatetime := utils.GetSQLiteFormattedDateTime(time.Now()) + + _, err = statement.Exec("STORAGE_DEVICES_LIST", devicesJSON, formatedDatetime, formatedDatetime) // Execute SQL Statement + if err != nil { + log.Fatal(err.Error()) + } + + db.Close() + + return string(devicesJSON) + +}