-
Notifications
You must be signed in to change notification settings - Fork 122
/
newrelic-infra-ctl.go
104 lines (88 loc) · 2.39 KB
/
newrelic-infra-ctl.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
// Copyright 2020 New Relic Corporation. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
//go:generate goversioninfo
package main
import (
"context"
"flag"
"fmt"
"os"
"os/signal"
"runtime"
"syscall"
"github.com/newrelic/infrastructure-agent/pkg/ipc"
"github.com/newrelic/infrastructure-agent/pkg/config"
"github.com/newrelic/infrastructure-agent/pkg/ctl/sender"
"github.com/sirupsen/logrus"
)
var (
agentPID int
containerID string
apiVersion string
containerdNamespace string
containerRuntime string
)
func init() {
flag.IntVar(
&agentPID,
"pid",
0,
"New Relic infrastructure agent PID",
)
flag.StringVar(
&containerID,
"cid",
"",
"New Relic infrastructure agent container ID (Containerised agent)",
)
flag.StringVar(
&apiVersion,
"docker-api-version",
config.DefaultDockerApiVersion,
"Docker API version [Optional] (Containerised agent)",
)
flag.StringVar(
&containerdNamespace,
"containerd-namespace",
"default",
"Namespace in containerd where container is runing [Optional] (Containerised agent)",
)
flag.StringVar(
&containerRuntime,
"container-runtime",
sender.RuntimeDocker,
"Container runtime [Optional] ('docker' or 'containerd') (Containerised agent)",
)
}
func main() {
flag.Parse()
ctx, cancel := context.WithCancel(context.Background())
// Enables Control+C termination
go func() {
s := make(chan os.Signal, 1)
signal.Notify(s, syscall.SIGQUIT)
<-s
cancel()
}()
client, err := getClient()
if err != nil {
logrus.WithError(err).Fatal("Failed to initialize the notification client.")
}
// Default message is "enable verbose logging" to maintain backwards compatibility.
msg := ipc.EnableVerboseLogging
logrus.Debug("Sending message to agent: " + fmt.Sprint(msg))
if err := client.Notify(ctx, msg); err != nil {
logrus.WithError(err).Fatal("Error occurred while notifying the NRI Agent.")
}
logrus.Infof("Notification successfully sent to the NRI Agent with ID '%s'", client.GetID())
}
// getClient returns an agent notification client.
func getClient() (sender.Client, error) {
if runtime.GOOS == "windows" || agentPID != 0 {
return sender.NewClient(agentPID)
}
if containerID != "" {
return sender.NewContainerClient(apiVersion, containerdNamespace, containerID, containerRuntime)
}
return sender.NewAutoDetectedClient(apiVersion, containerdNamespace, containerRuntime)
}