@@ -23,107 +23,26 @@ import (
23
23
24
24
// ExporterConfig configures ports to scan to what filename to save it to.
25
25
// if path is set we will try to make a HTTP get and find # TYPE in the first 10 rows of the response to make sure we know its prometheus metrics.
26
- // TODO make this runtime configurable.
27
26
type ExporterConfig []struct {
28
27
port string
29
28
filename string
30
29
path string
31
30
}
32
31
33
- const ExporterExporterPort = "9999"
34
-
35
32
var exporterConfig = ExporterConfig {
36
- { // special exporter_exporter we scan this first to know if we can skip the other ports.
37
- port : ExporterExporterPort ,
33
+ // We only support exporter_exporter at the moment
34
+ {
35
+ port : "" ,
38
36
filename : "" ,
39
37
},
40
- /*
41
- disabled those since we only use exporter_exporter on 9999 for now.
42
- TODO move this to a config
43
- {
44
- port: "8081",
45
- filename: "php",
46
- path: "http://%s/metrics",
47
- },
48
- {
49
- port: "9100",
50
- filename: "node",
51
- },
52
- {
53
- port: "9108",
54
- filename: "elasticsearch",
55
- },
56
- {
57
- port: "9114",
58
- filename: "elasticsearch",
59
- },
60
- {
61
- port: "9216",
62
- filename: "mongodb",
63
- },
64
- {
65
- port: "9091",
66
- filename: "minio",
67
- path: "http://%s/minio/prometheus/metrics",
68
- },
69
- {
70
- port: "9101",
71
- filename: "haproxy",
72
- },
73
- {
74
- port: "9104",
75
- filename: "mysql",
76
- },
77
- {
78
- port: "9113",
79
- filename: "nginx",
80
- },
81
- {
82
- port: "9121",
83
- filename: "redis",
84
- },
85
- {
86
- port: "9150",
87
- filename: "memcached",
88
- },
89
- {
90
- port: "9154",
91
- filename: "postfix",
92
- },
93
- {
94
- port: "9182",
95
- filename: "wmi",
96
- },
97
- {
98
- port: "9187",
99
- filename: "postgres",
100
- },
101
- {
102
- port: "9188",
103
- filename: "pgbouncer",
104
- },
105
- {
106
- port: "9189",
107
- filename: "barman",
108
- },
109
- {
110
- port: "9253",
111
- filename: "php-fpm",
112
- },
113
- {
114
- port: "9308",
115
- filename: "kafka",
116
- },
117
- {
118
- port: "9496",
119
- filename: "389ds",
120
- },
121
- */
122
38
}
123
39
40
+ var mutex sync.Mutex
41
+
124
42
func main () {
125
43
config := & Config {}
126
44
multiconfig .MustLoad (& config )
45
+ exporterConfig [0 ].port = config .ExpoterExporterPort
127
46
128
47
fnxlogrus .Init (config .Log , logrus .StandardLogger ())
129
48
@@ -138,14 +57,11 @@ func main() {
138
57
logrus .Infof ("Running with interval %s" , interval )
139
58
140
59
ticker := time .NewTicker (interval )
141
- interrupt := make (chan os.Signal , 1 )
142
- signal .Notify (interrupt , os .Interrupt , syscall .SIGQUIT , syscall .SIGTERM , syscall .SIGINT )
143
-
144
- ctx , cancel := context .WithCancel (context .Background ())
60
+ ctx , cancel := signal .NotifyContext (context .Background (), os .Interrupt , syscall .SIGQUIT , syscall .SIGTERM )
61
+ defer cancel ()
145
62
146
63
go func () {
147
- <- interrupt
148
- cancel ()
64
+ startWs (config , ctx )
149
65
ticker .Stop ()
150
66
log .Println ("Shutting down" )
151
67
}()
@@ -226,6 +142,8 @@ func runDiscovery(parentCtx context.Context, config *Config, networks []string)
226
142
}
227
143
228
144
func saveConfigs (ctx context.Context , config * Config , exporters Exporters ) {
145
+ mutex .Lock ()
146
+ defer mutex .Unlock ()
229
147
for name , addresses := range exporters {
230
148
if ctx .Err () != nil {
231
149
return
@@ -260,16 +178,11 @@ func discoverNetwork(network string, queue chan func(context.Context), exporter
260
178
port := data .port
261
179
logrus .Debugf ("scanning port: %s:%s" , ip , port )
262
180
var exporters []string
263
- if port == ExporterExporterPort {
264
- exporters , err = checkExporterExporter (ctx , ip , port )
265
- if err != nil {
266
- if ! errors .Is (err , context .DeadlineExceeded ) && ! IsTimeout (err ) {
267
- logrus .Errorf ("error fetching from exporter_exporter: %s" , err )
268
- }
269
- logrus .Debugf ("%s:%s was not open" , ip , port )
270
- continue
181
+ exporters , err = checkExporterExporter (ctx , ip , port )
182
+ if err != nil {
183
+ if ! errors .Is (err , context .DeadlineExceeded ) && ! IsTimeout (err ) {
184
+ logrus .Debugf ("error fetching from exporter_exporter: %s" , err )
271
185
}
272
- } else if ! alive (ctx , ip , port , data .path ) {
273
186
logrus .Debugf ("%s:%s was not open" , ip , port )
274
187
continue
275
188
}
@@ -297,18 +210,7 @@ func discoverNetwork(network string, queue chan func(context.Context), exporter
297
210
}
298
211
exporter <- & a
299
212
}
300
- return // we found exporter_exporter dont scan other ports so return here
301
213
}
302
-
303
- a := Address {
304
- IP : strings .TrimSpace (ip ),
305
- Hostname : strings .TrimSpace (hostname ),
306
- Subnet : strings .TrimSpace (network ),
307
- Exporter : data .filename ,
308
- Port : port ,
309
- }
310
-
311
- exporter <- & a
312
214
}
313
215
}
314
216
}
@@ -335,9 +237,9 @@ func getOldGroups(path string) ([]Group, error) {
335
237
func writeFileSDConfig (config * Config , exporterName string , addresses []Address ) error {
336
238
path := filepath .Join (config .FileSdPath , exporterName + ".json" )
337
239
338
- if _ , err := os .Stat (config .FileSdPath ); os .IsNotExist (err ) {
339
- os .MkdirAll (config .FileSdPath , 0755 )
340
- }
240
+ if _ , err := os .Stat (config .FileSdPath ); os .IsNotExist (err ) {
241
+ os .MkdirAll (config .FileSdPath , 0755 )
242
+ }
341
243
342
244
groups := []Group {}
343
245
@@ -349,7 +251,7 @@ func writeFileSDConfig(config *Config, exporterName string, addresses []Address)
349
251
"host" : v .Hostname ,
350
252
},
351
253
}
352
- if v .Port == ExporterExporterPort {
254
+ if v .Port == config . ExpoterExporterPort {
353
255
group .Labels ["__metrics_path__" ] = "/proxy"
354
256
group .Labels ["__param_module" ] = exporterName
355
257
}
0 commit comments