-
Notifications
You must be signed in to change notification settings - Fork 0
/
syncovery_collector.go
148 lines (135 loc) · 3.31 KB
/
syncovery_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
package main
import (
"log"
"strconv"
"github.com/prometheus/client_golang/prometheus"
)
const (
namespace string = "syncovery_profile_"
)
type SyncoveryCollector struct {
client *SyncoveryClient
LastRunDateTime *prometheus.Desc
NextRunDateTime *prometheus.Desc
LastRunFailure *prometheus.Desc
IsScheduled *prometheus.Desc
IsLocked *prometheus.Desc
IsDisabled *prometheus.Desc
IsRunning *prometheus.Desc
}
func NewSyncoveryCollector(client *SyncoveryClient) *SyncoveryCollector {
return &SyncoveryCollector{
client: client,
LastRunDateTime: prometheus.NewDesc(
namespace+"last_run_timestamp_seconds",
"Timestamp of last run of profile",
[]string{"number", "name"},
nil,
),
NextRunDateTime: prometheus.NewDesc(
namespace+"next_run_timestamp_seconds",
"Timestamp of next run of profile",
[]string{"number", "name"},
nil,
),
LastRunFailure: prometheus.NewDesc(
namespace+"last_run_failure",
"Displays whether or not the last profile run was a failure",
[]string{"number", "name"},
nil,
),
IsScheduled: prometheus.NewDesc(
namespace+"scheduled",
"Displays whether or not the profile is scheduled",
[]string{"number", "name"},
nil,
),
IsRunning: prometheus.NewDesc(
namespace+"running",
"Displays whether or not the profile is running",
[]string{"number", "name"},
nil,
),
IsDisabled: prometheus.NewDesc(
namespace+"disabled",
"Displays whether or not the profile is disabled",
[]string{"number", "name"},
nil,
),
IsLocked: prometheus.NewDesc(
namespace+"locked",
"Displays whether or not the profile is locked",
[]string{"number", "name"},
nil,
),
}
}
func (sc *SyncoveryCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- sc.LastRunDateTime
ch <- sc.LastRunFailure
ch <- sc.NextRunDateTime
ch <- sc.IsRunning
ch <- sc.IsDisabled
ch <- sc.IsScheduled
ch <- sc.IsLocked
}
func (sc *SyncoveryCollector) Collect(ch chan<- prometheus.Metric) {
profiles, err := sc.client.GetProfiles()
if err != nil {
log.Printf("failed to get profiles: %v", err)
return
}
for _, v := range *profiles {
ch <- prometheus.MustNewConstMetric(
sc.LastRunDateTime,
prometheus.GaugeValue,
float64(v.LastRunDateTime.Unix()),
strconv.Itoa(v.Number), v.Name,
)
ch <- prometheus.MustNewConstMetric(
sc.LastRunFailure,
prometheus.GaugeValue,
float64Bool(v.LastRunHadError),
strconv.Itoa(v.Number), v.Name,
)
ch <- prometheus.MustNewConstMetric(
sc.IsLocked,
prometheus.GaugeValue,
float64Bool(v.IsLocked),
strconv.Itoa(v.Number), v.Name,
)
ch <- prometheus.MustNewConstMetric(
sc.IsDisabled,
prometheus.GaugeValue,
float64Bool(v.IsDisabled),
strconv.Itoa(v.Number), v.Name,
)
ch <- prometheus.MustNewConstMetric(
sc.IsRunning,
prometheus.GaugeValue,
float64Bool(v.IsRunning),
strconv.Itoa(v.Number), v.Name,
)
ch <- prometheus.MustNewConstMetric(
sc.IsScheduled,
prometheus.GaugeValue,
float64Bool(v.IsScheduled),
strconv.Itoa(v.Number), v.Name,
)
if v.NextRunDateTime != nil {
ch <- prometheus.MustNewConstMetric(
sc.NextRunDateTime,
prometheus.GaugeValue,
float64(v.NextRunDateTime.Unix()),
strconv.Itoa(v.Number), v.Name,
)
}
}
}
func float64Bool(b bool) float64 {
if b {
return 1
} else {
return 0
}
}