forked from DataDog/datadog-agent
-
Notifications
You must be signed in to change notification settings - Fork 2
/
agent.go
146 lines (126 loc) · 4.62 KB
/
agent.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
// 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-present Datadog, Inc.
// Package agent implements the api endpoints for the `/agent` prefix.
// This group of endpoints is meant to provide high-level functionalities
// at the agent level.
package agent
import (
"encoding/json"
"net/http"
"github.com/gorilla/mux"
"github.com/StackVista/stackstate-agent/cmd/agent/common"
"github.com/StackVista/stackstate-agent/cmd/agent/common/signals"
"github.com/StackVista/stackstate-agent/pkg/config"
settingshttp "github.com/StackVista/stackstate-agent/pkg/config/settings/http"
"github.com/StackVista/stackstate-agent/pkg/flare"
secagent "github.com/StackVista/stackstate-agent/pkg/security/agent"
"github.com/StackVista/stackstate-agent/pkg/status"
"github.com/StackVista/stackstate-agent/pkg/status/health"
"github.com/StackVista/stackstate-agent/pkg/util"
"github.com/StackVista/stackstate-agent/pkg/util/log"
)
// Agent handles REST API calls
type Agent struct {
runtimeAgent *secagent.RuntimeSecurityAgent
}
// NewAgent returns a new Agent
func NewAgent(runtimeAgent *secagent.RuntimeSecurityAgent) *Agent {
return &Agent{
runtimeAgent: runtimeAgent,
}
}
// SetupHandlers adds the specific handlers for /agent endpoints
func (a *Agent) SetupHandlers(r *mux.Router) {
r.HandleFunc("/version", common.GetVersion).Methods("GET")
r.HandleFunc("/flare", a.makeFlare).Methods("POST")
r.HandleFunc("/hostname", a.getHostname).Methods("GET")
r.HandleFunc("/stop", a.stopAgent).Methods("POST")
r.HandleFunc("/status", a.getStatus).Methods("GET")
r.HandleFunc("/status/health", a.getHealth).Methods("GET")
r.HandleFunc("/config", settingshttp.Server.GetFull("")).Methods("GET")
r.HandleFunc("/config/list-runtime", settingshttp.Server.ListConfigurable).Methods("GET")
r.HandleFunc("/config/{setting}", settingshttp.Server.GetValue).Methods("GET")
r.HandleFunc("/config/{setting}", settingshttp.Server.SetValue).Methods("POST")
}
func (a *Agent) stopAgent(w http.ResponseWriter, r *http.Request) {
signals.Stopper <- true
w.Header().Set("Content-Type", "application/json")
j, err := json.Marshal("")
if err != nil {
log.Warnf("Failed to serialize json: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Write(j)
}
func (a *Agent) getHostname(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
hname, err := util.GetHostname(r.Context())
if err != nil {
log.Warnf("Error getting hostname: %s\n", err) // or something like this
hname = ""
}
j, err := json.Marshal(hname)
if err != nil {
log.Warnf("Failed to serialize json: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Write(j)
}
func (a *Agent) getStatus(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
s, err := status.GetStatus()
if err != nil {
log.Errorf("Error getting status. Error: %v, Status: %v", err, s)
body, _ := json.Marshal(map[string]string{"error": err.Error()})
http.Error(w, string(body), 500)
return
}
if a.runtimeAgent != nil {
s["runtimeSecurityStatus"] = a.runtimeAgent.GetStatus()
}
jsonStats, err := json.Marshal(s)
if err != nil {
log.Errorf("Error marshalling status. Error: %v, Status: %v", err, s)
body, _ := json.Marshal(map[string]string{"error": err.Error()})
http.Error(w, string(body), 500)
return
}
w.Write(jsonStats)
}
func (a *Agent) getHealth(w http.ResponseWriter, r *http.Request) {
h := health.GetReady()
if len(h.Unhealthy) > 0 {
log.Debugf("Healthcheck failed on: %v", h.Unhealthy)
}
jsonHealth, err := json.Marshal(h)
if err != nil {
log.Errorf("Error marshalling status. Error: %v, Status: %v", err, h)
body, _ := json.Marshal(map[string]string{"error": err.Error()})
http.Error(w, string(body), 500)
return
}
w.Write(jsonHealth)
}
func (a *Agent) makeFlare(w http.ResponseWriter, r *http.Request) {
log.Infof("Making a flare")
w.Header().Set("Content-Type", "application/json")
logFile := config.Datadog.GetString("security_agent.log_file")
var runtimeAgentStatus map[string]interface{}
if a.runtimeAgent != nil {
runtimeAgentStatus = a.runtimeAgent.GetStatus()
}
filePath, err := flare.CreateSecurityAgentArchive(false, logFile, runtimeAgentStatus)
if err != nil || filePath == "" {
if err != nil {
log.Errorf("The flare failed to be created: %s", err)
} else {
log.Warnf("The flare failed to be created")
}
http.Error(w, err.Error(), 500)
}
w.Write([]byte(filePath))
}