mirror of
https://github.com/fankes/komari-agent.git
synced 2025-10-18 10:39:24 +08:00
- 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.
82 lines
1.7 KiB
Go
82 lines
1.7 KiB
Go
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
|
|
}
|