forked from DataDog/datadog-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.go
103 lines (83 loc) · 2.71 KB
/
run.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
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-2019 Datadog, Inc.
package app
import (
"fmt"
"syscall"
_ "expvar" // Blank import used because this isn't directly used in this file
_ "net/http/pprof" // Blank import used because this isn't directly used in this file
"os"
"os/signal"
"github.com/ninnemana/datadog-agent/cmd/agent/common/signals"
"github.com/ninnemana/datadog-agent/pkg/util/log"
"github.com/spf13/cobra"
)
var (
runCmd = &cobra.Command{
Use: "run",
Short: "Run the Agent",
Long: `Runs the agent in the foreground`,
RunE: run,
}
)
var (
// flags variables
runForeground bool
pidfilePath string
)
// run the host metadata collector every 14400 seconds (4 hours)
const hostMetadataCollectorInterval = 14400
// run the agent checks metadata collector every 600 seconds (10 minutes)
const agentChecksMetadataCollectorInterval = 600
// run the resources metadata collector every 300 seconds (5 minutes) by default, configurable
const defaultResourcesMetadataCollectorInterval = 300
func init() {
// attach the command to the root
AgentCmd.AddCommand(runCmd)
// local flags
runCmd.Flags().StringVarP(&pidfilePath, "pidfile", "p", "", "path to the pidfile")
}
// Start the main loop
func run(cmd *cobra.Command, args []string) error {
defer func() {
StopAgent()
}()
// Setup a channel to catch OS signals
signalCh := make(chan os.Signal, 1)
signal.Notify(signalCh, os.Interrupt, syscall.SIGTERM)
// Make a channel to exit the function
stopCh := make(chan error)
go func() {
// Set up the signals async so we can Start the agent
select {
case <-signals.Stopper:
log.Info("Received stop command, shutting down...")
stopCh <- nil
case <-signals.ErrorStopper:
log.Critical("The Agent has encountered an error, shutting down...")
stopCh <- fmt.Errorf("shutting down because of an error")
case sig := <-signalCh:
log.Infof("Received signal '%s', shutting down...", sig)
stopCh <- nil
}
}()
// By default systemd redirects the stdout to journald. When journald is stopped or crashes we receive a SIGPIPE signal.
// Go ignores SIGPIPE signals unless it is when stdout or stdout is closed, in this case the agent is stopped.
// We never want the agent to stop upon receiving SIGPIPE, so we intercept the SIGPIPE signals and just discard them.
sigpipeCh := make(chan os.Signal, 1)
signal.Notify(sigpipeCh, syscall.SIGPIPE)
go func() {
for range sigpipeCh {
// do nothing
}
}()
if err := StartAgent(); err != nil {
return err
}
select {
case err := <-stopCh:
return err
}
}