forked from mrtc0/wazuh
/
agents.go
183 lines (157 loc) · 5.25 KB
/
agents.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
package wazuh
import (
"context"
"path"
)
type AgentInformationData struct {
TotalItems int `json:"totalItems"`
Items []AgentInformation `json:"items"`
}
type GetAllAgentsResponse struct {
Error int `json:"error"`
Data AgentInformationData `json:"data"`
}
type GetAnAgentResponse struct {
Error int `json:"error"`
Data AgentInformation `json::"data"`
}
type GetGroupsResponse struct {
Error int `json:"error"`
Data GroupData `json:"data"`
}
type AgentRestartResponse struct {
Error int `json:"error"`
Data AgentRestartData `json:"data"`
}
type AgentRestartData struct {
Msg string `json:"msg"`
AffectedAgents []string `json:"affectedAgents"`
}
type GroupData struct {
TotalItems int `json:"totalItems"`
Items []Group `json:"items"`
}
type Group struct {
Count int `json:"count"`
MergedSum string `json:"mergedSum"`
ConfigSum string `json:"configSum"`
Name string `json:"name"`
}
type Os struct {
Major string `json:"major"`
Name string `json:"name"`
Uname string `json:"uname"`
Platform string `json:"platform"`
Version string `json:"version"`
Codename string `json:"codename"`
Arch string `json:"arch"`
Minor string `json:"minor"`
}
type AgentInformation struct {
Status string `json:"status"`
Name string `json:"name"`
IP string `json:"ip"`
Manager string `json:"manager"`
NodeName string `json:"node_name"`
DateAdd string `json:"dateAdd"`
Version string `json:"version"`
LastKeepAlive string `json:"lastKeepAlive"`
Os Os `json:"os"`
ID string `json:"id"`
ConfigSum string `json:"configSum,omitempty"`
Group []string `json:"group,omitempty"`
MergedSum string `json:"mergedSum,omitempty"`
}
func GetAllAgentsRequest(ctx context.Context, client *Client, path string) (*GetAllAgentsResponse, error) {
response := &GetAllAgentsResponse{}
err := GetJson(ctx, client, path, response)
if err != nil {
return nil, err
}
return response, nil
}
func GetAnAgentRequest(ctx context.Context, client *Client, path string) (*GetAnAgentResponse, error) {
response := &GetAnAgentResponse{}
err := GetJson(ctx, client, path, response)
if err != nil {
return nil, err
}
return response, nil
}
// https://documentation.wazuh.com/current/user-manual/api/reference.html#get-all-agents
func (client *Client) GetAllAgents() (*[]AgentInformation, error) {
return client.GetAllAgentsContext(context.Background())
}
func (client *Client) GetAllAgentsContext(ctx context.Context) (*[]AgentInformation, error) {
response, err := GetAllAgentsRequest(ctx, client, "agents")
if err != nil {
return nil, err
}
return &response.Data.Items, nil
}
// https://documentation.wazuh.com/current/user-manual/api/reference.html#get-an-agent
func (client *Client) GetAnAgent(agentID string) (*AgentInformation, error) {
return client.GetAnAgentContext(context.Background(), agentID)
}
func (client *Client) GetAnAgentContext(ctx context.Context, agentID string) (*AgentInformation, error) {
response, err := GetAnAgentRequest(ctx, client, path.Join("agents", agentID))
if err != nil {
return nil, err
}
return &response.Data, nil
}
// https://documentation.wazuh.com/current/user-manual/api/reference.html#get-groups
func (client *Client) GetGroups() (*[]Group, error) {
return client.GetGroupsContext(context.Background())
}
func (client *Client) GetGroupsContext(ctx context.Context) (*[]Group, error) {
response, err := GetGroupsRequest(ctx, client, path.Join("agents", "groups"))
if err != nil {
return nil, err
}
return &response.Data.Items, nil
}
func GetGroupsRequest(ctx context.Context, client *Client, path string) (*GetGroupsResponse, error) {
response := &GetGroupsResponse{}
err := GetJson(ctx, client, path, response)
if err != nil {
return nil, err
}
return response, nil
}
func (client *Client) GetAgentsByGroup(groupId string) (*[]AgentInformation, error) {
return client.GetAgentsByGroupContext(context.Background(), groupId)
}
func (client *Client) GetAgentsByGroupContext(ctx context.Context, groupId string) (*[]AgentInformation, error) {
response, err := GetAgentsByGroupRequest(ctx, client, path.Join("agents", "groups", groupId))
if err != nil {
return nil, err
}
return &response.Data.Items, nil
}
func GetAgentsByGroupRequest(ctx context.Context, client *Client, path string) (*GetAllAgentsResponse, error) {
response := &GetAllAgentsResponse{}
err := GetJson(ctx, client, path, response)
if err != nil {
return nil, err
}
return response, nil
}
func (client *Client) RestartAgent(agentId string) (*AgentRestartData, error) {
return client.RestartAgentContext(context.Background(), agentId)
}
func (client *Client) RestartAgentContext(ctx context.Context, agentId string) (*AgentRestartData, error) {
response, err := RestartAgentRequest(ctx, client, path.Join("agents", agentId, "restart"))
if err != nil {
return nil, err
}
return &response.Data, nil
}
func RestartAgentRequest(ctx context.Context, client *Client, path string) (*AgentRestartResponse, error) {
response := &AgentRestartResponse{}
err := PutJson(ctx, client, path, []byte{}, response)
if err != nil {
return nil, err
}
return response, nil
}