-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
95 lines (81 loc) · 2.59 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
package main
import (
"encoding/json"
"flag"
"log"
"net/http"
"text/template"
)
var (
addr = flag.String("web.listen-address", ":9889", "Address on which to expose metrics")
jibriURL = flag.String("jibri-status-url", "http://localhost:2222/jibri/api/v1.0/health", "Jitsi jibri URL to scrape")
)
type jibriStats struct {
Status struct {
BusyStatus string `json:"busyStatus"`
Health struct {
HealthStatus string `json:"healthStatus"`
Details struct{} `json:"details"`
} `json:"health"`
} `json:"status"`
}
// this is a workaround since prometheus is unable to parse IDLE. it is showing error in targets
// error: strconv.ParseFloat: parsing "IDLE": invalid syntax.
type newjibriStats struct {
Status struct {
BusyStatus int `json:"busyStatus"`
Health struct {
HealthStatus int `json:"healthStatus"`
} `json:"health"`
} `json:"status"`
}
var tpl = template.Must(template.New("stats").Parse(`# HELP jibri_busystatus It check the status of the jibri whether BUSY, IDLE.
# TYPE jibri_busystatus gauge
jibri_busystatus {{.Status.BusyStatus}}
# HELP jibri_healthstatus It check the health status of the jibri whether HEALTHY or not.
# TYPE jibri_healthstatus gauge
jibri_healthstatus {{.Status.Health.HealthStatus}}`))
type handler struct {
sourceURL string
}
func (h handler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
resp, err := http.Get(h.sourceURL)
if err != nil {
log.Printf("scrape error: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer resp.Body.Close()
var stats jibriStats
var newstats newjibriStats
if err := json.NewDecoder(resp.Body).Decode(&stats); err != nil {
log.Printf("json decoding error: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Replacing IDLE with 0, BUSY with 1 and EXPIRED with 2
// HEALTHY 1 and UNHEALTHY 0
if stats.Status.BusyStatus == "IDLE" {
newstats.Status.BusyStatus = 0
} else if stats.Status.BusyStatus == "BUSY" {
newstats.Status.BusyStatus = 1
} else if stats.Status.BusyStatus == "EXPIRED" {
newstats.Status.BusyStatus = 2
}
if stats.Status.Health.HealthStatus == "HEALTHY" {
newstats.Status.Health.HealthStatus = 1
} else if stats.Status.Health.HealthStatus == "UNHEALTHY" {
newstats.Status.Health.HealthStatus = 0
}
w.Header().Set("Content-Type", "text/plain")
_ = tpl.Execute(w, &newstats)
}
func main() {
log.SetFlags(0)
flag.Parse()
http.Handle("/metrics", handler{sourceURL: *jibriURL})
if err := http.ListenAndServe(*addr, nil); err != nil {
log.Fatal(err)
}
log.Println("Started Jitsi Jibri Metrics Exporter")
}