-
Notifications
You must be signed in to change notification settings - Fork 2
/
common.go
462 lines (409 loc) 路 11.1 KB
/
common.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
// Copyright 2015-2023 Bleemeo
//
// bleemeo.com an infrastructure monitoring solution in the Cloud
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package discovery
import (
"context"
"fmt"
"net"
"strconv"
"time"
"github.com/bleemeo/glouton/config"
"github.com/bleemeo/glouton/facts"
"github.com/bleemeo/glouton/logger"
"github.com/bleemeo/glouton/types"
"dario.cat/mergo"
)
const tcpPortocol = "tcp"
// Discoverer allow to discover services. See DynamicDiscovery and Discovery.
type Discoverer interface {
Discovery(ctx context.Context, maxAge time.Duration) (services []Service, err error)
LastUpdate() time.Time
}
// PersistentDiscoverer also allow to remove a non-running service.
type PersistentDiscoverer interface {
Discoverer
RemoveIfNonRunning(ctx context.Context, services []Service)
}
// NameInstance contains the service and instance names.
//
// The instance could be either a container name OR simply a arbitrary value.
type NameInstance struct {
Name string
Instance string
}
type ServiceOverride struct {
IgnoredPorts []int
Interval time.Duration
ExtraAttribute map[string]string
}
// ServiceName is the name of a supported service.
type ServiceName string
// List of known service names.
const (
ApacheService ServiceName = "apache"
AsteriskService ServiceName = "asterisk"
BindService ServiceName = "bind"
BitBucketService ServiceName = "bitbucket"
CassandraService ServiceName = "cassandra"
ConfluenceService ServiceName = "confluence"
DovecotService ServiceName = "dovecot"
EjabberService ServiceName = "ejabberd"
ElasticSearchService ServiceName = "elasticsearch"
EximService ServiceName = "exim"
Fail2banService ServiceName = "fail2ban"
FreeradiusService ServiceName = "freeradius"
HAProxyService ServiceName = "haproxy"
InfluxDBService ServiceName = "influxdb"
JenkinsService ServiceName = "jenkins"
JIRAService ServiceName = "jira"
KafkaService ServiceName = "kafka"
LibvirtService ServiceName = "libvirt"
MemcachedService ServiceName = "memcached"
MongoDBService ServiceName = "mongodb"
MosquittoService ServiceName = "mosquitto" //nolint:misspell
MySQLService ServiceName = "mysql"
NatsService ServiceName = "nats"
NfsService ServiceName = "nfs"
NginxService ServiceName = "nginx"
NTPService ServiceName = "ntp"
OpenLDAPService ServiceName = "openldap"
OpenVPNService ServiceName = "openvpn"
PHPFPMService ServiceName = "phpfpm"
PostfixService ServiceName = "postfix"
PostgreSQLService ServiceName = "postgresql"
RabbitMQService ServiceName = "rabbitmq"
RedisService ServiceName = "redis"
SaltMasterService ServiceName = "salt_master"
SquidService ServiceName = "squid"
UWSGIService ServiceName = "uwsgi"
VarnishService ServiceName = "varnish"
UPSDService ServiceName = "upsd"
ZookeeperService ServiceName = "zookeeper"
CustomService ServiceName = "__custom__"
)
type ApplicationType int
const (
ApplicationUnset ApplicationType = 0
ApplicationDockerCompose ApplicationType = 1
)
type Application struct {
Name string
Type ApplicationType
}
// Service is the information found about a given service.
type Service struct {
Config config.Service
Name string
Instance string
Tags []string
Applications []Application
ServiceType ServiceName
ContainerID string
ContainerName string // If ContainerName is set, Instance must be the same value.
IPAddress string // IPAddress is the IPv4 address to reach service for metrics gathering. If empty, it means IP was not found
ListenAddresses []facts.ListenAddress
ExePath string
IgnoredPorts map[int]bool
Active bool
CheckIgnored bool
MetricsIgnored bool
// The interval of the check, used only for custom checks.
Interval time.Duration
HasNetstatInfo bool
LastNetstatInfo time.Time
container facts.Container
}
func (s Service) String() string {
if s.ContainerName != "" {
return fmt.Sprintf("%s (on container %s)", s.Name, s.Instance)
}
if s.Instance != "" {
return fmt.Sprintf("%s (instance %s)", s.Name, s.Instance)
}
return s.Name
}
// AddressForPort return the IP address for given port & network (tcp, udp).
func (s Service) AddressForPort(port int, network string, force bool) string {
if s.Config.Address != "" {
return s.Config.Address
}
for _, a := range s.ListenAddresses {
if a.Network() != network {
continue
}
address, portStr, err := net.SplitHostPort(a.String())
if err != nil {
continue
}
p, err := strconv.Atoi(portStr)
if err != nil {
continue
}
if address == net.IPv4zero.String() {
address = s.IPAddress
}
if p == port {
return address
}
}
if force {
return s.IPAddress
}
return ""
}
// AddressPort return the IP address &port for the "main" service (e.g. for RabbitMQ the AMQP port, not the management port).
func (s Service) AddressPort() (string, int) {
di := servicesDiscoveryInfo[s.ServiceType]
port := di.ServicePort
force := false
if s.Config.Port != 0 {
port = s.Config.Port
}
if port == 0 {
return "", 0
}
return s.AddressForPort(port, di.ServiceProtocol, force), port
}
// LabelsOfStatus returns the labels for the status metrics of this service.
func (s Service) LabelsOfStatus() map[string]string {
labels := map[string]string{
types.LabelName: types.MetricServiceStatus,
types.LabelService: s.Name,
}
if s.Instance != "" {
labels[types.LabelServiceInstance] = s.Instance
}
return labels
}
// AnnotationsOfStatus returns the annotations for the status metrics of this service.
func (s Service) AnnotationsOfStatus() types.MetricAnnotations {
annotations := types.MetricAnnotations{
ServiceName: s.Name,
ServiceInstance: s.Instance,
}
if s.ContainerID != "" {
annotations.ContainerID = s.ContainerID
}
return annotations
}
// merge updates existing service with update.
// Value from existing Service are preferred, but "list" value are merged.
func (s Service) merge(update Service) Service {
switch {
case update.HasNetstatInfo && !s.HasNetstatInfo:
s.ListenAddresses = update.ListenAddresses
s.HasNetstatInfo = true
s.LastNetstatInfo = update.LastNetstatInfo
case update.HasNetstatInfo:
for _, v := range update.ListenAddresses {
alreadyExist := false
for _, existing := range s.ListenAddresses {
if v == existing {
alreadyExist = true
break
}
}
if !alreadyExist {
s.ListenAddresses = append(s.ListenAddresses, v)
}
}
if s.LastNetstatInfo.Before(update.LastNetstatInfo) {
s.LastNetstatInfo = update.LastNetstatInfo
}
}
for k, v := range update.IgnoredPorts {
if _, ok := s.IgnoredPorts[k]; !ok {
s.IgnoredPorts[k] = v
}
}
// Merge configs, existing values are preferred.
if err := mergo.Merge(&s.Config, update.Config); err != nil {
logger.V(1).Printf("Failed to merge service configs: %s", err)
}
return s
}
//nolint:gochecknoglobals
var (
servicesDiscoveryInfo = map[ServiceName]discoveryInfo{
ApacheService: {
ServicePort: 80,
ServiceProtocol: "tcp",
},
BitBucketService: {
ServicePort: 7990,
ServiceProtocol: "tcp",
IgnoreHighPort: true,
DefaultIgnoredPorts: map[int]bool{
5701: true,
},
},
BindService: {
ServicePort: 53,
ServiceProtocol: "tcp",
},
CassandraService: {
ServicePort: 9042,
ServiceProtocol: "tcp",
IgnoreHighPort: true,
},
ConfluenceService: {
ServicePort: 8090,
ServiceProtocol: "tcp",
IgnoreHighPort: true,
},
DovecotService: {
ServicePort: 143,
ServiceProtocol: "tcp",
},
ElasticSearchService: {
ServicePort: 9200,
ServiceProtocol: "tcp",
IgnoreHighPort: true,
},
EjabberService: {
ServicePort: 5222,
ServiceProtocol: "tcp",
IgnoreHighPort: true,
},
EximService: {
ServicePort: 25,
ServiceProtocol: "tcp",
},
Fail2banService: {},
HAProxyService: {
IgnoreHighPort: true, // HAProxy use a random high-port when Syslog over-UDP is enabled.
ServiceProtocol: "tcp",
},
InfluxDBService: {
ServicePort: 8086,
ServiceProtocol: "tcp",
},
JenkinsService: {
ServicePort: 8080,
ServiceProtocol: "tcp",
IgnoreHighPort: true,
},
JIRAService: {
ServicePort: 8080,
ServiceProtocol: "tcp",
IgnoreHighPort: true,
},
KafkaService: {
ServicePort: 9092,
ServiceProtocol: "tcp",
IgnoreHighPort: true,
},
MemcachedService: {
ServicePort: 11211,
ServiceProtocol: "tcp",
},
MongoDBService: {
ServicePort: 27017,
ServiceProtocol: "tcp",
},
MosquittoService: {
ServicePort: 1883,
ServiceProtocol: "tcp",
},
MySQLService: {
ServicePort: 3306,
ServiceProtocol: "tcp",
},
NginxService: {
ServicePort: 80,
ServiceProtocol: "tcp",
},
NatsService: {
ServicePort: 4222,
ServiceProtocol: "tcp",
},
NfsService: {},
NTPService: {
ServicePort: 123,
ServiceProtocol: "udp",
},
OpenLDAPService: {
ServicePort: 389,
ServiceProtocol: "tcp",
},
OpenVPNService: {
DisablePersistentConnection: true,
},
PHPFPMService: {
ServiceProtocol: "tcp",
},
PostfixService: {
ServicePort: 25,
ServiceProtocol: "tcp",
},
PostgreSQLService: {
ServicePort: 5432,
ServiceProtocol: "tcp",
},
RabbitMQService: {
ServicePort: 5672,
ServiceProtocol: "tcp",
IgnoreHighPort: true,
},
RedisService: {
ServicePort: 6379,
ServiceProtocol: "tcp",
},
SaltMasterService: {
ServicePort: 4505,
ServiceProtocol: "tcp",
},
SquidService: {
ServicePort: 3128,
ServiceProtocol: "tcp",
},
UPSDService: {
ServicePort: 3493,
ServiceProtocol: "tcp",
},
UWSGIService: {
IgnoreHighPort: true,
},
VarnishService: {
ServicePort: 6082,
ServiceProtocol: "tcp",
},
ZookeeperService: {
ServicePort: 2181,
ServiceProtocol: "tcp",
IgnoreHighPort: true,
},
CustomService: {},
}
)
type discoveryInfo struct {
ServicePort int
ServiceProtocol string // "tcp", "udp" or "unix"
IgnoreHighPort bool
DisablePersistentConnection bool
DefaultIgnoredPorts map[int]bool
}
func serviceListToMap(services []Service) map[NameInstance]Service {
servicesMap := make(map[NameInstance]Service, len(services))
for _, service := range services {
key := NameInstance{
Name: service.Name,
Instance: service.Instance,
}
servicesMap[key] = service
}
return servicesMap
}