aboutsummaryrefslogtreecommitdiff
path: root/main.go
blob: 3e206a0531714daef35325d52cc59752985efc29 (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
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
package main

import (
	"bareos-zabbix-check/config"
	"bareos-zabbix-check/job"
	"bareos-zabbix-check/spool"
	"bareos-zabbix-check/state"
	"fmt"
	"os"
	"time"
)

func main() {
	var (
		config        config.Config
		spool         spool.Spool
		state         state.State
		errorString   string
		missingString string
	)
	config.Init()
	err := state.Parse(&config)
	if err != nil {
		fmt.Print(err)
		os.Exit(0)
	}
	// 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
	err = spool.Load(&config)

	jobs := job.KeepOldestOnly(append(state.Jobs(), spool.Jobs()...))
	spool.SetJobs(job.KeepSuccessOnly(jobs))

	// we write this new spool
	if err2 := spool.Save(); err2 != nil {
		fmt.Printf("AVERAGE: Error saving the spool file : %s\n", err2)
		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 err != nil {
			fmt.Printf(" additionnal errors: %s", err)
		}
	} else {
		fmt.Printf("OK")
	}
}