-
Notifications
You must be signed in to change notification settings - Fork 17
/
opensips_exporter.go
140 lines (122 loc) · 4.37 KB
/
opensips_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
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
package main
import (
"flag"
"log"
"net/http"
"os"
"strings"
"fmt"
"github.com/VoIPGRID/opensips_exporter/opensips"
"github.com/VoIPGRID/opensips_exporter/opensips/jsonrpc"
"github.com/VoIPGRID/opensips_exporter/processors"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var o *opensips.OpenSIPS
var j *jsonrpc.JSONRPC
var collectAll = []string{"core:", "shmem:", "net:", "uri:", "tm:", "sl:", "usrloc:", "dialog:", "registrar:", "pkmem:", "load:", "tmx:"}
const envPrefix = "OPENSIPS_EXPORTER"
func handler(w http.ResponseWriter, r *http.Request) {
collect := r.URL.Query()["collect[]"]
collectors := make(map[prometheus.Collector]bool)
if len(collect) == 0 {
// Collect everything if nothing is specified
collect = collectAll
}
var scrapeProcessor prometheus.Collector
var statistics map[string]opensips.Statistic
var err error
switch *protocol {
case "mi_datagram":
o, err = opensips.New(*socketPath)
if err != nil {
log.Fatalf("Could not create datagram socket: %v", err)
}
statistics, err = o.GetStatistics(collect...)
case "mi_http":
j = jsonrpc.New(*httpEndpoint)
statistics, err = j.GetStatistics(collect...)
}
if err != nil {
scrapeProcessor = processors.NewScrapeProcessor(0)
log.Printf("Error encountered while reading statistics from opensips socket: %v", err)
} else {
scrapeProcessor = processors.NewScrapeProcessor(1)
}
collectors[scrapeProcessor] = true
var selectedProcessors = map[string]bool{}
for _, processor := range collect {
if p, ok := processors.OpensipsProcessors[processor]; ok {
processorFunc := fmt.Sprintf("%p", p)
if _, ok := selectedProcessors[processorFunc]; !ok {
selectedProcessors[processorFunc] = true
collectors[p(statistics)] = true
}
}
}
registry := prometheus.NewRegistry()
for collector := range collectors {
err := registry.Register(collector)
if err != nil {
log.Printf("Problems registering the %T processor (could be due to no metrics found for this processor). Error: %v\n", collector, err)
}
}
gatherers := prometheus.Gatherers{
prometheus.DefaultGatherer,
registry,
}
// Delegate http serving to Prometheus client library, which will call collector.Collect.
h := promhttp.HandlerFor(gatherers,
promhttp.HandlerOpts{
ErrorHandling: promhttp.ContinueOnError,
})
h.ServeHTTP(w, r)
}
// strflag is like flag.String, with value overridden by an environment
// variable (when present). e.g. with socket path, the env var used as default
// is OPENSIPS_EXPORTER_SOCKET_PATH, if present in env.
func strflag(name string, value string, usage string) *string {
if v, ok := os.LookupEnv(envPrefix + strings.ToUpper(name)); ok {
return flag.String(name, v, usage)
}
return flag.String(name, value, usage)
}
var (
socketPath *string
metricsPath *string
addr *string
protocol *string
httpEndpoint *string
)
func main() {
addr = strflag("addr", ":9434", "Address on which the OpenSIPS exporter listens. (e.g. 127.0.0.1:9434)")
metricsPath = strflag("path", "/metrics", "The path where metrics will be served.")
socketPath = strflag("socket", "/var/run/ser-fg/ser.sock", "Path to the socket file for OpenSIPS.)")
httpEndpoint = strflag("http_address", "http://127.0.0.1:8888/mi/", "Address to query the Management Interface through HTTP with (e.g. http://127.0.0.1:8888/mi/)")
protocol = strflag("protocol", "", "Which protocol to use to get data from the Management Interface (mi_datagram & mi_http currently supported)")
flag.Parse()
switch *protocol {
case "mi_datagram":
if *socketPath == "" {
log.Fatalf("The -protocol flag is set to mi_datagram but the -socket param is not set. Exiting.")
}
case "mi_http":
if *httpEndpoint == "" {
log.Fatalf("The -protocol is set to mi_http but the -http_address flag is not set. Exiting.")
}
default:
log.Fatalf("Please set the -protocol flag to define which protocol the exporter should use to query for metrics. (mi_datagram or mi_http)")
}
http.HandleFunc(*metricsPath, handler)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`<html>
<head><title>OpenSIPS Exporter</title></head>
<body>
<h1>OpenSIPS Exporter</h1>
<p><a href="` + *metricsPath + `">Metrics</a></p>
</body>
</html>`))
})
log.Printf("Started OpenSIPS exporter, listening on %v", *addr)
log.Fatal(http.ListenAndServe(*addr, nil))
}