-
Notifications
You must be signed in to change notification settings - Fork 1
/
ab_business.go
85 lines (73 loc) · 2.94 KB
/
ab_business.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
package main
import (
"log"
"path"
"github.com/jmoiron/sqlx"
"github.com/prometheus/client_golang/prometheus"
)
type abbCollector struct {
activityDB_Path string
statusMetric *prometheus.Desc
startTimeMetric *prometheus.Desc
endTimeMetric *prometheus.Desc
transferedBytesMetric *prometheus.Desc
}
type deviceResult struct {
Status int
DeviceName string `db:"device_name"`
TimeStart int `db:"time_start"`
TimeEnd int `db:"time_end"`
TransferedBytes int `db:"transfered_bytes"`
}
func newABBCollector(dataDir string) (*abbCollector, error) {
// TODO: Keine connection reinreichen sondern nur filename damit man innendrin die connection auf und zu machen kann
activityDB_Path := path.Join(dataDir, "@ActiveBackup/activity.db")
return &abbCollector{
activityDB_Path: activityDB_Path,
statusMetric: prometheus.NewDesc("ab_business_device_result_status",
"Status of the latest device backup",
[]string{"device_name"},
nil,
),
startTimeMetric: prometheus.NewDesc("ab_business_device_result_time_start",
"Start time for the latest device backup",
[]string{"device_name"},
nil,
),
endTimeMetric: prometheus.NewDesc("ab_business_device_result_time_end",
"End time for the latest device backup",
[]string{"device_name"},
nil,
),
transferedBytesMetric: prometheus.NewDesc("ab_business_device_result_transfered_bytes",
"Amount of transfered bytes for the latest device backup",
[]string{"device_name"},
nil,
),
}, nil
}
// Each and every collector must implement the Describe function.
// It essentially writes all descriptors to the prometheus desc channel.
func (collector *abbCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- collector.statusMetric
ch <- collector.startTimeMetric
ch <- collector.endTimeMetric
ch <- collector.transferedBytesMetric
}
// Collect implements required collect function for all promehteus collectors
func (collector *abbCollector) Collect(ch chan<- prometheus.Metric) {
results := []deviceResult{}
activityDB, err := sqlx.Connect("sqlite3", collector.activityDB_Path)
err = activityDB.Select(&results, `SELECT status, device_name, time_start,time_end,transfered_bytes FROM device_result_table
WHERE device_result_id IN (SELECT max(device_result_id) FROM device_result_table WHERE time_end != 0 GROUP BY device_name);`)
if err != nil {
log.Fatalln(err)
}
for _, res := range results {
labels := []string{res.DeviceName}
ch <- prometheus.MustNewConstMetric(collector.statusMetric, prometheus.GaugeValue, float64(res.Status), labels...)
ch <- prometheus.MustNewConstMetric(collector.startTimeMetric, prometheus.GaugeValue, float64(res.TimeStart), labels...)
ch <- prometheus.MustNewConstMetric(collector.endTimeMetric, prometheus.GaugeValue, float64(res.TimeEnd), labels...)
ch <- prometheus.MustNewConstMetric(collector.transferedBytesMetric, prometheus.GaugeValue, float64(res.TransferedBytes), labels...)
}
}