-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathhost_identifier.go
125 lines (104 loc) · 2.76 KB
/
host_identifier.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
package helper
import (
"fmt"
"net"
"os"
"github.com/observiq/stanza/entry"
"github.com/observiq/stanza/errors"
)
// NewHostIdentifierConfig returns a HostIdentifierConfig with default values
func NewHostIdentifierConfig() HostIdentifierConfig {
return HostIdentifierConfig{
IncludeHostname: true,
IncludeIP: true,
getHostname: getHostname,
getIP: getIP,
}
}
// HostIdentifierConfig is the configuration of a host identifier
type HostIdentifierConfig struct {
IncludeHostname bool `json:"include_hostname,omitempty" yaml:"include_hostname,omitempty"`
IncludeIP bool `json:"include_ip,omitempty" yaml:"include_ip,omitempty"`
getHostname func() (string, error)
getIP func() (string, error)
}
// Build will build a host labeler from the supplied configuration
func (c HostIdentifierConfig) Build() (HostIdentifier, error) {
identifier := HostIdentifier{
includeHostname: c.IncludeHostname,
includeIP: c.IncludeIP,
}
if c.getHostname == nil {
return identifier, fmt.Errorf("getHostname func is not set")
}
if c.getIP == nil {
return identifier, fmt.Errorf("getIP func is not set")
}
if c.IncludeHostname {
hostname, err := c.getHostname()
if err != nil {
return identifier, errors.Wrap(err, "get hostname")
}
identifier.hostname = hostname
}
if c.IncludeIP {
ip, err := c.getIP()
if err != nil {
return identifier, errors.Wrap(err, "get ip address")
}
identifier.ip = ip
}
return identifier, nil
}
// getHostname will return the hostname of the current host
func getHostname() (string, error) {
return os.Hostname()
}
// getIP will return the IP address of the current host
func getIP() (string, error) {
var ip string
interfaces, err := net.Interfaces()
if err != nil {
return "", errors.Wrap(err, "list interfaces")
}
for _, i := range interfaces {
// Skip loopback interfaces
if i.Flags&net.FlagLoopback != 0 {
continue
}
// Skip down interfaces
if i.Flags&net.FlagUp == 0 {
continue
}
addrs, err := i.Addrs()
if err != nil {
continue
}
if len(addrs) > 0 {
ip = addrs[0].String()
}
}
if len(ip) == 0 {
return "", errors.NewError(
"failed to find ip address",
"check that a non-loopback interface with an assigned IP address exists and is running",
)
}
return ip, nil
}
// HostIdentifier is a helper that adds host related metadata to an entry's resource
type HostIdentifier struct {
hostname string
ip string
includeHostname bool
includeIP bool
}
// Identify will add host related metadata to an entry's resource
func (h *HostIdentifier) Identify(entry *entry.Entry) {
if h.includeHostname {
entry.AddResourceKey("host.name", h.hostname)
}
if h.includeIP {
entry.AddResourceKey("host.ip", h.ip)
}
}