-
Notifications
You must be signed in to change notification settings - Fork 2
/
blueiris_exporter.go
180 lines (157 loc) · 4.44 KB
/
blueiris_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
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
package main
import (
"fmt"
"net/http"
"strings"
"sync"
"github.com/prometheus/client_golang/prometheus"
promcollectors "github.com/prometheus/client_golang/prometheus/collectors"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/wymangr/blueiris_exporter/common"
"gopkg.in/alecthomas/kingpin.v2"
)
func NewExporterBlueIris(selectedServerMetrics map[int]common.MetricInfo, logpath string) (*ExporterBlueIris, error) {
return &ExporterBlueIris{
blueIrisServerMetrics: selectedServerMetrics,
logpath: logpath,
}, nil
}
func (e *ExporterBlueIris) Describe(ch chan<- *prometheus.Desc) {
for _, m := range blueIrisServerMetrics {
ch <- m.Desc
ch <- m.Timer
}
}
func (e *ExporterBlueIris) Collect(ch chan<- prometheus.Metric) {
var wg sync.WaitGroup
for _, m := range e.blueIrisServerMetrics {
if m.Collect {
name := m.Name
wg.Add(1)
go CollectMetrics(&wg, ch, m, name, e.logpath)
}
}
wg.Wait()
}
func start(logpath string, metricsPath string, port string) error {
var finalPort string
var finalLogpath string
if strings.HasSuffix(logpath, `\`) {
finalLogpath = logpath
} else if strings.HasSuffix(logpath, `/`) {
finalLogpath = logpath
} else {
if strings.Contains(logpath, `\`) {
finalLogpath = logpath + `\`
} else if strings.Contains(logpath, `/`) {
finalLogpath = logpath + `/`
}
}
exporterBlueIris, _ := NewExporterBlueIris(blueIrisServerMetrics, finalLogpath)
blueIrisReg := prometheus.NewRegistry()
blueIrisReg.MustRegister(exporterBlueIris, promcollectors.NewGoCollector())
blueIrisReg.Gather()
http.Handle(metricsPath, promhttp.HandlerFor(blueIrisReg, promhttp.HandlerOpts{}))
if strings.Contains(port, ":") {
finalPort = port
} else {
finalPort = ":" + port
}
common.BIlogger("Starting Blue Iris Exporter http server", "info")
err := http.ListenAndServe(finalPort, nil)
if err != nil {
return err
}
return nil
}
func main() {
const svcName = "blueiris_exporter"
const svcNameLong = "Blue Iris Exporter"
err := IsService(svcName)
if err != nil {
common.BIlogger(err.Error(), "error")
return
}
var (
install = kingpin.Flag(
"service.install",
"Install as windows Service",
).Bool()
uninstall = kingpin.Flag(
"service.uninstall",
"Uninstall as windows Service",
).Bool()
serviceStart = kingpin.Flag(
"service.start",
"Start Blue Iris Exporter Windows Service",
).Bool()
serviceStop = kingpin.Flag(
"service.stop",
"Stop Blue Iris Exporter Windows Service",
).Bool()
servicePause = kingpin.Flag(
"service.pause",
"Pause Blue Iris Exporter Windows Service",
).Bool()
serviceContinue = kingpin.Flag(
"service.continue",
"Continue Blue Iris Exporter Windows Service",
).Bool()
port = kingpin.Flag(
"telemetry.addr",
"Addresses on which to expose metrics",
).Default(":2112").String()
logpath = kingpin.Flag(
"logpath",
"Directory path to the Blue Iris Logs",
).Default(`C:\BlueIris\log\`).String()
metricsPath = kingpin.Flag(
"telemetry.path",
"URL path for surfacing collected metrics.",
).Default("/metrics").String()
)
kingpin.HelpFlag.Short('h')
kingpin.Parse()
if *install {
err := installService(svcName, svcNameLong, *logpath, *metricsPath, *port)
if err != nil {
common.BIlogger(err.Error(), "error")
}
} else if *uninstall {
err := removeService(svcName)
if err != nil {
common.BIlogger(err.Error(), "error")
}
return
} else if *serviceStart {
err := startService(svcName)
if err != nil {
common.BIlogger(err.Error(), "error")
}
} else if *serviceStop {
err = controlService(svcName, "Stop")
if err != nil {
common.BIlogger(err.Error(), "error")
}
} else if *servicePause {
err = controlService(svcName, "Pause")
if err != nil {
common.BIlogger(err.Error(), "error")
}
} else if *serviceContinue {
err = controlService(svcName, "Continue")
if err != nil {
common.BIlogger(err.Error(), "error")
}
} else {
var a string = fmt.Sprintf(`Starting Blue Iris Exporter with the following:
Log Path: %v
Metric Path: %v
Port: %v`, *logpath, *metricsPath, *port)
common.BIlogger(a, "info")
err := start(*logpath, *metricsPath, *port)
if err != nil {
common.BIlogger(fmt.Sprintf("Error starting blueiris_exporter. err: %v", err), "error")
}
}
}