Files
komari-agent/cmd/root.go
2025-07-20 07:58:13 +00:00

77 lines
3.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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()
for {
server.UpdateBasicInfo()
server.EstablishWebSocketConnection()
}
},
}
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().BoolVar(&flags.DisableAutoUpdate, "disable-auto-update", false, "Disable automatic updates")
RootCmd.PersistentFlags().BoolVar(&flags.DisableWebSsh, "disable-web-ssh", false, "Disable remote control(web ssh and rce)")
RootCmd.PersistentFlags().BoolVar(&flags.MemoryModeAvailable, "memory-mode-available", false, "Report memory as available instead of used.")
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().IntVar(&flags.InfoReportInterval, "info-report-interval", 5, "Interval in minutes for reporting basic info")
RootCmd.PersistentFlags().StringVar(&flags.IncludeNics, "include-nics", "", "Comma-separated list of network interfaces to include")
RootCmd.PersistentFlags().StringVar(&flags.ExcludeNics, "exclude-nics", "", "Comma-separated list of network interfaces to exclude")
RootCmd.PersistentFlags().StringVar(&flags.IncludeMountpoints, "include-mountpoint", "", "Semicolon-separated list of mount points to include for disk statistics")
RootCmd.PersistentFlags().IntVar(&flags.MonthRotate, "month-rotate", 0, "Month reset for network statistics (0 to disable)")
RootCmd.PersistentFlags().ParseErrorsWhitelist.UnknownFlags = true
}