-
Notifications
You must be signed in to change notification settings - Fork 1
/
freeswitch-prometheus-exporter.go
74 lines (60 loc) · 1.46 KB
/
freeswitch-prometheus-exporter.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
package main
import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/tomponline/fsclient/fsclient"
"log"
"log/syslog"
"net/http"
"strconv"
"strings"
"time"
)
var fs *fsclient.Client
var (
activeCalls = promauto.NewGauge(prometheus.GaugeOpts{
Name: "freeswitch_active_calls",
Help: "The total number of active calls",
})
)
func main() {
log.SetFlags(0)
syslogWriter, err := syslog.New(syslog.LOG_INFO, "freeswitch-prometheus-exporter")
if err == nil {
log.SetOutput(syslogWriter)
}
filters := []string{
"Event-Name HEARTBEAT",
}
subs := []string{
"HEARTBEAT",
}
fs = fsclient.NewClient("127.0.0.1:8021", "ClueCon", filters, subs, initFunc)
go statsPoller()
http.Handle("/metrics", promhttp.Handler())
http.ListenAndServe(":2112", nil)
}
func initFunc(fsclient *fsclient.Client) {
log.Print("Connected to freeswitch")
}
func statsPoller() {
for {
activeCalls.Set(getCallsCount())
time.Sleep(2 * time.Second)
}
}
func getCallsCount() float64 {
callCountRes, err := fs.API("show calls count")
if err != nil {
log.Print("Call count API error: ", err)
return 0
}
callCountRes = strings.TrimSpace(callCountRes)
callCountParts := strings.Split(callCountRes, " ")
callCount, err := strconv.ParseFloat(callCountParts[0], 64)
if err != nil {
log.Print("Call count conversion error: ", err)
}
return callCount
}