diff options
author | Julien Dessaux | 2020-01-30 17:44:42 +0100 |
---|---|---|
committer | Julien Dessaux | 2020-01-30 17:44:42 +0100 |
commit | bea8e5aba8fc84dcb0c980c3948ed6d78719dded (patch) | |
tree | f84f2722b98f4c2996b64ce24ac322c9709617ed /config | |
parent | Document where C structures for state file header and job entry come from (diff) | |
download | bareos-zabbix-check-bea8e5aba8fc84dcb0c980c3948ed6d78719dded.tar.gz bareos-zabbix-check-bea8e5aba8fc84dcb0c980c3948ed6d78719dded.tar.bz2 bareos-zabbix-check-bea8e5aba8fc84dcb0c980c3948ed6d78719dded.zip |
Big rafactoring : code split in several modules and some other best practices
Diffstat (limited to 'config')
-rw-r--r-- | config/config.go | 21 | ||||
-rw-r--r-- | config/init.go | 16 | ||||
-rw-r--r-- | config/statefile.go | 39 | ||||
-rw-r--r-- | config/workdir.go | 40 |
4 files changed, 116 insertions, 0 deletions
diff --git a/config/config.go b/config/config.go new file mode 100644 index 0000000..262a5d2 --- /dev/null +++ b/config/config.go @@ -0,0 +1,21 @@ +package config + +// Config object +type Config struct { + verbose bool + quiet bool + stateFile string + workDir string +} + +// Verbose gets the verbose field of the configuration +func (config *Config) Verbose() bool { return config.verbose } + +// Quiet gets the quiet field of the configuration +func (config *Config) Quiet() bool { return config.quiet } + +// StateFile gets the stateFile field of the configuration +func (config *Config) StateFile() string { return config.stateFile } + +// WorkDir gets the workDir field of the configuration +func (config *Config) WorkDir() string { return config.workDir } diff --git a/config/init.go b/config/init.go new file mode 100644 index 0000000..f737fb3 --- /dev/null +++ b/config/init.go @@ -0,0 +1,16 @@ +package config + +import "flag" + +// Init initialises a program config from the command line flags +func (c *Config) Init() { + flag.BoolVar(&c.verbose, "v", false, "Activates verbose debugging output, defaults to false.") + flag.BoolVar(&c.quiet, "q", false, "Suppress all output, suitable to force a silent update of the spool file.") + flag.StringVar(&c.stateFile, "f", "", "Force the state file to use, defaults to "+bareosStateFile+" if it exists else "+baculaStateFile+".") + flag.StringVar(&c.workDir, "w", "", "Force the work directory to use, defaults to "+bareosWorkDir+" if it exists else "+baculaWorkDir+".") + + // command line arguments parsing + flag.Parse() + c.checkWorkDir() + c.checkStateFile() +} diff --git a/config/statefile.go b/config/statefile.go new file mode 100644 index 0000000..8b9f0a9 --- /dev/null +++ b/config/statefile.go @@ -0,0 +1,39 @@ +package config + +import ( + "fmt" + "log" + "os" + "path" +) + +const ( + bareosStateFile = "bareos-fd.9102.state" + baculaStateFile = "bacula-fd.9102.state" +) + +func (c *Config) checkStateFile() { + // Finds the state file to parse + if c.stateFile != "" { + c.stateFile = path.Join(c.workDir, c.stateFile) + info, err := os.Stat(c.stateFile) + if os.IsNotExist(err) || info.IsDir() { + fmt.Printf("INFO The state file %s does not exist.\n", c.stateFile) + os.Exit(0) + } + } else { + c.stateFile = path.Join(c.workDir, bareosStateFile) + info, err := os.Stat(c.stateFile) + if os.IsNotExist(err) || info.IsDir() { + c.stateFile = path.Join(c.workDir, baculaStateFile) + info, err = os.Stat(c.stateFile) + if os.IsNotExist(err) || info.IsDir() { + fmt.Println("INFO Could not find a suitable state file. Has a job ever run?") + os.Exit(0) + } + } + } + if c.verbose { + log.Println("Using state file ", c.stateFile) + } +} diff --git a/config/workdir.go b/config/workdir.go new file mode 100644 index 0000000..283fdc5 --- /dev/null +++ b/config/workdir.go @@ -0,0 +1,40 @@ +package config + +import ( + "fmt" + "log" + "os" + "path" +) + +const ( + bareosWorkDir = "/var/lib/bareos" + baculaWorkDir = "/var/lib/bacula" +) + +// checkWorkDir checks if a work directory is valid +func (c *Config) checkWorkDir() { + // Determine the work directory to use. + if c.workDir != "" { + info, err := os.Stat(c.workDir) + if os.IsNotExist(err) || !info.IsDir() { + fmt.Printf("INFO Invalid work directory %s : it does not exist or is not a directory.\n", c.workDir) + os.Exit(0) + } + } else { + c.workDir = bareosWorkDir + info, err := os.Stat(c.workDir) + if os.IsNotExist(err) || !info.IsDir() { + c.workDir = baculaWorkDir + info, err := os.Stat(c.workDir) + if os.IsNotExist(err) || !info.IsDir() { + fmt.Println("INFO Could not find a suitable work directory. Is bareos or bacula installed?") + os.Exit(0) + } + } + } + c.workDir = path.Clean(c.workDir) + if c.verbose { + log.Println("Setting work directory to ", c.workDir) + } +} |