This repository has been archived by the owner on Aug 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
idbvamp.go
142 lines (113 loc) · 2.69 KB
/
idbvamp.go
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package main
import (
"github.com/idb-project/idbclient"
"github.com/idb-project/idbclient/machine"
"github.com/idb-project/idbvamp/bacula"
"log"
"strings"
"time"
)
func main() {
c := newConfig()
err := c.load(*configFile)
if err != nil {
log.Fatal(err)
}
db, err := bacula.NewDB("mysql", c.Dsn)
if err != nil {
log.Fatal(err)
}
idb, err := idbclient.NewIdb(c.Url, c.ApiToken, c.InsecureSkipVerify)
if err != nil {
log.Fatal(err)
}
if c.Debug {
idb.Debug = true
}
cs := make(chan bacula.Client)
ms := make(chan machine.Machine)
// error channel for the goroutines
errs := make(chan error)
// log all errors
go logErrors(errs)
go clients(db, errs, cs)
go jobs(db, errs, ms, cs)
sendMachines(idb, errs, ms, c.Create)
}
// logErrors logs all errors received on a chan error.
func logErrors(errs chan error) {
for v := range errs {
log.Println(v)
}
}
// clients retrieves all clients from the bacula database and writes them to channel cs.
func clients(db *bacula.DB, errs chan error, cs chan bacula.Client) {
clients, err := db.Clients()
if err != nil {
errs <- err
close(cs)
}
for _, v := range clients {
cs <- v
}
close(cs)
}
// jobs reads clients from channel cs and retrieves their jobs from the database, writing machines filled
// with the job data to channel ms.
func jobs(db *bacula.DB, errs chan error, ms chan machine.Machine, cs chan bacula.Client) {
for c := range cs {
incJobs, err := db.LevelJobs("I", c)
if err != nil {
errs <- err
continue
}
diffJobs, err := db.LevelJobs("D", c)
if err != nil {
errs <- err
continue
}
fullJobs, err := db.LevelJobs("F", c)
if err != nil {
errs <- err
continue
}
var m machine.Machine
var timeFull time.Time
var timeInc time.Time
var timeDiff time.Time
if len(fullJobs) > 0 {
timeFull = fullJobs[len(fullJobs)-1].RealEndTime
}
if len(incJobs) > 0 {
timeInc = incJobs[len(incJobs)-1].RealEndTime
}
if len(diffJobs) > 0 {
timeDiff = diffJobs[len(diffJobs)-1].RealEndTime
}
var sizeFull int64
for _, v := range fullJobs {
sizeFull += v.Bytes
}
var sizeInc int64
for _, v := range incJobs {
sizeInc += v.Bytes
}
var sizeDiff int64
for _, v := range diffJobs {
sizeDiff += v.Bytes
}
m.Backup(strings.TrimRight(c.Name, "-fd"), machine.BackupBrandBacula, timeFull, timeInc, timeDiff, sizeFull, sizeInc, sizeDiff)
ms <- m
}
close(ms)
}
// sendMachines reads from channel ms and updates every machine in the idb.
func sendMachines(idb *idbclient.Idb, errs chan error, ms chan machine.Machine, create bool) {
for m := range ms {
_, err := idb.UpdateMachine(&m, create)
if err != nil {
log.Printf("%+v\n", m)
errs <- err
}
}
}