72 lines
1.4 KiB
Go
72 lines
1.4 KiB
Go
package pkg
|
|
|
|
import (
|
|
"encoding/json"
|
|
"log"
|
|
"net/url"
|
|
"os"
|
|
|
|
"github.com/gorilla/websocket"
|
|
)
|
|
|
|
// Struct
|
|
type Message struct {
|
|
Now int64 `json:"now"` // timestamp
|
|
Online int `json:"online"` // online number
|
|
Servers []Servers `json:"servers"`
|
|
}
|
|
|
|
type Servers struct {
|
|
Id int `json:"id"`
|
|
Name string `json:"name"`
|
|
Host Host `json:"host"`
|
|
State State `json:"state"`
|
|
}
|
|
|
|
type Host struct {
|
|
Disk_total int `json:"disk_total"`
|
|
Mem_total int `json:"mem_total"`
|
|
Arch string `json:"arch"`
|
|
Platform string `json:"platform"`
|
|
}
|
|
|
|
type State struct {
|
|
Cpu float64 `json:"cpu"`
|
|
Disk_used int `json:"disk_used"`
|
|
Load_5 float32 `json:"load_5"`
|
|
Net_in_speed int `json:"net_in_speed"`
|
|
Net_out_speed int `json:"net_out_speed"`
|
|
Process_count int `json:"process_count"`
|
|
}
|
|
|
|
func GetData() (servers Message) {
|
|
nazha_host := os.Getenv("NEZHA_HOST")
|
|
if nazha_host == "" {
|
|
log.Fatal("NEZHA_HOST environment variable is not set")
|
|
return
|
|
}
|
|
|
|
u := url.URL{Scheme: "ws", Host: nazha_host, Path: "/api/v1/ws/server"}
|
|
|
|
conn, _, err := websocket.DefaultDialer.Dial(u.String(), nil)
|
|
if err != nil {
|
|
log.Fatal("dial error:", err)
|
|
return
|
|
}
|
|
defer conn.Close()
|
|
|
|
_, data, err := conn.ReadMessage()
|
|
if err != nil {
|
|
log.Fatal("read error:", err)
|
|
return
|
|
}
|
|
|
|
var msg Message
|
|
if err = json.Unmarshal(data, &msg); err != nil {
|
|
log.Println("json parse error:", err)
|
|
return
|
|
}
|
|
|
|
return msg
|
|
}
|