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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
package main
import (
"bareos-zabbix-check/config"
"bareos-zabbix-check/job"
"bareos-zabbix-check/spool"
"bareos-zabbix-check/state"
"fmt"
"log"
"os"
"path/filepath"
"time"
)
const (
spoolFileName = "bareos-zabbix-check.spool"
)
func main() {
var (
config config.Config
errorString string
missingString string
)
config.Init()
// Open the state file
stateFile, err := os.Open(config.StateFile())
if err != nil {
fmt.Printf("INFO Couldn't open state file : %s", err)
os.Exit(0)
}
defer stateFile.Close()
// parse the state file
header, err := state.ParseHeader(stateFile)
if err != nil {
fmt.Printf("INFO Could not parse state file header : %s", err)
os.Exit(0)
}
if config.Verbose() {
log.Printf("Parsed header: %+s\n", header)
}
// seek to the job entries in the state file
offset, err := stateFile.Seek(int64(header.LastJobsAddr), 0)
if err != nil {
fmt.Printf("INFO Couldn't seek to jobs position in state file : %s", err)
}
if uint64(offset) != header.LastJobsAddr {
fmt.Print("INFO Truncated state file")
}
// Then parse the jobs in the state file
jobs, err := state.ParseJobs(stateFile)
if err != nil {
fmt.Printf("INFO Could not parse jobs in state file : %s", err)
}
if config.Verbose() {
log.Printf("%d jobs found in state file\n", len(jobs))
for i := 0; i < len(jobs); i++ {
log.Print(jobs[i])
}
}
// 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(config.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(config.WorkDir(), spoolFileName))
if err == nil {
defer spoolFile.Close()
err = spool.Serialize(spoolFile, jobs)
}
if err != nil {
fmt.Printf("AVERAGE: Error saving the spool file : %s\n", err)
os.Exit(0)
}
now := uint64(time.Now().Unix())
// We build the error strings
for _, job := range jobs {
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 != "" {
fmt.Printf("AVERAGE: %s %s", errorString, missingString)
if spoolErr != nil {
fmt.Printf(" additionnal errors: %s", spoolErr)
}
} else {
fmt.Printf("OK")
}
}
|