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:
12
cmd/flags/flag.go
Normal file
12
cmd/flags/flag.go
Normal file
@@ -0,0 +1,12 @@
|
||||
package flags
|
||||
|
||||
var (
|
||||
DisableAutoUpdate bool
|
||||
DisableWebSsh bool
|
||||
Token string
|
||||
Endpoint string
|
||||
Interval float64
|
||||
IgnoreUnsafeCert bool
|
||||
MaxRetries int
|
||||
ReconnectInterval int
|
||||
)
|
68
cmd/root.go
Normal file
68
cmd/root.go
Normal file
@@ -0,0 +1,68 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"github.com/komari-monitor/komari-agent/cmd/flags"
|
||||
"github.com/komari-monitor/komari-agent/server"
|
||||
"github.com/komari-monitor/komari-agent/update"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var RootCmd = &cobra.Command{
|
||||
Use: "komari-agent",
|
||||
Short: "komari agent",
|
||||
Long: `komari agent`,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
log.Println("Komari Agent", update.CurrentVersion)
|
||||
log.Println("Github Repo:", update.Repo)
|
||||
|
||||
// 忽略不安全的证书
|
||||
if flags.IgnoreUnsafeCert {
|
||||
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
|
||||
}
|
||||
// 自动更新
|
||||
if !flags.DisableAutoUpdate {
|
||||
err := update.CheckAndUpdate()
|
||||
if err != nil {
|
||||
log.Println("[ERROR]", err)
|
||||
}
|
||||
go update.DoUpdateWorks()
|
||||
}
|
||||
go server.DoUploadBasicInfoWorks()
|
||||
server.EstablishWebSocketConnection()
|
||||
os.Exit(0)
|
||||
},
|
||||
}
|
||||
|
||||
func Execute() {
|
||||
for i, arg := range os.Args {
|
||||
if arg == "-autoUpdate" || arg == "--autoUpdate" {
|
||||
log.Println("WARNING: The -autoUpdate flag is deprecated in version 0.0.9 and later. Use --disable-auto-update to configure auto-update behavior.")
|
||||
// 从参数列表中移除该参数,防止cobra解析错误
|
||||
os.Args = append(os.Args[:i], os.Args[i+1:]...)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if err := RootCmd.Execute(); err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
}
|
||||
|
||||
func init() {
|
||||
RootCmd.PersistentFlags().StringVarP(&flags.Token, "token", "t", "", "API token")
|
||||
RootCmd.MarkPersistentFlagRequired("token")
|
||||
RootCmd.PersistentFlags().StringVarP(&flags.Endpoint, "endpoint", "e", "", "API endpoint")
|
||||
RootCmd.MarkPersistentFlagRequired("endpoint")
|
||||
RootCmd.PersistentFlags().BoolVarP(&flags.DisableAutoUpdate, "disable-auto-update", "d", false, "Disable automatic updates")
|
||||
RootCmd.PersistentFlags().BoolVarP(&flags.DisableWebSsh, "disable-web-ssh", "w", false, "Disable web SSH")
|
||||
RootCmd.PersistentFlags().Float64VarP(&flags.Interval, "interval", "i", 1.0, "Interval in seconds")
|
||||
RootCmd.PersistentFlags().BoolVarP(&flags.IgnoreUnsafeCert, "ignore-unsafe-cert", "u", false, "Ignore unsafe certificate errors")
|
||||
RootCmd.PersistentFlags().IntVarP(&flags.MaxRetries, "max-retries", "r", 3, "Maximum number of retries")
|
||||
RootCmd.PersistentFlags().IntVarP(&flags.ReconnectInterval, "reconnect-interval", "c", 5, "Reconnect interval in seconds")
|
||||
RootCmd.PersistentFlags().ParseErrorsWhitelist.UnknownFlags = true
|
||||
}
|
Reference in New Issue
Block a user