diff options
Diffstat (limited to 'zabbix')
-rw-r--r-- | zabbix/flags.go | 21 | ||||
-rw-r--r-- | zabbix/main.go | 104 | ||||
-rw-r--r-- | zabbix/main_test.go | 61 | ||||
-rw-r--r-- | zabbix/statefile.go | 34 | ||||
-rw-r--r-- | zabbix/testdata/bareos-fd-17.2.state | bin | 2196 -> 0 bytes | |||
-rw-r--r-- | zabbix/testdata/bareos-fd-18.2.state | bin | 2196 -> 0 bytes | |||
-rw-r--r-- | zabbix/testdata/bareos-fd-18.2.state-with-error | bin | 2196 -> 0 bytes | |||
-rw-r--r-- | zabbix/workdir.go | 38 |
8 files changed, 0 insertions, 258 deletions
diff --git a/zabbix/flags.go b/zabbix/flags.go deleted file mode 100644 index 3e95321..0000000 --- a/zabbix/flags.go +++ /dev/null @@ -1,21 +0,0 @@ -package zabbix - -import ( - "flag" -) - -var ( - stateFileName string - workDir string -) - -func processFlags() (err error) { - flag.StringVar(&stateFileName, "f", "", "Force the state file to use, defaults to "+bareosStateFile+" if it exists else "+baculaStateFile+".") - flag.StringVar(&workDir, "w", "", "Force the work directory to use, defaults to "+bareosWorkDir+" if it exists else "+baculaWorkDir+".") - flag.Parse() - err = checkWorkDir() - if err == nil { - err = checkStateFile() - } - return -} diff --git a/zabbix/main.go b/zabbix/main.go deleted file mode 100644 index 576fc17..0000000 --- a/zabbix/main.go +++ /dev/null @@ -1,104 +0,0 @@ -package zabbix - -import ( - "bareos-zabbix-check/job" - "bareos-zabbix-check/spool" - "bareos-zabbix-check/state" - "fmt" - "os" - "path/filepath" - "time" -) - -const ( - spoolFileName = "bareos-zabbix-check.spool" -) - -var now = uint64(time.Now().Unix()) - -// Main the true main function of this program -func Main() string { - err := processFlags() - if err != nil { - return fmt.Sprintf("INFO Failed to init programm : %s", err) - } - // Open the state file - stateFile, err := os.Open(stateFileName) - if err != nil { - return fmt.Sprintf("INFO Could not open state file : %s", err) - } - defer stateFile.Close() - // parse the state file - header, err := state.ParseHeader(stateFile) - if err != nil { - return fmt.Sprintf("INFO Could not parse state file header : %s", err) - } - // seek to the job entries in the state file - offset, err := stateFile.Seek(int64(header.LastJobsAddr), 0) - if err != nil { - return fmt.Sprintf("INFO Couldn't seek to jobs position in state file : %s", err) - } - if uint64(offset) != header.LastJobsAddr { - return fmt.Sprint("INFO Truncated state file") - } - // Then parse the jobs in the state file - jobs, err := state.ParseJobs(stateFile) - if err != nil { - return fmt.Sprintf("INFO Could not parse jobs in state file : %s", err) - } - - // We will check for errors in loading the spool file only at the end. If all jobs ran successfully without errors - // in the state file and we manage to write a new spool file without errors, then we will ignore any error here to - // avoid false positives during backup bootstrap - // Open the spool file - spoolFile, spoolErr := os.Open(filepath.Join(workDir, spoolFileName)) - var spoolJobs []job.Job - if err == nil { - defer spoolFile.Close() - spoolJobs, spoolErr = spool.Parse(spoolFile) - } - - jobs = job.KeepOldestOnly(append(jobs, spoolJobs...)) - - // we write this new spool - spoolFile, err = os.Create(filepath.Join(workDir, spoolFileName)) - if err == nil { - defer spoolFile.Close() - err = spool.Serialize(spoolFile, jobs) - } - if err != nil { - return fmt.Sprintf("AVERAGE: Error saving the spool file : %s\n", err) - } - - var ( - errorString string - missingString string - ) - // We build the error strings - for i := 0; i < len(jobs); i++ { - job := jobs[i] - if job.Success { - if job.Timestamp < now-24*3600 { - if missingString == "" { - missingString = fmt.Sprintf("missing: %s", job.Name) - } else { - missingString = fmt.Sprintf("%s, %s", missingString, job.Name) - } - } - } else { - if errorString == "" { - errorString = fmt.Sprintf("errors: %s", job.Name) - } else { - errorString = fmt.Sprintf("%s, %s", errorString, job.Name) - } - } - } - // Finally we output - if errorString != "" || missingString != "" { - if spoolErr != nil { - return fmt.Sprintf("AVERAGE: %s %s %s", errorString, missingString, spoolErr) - } - return fmt.Sprintf("AVERAGE: %s %s", errorString, missingString) - } - return "OK" -} diff --git a/zabbix/main_test.go b/zabbix/main_test.go deleted file mode 100644 index ca722f6..0000000 --- a/zabbix/main_test.go +++ /dev/null @@ -1,61 +0,0 @@ -package zabbix - -import ( - "flag" - "fmt" - "os" - "path/filepath" - "testing" -) - -func TestMain(t *testing.T) { - os.RemoveAll("tmp") - cwd, _ := os.Getwd() - err := os.MkdirAll("tmp/ok-18.2", 0777) - if err != nil { - t.Skipf("skipping main tests because tmp directory cannot be created : %s", err) - } - wd, err := os.Getwd() - if err != nil { - t.Skipf("skipping main tests because cannot get working directory : %s", err) - } - os.MkdirAll("tmp/ok-17.2", 0777) - os.MkdirAll("tmp/no_state_file", 0777) - os.MkdirAll("tmp/bacula_auto_detect_failed/var/lib/bacula", 0777) - os.MkdirAll("tmp/bareos_auto_detect_failed/var/lib/bareos", 0777) - os.MkdirAll("tmp/error", 0777) - os.Symlink("../../testdata/bareos-fd-17.2.state", "tmp/ok-17.2/state") - os.Symlink("../../testdata/bareos-fd-18.2.state", "tmp/ok-18.2/state") - os.Symlink("../../testdata/bareos-fd-18.2.state-with-error", "tmp/error/state") - tests := []struct { - name string - timestamp uint64 - rootDir string - args []string - want string - }{ - {"failed bacula_auto_detect", 0, "tmp/bacula_auto_detect_failed", []string{}, "INFO Failed to init programm : Could not find a suitable state file. Has a job ever run?"}, - {"failed bareos_auto_detect", 0, "tmp/bareos_auto_detect_failed", []string{}, "INFO Failed to init programm : Could not find a suitable state file. Has a job ever run?"}, - {"failed auto_detect", 0, "tmp/non_existent", []string{}, "INFO Failed to init programm : Could not find a suitable work directory. Is bareos or bacula installed?"}, - {"no work directory", 0, "tmp", []string{"-w", "/non_existent"}, fmt.Sprintf("INFO Failed to init programm : Invalid work directory %s/zabbix/tmp/non_existent : it does not exist or is not a directory", wd)}, - {"no state file auto_detect", 0, "tmp", []string{"-w", "/no_state_file"}, "INFO Failed to init programm : Could not find a suitable state file. Has a job ever run?"}, - {"no state file", 0, "tmp", []string{"-w", "/no_state_file", "-f", "test"}, fmt.Sprintf("INFO Failed to init programm : The state file %s/zabbix/tmp/no_state_file/test does not exist", wd}, - {"ok bareos 18.2", 1582579731, "tmp/ok-18.2", []string{"-w", "/", "-f", "state"}, "OK"}, - {"ok bareos 17.2", 1582579731, "tmp/ok-17.2", []string{"-w", "/", "-f", "state"}, "OK"}, - {"missing", 1582709331, "tmp/ok-18.2", []string{"-w", "/", "-f", "state"}, "AVERAGE: missing: awhphpipam1_percona_xtrabackup, awhphpipam1_LinuxAll, awhphpipam1_www"}, - {"error", 1582579731, "tmp/error", []string{"-w", "/", "-f", "state"}, "AVERAGE: errors: awhphpipam1_percona_xtrabackup, awhphpipam1_www Corrupted spool file: invalid argument"}, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - now = tt.timestamp - root = filepath.Join(cwd, tt.rootDir) - flag.CommandLine = flag.NewFlagSet("bareos-zabbix-check", flag.ExitOnError) //flags are now reset - os.Args = append([]string{"bareos-zabbix-check"}, tt.args...) - if got := Main(); got != tt.want { - t.Log(workDir) - t.Errorf("Main() = %v, want %v", got, tt.want) - } - }) - } - os.RemoveAll("tmp") -} diff --git a/zabbix/statefile.go b/zabbix/statefile.go deleted file mode 100644 index 26ea650..0000000 --- a/zabbix/statefile.go +++ /dev/null @@ -1,34 +0,0 @@ -package zabbix - -import ( - "fmt" - "os" - "path/filepath" -) - -const ( - bareosStateFile = "bareos-fd.9102.state" - baculaStateFile = "bacula-fd.9102.state" -) - -func checkStateFile() error { - // Finds the state file to parse - if stateFileName != "" { - stateFileName = filepath.Join(workDir, stateFileName) - info, err := os.Stat(stateFileName) - if os.IsNotExist(err) || info.IsDir() { - return fmt.Errorf("The state file %s does not exist", stateFileName) - } - } else { - stateFileName = filepath.Join(workDir, bareosStateFile) - info, err := os.Stat(stateFileName) - if os.IsNotExist(err) || info.IsDir() { - stateFileName = filepath.Join(workDir, baculaStateFile) - info, err = os.Stat(stateFileName) - if os.IsNotExist(err) || info.IsDir() { - return fmt.Errorf("Could not find a suitable state file. Has a job ever run?") - } - } - } - return nil -} diff --git a/zabbix/testdata/bareos-fd-17.2.state b/zabbix/testdata/bareos-fd-17.2.state Binary files differdeleted file mode 100644 index e98786c..0000000 --- a/zabbix/testdata/bareos-fd-17.2.state +++ /dev/null diff --git a/zabbix/testdata/bareos-fd-18.2.state b/zabbix/testdata/bareos-fd-18.2.state Binary files differdeleted file mode 100644 index ca1f30c..0000000 --- a/zabbix/testdata/bareos-fd-18.2.state +++ /dev/null diff --git a/zabbix/testdata/bareos-fd-18.2.state-with-error b/zabbix/testdata/bareos-fd-18.2.state-with-error Binary files differdeleted file mode 100644 index 3d28356..0000000 --- a/zabbix/testdata/bareos-fd-18.2.state-with-error +++ /dev/null diff --git a/zabbix/workdir.go b/zabbix/workdir.go deleted file mode 100644 index 287c80a..0000000 --- a/zabbix/workdir.go +++ /dev/null @@ -1,38 +0,0 @@ -package zabbix - -import ( - "fmt" - "os" - "path/filepath" -) - -const ( - bareosWorkDir = "/var/lib/bareos" - baculaWorkDir = "/var/lib/bacula" -) - -var root = "/" - -// checkWorkDir checks if a work directory is valid -func checkWorkDir() error { - // Determine the work directory to use. - if workDir != "" { - workDir = filepath.Join(root, workDir) - info, err := os.Stat(workDir) - if os.IsNotExist(err) || !info.IsDir() { - return fmt.Errorf("Invalid work directory %s : it does not exist or is not a directory", workDir) - } - } else { - workDir = filepath.Join(root, bareosWorkDir) - info, err := os.Stat(workDir) - if os.IsNotExist(err) || !info.IsDir() { - workDir = filepath.Join(root, baculaWorkDir) - info, err := os.Stat(workDir) - if os.IsNotExist(err) || !info.IsDir() { - return fmt.Errorf("Could not find a suitable work directory. Is bareos or bacula installed?") - } - } - } - workDir = filepath.Clean(workDir) - return nil -} |