This repository has been archived by the owner on Oct 25, 2022. It is now read-only.
forked from dreyau/bareos_exporter
-
Notifications
You must be signed in to change notification settings - Fork 7
/
collector.go
209 lines (175 loc) · 6.85 KB
/
collector.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package main
import (
"github.com/prometheus/client_golang/prometheus"
log "github.com/sirupsen/logrus"
"strconv"
)
type bareosMetrics struct {
TotalJobs *prometheus.Desc
TotalFiles *prometheus.Desc
TotalBytes *prometheus.Desc
LastJobBytes *prometheus.Desc
LastJobFiles *prometheus.Desc
LastJobErrors *prometheus.Desc
LastJobStartTimestamp *prometheus.Desc
LastJobEndTimestamp *prometheus.Desc
LastJobStatus *prometheus.Desc
PoolBytes *prometheus.Desc
PoolVolumes *prometheus.Desc
connection *Connection
}
type jobLabels []string
var jobInfoLabelNames = jobLabels{"jobname", "jobtype", "client", "fileset"}
func (jobInfo JobInfo) toLabels() jobLabels {
return jobLabels{jobInfo.JobName, string(jobInfo.JobType), jobInfo.ClientName, jobInfo.FileSetName}
}
func (s jobLabels) newAppend(label string) jobLabels {
cpy := make(jobLabels, len(s), len(s)+1)
copy(cpy, s)
cpy = append(cpy, label)
return cpy
}
var jobInfoAndWhichLabelNames = jobInfoLabelNames.newAppend("last_selector")
var poolInfoLabelNames = jobLabels{"pool", "prunable", "expired"}
func (poolInfo PoolInfo) toLabels() jobLabels {
return jobLabels{poolInfo.Name, strconv.FormatBool(poolInfo.Prunable), strconv.FormatBool(poolInfo.Expired)}
}
func bareosCollector(conn *Connection) *bareosMetrics {
return &bareosMetrics{
TotalJobs: prometheus.NewDesc("bareos_jobs_run",
"Total backup jobs for jobname",
jobInfoLabelNames, nil,
),
TotalFiles: prometheus.NewDesc("bareos_files_saved",
"Total files saved for during all jobs combined",
jobInfoLabelNames, nil,
),
TotalBytes: prometheus.NewDesc("bareos_bytes_saved",
"Total bytes saved for during all jobs combined",
jobInfoLabelNames, nil,
),
LastJobBytes: prometheus.NewDesc("bareos_last_job_bytes_saved",
"Bytes saved during last backup",
jobInfoAndWhichLabelNames, nil,
),
LastJobFiles: prometheus.NewDesc("bareos_last_job_files_saved",
"Files saved during last job",
jobInfoAndWhichLabelNames, nil,
),
LastJobErrors: prometheus.NewDesc("bareos_last_job_errors",
"Errors occurred during last job",
jobInfoAndWhichLabelNames, nil,
),
LastJobStartTimestamp: prometheus.NewDesc("bareos_last_job_start_unix_timestamp",
"Execution start timestamp of last job",
jobInfoAndWhichLabelNames, nil,
),
LastJobEndTimestamp: prometheus.NewDesc("bareos_last_job_end_unix_timestamp",
"Execution end timestamp of last job",
jobInfoAndWhichLabelNames, nil,
),
LastJobStatus: prometheus.NewDesc("bareos_last_job_status",
"Status of the last job",
jobInfoAndWhichLabelNames.newAppend("status"), nil,
),
PoolBytes: prometheus.NewDesc("bareos_pool_bytes",
"Total bytes saved in a pool",
poolInfoLabelNames, nil,
),
PoolVolumes: prometheus.NewDesc("bareos_pool_volumes",
"Total volumes in a pool",
poolInfoLabelNames, nil,
),
connection: conn,
}
}
func (collector *bareosMetrics) Describe(ch chan<- *prometheus.Desc) {
ch <- collector.TotalJobs
ch <- collector.TotalFiles
ch <- collector.TotalBytes
ch <- collector.LastJobBytes
ch <- collector.LastJobFiles
ch <- collector.LastJobErrors
ch <- collector.LastJobStartTimestamp
ch <- collector.LastJobEndTimestamp
ch <- collector.LastJobStatus
ch <- collector.PoolBytes
ch <- collector.PoolVolumes
}
func (collector *bareosMetrics) Collect(ch chan<- prometheus.Metric) {
var jobs, jobsErr = collector.connection.JobList()
if jobsErr != nil {
log.WithFields(log.Fields{
"method": "JobList",
}).Error(jobsErr)
} else {
for _, jobInfo := range jobs {
ch <- prometheus.MustNewConstMetric(collector.TotalJobs, prometheus.CounterValue, float64(jobInfo.TotalCount), jobInfo.toLabels()...)
ch <- prometheus.MustNewConstMetric(collector.TotalBytes, prometheus.CounterValue, float64(jobInfo.TotalBytes), jobInfo.toLabels()...)
ch <- prometheus.MustNewConstMetric(collector.TotalFiles, prometheus.CounterValue, float64(jobInfo.TotalFiles), jobInfo.toLabels()...)
lastJob, lastJobErr := collector.connection.LastJob(&jobInfo)
lastSuccessfulJob, lastSuccessfulJobErr := collector.connection.LastSuccessfulJob(&jobInfo)
lastSuccessfulFullJob, lastSuccessfulFullJobErr := collector.connection.LastSuccessfulFullJob(&jobInfo)
if lastJobErr != nil {
log.WithFields(log.Fields{
"job": jobInfo,
"method": "LastJob",
}).Error(lastJobErr)
} else {
collector.collectLastJob(ch, jobInfo, lastJob, "last")
}
if lastSuccessfulJobErr != nil {
log.WithFields(log.Fields{
"job": jobInfo,
"method": "LastSuccessfulJob",
}).Error(lastSuccessfulJobErr)
} else {
collector.collectLastJob(ch, jobInfo, lastSuccessfulJob, "last_successful")
}
if lastSuccessfulFullJobErr != nil {
log.WithFields(log.Fields{
"job": jobInfo,
"method": "LastSuccessfulFullJob",
}).Error(lastSuccessfulFullJobErr)
} else {
collector.collectLastJob(ch, jobInfo, lastSuccessfulFullJob, "last_successful_full")
}
}
}
var poolInfoList, poolInfoErr = collector.connection.PoolInfo()
if poolInfoErr != nil {
log.WithFields(log.Fields{
"method": "PoolInfo",
}).Error(poolInfoErr)
} else {
for _, poolInfo := range poolInfoList {
ch <- prometheus.MustNewConstMetric(collector.PoolBytes, prometheus.CounterValue, float64(poolInfo.Bytes), poolInfo.toLabels()...)
ch <- prometheus.MustNewConstMetric(collector.PoolVolumes, prometheus.CounterValue, float64(poolInfo.Volumes), poolInfo.toLabels()...)
}
}
}
func (collector *bareosMetrics) collectLastJob(ch chan<- prometheus.Metric, jobInfo JobInfo, lastJob *LastJob, whichLabel string) {
labels := jobInfo.toLabels().newAppend(whichLabel)
ch <- prometheus.MustNewConstMetric(collector.LastJobBytes, prometheus.CounterValue, float64(lastJob.JobBytes), labels...)
ch <- prometheus.MustNewConstMetric(collector.LastJobFiles, prometheus.CounterValue, float64(lastJob.JobFiles), labels...)
ch <- prometheus.MustNewConstMetric(collector.LastJobErrors, prometheus.CounterValue, float64(lastJob.JobErrors), labels...)
ch <- prometheus.MustNewConstMetric(collector.LastJobStartTimestamp, prometheus.CounterValue, float64(lastJob.JobStartDate.Unix()), labels...)
ch <- prometheus.MustNewConstMetric(collector.LastJobEndTimestamp, prometheus.CounterValue, float64(lastJob.JobEndDate.Unix()), labels...)
var bareosTerminationStates, bareosTerminationStatesErr = collector.connection.JobStates()
if bareosTerminationStatesErr != nil {
log.WithFields(log.Fields{
"job": jobInfo,
"method": "JobStates",
}).Error(bareosTerminationStatesErr)
} else {
for _, terminationState := range bareosTerminationStates {
var state = float64(0)
if lastJob != nil {
if terminationState == lastJob.JobStatus {
state = 1
}
}
ch <- prometheus.MustNewConstMetric(collector.LastJobStatus, prometheus.CounterValue, state, labels.newAppend(terminationState)...)
}
}
}