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
|
||||
}
|
Reference in New Issue
Block a user