-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
360 lines (324 loc) · 10.4 KB
/
main.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
package main
import (
// "fmt"
"bufio"
"log/slog"
"net"
"net/http"
"os"
"os/signal"
"strconv"
"strings"
"syscall"
"time"
"github.com/alecthomas/kingpin/v2"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/client_golang/prometheus/collectors"
versioncollector "github.com/prometheus/client_golang/prometheus/collectors/version"
"github.com/prometheus/common/promslog"
"github.com/prometheus/common/promslog/flag"
"github.com/prometheus/common/version"
"github.com/prometheus/exporter-toolkit/web"
webflag "github.com/prometheus/exporter-toolkit/web/kingpinflag"
)
const DEFAULT_VIDEOHUB_PORT = "9990"
const DEFAULT_NAMESPACE = "videohub"
const TCP_TIMEOUT = 2 // Timeout for net.Dial to connect to Videohub (in seconds)
const READ_TIMEOUT = "100ms" // Since the connection is persistent. we need to define a read timeout to know that the Videohub sent all it's data
const DEFAULT_HTTP_PORT = ":9990"
var ( // config flags
webConfig = webflag.AddFlags(kingpin.CommandLine, DEFAULT_HTTP_PORT)
namespace = kingpin.Flag("videohub.namespace", "The namespace to use in the exported metrics (ie: <namespace>_device_info ...)").Default(DEFAULT_NAMESPACE).String()
videohubPort = kingpin.Flag("videohub.port", "The videohub TCP port (Very unlikely to be changed)").Default(DEFAULT_VIDEOHUB_PORT).String()
readTimeout = kingpin.Flag("videohub.timeout", "The timeout for reading the data sent from a videohub").Default(READ_TIMEOUT).Duration()
)
var logger *slog.Logger
var versionCollector prometheus.Collector
type VideoHub struct {
Version string
ModelName string
FriendlyName string
NumInputs int
NumOutputs int
NumProcessingUnits int
NumMonitoringOutputs int
InputLabels []string
OutputLabels []string
Routing []int
Locks []string
}
// Enum for the different categories returned by videohub
const (
PROTOCOL_PREAMBULE = iota + 1
DEVICE_INFO
INPUT_LABELS
OUTPUT_LABELS
ROUTING
LOCKS
)
var Categories = map[string]int{
"PROTOCOL PREAMBLE:": PROTOCOL_PREAMBULE,
"VIDEOHUB DEVICE:": DEVICE_INFO,
"INPUT LABELS:": INPUT_LABELS,
"OUTPUT LABELS:": OUTPUT_LABELS,
"VIDEO OUTPUT ROUTING:": ROUTING,
"VIDEO OUTPUT LOCKS:": LOCKS,
}
type metrics struct {
scapeSuccess prometheus.Gauge
scrapeDuration prometheus.Gauge
info *prometheus.GaugeVec
numInputs prometheus.Gauge
numOutputs prometheus.Gauge
inputLabels *prometheus.GaugeVec
outputLabels *prometheus.GaugeVec
routing *prometheus.GaugeVec
}
func main() {
promslogConfig := &promslog.Config{}
flag.AddFlags(kingpin.CommandLine, promslogConfig)
kingpin.Version(version.Print("videohub_exporter"))
kingpin.HelpFlag.Short('h')
kingpin.Parse()
logger = promslog.New(promslogConfig)
versionCollector = versioncollector.NewCollector("videohub_exporter")
prometheus.MustRegister(versionCollector) // register here for default registry
prometheus.MustRegister(collectors.NewBuildInfoCollector())
logger.Info("Starting videohub_exporter", "version", version.Info())
logger.Info("", "build_context", version.BuildContext())
http.Handle("/metrics", promhttp.Handler()) // Regular metrics endpoint for local exporter metrics. (GoCollector, ProcessCollector, VersionCollector)
http.HandleFunc("/scrape", ScrapeVideohub) // Endpoint to do videohub scrapes.
http.HandleFunc("/-/healthy", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("Healthy"))
})
srv := &http.Server{}
srvc := make(chan struct{})
term := make(chan os.Signal, 1)
signal.Notify(term, os.Interrupt, syscall.SIGTERM)
go func() {
if err := web.ListenAndServe(srv, webConfig, logger); err != nil {
logger.Error("msg", "Error starting HTTP server", "err", err)
close(srvc)
}
}()
for {
select {
case <-term:
logger.Info("msg", "Received SIGTERM, exiting gracefully...")
os.Exit(0)
case <-srvc:
os.Exit(1)
}
}
}
func ScrapeVideohub(w http.ResponseWriter, r *http.Request) {
params := r.URL.Query()
target := params.Get("target")
if target == "" {
http.Error(w, "Target parameter is missing", http.StatusBadRequest)
return
}
start := time.Now()
registry := prometheus.NewRegistry()
registry.MustRegister(versionCollector)
metrics := NewMetrics(registry)
logger.Info("Scrapping videohub", "target", target)
videohub, err := ParseVideohub(target)
if err != nil {
metrics.scapeSuccess.Set(0)
logger.Error("Error scrapping videohub", "target", target, "err", err)
} else {
metrics.scapeSuccess.Set(1)
logger.Debug("Generating metrics", "target", target)
SetVideoHubMetrics(videohub, metrics)
}
duration := time.Since(start).Seconds()
metrics.scrapeDuration.Set(duration)
h := promhttp.HandlerFor(registry, promhttp.HandlerOpts{})
h.ServeHTTP(w, r)
}
func ParseVideohub(ip string) (VideoHub, error) {
videohub := VideoHub{}
category := 0
addr := net.JoinHostPort(ip, *videohubPort)
conn, err := net.DialTimeout("tcp", addr, TCP_TIMEOUT*time.Second)
if err != nil {
return videohub, err
}
conn.SetReadDeadline(time.Now().Add(*readTimeout))
scanner := bufio.NewScanner(conn)
scanner.Split(bufio.ScanLines)
for scanner.Scan() {
line := scanner.Text()
// fmt.Println(line)
// Select category
new_category, exists := Categories[line]
if exists {
category = new_category
continue
}
// Parse each category data accordingly
switch category {
case PROTOCOL_PREAMBULE, DEVICE_INFO:
words := strings.Split(line, ": ")
if len(words) < 2 {
break
}
key := words[0]
value := words[1]
switch key {
case "Version":
videohub.Version = strings.Trim(value, " ")
case "Model name":
videohub.ModelName = value
case "Friendly name":
videohub.FriendlyName = value
case "Video inputs":
videohub.NumInputs, _ = strconv.Atoi(value)
videohub.InputLabels = make([]string, videohub.NumInputs)
case "Video outputs":
videohub.NumOutputs, _ = strconv.Atoi(value)
videohub.OutputLabels = make([]string, videohub.NumOutputs)
videohub.Routing = make([]int, videohub.NumOutputs)
videohub.Locks = make([]string, videohub.NumOutputs)
case "Video processing units":
videohub.NumProcessingUnits, _ = strconv.Atoi(value)
case "Video monitoring outputs":
videohub.NumMonitoringOutputs, _ = strconv.Atoi(value)
}
case INPUT_LABELS:
num, label, _ := strings.Cut(line, " ")
number, err := strconv.Atoi(num)
if err != nil {
break
}
videohub.InputLabels[number] = label
case OUTPUT_LABELS:
num, label, _ := strings.Cut(line, " ")
number, err := strconv.Atoi(num)
if err != nil {
break
}
videohub.OutputLabels[number] = label
case ROUTING:
out, in, _ := strings.Cut(line, " ")
output, _ := strconv.Atoi(out)
input, _ := strconv.Atoi(in)
videohub.Routing[output] = input
case LOCKS:
out, lock, _ := strings.Cut(line, " ")
output, _ := strconv.Atoi(out)
videohub.Locks[output] = lock
}
}
logger.Debug("Timeout, done parsing videohub response", "target", ip)
return videohub, nil
}
func NewMetrics(reg prometheus.Registerer) *metrics {
m := &metrics{
scapeSuccess: prometheus.NewGauge(
prometheus.GaugeOpts{
Namespace: *namespace,
Name: "scrape_success",
Help: "Displays whether or not the scrape was a success",
},
),
scrapeDuration: prometheus.NewGauge(
prometheus.GaugeOpts{
Namespace: *namespace,
Name: "scrape_duration_seconds",
Help: "Returns how long the scrape took to complete in seconds",
},
),
info: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: *namespace,
Name: "device_info",
Help: "General device information",
},
[]string{"model_name", "name", "version", "inputs", "outputs", "processing_units", "monitoring_ouputs"},
),
numInputs: prometheus.NewGauge(
prometheus.GaugeOpts{
Namespace: *namespace,
Name: "inputs_total",
Help: "The number of SDI inputs",
},
),
numOutputs: prometheus.NewGauge(
prometheus.GaugeOpts{
Namespace: *namespace,
Name: "outputs_total",
Help: "The number of SDI outputs",
},
),
inputLabels: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: *namespace,
Name: "input_labels",
Help: "The label associated with each SDI input",
},
[]string{"input", "input_label"},
),
outputLabels: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: *namespace,
Name: "output_labels",
Help: "The label associated with each SDI output",
},
[]string{"output", "output_label"},
),
routing: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: *namespace,
Name: "routing",
Help: "The input routed to each output",
},
[]string{"output", "output_label", "input", "input_label"},
),
}
reg.MustRegister(m.scapeSuccess)
reg.MustRegister(m.scrapeDuration)
reg.MustRegister(m.info)
reg.MustRegister(m.numInputs)
reg.MustRegister(m.numOutputs)
reg.MustRegister(m.inputLabels)
reg.MustRegister(m.outputLabels)
reg.MustRegister(m.routing)
return m
}
func SetVideoHubMetrics(videohub VideoHub, metrics *metrics) {
metrics.info.With(prometheus.Labels{
"model_name": videohub.ModelName,
"name": videohub.FriendlyName,
"version": videohub.Version,
"inputs": strconv.Itoa(videohub.NumInputs),
"outputs": strconv.Itoa(videohub.NumOutputs),
"processing_units": strconv.Itoa(videohub.NumProcessingUnits),
"monitoring_ouputs": strconv.Itoa(videohub.NumMonitoringOutputs),
}).Set(1)
metrics.numInputs.Set(float64(videohub.NumInputs))
metrics.numOutputs.Set(float64(videohub.NumOutputs))
for input, label := range videohub.InputLabels {
metrics.inputLabels.With(prometheus.Labels{
"input": strconv.Itoa(input),
"input_label": label,
}).Set(float64(input))
}
for output, label := range videohub.OutputLabels {
metrics.outputLabels.With(prometheus.Labels{
"output": strconv.Itoa(output),
"output_label": label,
}).Set(float64(output))
}
for output, input := range videohub.Routing {
metrics.routing.With(prometheus.Labels{
"output": strconv.Itoa(output),
"output_label": videohub.OutputLabels[output],
"input": strconv.Itoa(input),
"input_label": videohub.InputLabels[input],
}).Set(float64(input))
}
}