aboutsummaryrefslogtreecommitdiff
path: root/state/parser.go
blob: 60f53947496c80d32ead0ce4b6d0cae650b67529 (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 state

import (
	"bareos-zabbix-check/config"
	"fmt"
	"os"
)

// Parse parses a bareos state file
func (s *State) Parse(c *config.Config) (err error) {
	s.config = c
	// Open the state file
	file, err := os.Open(c.StateFile())
	if err != nil {
		return fmt.Errorf("INFO Couldn't open state file : %s", err)
	}
	defer file.Close()

	err = s.parseHeader(file)
	if err != nil {
		return err
	}
	err = s.parseJobs(file)
	if err != nil {
		return err
	}

	return
}

// readNextBytes : Reads the next "number" bytes from a "file", returns the number of bytes actually read as well as the bytes read
func (s *State) readNextBytes(file *os.File, number int) (n int, bytes []byte, err error) {
	bytes = make([]byte, number)
	n, err = file.Read(bytes)
	if err != nil {
		return 0, nil, fmt.Errorf("file.Read failed in %s : %s", s.config.StateFile(), err)
	}

	return
}