mirror of
https://github.com/edgebox-iot/edgeboxctl.git
synced 2026-09-24 05:41:43 +02:00
Added system module, GetUptimeSeconds and GetUptimeFormatted funcs, taskGetSystemUptime
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
package system
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"github.com/shirou/gopsutil/host"
|
||||
)
|
||||
|
||||
func GetUptimeInSeconds() string {
|
||||
uptime, _ := host.Uptime()
|
||||
|
||||
return strconv.FormatUint(uptime, 10)
|
||||
}
|
||||
|
||||
func GetUptimeFormatted() string {
|
||||
uptime, _ := host.Uptime()
|
||||
|
||||
days := uptime / (60 * 60 * 24)
|
||||
hours := (uptime - (days * 60 * 60 * 24)) / (60 * 60)
|
||||
minutes := ((uptime - (days * 60 * 60 * 24)) - (hours * 60 * 60)) / 60
|
||||
return fmt.Sprintf("%d days, %d hours, %d minutes", days, hours, minutes)
|
||||
}
|
||||
@@ -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/system"
|
||||
"github.com/edgebox-iot/edgeboxctl/internal/utils"
|
||||
_ "github.com/go-sql-driver/mysql" // Mysql Driver
|
||||
_ "github.com/mattn/go-sqlite3" // SQlite Driver
|
||||
@@ -247,8 +248,13 @@ func ExecuteTask(task Task) Task {
|
||||
func ExecuteSchedules(tick int) {
|
||||
|
||||
if tick == 1 {
|
||||
|
||||
// Executing on startup (first tick). Schedules run before tasks in the SystemIterator
|
||||
uptime := taskGetSystemUptime()
|
||||
log.Println("Uptime is " + uptime + " seconds (" + system.GetUptimeFormatted() + ")")
|
||||
|
||||
log.Println(taskGetEdgeApps())
|
||||
|
||||
}
|
||||
|
||||
if tick%30 == 0 {
|
||||
@@ -400,3 +406,32 @@ func taskGetEdgeApps() string {
|
||||
return string(edgeAppsJSON)
|
||||
|
||||
}
|
||||
|
||||
func taskGetSystemUptime() string {
|
||||
fmt.Println("Executing taskGetSystemUptime")
|
||||
|
||||
uptime := system.GetUptimeInSeconds()
|
||||
|
||||
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("SYSTEM_UPTIME", uptime, formatedDatetime, formatedDatetime) // Execute SQL Statement
|
||||
if err != nil {
|
||||
log.Fatal(err.Error())
|
||||
}
|
||||
|
||||
db.Close()
|
||||
|
||||
return uptime
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user