parent
19a2cac355
commit
cd31bdece1
3 changed files with 48 additions and 3 deletions
|
|
@ -29,7 +29,9 @@ func (m *multiValue) Set(value string) error {
|
||||||
func main() {
|
func main() {
|
||||||
cfg := loadConfig()
|
cfg := loadConfig()
|
||||||
|
|
||||||
logger := zerolog.New(os.Stdout).With().Timestamp().Logger()
|
zerolog.SetGlobalLevel(cfg.LogLevel)
|
||||||
|
|
||||||
|
logger := zerolog.New(os.Stdout).Level(cfg.LogLevel).With().Timestamp().Logger()
|
||||||
cfg.Logger = &logger
|
cfg.Logger = &logger
|
||||||
|
|
||||||
// Check if we should run as a Windows service
|
// Check if we should run as a Windows service
|
||||||
|
|
@ -69,6 +71,7 @@ func loadConfig() hostagent.Config {
|
||||||
envInsecure := utils.GetenvTrim("PULSE_INSECURE_SKIP_VERIFY")
|
envInsecure := utils.GetenvTrim("PULSE_INSECURE_SKIP_VERIFY")
|
||||||
envTags := utils.GetenvTrim("PULSE_TAGS")
|
envTags := utils.GetenvTrim("PULSE_TAGS")
|
||||||
envRunOnce := utils.GetenvTrim("PULSE_ONCE")
|
envRunOnce := utils.GetenvTrim("PULSE_ONCE")
|
||||||
|
envLogLevel := utils.GetenvTrim("LOG_LEVEL")
|
||||||
|
|
||||||
defaultInterval := 30 * time.Second
|
defaultInterval := 30 * time.Second
|
||||||
if envInterval != "" {
|
if envInterval != "" {
|
||||||
|
|
@ -85,6 +88,7 @@ func loadConfig() hostagent.Config {
|
||||||
insecureFlag := flag.Bool("insecure", utils.ParseBool(envInsecure), "Skip TLS certificate verification")
|
insecureFlag := flag.Bool("insecure", utils.ParseBool(envInsecure), "Skip TLS certificate verification")
|
||||||
runOnceFlag := flag.Bool("once", utils.ParseBool(envRunOnce), "Collect and send a single report, then exit")
|
runOnceFlag := flag.Bool("once", utils.ParseBool(envRunOnce), "Collect and send a single report, then exit")
|
||||||
showVersion := flag.Bool("version", false, "Print the agent version and exit")
|
showVersion := flag.Bool("version", false, "Print the agent version and exit")
|
||||||
|
logLevelFlag := flag.String("log-level", defaultLogLevel(envLogLevel), "Log level: debug, info, warn, error")
|
||||||
|
|
||||||
var tagFlags multiValue
|
var tagFlags multiValue
|
||||||
flag.Var(&tagFlags, "tag", "Tag to apply to this host (repeatable)")
|
flag.Var(&tagFlags, "tag", "Tag to apply to this host (repeatable)")
|
||||||
|
|
@ -112,6 +116,12 @@ func loadConfig() hostagent.Config {
|
||||||
interval = 30 * time.Second
|
interval = 30 * time.Second
|
||||||
}
|
}
|
||||||
|
|
||||||
|
logLevel, err := parseLogLevel(*logLevelFlag)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintf(os.Stderr, "error: %v\n", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
tags := gatherTags(envTags, tagFlags)
|
tags := gatherTags(envTags, tagFlags)
|
||||||
|
|
||||||
return hostagent.Config{
|
return hostagent.Config{
|
||||||
|
|
@ -123,6 +133,7 @@ func loadConfig() hostagent.Config {
|
||||||
Tags: tags,
|
Tags: tags,
|
||||||
InsecureSkipVerify: *insecureFlag,
|
InsecureSkipVerify: *insecureFlag,
|
||||||
RunOnce: *runOnceFlag,
|
RunOnce: *runOnceFlag,
|
||||||
|
LogLevel: logLevel,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -144,3 +155,27 @@ func gatherTags(env string, flags []string) []string {
|
||||||
}
|
}
|
||||||
return tags
|
return tags
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func parseLogLevel(value string) (zerolog.Level, error) {
|
||||||
|
normalized := strings.ToLower(strings.TrimSpace(value))
|
||||||
|
if normalized == "" {
|
||||||
|
return zerolog.InfoLevel, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
level, err := zerolog.ParseLevel(normalized)
|
||||||
|
if err != nil {
|
||||||
|
return zerolog.InfoLevel, fmt.Errorf("invalid log level %q: must be debug, info, warn, or error", value)
|
||||||
|
}
|
||||||
|
if level < zerolog.DebugLevel || level > zerolog.ErrorLevel {
|
||||||
|
return zerolog.InfoLevel, fmt.Errorf("invalid log level %q: must be debug, info, warn, or error", value)
|
||||||
|
}
|
||||||
|
|
||||||
|
return level, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func defaultLogLevel(envValue string) string {
|
||||||
|
if strings.TrimSpace(envValue) == "" {
|
||||||
|
return "info"
|
||||||
|
}
|
||||||
|
return envValue
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -204,6 +204,7 @@ Start-Service -Name PulseHostAgent
|
||||||
| `--url` | Pulse base URL (defaults to `http://localhost:7655`) |
|
| `--url` | Pulse base URL (defaults to `http://localhost:7655`) |
|
||||||
| `--token` | API token with the `host-agent:report` scope |
|
| `--token` | API token with the `host-agent:report` scope |
|
||||||
| `--interval` | Polling interval (`30s` default) |
|
| `--interval` | Polling interval (`30s` default) |
|
||||||
|
| `--log-level` | Log verbosity (`debug`, `info`, `warn`, `error`; defaults to `info`). Also respects `LOG_LEVEL`. Use `warn` on noisy journald setups. |
|
||||||
| `--hostname` | Override reported hostname |
|
| `--hostname` | Override reported hostname |
|
||||||
| `--agent-id` | Override agent identifier (used as dedupe key) |
|
| `--agent-id` | Override agent identifier (used as dedupe key) |
|
||||||
| `--tag` | Optional tag(s) to annotate the host (repeatable) |
|
| `--tag` | Optional tag(s) to annotate the host (repeatable) |
|
||||||
|
|
|
||||||
|
|
@ -30,6 +30,7 @@ type Config struct {
|
||||||
Tags []string
|
Tags []string
|
||||||
InsecureSkipVerify bool
|
InsecureSkipVerify bool
|
||||||
RunOnce bool
|
RunOnce bool
|
||||||
|
LogLevel zerolog.Level
|
||||||
Logger *zerolog.Logger
|
Logger *zerolog.Logger
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -61,12 +62,20 @@ func New(cfg Config) (*Agent, error) {
|
||||||
cfg.Interval = defaultInterval
|
cfg.Interval = defaultInterval
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if zerolog.GlobalLevel() == zerolog.DebugLevel && cfg.LogLevel != zerolog.DebugLevel {
|
||||||
|
zerolog.SetGlobalLevel(cfg.LogLevel)
|
||||||
|
}
|
||||||
|
|
||||||
if cfg.Logger == nil {
|
if cfg.Logger == nil {
|
||||||
defaultLogger := zerolog.New(zerolog.NewConsoleWriter()).With().Timestamp().Logger()
|
defaultLogger := zerolog.New(zerolog.NewConsoleWriter()).
|
||||||
|
Level(cfg.LogLevel).
|
||||||
|
With().
|
||||||
|
Timestamp().
|
||||||
|
Logger()
|
||||||
cfg.Logger = &defaultLogger
|
cfg.Logger = &defaultLogger
|
||||||
}
|
}
|
||||||
|
|
||||||
logger := cfg.Logger.With().Str("component", "host-agent").Logger()
|
logger := cfg.Logger.Level(cfg.LogLevel).With().Str("component", "host-agent").Logger()
|
||||||
|
|
||||||
if strings.TrimSpace(cfg.APIToken) == "" {
|
if strings.TrimSpace(cfg.APIToken) == "" {
|
||||||
return nil, fmt.Errorf("api token is required")
|
return nil, fmt.Errorf("api token is required")
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue