aboutsummaryrefslogtreecommitdiff
path: root/spool
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--spool.go70
-rw-r--r--spool/load.go40
-rw-r--r--spool/save.go28
-rw-r--r--spool/spool.go26
4 files changed, 94 insertions, 70 deletions
diff --git a/spool.go b/spool.go
deleted file mode 100644
index 071a469..0000000
--- a/spool.go
+++ /dev/null
@@ -1,70 +0,0 @@
-package main
-
-import (
- "encoding/csv"
- "fmt"
- "log"
- "os"
- "path"
- "strconv"
-)
-
-// jobs is a map that matches a job name string to its last successfull run timestamp
-type jobs map[string]uint64
-
-func loadSpool() (entries jobs, err error) {
- var (
- file *os.File
- lines [][]string
- )
- // We read the spool
- file, err = os.Open(path.Join(workDir, spoolFile))
- if err != nil {
- return nil, fmt.Errorf("Couldn't open spool file, starting from scratch: %s", err)
- }
- defer file.Close()
- lines, err = csv.NewReader(file).ReadAll()
- if err != nil {
- return nil, fmt.Errorf("Corrupted spool file, starting from scratch : %s", err)
- }
- if verbose {
- log.Printf("Spool file content : %v\n", lines)
- }
-
- entries = make(map[string]uint64)
- for _, line := range lines {
- var i int
- i, err = strconv.Atoi(line[1])
- if err != nil {
- return nil, fmt.Errorf("Corrupted spool file : couldn't parse timestamp entry")
- }
- entries[line[0]] = uint64(i)
- }
- return
-}
-
-func saveSpool(entries jobs) (err error) {
- var (
- file *os.File
- lines [][]string
- jobName string
- ts uint64
- i int
- )
- file, err = os.Create(path.Join(workDir, spoolFile))
- if err != nil {
- return
- }
- defer file.Close()
-
- lines = make([][]string, len(entries))
- i = 0
- for jobName, ts = range entries {
- lines[i] = make([]string, 2)
- lines[i][0] = jobName
- lines[i][1] = fmt.Sprintf("%d", ts)
- i++
- }
- err = csv.NewWriter(file).WriteAll(lines)
- return
-}
diff --git a/spool/load.go b/spool/load.go
new file mode 100644
index 0000000..282fdc6
--- /dev/null
+++ b/spool/load.go
@@ -0,0 +1,40 @@
+package spool
+
+import (
+ "bareos-zabbix-check/config"
+ "bareos-zabbix-check/job"
+ "encoding/csv"
+ "fmt"
+ "log"
+ "os"
+ "path"
+ "strconv"
+)
+
+// Load loads a spool file in path
+func (s *Spool) Load(c *config.Config) (err error) {
+ s.config = c
+ // We read the spool
+ file, err := os.Open(path.Join(c.WorkDir(), spoolFile))
+ if err != nil {
+ return fmt.Errorf("Couldn't open spool file, starting from scratch: %s", err)
+ }
+ defer file.Close()
+ lines, err := csv.NewReader(file).ReadAll()
+ if err != nil {
+ return fmt.Errorf("Corrupted spool file, starting from scratch : %s", err)
+ }
+ if c.Verbose() {
+ log.Printf("Spool file content : %v\n", lines)
+ }
+
+ for _, line := range lines {
+ var i int
+ i, err = strconv.Atoi(line[1])
+ if err != nil {
+ return fmt.Errorf("Corrupted spool file : couldn't parse timestamp entry")
+ }
+ s.jobs = append(s.jobs, job.Job{Name: line[0], Timestamp: uint64(i), Success: true})
+ }
+ return
+}
diff --git a/spool/save.go b/spool/save.go
new file mode 100644
index 0000000..b01dc7b
--- /dev/null
+++ b/spool/save.go
@@ -0,0 +1,28 @@
+package spool
+
+import (
+ "encoding/csv"
+ "fmt"
+ "os"
+ "path"
+)
+
+// Save writes a spool on the disk
+func (s *Spool) Save() (err error) {
+ file, err := os.Create(path.Join(s.config.WorkDir(), spoolFile))
+ if err != nil {
+ return
+ }
+ defer file.Close()
+
+ lines := make([][]string, len(s.jobs))
+ var i int = 0
+ for _, job := range s.jobs {
+ lines[i] = make([]string, 2)
+ lines[i][0] = job.Name
+ lines[i][1] = fmt.Sprintf("%d", job.Timestamp)
+ i++
+ }
+ err = csv.NewWriter(file).WriteAll(lines)
+ return
+}
diff --git a/spool/spool.go b/spool/spool.go
new file mode 100644
index 0000000..e095979
--- /dev/null
+++ b/spool/spool.go
@@ -0,0 +1,26 @@
+package spool
+
+import (
+ "bareos-zabbix-check/config"
+ "bareos-zabbix-check/job"
+)
+
+const (
+ spoolFile = "bareos-zabbix-check.spool"
+)
+
+// Spool is an object for manipulating a bareos spool file
+type Spool struct {
+ config *config.Config
+ jobs []job.Job
+}
+
+// Jobs exports a spool to a jobs list
+func (s *Spool) Jobs() []job.Job {
+ return s.jobs
+}
+
+// SetJobs sets a jobs list
+func (s *Spool) SetJobs(jobs []job.Job) {
+ s.jobs = jobs
+}