aboutsummaryrefslogtreecommitdiff
path: root/spool/load.go
blob: 282fdc6039f779ea0be2c6349f299f47e5efd5db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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
}