2020-11-26 03:02:43 -05:00
|
|
|
package config
|
|
|
|
|
|
|
|
|
|
import (
|
2024-05-21 05:58:29 -04:00
|
|
|
"github.com/creasty/defaults"
|
2024-05-22 05:47:44 -04:00
|
|
|
"github.com/icinga/icinga-go-library/database"
|
|
|
|
|
"github.com/icinga/icinga-go-library/logging"
|
|
|
|
|
"github.com/icinga/icinga-go-library/redis"
|
2024-05-21 05:57:33 -04:00
|
|
|
"github.com/icinga/icingadb/pkg/icingadb/history"
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
"time"
|
2020-11-26 03:02:43 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Config defines Icinga DB config.
|
|
|
|
|
type Config struct {
|
2024-05-21 05:56:26 -04:00
|
|
|
Database database.Config `yaml:"database"`
|
2024-05-21 05:58:08 -04:00
|
|
|
Redis redis.Config `yaml:"redis"`
|
2024-05-21 05:56:26 -04:00
|
|
|
Logging logging.Config `yaml:"logging"`
|
2024-05-21 05:57:33 -04:00
|
|
|
Retention RetentionConfig `yaml:"retention"`
|
2020-11-26 03:02:43 -05:00
|
|
|
}
|
|
|
|
|
|
2024-05-21 05:58:29 -04:00
|
|
|
func (c *Config) SetDefaults() {
|
|
|
|
|
// Since SetDefaults() is called after the default values of the struct's fields have been evaluated,
|
|
|
|
|
// setting the default port only works here because
|
|
|
|
|
// the embedded Redis config struct itself does not provide a default value.
|
|
|
|
|
if defaults.CanUpdate(c.Redis.Port) {
|
|
|
|
|
c.Redis.Port = 6380
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-25 10:57:11 -04:00
|
|
|
// Validate checks constraints in the supplied configuration and returns an error if they are violated.
|
|
|
|
|
func (c *Config) Validate() error {
|
|
|
|
|
if err := c.Database.Validate(); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if err := c.Redis.Validate(); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2021-09-29 03:16:33 -04:00
|
|
|
if err := c.Logging.Validate(); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2022-03-23 10:37:28 -04:00
|
|
|
if err := c.Retention.Validate(); err != nil {
|
2022-01-31 10:12:38 -05:00
|
|
|
return err
|
|
|
|
|
}
|
2021-09-29 03:16:33 -04:00
|
|
|
|
2021-08-25 10:57:11 -04:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-26 03:02:43 -05:00
|
|
|
// Flags defines CLI flags.
|
|
|
|
|
type Flags struct {
|
2021-08-05 07:07:59 -04:00
|
|
|
// Version decides whether to just print the version and exit.
|
|
|
|
|
Version bool `long:"version" description:"print version and exit"`
|
2020-11-26 03:02:43 -05:00
|
|
|
// Config is the path to the config file
|
2021-11-12 08:43:33 -05:00
|
|
|
Config string `short:"c" long:"config" description:"path to config file" required:"true" default:"/etc/icingadb/config.yml"`
|
2020-11-26 03:02:43 -05:00
|
|
|
}
|
2024-05-21 05:57:33 -04:00
|
|
|
|
|
|
|
|
// RetentionConfig defines configuration for history retention.
|
|
|
|
|
type RetentionConfig struct {
|
|
|
|
|
HistoryDays uint64 `yaml:"history-days"`
|
|
|
|
|
SlaDays uint64 `yaml:"sla-days"`
|
|
|
|
|
Interval time.Duration `yaml:"interval" default:"1h"`
|
|
|
|
|
Count uint64 `yaml:"count" default:"5000"`
|
|
|
|
|
Options history.RetentionOptions `yaml:"options"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Validate checks constraints in the supplied retention configuration and
|
|
|
|
|
// returns an error if they are violated.
|
|
|
|
|
func (r *RetentionConfig) Validate() error {
|
|
|
|
|
if r.Interval <= 0 {
|
|
|
|
|
return errors.New("retention interval must be positive")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if r.Count == 0 {
|
|
|
|
|
return errors.New("count must be greater than zero")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return r.Options.Validate()
|
|
|
|
|
}
|