-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcollector.go
383 lines (317 loc) · 10.5 KB
/
collector.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
package collector
import (
"context"
"fmt"
"net"
"strconv"
"strings"
"sync"
"github.com/devzero-inc/oda/client"
gen "github.com/devzero-inc/oda/gen/api/v1"
"github.com/devzero-inc/oda/process"
"github.com/devzero-inc/oda/util"
"github.com/rs/zerolog"
"time"
)
// TODO move this to /var/run or other appropriate location based on OS,
// TODO /var/run has issues with permisisons os have to explore a bit more.
const SocketPath = "/tmp/oda.socket"
// Collector collects command and system information
type Collector struct {
socketPath string
client *client.Client
logger zerolog.Logger
excludeRegex string
excludeCommands []string
collectionConfig collectionConfig
authConfig AuthConfig
protoAuthConfig *gen.Auth
intervalConfig IntervalConfig
}
// IntervalConfig contains the configuration for the collection intervals
type IntervalConfig struct {
ProcessInterval time.Duration
CommandInterval time.Duration
CommandIntervalMultiplier float64
MaxConcurrentCommands int
MaxDuration time.Duration
}
// AuthConfig contains the configuration for the command processing and authentication
type AuthConfig struct {
TeamID string
UserID string
WorkspaceID string
UserEmail string
}
// collectionConfig contains the configuration for the collection process
type collectionConfig struct {
// ongoingCommands is a map of currently running commands
ongoingCommands map[string]Command
// collectionMutex is a mutex to protect the ongoingCommands map
collectionMutex sync.Mutex
// activeCommandsCounter is a counter for the number of active commands
activeCommandsCounter int
// collectionContext is the context for the collection process
collectionContext context.Context
// collectionCancelFunc is the cancel function for the collection context
collectionCancelFunc context.CancelFunc
// isCollectionRunning is a flag to indicate if the collection is running
isCollectionRunning bool
// process is the system process collector
process process.SystemProcess
}
// NewCollector creates a new collector instance
func NewCollector(socketPath string, client *client.Client, logger zerolog.Logger, config IntervalConfig, auth AuthConfig, excludeRegex string, excludeCommands []string, process process.SystemProcess) *Collector {
collector := &Collector{
socketPath: socketPath,
client: client,
logger: logger,
collectionConfig: collectionConfig{
ongoingCommands: make(map[string]Command),
process: process,
},
intervalConfig: config,
authConfig: auth,
excludeRegex: excludeRegex,
excludeCommands: excludeCommands,
}
if auth.TeamID != "" && auth.UserEmail != "" {
collector.protoAuthConfig = &gen.Auth{
UserId: auth.UserID,
TeamId: auth.TeamID,
WorkspaceId: &auth.WorkspaceID,
UserEmail: auth.UserEmail,
}
}
return collector
}
// Collect starts the collection of command and system information
func (c *Collector) Collect() {
c.logger.Info().Msg("Collecting command and system information")
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
c.collectSystemInformation(ctx, c.intervalConfig.ProcessInterval, 3, c.intervalConfig.MaxDuration)
}()
wg.Add(1)
go func() {
defer wg.Done()
if err := c.collectCommandInformation(); err != nil {
c.logger.Error().Err(err).Msg("Failed to collect command information")
cancel()
}
}()
wg.Wait()
c.logger.Info().Msg("Collection stopped")
}
// collectSystemInformation uses exponential backoff for intervals between collections.
func (c *Collector) collectSystemInformation(ctx context.Context, initialDuration time.Duration, increaseFactor float64, maxDuration time.Duration) {
currentDuration := initialDuration
for {
select {
case <-ctx.Done():
c.logger.Debug().Msg("Shutting down collection of system information")
return
case <-time.After(currentDuration):
// Perform the collection on each tick
if err := c.collectOnce(); err != nil {
c.logger.Error().Err(err).Msg("Failed to collect system information")
}
// Calculate the next interval with exponential backoff
currentDuration = time.Duration(float64(currentDuration) * increaseFactor)
if currentDuration > maxDuration {
currentDuration = maxDuration
}
c.logger.Debug().Msgf("Next collection in %s", currentDuration)
}
}
}
func (c *Collector) collectOnce() error {
c.logger.Debug().Msg("Collecting process")
processes, err := c.collectionConfig.process.Collect()
if err != nil {
c.logger.Err(err).Msg("Failed to collect processes")
return err
}
if err := process.InsertProcesses(processes); err != nil {
c.logger.Error().Err(err).Msg("Failed to insert processes")
}
if c.client != nil {
var processMetrics []*gen.Process
for _, p := range processes {
processMetrics = append(
processMetrics,
process.MapProcessToProto(p),
)
}
go func() {
if err := c.client.SendProcesses(processMetrics, c.protoAuthConfig); err != nil {
c.logger.Error().Err(err).Msg("Failed to send processes")
}
}()
}
return nil
}
func (c *Collector) onStartCommand() {
c.collectionConfig.collectionMutex.Lock()
defer c.collectionConfig.collectionMutex.Unlock()
// Perform initial collection for every command
if err := c.collectOnce(); err != nil {
c.logger.Error().Err(err).Msg("Failed to collect system information")
}
c.collectionConfig.activeCommandsCounter++
// If the collection is not running, start it with a timeout
if !c.collectionConfig.isCollectionRunning {
c.logger.Debug().Msg("Starting collection")
c.collectionConfig.collectionContext, c.collectionConfig.collectionCancelFunc =
context.WithTimeout(context.Background(), c.intervalConfig.MaxDuration)
go c.collectSystemInformation(
c.collectionConfig.collectionContext,
c.intervalConfig.CommandInterval,
c.intervalConfig.CommandIntervalMultiplier,
c.intervalConfig.MaxDuration,
)
c.collectionConfig.isCollectionRunning = true
}
}
func (c *Collector) onEndCommand() {
c.collectionConfig.collectionMutex.Lock()
defer c.collectionConfig.collectionMutex.Unlock()
c.collectionConfig.activeCommandsCounter--
// If there are no more active commands, stop the collection
if c.collectionConfig.activeCommandsCounter == 0 && c.collectionConfig.isCollectionRunning {
c.logger.Debug().Msg("Stopping collection")
c.collectionConfig.collectionCancelFunc()
c.collectionConfig.isCollectionRunning = false
}
}
func (c *Collector) collectCommandInformation() error {
if err := util.Fs.RemoveAll(SocketPath); err != nil {
c.logger.Error().Err(err).Msg("Failed to clean up existing socket")
return err
}
listener, err := net.Listen("unix", SocketPath)
if err != nil {
c.logger.Error().Err(err).Msg("Failed to listen on UNIX socket")
return err
}
defer listener.Close()
// Limit the number of concurrent goroutines handling connections
semaphore := make(chan struct{}, c.intervalConfig.MaxConcurrentCommands)
// Context for graceful shutdown
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go func() {
<-ctx.Done() // Wait for context cancellation
listener.Close()
}()
for {
conn, err := listener.Accept()
if err != nil {
select {
case <-ctx.Done():
// If the context is canceled, stop accepting new connections
return nil
default:
c.logger.Error().Err(err).Msg("Failed to accept connection")
continue
}
}
semaphore <- struct{}{} // Acquire
go func(conn net.Conn) {
defer func() {
<-semaphore // Release
}()
if err := c.handleSocketCollection(conn); err != nil {
c.logger.Error().Err(err).Msg("Error handling socket collection")
}
}(conn)
}
}
func (c *Collector) handleSocketCollection(con net.Conn) error {
defer con.Close()
var buf [1024]byte
n, err := con.Read(buf[:])
if err != nil {
c.logger.Error().Err(err).Msg("Error reading from socket")
return err
}
data := string(buf[:n])
parts := strings.Split(data, "|")
c.logger.Debug().Msgf("Received: %s", string(buf[:n]))
if len(parts) != 8 {
c.logger.Error().Msg("Invalid command format")
return fmt.Errorf("invalid command format")
}
if parts[0] == "start" {
if err := c.handleStartCommand(parts); err != nil {
c.logger.Error().Err(err).Msg("Error handling start command")
}
} else if parts[0] == "end" {
if err := c.handleEndCommand(parts); err != nil {
c.logger.Error().Err(err).Msg("Error handling end command")
}
} else {
c.logger.Error().Msg("Invalid command format")
return err
}
return nil
}
func (c *Collector) handleStartCommand(parts []string) error {
if !IsCommandAcceptable(parts[1], c.excludeRegex, c.excludeCommands) {
c.logger.Debug().Msg("Command is not acceptable")
return fmt.Errorf("command is not acceptable")
}
c.logger.Debug().Msgf("Parsing command: %s", parts[0])
repo, err := util.GetRepoNameFromConfig(parts[2])
if err != nil {
c.logger.Error().Err(err).Msg("Failed to get repository name")
}
pid, _ := strconv.ParseInt(parts[5], 10, 64)
command := Command{
Category: ParseCommand(parts[1]),
Command: parts[1],
Directory: parts[2],
User: parts[3],
StartTime: time.Now().UnixMilli(), // TODO: there are some issues with sending time through shell because of ms support on MAC, explore more
Repository: repo,
PID: pid,
}
c.collectionConfig.ongoingCommands[parts[4]] = command
c.onStartCommand()
return nil
}
func (c *Collector) handleEndCommand(parts []string) error {
if !IsCommandAcceptable(parts[1], c.excludeRegex, c.excludeCommands) {
c.logger.Debug().Msg("Command is not acceptable")
return fmt.Errorf("command is not acceptable")
}
c.logger.Debug().Msgf("Parsing command: %s", parts[0])
if command, exists := c.collectionConfig.ongoingCommands[parts[4]]; exists {
command.EndTime = time.Now().UnixMilli()
command.ExecutionTime = command.EndTime - command.StartTime
command.Result = parts[6]
command.Status = parts[7]
c.logger.Debug().Msgf("Command: %+v", command)
if err := InsertCommand(command); err != nil {
c.logger.Error().Err(err).Msg("Failed to insert command")
return err
}
delete(c.collectionConfig.ongoingCommands, parts[4])
c.onEndCommand()
if c.client != nil {
go func() {
if err := c.client.SendCommands([]*gen.Command{MapCommandToProto(command)}, c.protoAuthConfig); err != nil {
c.logger.Error().Err(err).Msg("Failed to send command")
}
}()
}
} else {
c.logger.Error().Msg("Matching start command not found")
return fmt.Errorf("matching start command not found")
}
return nil
}