mirror of
https://github.com/fankes/komari-agent.git
synced 2025-10-18 18:49:23 +08:00
Refactor monitoring package: remove platform-specific files and consolidate OS detection and process counting logic
- Deleted os_windows.go and process_windows.go, replacing them with platform-agnostic implementations in unit directory. - Removed Linux-specific process counting logic from process_linux.go and integrated it into unit. - Consolidated uptime and OS name retrieval into unit files for better organization. - Updated update mechanism to use global variables for current version and repository. - Introduced command-line flags for configuration, including disabling auto-update and web SSH. - Implemented WebSocket connection handling and terminal interaction for both Unix and Windows systems. - Added basic info upload functionality to server package, enhancing monitoring capabilities.
This commit is contained in:
81
server/basicInfo.go
Normal file
81
server/basicInfo.go
Normal file
@@ -0,0 +1,81 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/komari-monitor/komari-agent/cmd/flags"
|
||||
monitoring "github.com/komari-monitor/komari-agent/monitoring/unit"
|
||||
"github.com/komari-monitor/komari-agent/update"
|
||||
)
|
||||
|
||||
func DoUploadBasicInfoWorks() {
|
||||
err := uploadBasicInfo()
|
||||
if err != nil {
|
||||
log.Println("Error uploading basic info:", err)
|
||||
}
|
||||
ticker := time.NewTicker(time.Duration(15) * time.Minute)
|
||||
for range ticker.C {
|
||||
err := uploadBasicInfo()
|
||||
if err != nil {
|
||||
log.Println("Error uploading basic info:", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func uploadBasicInfo() error {
|
||||
cpu := monitoring.Cpu()
|
||||
|
||||
osname := monitoring.OSName()
|
||||
ipv4, ipv6, _ := monitoring.GetIPAddress()
|
||||
|
||||
data := map[string]interface{}{
|
||||
"cpu_name": cpu.CPUName,
|
||||
"cpu_cores": cpu.CPUCores,
|
||||
"arch": cpu.CPUArchitecture,
|
||||
"os": osname,
|
||||
"ipv4": ipv4,
|
||||
"ipv6": ipv6,
|
||||
"mem_total": monitoring.Ram().Total,
|
||||
"swap_total": monitoring.Swap().Total,
|
||||
"disk_total": monitoring.Disk().Total,
|
||||
"gpu_name": "Unknown",
|
||||
"version": update.CurrentVersion,
|
||||
}
|
||||
|
||||
endpoint := strings.TrimSuffix(flags.Endpoint, "/") + "/api/clients/uploadBasicInfo?token=" + flags.Token
|
||||
payload, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("POST", endpoint, strings.NewReader(string(payload)))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
message := string(body)
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return fmt.Errorf("status code: %d,%s", resp.StatusCode, message)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
138
server/websocket.go
Normal file
138
server/websocket.go
Normal file
@@ -0,0 +1,138 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gorilla/websocket"
|
||||
"github.com/komari-monitor/komari-agent/cmd/flags"
|
||||
"github.com/komari-monitor/komari-agent/monitoring"
|
||||
"github.com/komari-monitor/komari-agent/terminal"
|
||||
)
|
||||
|
||||
func EstablishWebSocketConnection() {
|
||||
|
||||
websocketEndpoint := strings.TrimSuffix(flags.Endpoint, "/") + "/api/clients/report?token=" + flags.Token
|
||||
websocketEndpoint = "ws" + strings.TrimPrefix(websocketEndpoint, "http")
|
||||
|
||||
var conn *websocket.Conn
|
||||
defer func() {
|
||||
if conn != nil {
|
||||
conn.Close()
|
||||
}
|
||||
}()
|
||||
var err error
|
||||
var interval float64
|
||||
if flags.Interval <= 1 {
|
||||
interval = 1
|
||||
} else {
|
||||
interval = flags.Interval - 1
|
||||
}
|
||||
|
||||
ticker := time.NewTicker(time.Duration(interval * float64(time.Second)))
|
||||
defer ticker.Stop()
|
||||
|
||||
for range ticker.C {
|
||||
// If no connection, attempt to connect
|
||||
if conn == nil {
|
||||
log.Println("Attempting to connect to WebSocket...")
|
||||
retry := 0
|
||||
for retry <= flags.MaxRetries {
|
||||
if retry > 0 {
|
||||
log.Println("Retrying websocket connection, attempt:", retry)
|
||||
}
|
||||
conn, err = connectWebSocket(websocketEndpoint)
|
||||
if err == nil {
|
||||
log.Println("WebSocket connected")
|
||||
go handleWebSocketMessages(conn, make(chan struct{}))
|
||||
break
|
||||
} else {
|
||||
log.Println("Failed to connect to WebSocket:", err)
|
||||
}
|
||||
retry++
|
||||
time.Sleep(time.Duration(flags.ReconnectInterval) * time.Second)
|
||||
}
|
||||
|
||||
if retry > flags.MaxRetries {
|
||||
log.Println("Max retries reached.")
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
data := monitoring.GenerateReport()
|
||||
err = conn.WriteMessage(websocket.TextMessage, data)
|
||||
if err != nil {
|
||||
log.Println("Failed to send WebSocket message:", err)
|
||||
conn.Close()
|
||||
conn = nil // Mark connection as dead
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func connectWebSocket(websocketEndpoint string) (*websocket.Conn, error) {
|
||||
dialer := &websocket.Dialer{
|
||||
HandshakeTimeout: 5 * time.Second,
|
||||
}
|
||||
conn, resp, err := dialer.Dial(websocketEndpoint, nil)
|
||||
if err != nil {
|
||||
if resp != nil && resp.StatusCode != 101 {
|
||||
return nil, fmt.Errorf("%s", resp.Status)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return conn, nil
|
||||
}
|
||||
|
||||
func handleWebSocketMessages(conn *websocket.Conn, done chan<- struct{}) {
|
||||
|
||||
defer close(done)
|
||||
for {
|
||||
_, message_raw, err := conn.ReadMessage()
|
||||
if err != nil {
|
||||
log.Println("WebSocket read error:", err)
|
||||
return
|
||||
}
|
||||
var message struct {
|
||||
Message string `json:"message"`
|
||||
ID string `json:"request_id"`
|
||||
}
|
||||
err = json.Unmarshal(message_raw, &message)
|
||||
if err != nil {
|
||||
log.Println("Bad ws message:", err)
|
||||
continue
|
||||
}
|
||||
|
||||
if message.Message == "terminal" || message.ID != "" {
|
||||
go establishTerminalConnection(flags.Token, message.ID, flags.Endpoint)
|
||||
continue
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// connectWebSocket attempts to establish a WebSocket connection and upload basic info
|
||||
|
||||
// establishTerminalConnection 建立终端连接并使用terminal包处理终端操作
|
||||
func establishTerminalConnection(token, id, endpoint string) {
|
||||
endpoint = strings.TrimSuffix(endpoint, "/") + "/api/clients/terminal?token=" + token + "&id=" + id
|
||||
endpoint = "ws" + strings.TrimPrefix(endpoint, "http")
|
||||
dialer := &websocket.Dialer{
|
||||
HandshakeTimeout: 5 * time.Second,
|
||||
}
|
||||
conn, _, err := dialer.Dial(endpoint, nil)
|
||||
if err != nil {
|
||||
log.Println("Failed to establish terminal connection:", err)
|
||||
return
|
||||
}
|
||||
|
||||
// 启动终端
|
||||
terminal.StartTerminal(conn)
|
||||
if conn != nil {
|
||||
conn.Close()
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user