-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathcollector.go
153 lines (129 loc) · 5.66 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
package main
import (
"github.com/dreyau/bareos_exporter/dataaccess"
"github.com/prometheus/client_golang/prometheus"
log "github.com/sirupsen/logrus"
)
type bareosMetrics struct {
TotalFiles *prometheus.Desc
TotalBytes *prometheus.Desc
LastJobBytes *prometheus.Desc
LastJobFiles *prometheus.Desc
LastJobErrors *prometheus.Desc
LastJobTimestamp *prometheus.Desc
LastFullJobBytes *prometheus.Desc
LastFullJobFiles *prometheus.Desc
LastFullJobErrors *prometheus.Desc
LastFullJobTimestamp *prometheus.Desc
ScheduledJob *prometheus.Desc
}
func bareosCollector() *bareosMetrics {
return &bareosMetrics{
TotalFiles: prometheus.NewDesc("bareos_files_saved_total",
"Total files saved for server during all backups for hostname combined",
[]string{"hostname"}, nil,
),
TotalBytes: prometheus.NewDesc("bareos_bytes_saved_total",
"Total bytes saved for server during all backups for hostname combined",
[]string{"hostname"}, nil,
),
LastJobBytes: prometheus.NewDesc("bareos_last_backup_job_bytes_saved_total",
"Total bytes saved during last backup for hostname",
[]string{"hostname", "level"}, nil,
),
LastJobFiles: prometheus.NewDesc("bareos_last_backup_job_files_saved_total",
"Total files saved during last backup for hostname",
[]string{"hostname", "level"}, nil,
),
LastJobErrors: prometheus.NewDesc("bareos_last_backup_job_errors_occurred_while_saving_total",
"Total errors occurred during last backup for hostname",
[]string{"hostname", "level"}, nil,
),
LastJobTimestamp: prometheus.NewDesc("bareos_last_backup_job_unix_timestamp",
"Execution timestamp of last backup for hostname",
[]string{"hostname", "level"}, nil,
),
LastFullJobBytes: prometheus.NewDesc("bareos_last_full_backup_job_bytes_saved_total",
"Total bytes saved during last full backup (Level = F) for hostname",
[]string{"hostname"}, nil,
),
LastFullJobFiles: prometheus.NewDesc("bareos_last_full_backup_job_files_saved_total",
"Total files saved during last full backup (Level = F) for hostname",
[]string{"hostname"}, nil,
),
LastFullJobErrors: prometheus.NewDesc("bareos_last_full_backup_job_errors_occurred_while_saving_total",
"Total errors occurred during last full backup (Level = F) for hostname",
[]string{"hostname"}, nil,
),
LastFullJobTimestamp: prometheus.NewDesc("bareos_last_full_backup_job_unix_timestamp",
"Execution timestamp of last full backup (Level = F) for hostname",
[]string{"hostname"}, nil,
),
ScheduledJob: prometheus.NewDesc("bareos_scheduled_jobs_total",
"Probable execution timestamp of next backup for hostname",
[]string{"hostname"}, nil,
),
}
}
func (collector *bareosMetrics) Describe(ch chan<- *prometheus.Desc) {
ch <- collector.TotalFiles
ch <- collector.TotalBytes
ch <- collector.LastJobBytes
ch <- collector.LastJobFiles
ch <- collector.LastJobErrors
ch <- collector.LastJobTimestamp
ch <- collector.LastFullJobBytes
ch <- collector.LastFullJobFiles
ch <- collector.LastFullJobErrors
ch <- collector.LastFullJobTimestamp
ch <- collector.ScheduledJob
}
func (collector *bareosMetrics) Collect(ch chan<- prometheus.Metric) {
connection, connectionErr := dataaccess.GetConnection(connectionString)
defer connection.DB.Close()
if connectionErr != nil {
log.Error(connectionErr)
return
}
var servers, getServerListErr = connection.GetServerList()
if getServerListErr != nil {
log.Error(getServerListErr)
return
}
for _, server := range servers {
serverFiles, filesErr := connection.TotalFiles(server)
serverBytes, bytesErr := connection.TotalBytes(server)
lastServerJob, jobErr := connection.LastJob(server)
lastFullServerJob, fullJobErr := connection.LastFullJob(server)
scheduledJob, scheduledJobErr := connection.ScheduledJobs(server)
if filesErr != nil || bytesErr != nil || jobErr != nil || fullJobErr != nil || scheduledJobErr != nil{
log.Info(server)
}
if filesErr != nil {
log.Error(filesErr)
}
if bytesErr != nil {
log.Error(bytesErr)
}
if jobErr != nil {
log.Error(jobErr)
}
if fullJobErr != nil {
log.Error(fullJobErr)
}
if scheduledJobErr != nil {
log.Error(scheduledJobErr)
}
ch <- prometheus.MustNewConstMetric(collector.TotalFiles, prometheus.CounterValue, float64(serverFiles.Files), server)
ch <- prometheus.MustNewConstMetric(collector.TotalBytes, prometheus.CounterValue, float64(serverBytes.Bytes), server)
ch <- prometheus.MustNewConstMetric(collector.LastJobBytes, prometheus.CounterValue, float64(lastServerJob.JobBytes), server, lastServerJob.Level)
ch <- prometheus.MustNewConstMetric(collector.LastJobFiles, prometheus.CounterValue, float64(lastServerJob.JobFiles), server, lastServerJob.Level)
ch <- prometheus.MustNewConstMetric(collector.LastJobErrors, prometheus.CounterValue, float64(lastServerJob.JobErrors), server, lastServerJob.Level)
ch <- prometheus.MustNewConstMetric(collector.LastJobTimestamp, prometheus.CounterValue, float64(lastServerJob.JobDate.Unix()), server, lastServerJob.Level)
ch <- prometheus.MustNewConstMetric(collector.LastFullJobBytes, prometheus.CounterValue, float64(lastFullServerJob.JobBytes), server)
ch <- prometheus.MustNewConstMetric(collector.LastFullJobFiles, prometheus.CounterValue, float64(lastFullServerJob.JobFiles), server)
ch <- prometheus.MustNewConstMetric(collector.LastFullJobErrors, prometheus.CounterValue, float64(lastFullServerJob.JobErrors), server)
ch <- prometheus.MustNewConstMetric(collector.LastFullJobTimestamp, prometheus.CounterValue, float64(lastFullServerJob.JobDate.Unix()), server)
ch <- prometheus.MustNewConstMetric(collector.ScheduledJob, prometheus.CounterValue, float64(scheduledJob.ScheduledJobs), server)
}
}