-
Notifications
You must be signed in to change notification settings - Fork 25
/
agentsCtrl.js
260 lines (245 loc) · 8.08 KB
/
agentsCtrl.js
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
/*
* Wazuh app - Agents controller
* Copyright (C) 2015-2019 Wazuh, Inc.
*
* This program is free software you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation either version 2 of the License, or
* (at your option) any later version.
*
* Find more information about this on the LICENSE file.
*/
define([
'../../module',
'../../../services/visualizations/search/search-handler',
'FileSaver'
], function(app, SearchHandler) {
'use strict'
class Agents {
/**
* Class constructor
* @param {Object} $urlTokenModel
* @param {Object} $scope
* @param {Object} $currentDataService
* @param {Object} $state
* @param {Object} $notificationService
* @param {Object} $requestService
* @param {Object} agentData
*/
constructor(
$urlTokenModel,
$scope,
$currentDataService,
$state,
$notificationService,
$requestService,
$csvRequestService,
$tableFilterService,
agentData,
$mdDialog,
$groupHandler,
$dateDiffService
) {
this.scope = $scope
this.submittedTokenModel = $urlTokenModel.getSubmittedTokenModel()
this.submittedTokenModel.set('activeAgentToken', '-')
this.currentDataService = $currentDataService
this.api = this.currentDataService.getApi()
this.apiReq = $requestService.apiReq
this.state = $state
this.notification = $notificationService
this.currentClusterInfo = this.currentDataService.getClusterInfo()
this.filters = this.currentDataService.getSerializedFilters()
this.csvReq = $csvRequestService
this.wzTableFilter = $tableFilterService
this.$mdDialog = $mdDialog
this.groupHandler = $groupHandler
this.setBrowserOffset = $dateDiffService.setBrowserOffset
try {
const parsedResult = agentData.map(item =>
item && item.data && item.data.data ? item.data.data : false
)
let [
summary,
lastAgent,
platforms,
versions,
nodes,
groups
] = parsedResult
this.scope.agentsCountActive = summary.Active - 1
this.scope.lastAgent = lastAgent.items[0]
? lastAgent.items[0]
: 'Unknown'
const os = platforms
? platforms.items.map(item => item.os).filter(item => !!item)
: false
versions = versions
? versions.items.map(item => item.version).filter(item => !!item)
: false
nodes =
nodes && nodes.items
? nodes.items.map(item => item['node_name']).filter(item => !!item)
: false
groups = groups
? groups.items.map(item => item.name).filter(item => !!item)
: false
this.scope.agentsCountDisconnected = summary.Disconnected
this.scope.agentsCountNeverConnected = summary['Never connected']
const agentsCountTotal = summary.Total - 1
this.scope.agentsCoverity = agentsCountTotal
? (this.scope.agentsCountActive / agentsCountTotal) * 100
: 0
this.scope.searchBarModel = {
name: [],
status: ['Active', 'Disconnected', 'Never connected'],
group: groups ? groups.sort((a, b) => { return a.toString().localeCompare(b.toString()) }) : [],
version: versions ? versions.sort((a, b) => { return a.toString().localeCompare(b.toString(), undefined, { numeric: true, sensitivity: 'base' }) }) : [],
'os.platform': os ? os.map(x => x.platform).sort((a, b) => { return a.toString().localeCompare(b.toString()) }) : [],
'os.version': os ? os.map(x => x.version).sort((a, b) => { return a.toString().localeCompare(b.toString(), undefined, { numeric: true, sensitivity: 'base' }) }) : [],
'os.name': os ? os.map(x => x.name).sort((a, b) => { return a.toString().localeCompare(b.toString()) }) : []
}
if (this.clusterInfo && this.clusterInfo.status === 'enabled') {
this.scope.searchBarModel.node_name = nodes || []
}
} catch (error) {} //eslint-disable-line
this.topAgent = new SearchHandler(
'searchTopAgent',
`index=wazuh ${this.filters} NOT agent.id=000 | top agent.name`,
'activeAgentToken',
'$result.agent.name$',
'mostActiveAgent',
this.submittedTokenModel,
this.scope,
true,
'loadingSearch'
)
this.scope.$applyAsync()
}
/**
* On controller loads
*/
$onInit() {
this.scope.query = (query, search) => this.query(query, search)
this.scope.showAgent = agent => this.showAgent(agent)
this.scope.isClusterEnabled =
this.clusterInfo && this.clusterInfo.status === 'enabled'
this.scope.status = 'all'
this.scope.osPlatform = 'all'
this.scope.version = 'all'
this.scope.node_name = 'all'
this.scope.versionModel = 'all'
this.scope.downloadCsv = () => this.downloadCsv()
this.scope.$on('$destroy', () => {
this.topAgent.destroy()
})
this.scope.reloadList = () => this.reloadList()
this.scope.offsetTimestamp = (text, time) => {
try {
return text + this.setBrowserOffset(time)
} catch (error) {
return ''
}
}
this.scope.loadCharts = id => {
setTimeout(() => {
const chart = new Chart(document.getElementById(id), {
type: 'doughnut',
data: {
labels: ['Active', 'Disconected', 'Never connected'],
datasets: [
{
backgroundColor: ['#46BFBD', '#F7464A', '#949FB1'],
data: [
this.scope.agentsCountActive,
this.scope.agentsCountDisconnected,
this.scope.agentsCountNeverConnected
]
}
]
},
options: {
cutoutPercentage: 85,
legend: {
display: true,
position: 'right'
},
tooltips: {
displayColors: false
}
}
})
chart.update()
}, 250)
}
}
/**
* Exports the table in CSV format
*/
async downloadCsv() {
try {
this.notification.showSimpleToast(
'Your download should begin automatically...'
)
const currentApi = this.api['_key']
const output = await this.csvReq.fetch(
'/agents',
currentApi,
this.wzTableFilter.get()
)
const blob = new Blob([output], { type: 'text/csv' }) // eslint-disable-line
saveAs(blob, 'agents.csv') // eslint-disable-line
return
} catch (error) {
this.notification.showErrorToast('Error downloading CSV')
}
return
}
/**
* Launches the query
* @param {String} query
* @param {String} search
*/
query(query, search) {
this.scope.$broadcast('wazuhQuery', { query, search })
}
/**
* Selects an agent
* @param {String} agent
*/
async showAgent(agent) {
try {
if (agent) {
const agentName = typeof agent === 'object' ? agent.name : agent
const agentInfo = await this.apiReq(`/agents`, { name: agentName })
if (
!agentInfo ||
!agentInfo.data ||
!agentInfo.data.data ||
agentInfo.data.error
) {
throw Error('Error fetching agent data')
}
if (agentInfo.data.data.items[0].id !== '000') {
this.state.go(`agent-overview`, {
id: agentInfo.data.data.items[0].id
})
}
} else {
throw Error('Cannot fetch agent name')
}
} catch (err) {
this.notification.showErrorToast(
err.message || 'Error fetching agent data'
)
}
}
/**
* Reload list of agents
*/
reloadList() {
this.scope.$broadcast('wazuhSearch', { term: '' })
}
}
app.controller('agentsCtrl', Agents)
})