diff --git a/core/poller/build_bsd.go b/core/poller/build_bsd.go index 5605ef61..0f714e8f 100644 --- a/core/poller/build_bsd.go +++ b/core/poller/build_bsd.go @@ -3,7 +3,7 @@ package poller // DefaultConfFile is where to find config if --config is not prvided. -const DefaultConfFile = "/usr/local/etc/unifi-poller/up.conf" +const DefaultConfFile = "/etc/unifi-poller/up.conf,/usr/local/etc/unifi-poller/up.conf" // DefaultObjPath is the path to look for shared object libraries (plugins). const DefaultObjPath = "/usr/local/lib/unifi-poller" diff --git a/core/poller/build_unix.go b/core/poller/build_unix.go index a50be6a5..e425e451 100644 --- a/core/poller/build_unix.go +++ b/core/poller/build_unix.go @@ -3,7 +3,7 @@ package poller // DefaultConfFile is where to find config if --config is not prvided. -const DefaultConfFile = "/etc/unifi-poller/up.conf" +const DefaultConfFile = "/config/unifi-poller.conf,/etc/unifi-poller/up.conf" // DefaultObjPath is the path to look for shared object libraries (plugins). const DefaultObjPath = "/usr/lib/unifi-poller" diff --git a/core/poller/config.go b/core/poller/config.go index 2474f8a6..eb9fc79e 100644 --- a/core/poller/config.go +++ b/core/poller/config.go @@ -108,6 +108,19 @@ func (u *UnifiPoller) ParseConfigs() error { return u.parseOutputs() } +// getFirstFile returns the first file that exists and is "reachable" +func getFirstFile(files []string) (string, error) { + var err error + + for _, f := range files { + if _, err = os.Stat(f); err == nil { + return f, nil + } + } + + return "", err +} + // parseInterface parses the config file and environment variables into the provided interface. func (u *UnifiPoller) parseInterface(i interface{}) error { // Parse config file into provided interface. diff --git a/core/poller/start.go b/core/poller/start.go index 34cba9da..515aa2dc 100644 --- a/core/poller/start.go +++ b/core/poller/start.go @@ -5,6 +5,7 @@ import ( "fmt" "log" "os" + "strings" "github.com/prometheus/common/version" "github.com/spf13/pflag" @@ -28,6 +29,12 @@ func (u *UnifiPoller) Start() error { return nil // don't run anything else w/ version request. } + cfile, err := getFirstFile(strings.Split(u.Flags.ConfigFile, ",")) + if err != nil { + return err + } + + u.Flags.ConfigFile = cfile if u.Flags.DumpJSON == "" { // do not print this when dumping JSON. u.Logf("Loading Configuration File: %s", u.Flags.ConfigFile) } @@ -50,7 +57,8 @@ func (f *Flags) Parse(args []string) { f.StringVarP(&f.DumpJSON, "dumpjson", "j", "", "This debug option prints a json payload and exits. See man page for more info.") - f.StringVarP(&f.ConfigFile, "config", "c", DefaultConfFile, "Poller config file path.") + f.StringVarP(&f.ConfigFile, "config", "c", DefaultConfFile, + "Poller config file path. Separating multiple paths with a comma will load the first config file found.") f.BoolVarP(&f.ShowVer, "version", "v", false, "Print the version and exit.") _ = f.FlagSet.Parse(args) // pflag.ExitOnError means this will never return error. }