forked from elastic/elastic-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathagent_info.go
137 lines (112 loc) · 3.77 KB
/
agent_info.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
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.
package info
import (
"context"
"fmt"
"github.com/elastic/elastic-agent/internal/pkg/release"
"github.com/elastic/elastic-agent/pkg/core/logger"
"github.com/elastic/elastic-agent/pkg/utils"
)
type Agent interface {
// AgentID returns an agent identifier.
AgentID() string
// Headers returns custom headers used to communicate with elasticsearch.
Headers() map[string]string
// LogLevel retrieves a log level.
LogLevel() string
// ReloadID reloads agent info ID from configuration file.
ReloadID(ctx context.Context) error
// SetLogLevel updates log level of agent.
SetLogLevel(ctx context.Context, level string) error
// Snapshot returns if this version is a snapshot.
Snapshot() bool
// Version returns the version for this Agent.
Version() string
// Unprivileged returns true when this Agent is running unprivileged.
Unprivileged() bool
}
// AgentInfo is a collection of information about agent.
type AgentInfo struct {
agentID string
logLevel string
unprivileged bool
// esHeaders will be injected into the headers field of any elasticsearch
// output created by this agent (see component.toIntermediate).
esHeaders map[string]string
}
// NewAgentInfoWithLog creates a new agent information.
// In case when agent ID was already created it returns,
// this created ID otherwise it generates
// new unique identifier for agent.
// If agent config file does not exist it gets created.
// Initiates log level to predefined value.
func NewAgentInfoWithLog(ctx context.Context, level string, createAgentID bool) (*AgentInfo, error) {
agentInfo, err := loadAgentInfoWithBackoff(ctx, false, level, createAgentID)
if err != nil {
return nil, err
}
isRoot, err := utils.HasRoot()
if err != nil {
return nil, fmt.Errorf("failed to determine root/Administrator: %w", err)
}
return &AgentInfo{
agentID: agentInfo.ID,
logLevel: agentInfo.LogLevel,
unprivileged: !isRoot,
esHeaders: agentInfo.Headers,
}, nil
}
// NewAgentInfo creates a new agent information.
// In case when agent ID was already created it returns,
// this created ID otherwise it generates
// new unique identifier for agent.
// If agent config file does not exist it gets created.
func NewAgentInfo(ctx context.Context, createAgentID bool) (*AgentInfo, error) {
return NewAgentInfoWithLog(ctx, defaultLogLevel, createAgentID)
}
// LogLevel retrieves a log level.
func (i *AgentInfo) LogLevel() string {
if i.logLevel == "" {
return logger.DefaultLogLevel.String()
}
return i.logLevel
}
// SetLogLevel updates log level of agent.
func (i *AgentInfo) SetLogLevel(ctx context.Context, level string) error {
if err := updateLogLevel(ctx, level); err != nil {
return err
}
i.logLevel = level
return nil
}
// ReloadID reloads agent info ID from configuration file.
func (i *AgentInfo) ReloadID(ctx context.Context) error {
newInfo, err := NewAgentInfoWithLog(ctx, i.logLevel, false)
if err != nil {
return err
}
i.agentID = newInfo.agentID
return nil
}
// AgentID returns an agent identifier.
func (i *AgentInfo) AgentID() string {
return i.agentID
}
// Version returns the version for this Agent.
func (*AgentInfo) Version() string {
return release.Version()
}
// Snapshot returns if this version is a snapshot.
func (*AgentInfo) Snapshot() bool {
return release.Snapshot()
}
// Headers returns custom headers used to communicate with elasticsearch.
func (i *AgentInfo) Headers() map[string]string {
return i.esHeaders
}
// Unprivileged returns true when this Agent is running unprivileged.
func (i *AgentInfo) Unprivileged() bool {
return i.unprivileged
}