Compare commits
2
Commits
be44d008c7
...
cad4ae65f7
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
cad4ae65f7 | ||
|
|
88b5ff98cd |
No files matched your search
@@ -24,5 +24,3 @@ go.work.sum
|
|||||||
|
|
||||||
# env file
|
# env file
|
||||||
.env
|
.env
|
||||||
|
|
||||||
!lhm/*
|
|
||||||
@@ -5,8 +5,16 @@ import (
|
|||||||
"rpi5-health/internal/http_internal"
|
"rpi5-health/internal/http_internal"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
/* TODO:
|
||||||
|
* 1. Storage & Disk I/O: Disk capacity | Disk I/O rates | IOWAIT metrics
|
||||||
|
* 2. Network traffic: Bandwidth usage | Total network usage | Active connections
|
||||||
|
* 3. Process monitoring: Top consumers | Zombie processes */
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
srv := http_internal.NewAppServer()
|
srv := http_internal.NewAppServer()
|
||||||
|
|
||||||
|
srv.RegisterRoute("/api/events", http_internal.SSEHandler)
|
||||||
|
|
||||||
go srv.Start()
|
go srv.Start()
|
||||||
|
|
||||||
cli.Listen()
|
cli.Listen()
|
||||||
|
|||||||
@@ -1,12 +1,28 @@
|
|||||||
package http_internal
|
package http_internal
|
||||||
|
|
||||||
import "net/http"
|
import (
|
||||||
|
"net/http"
|
||||||
|
loadaverage "rpi5-health/internal/load_average"
|
||||||
|
"rpi5-health/internal/memory"
|
||||||
|
"rpi5-health/internal/processor"
|
||||||
|
"rpi5-health/internal/temperature"
|
||||||
|
"rpi5-health/internal/uptime"
|
||||||
|
)
|
||||||
|
|
||||||
type AppServer struct {
|
type AppServer struct {
|
||||||
srv *http.Server
|
srv *http.Server
|
||||||
mux *http.ServeMux
|
mux *http.ServeMux
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// To be sent to the client
|
||||||
|
type ServerStatistics struct {
|
||||||
|
LoadAvgInfo loadaverage.LoadAverageInformation `json:"load_average"`
|
||||||
|
MemoryInfo memory.MemoryInformation `json:"memory_info"`
|
||||||
|
ProcessorInfo processor.ProcessorInformation `json:"processor_info"`
|
||||||
|
TemperatureInfo temperature.TemperatureInformation `json:"temperature_info"`
|
||||||
|
UptimeInfo uptime.UptimeInformation `json:"uptime_info"`
|
||||||
|
}
|
||||||
|
|
||||||
func NewAppServer() *AppServer {
|
func NewAppServer() *AppServer {
|
||||||
mux := http.NewServeMux()
|
mux := http.NewServeMux()
|
||||||
|
|
||||||
@@ -28,3 +44,7 @@ func NewAppServer() *AppServer {
|
|||||||
func (srv *AppServer) Start() {
|
func (srv *AppServer) Start() {
|
||||||
srv.srv.ListenAndServe()
|
srv.srv.ListenAndServe()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (srv *AppServer) RegisterRoute(endpoint string, handler http.HandlerFunc) {
|
||||||
|
srv.mux.HandleFunc(endpoint, handler)
|
||||||
|
}
|
||||||
@@ -0,0 +1,67 @@
|
|||||||
|
package http_internal
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
loadaverage "rpi5-health/internal/load_average"
|
||||||
|
"rpi5-health/internal/memory"
|
||||||
|
"rpi5-health/internal/processor"
|
||||||
|
"rpi5-health/internal/temperature"
|
||||||
|
"rpi5-health/internal/uptime"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func SSEHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.Header().Set("Content-Type", "text/event-stream")
|
||||||
|
w.Header().Set("Cache-Control", "no-cache")
|
||||||
|
w.Header().Set("Connection", "keep-alive")
|
||||||
|
|
||||||
|
// For CORS requests
|
||||||
|
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||||||
|
|
||||||
|
clientGone := r.Context().Done() // Channel to signal client disconnection
|
||||||
|
|
||||||
|
rc := http.NewResponseController(w)
|
||||||
|
t := time.NewTicker(time.Second * 5)
|
||||||
|
defer t.Stop()
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-clientGone:
|
||||||
|
log.Println("Client disconnected")
|
||||||
|
return
|
||||||
|
case <-t.C:
|
||||||
|
resp := buildDashboardResponse()
|
||||||
|
_, err := fmt.Fprintf(w, "data: %s\n\n", resp)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
err = rc.Flush()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func buildDashboardResponse() []byte {
|
||||||
|
// TODO: proper error handling
|
||||||
|
loadavg, _ := loadaverage.GetLoadAverage()
|
||||||
|
memoryInfo, _ := memory.GetRamInformation()
|
||||||
|
processorInfo := processor.GetCPUInformation()
|
||||||
|
temperatureInfo, _ := temperature.GetTemperatureInformation()
|
||||||
|
uptimeInfo, _ := uptime.GetUptimeInformation()
|
||||||
|
|
||||||
|
stats := ServerStatistics{
|
||||||
|
LoadAvgInfo: loadavg,
|
||||||
|
MemoryInfo: memoryInfo,
|
||||||
|
ProcessorInfo: processorInfo,
|
||||||
|
TemperatureInfo: temperatureInfo,
|
||||||
|
UptimeInfo: uptimeInfo,
|
||||||
|
}
|
||||||
|
|
||||||
|
payload, _ := json.Marshal(stats)
|
||||||
|
|
||||||
|
return payload
|
||||||
|
}
|
||||||
@@ -37,7 +37,7 @@ func getLinuxUptime() (UptimeInformation, error) {
|
|||||||
busyTime, _ = strconv.ParseFloat(fields[0], 64)
|
busyTime, _ = strconv.ParseFloat(fields[0], 64)
|
||||||
|
|
||||||
return UptimeInformation{
|
return UptimeInformation{
|
||||||
uptime: busyTime,
|
Uptime: busyTime,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,15 +6,11 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type UptimeInformation struct {
|
type UptimeInformation struct {
|
||||||
uptime float64
|
Uptime float64
|
||||||
}
|
|
||||||
|
|
||||||
func (uptimeInfo UptimeInformation) GetUptime() (seconds float64) {
|
|
||||||
return uptimeInfo.uptime
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (uptimeInfo UptimeInformation) FormatSeconds() string {
|
func (uptimeInfo UptimeInformation) FormatSeconds() string {
|
||||||
total := int64(math.Round(uptimeInfo.uptime))
|
total := int64(math.Round(uptimeInfo.Uptime))
|
||||||
|
|
||||||
if total < 60 {
|
if total < 60 {
|
||||||
return fmt.Sprintf("%d", total)
|
return fmt.Sprintf("%d", total)
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ func getWinUptime(uptime *windows.LazyProc) (UptimeInformation, error) {
|
|||||||
|
|
||||||
uptimeSeconds := float64(uptimeMetric) / 1000.0
|
uptimeSeconds := float64(uptimeMetric) / 1000.0
|
||||||
|
|
||||||
return UptimeInformation{uptime: uptimeSeconds}, nil
|
return UptimeInformation{Uptime: uptimeSeconds}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetUptimeInformation() (UptimeInformation, error) {
|
func GetUptimeInformation() (UptimeInformation, error) {
|
||||||
|
|||||||
Reference in new issue
Block a user