wazuh-kibana-app/public/controllers/agent/agents-preview.js

176 lines
5.3 KiB
JavaScript
Raw Normal View History

/*
* Wazuh app - Agents preview controller
* Copyright (C) 2018 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.
*/
2018-09-10 08:32:49 +00:00
import * as FileSaver from '../../services/file-saver';
2018-11-02 18:59:51 +00:00
export class AgentsPreviewController {
2018-09-11 09:52:54 +00:00
constructor(
$scope,
genericReq,
appState,
$location,
errorHandler,
csvReq,
shareAgent,
wzTableFilter
) {
this.$scope = $scope;
this.genericReq = genericReq;
this.appState = appState;
this.$location = $location;
this.errorHandler = errorHandler;
this.csvReq = csvReq;
this.shareAgent = shareAgent;
this.wzTableFilter = wzTableFilter;
2018-09-10 08:32:49 +00:00
}
2018-09-11 09:52:54 +00:00
$onInit() {
2018-11-02 07:42:47 +00:00
this.init = true;
2018-09-18 15:30:45 +00:00
const loc = this.$location.search();
2018-09-28 13:23:47 +00:00
if (loc && loc.agent && loc.agent !== '000')
return this.showAgent({ id: loc.agent });
2018-09-18 15:30:45 +00:00
2018-11-02 07:42:47 +00:00
this.isClusterEnabled =
2018-09-11 09:52:54 +00:00
this.appState.getClusterInfo() &&
this.appState.getClusterInfo().status === 'enabled';
2018-11-02 07:42:47 +00:00
this.loading = true;
this.status = 'all';
this.osPlatform = 'all';
this.version = 'all';
this.osPlatforms = [];
this.versions = [];
this.groups = [];
this.nodes = [];
this.node_name = 'all';
2018-11-02 17:23:00 +00:00
this.selectedGroup = 'all';
2018-11-02 07:42:47 +00:00
this.mostActiveAgent = {
2018-09-11 09:52:54 +00:00
name: '',
id: ''
};
// Load URL params
if (loc && loc.tab) {
this.submenuNavItem = loc.tab;
2018-09-11 09:52:54 +00:00
}
// Watcher for URL params
this.$scope.$watch('submenuNavItem', () => {
2018-11-02 07:42:47 +00:00
this.$location.search('tab', this.submenuNavItem);
2018-09-11 09:52:54 +00:00
});
2018-11-02 07:42:47 +00:00
this.init = false;
2018-09-11 09:52:54 +00:00
//Load
this.load();
}
2018-12-04 09:27:59 +00:00
query(query, search) {
this.$scope.$broadcast('wazuhQuery', { query, search });
}
2018-09-11 09:52:54 +00:00
showAgent(agent) {
this.shareAgent.setAgent(agent);
this.$location.path('/agents');
}
2018-09-10 08:32:49 +00:00
2018-09-11 09:52:54 +00:00
async downloadCsv() {
2018-09-10 08:32:49 +00:00
try {
2018-09-11 09:52:54 +00:00
this.errorHandler.info(
'Your download should begin automatically...',
'CSV'
);
const currentApi = JSON.parse(this.appState.getCurrentAPI()).id;
const output = await this.csvReq.fetch(
2018-09-10 08:32:49 +00:00
'/agents',
currentApi,
2018-09-11 09:52:54 +00:00
this.wzTableFilter.get()
2018-09-10 08:32:49 +00:00
);
const blob = new Blob([output], { type: 'text/csv' }); // eslint-disable-line
FileSaver.saveAs(blob, 'agents.csv');
return;
} catch (error) {
2018-09-11 09:52:54 +00:00
this.errorHandler.handle(error, 'Download CSV');
}
2018-09-10 08:32:49 +00:00
return;
2018-09-11 09:52:54 +00:00
}
2018-09-10 08:32:49 +00:00
2018-09-11 09:52:54 +00:00
async load() {
2018-09-10 08:32:49 +00:00
try {
2018-09-11 09:52:54 +00:00
const api = JSON.parse(this.appState.getCurrentAPI()).id;
const clusterInfo = this.appState.getClusterInfo();
2018-09-10 08:32:49 +00:00
const firstUrlParam =
clusterInfo.status === 'enabled' ? 'cluster' : 'manager';
const secondUrlParam = clusterInfo[firstUrlParam];
2018-09-11 09:52:54 +00:00
const pattern = this.appState.getCurrentPattern();
2018-09-10 08:32:49 +00:00
const data = await Promise.all([
2018-11-02 17:23:00 +00:00
this.genericReq.request('GET', '/api/agents-unique/' + api, {}),
2018-09-11 09:52:54 +00:00
this.genericReq.request(
2018-09-10 08:32:49 +00:00
'GET',
2018-10-01 07:56:50 +00:00
`/elastic/top/${firstUrlParam}/${secondUrlParam}/agent.name/${pattern}`
2018-09-10 08:32:49 +00:00
)
]);
const [agentsUnique, agentsTop] = data;
const unique = agentsUnique.data.result;
this.searchBarModel = {
2018-12-05 08:37:05 +00:00
'status': ['Active', 'Disconnected', 'Never connected'],
'group': unique.groups,
'node_name': unique.nodes,
'version': unique.versions,
'os.platform': unique.osPlatforms.map(x => x.platform),
'os.version': unique.osPlatforms.map(x => x.version),
'os.name': unique.osPlatforms.map(x => x.name),
};
this.searchBarModel['os.name'] = Array.from(new Set(this.searchBarModel['os.name']));
this.searchBarModel['os.version'] = Array.from(new Set(this.searchBarModel['os.version']));
this.searchBarModel['os.platform'] = Array.from(new Set(this.searchBarModel['os.platform']));
2018-11-02 07:42:47 +00:00
this.groups = unique.groups;
this.nodes = unique.nodes.map(item => ({ id: item }));
this.versions = unique.versions.map(item => ({ id: item }));
this.osPlatforms = unique.osPlatforms;
this.lastAgent = unique.lastAgent;
this.agentsCountActive = unique.summary.agentsCountActive;
2018-11-02 17:23:00 +00:00
this.agentsCountDisconnected = unique.summary.agentsCountDisconnected;
this.agentsCountNeverConnected = unique.summary.agentsCountNeverConnected;
2018-11-02 07:42:47 +00:00
this.agentsCountTotal = unique.summary.agentsCountTotal;
this.agentsCoverity = unique.summary.agentsCoverity;
2018-09-10 08:32:49 +00:00
if (agentsTop.data.data === '') {
2018-11-02 07:42:47 +00:00
this.mostActiveAgent.name = this.appState.getClusterInfo().manager;
this.mostActiveAgent.id = '000';
2018-09-10 08:32:49 +00:00
} else {
2018-11-02 07:42:47 +00:00
this.mostActiveAgent.name = agentsTop.data.data;
2018-09-11 09:52:54 +00:00
const info = await this.genericReq.request(
2018-09-10 08:32:49 +00:00
'GET',
2018-10-01 07:56:50 +00:00
`/elastic/top/${firstUrlParam}/${secondUrlParam}/agent.id/${pattern}`
2018-09-10 08:32:49 +00:00
);
2018-11-02 07:42:47 +00:00
if (info.data.data === '' && this.mostActiveAgent.name !== '') {
this.mostActiveAgent.id = '000';
2018-09-10 08:32:49 +00:00
} else {
2018-11-02 07:42:47 +00:00
this.mostActiveAgent.id = info.data.data;
}
2018-09-10 08:32:49 +00:00
}
2016-09-16 18:44:04 +00:00
2018-11-02 07:42:47 +00:00
this.loading = false;
2018-09-11 09:52:54 +00:00
if (!this.$scope.$$phase) this.$scope.$digest();
2018-09-10 08:32:49 +00:00
return;
} catch (error) {
2018-09-11 09:52:54 +00:00
this.errorHandler.handle(error, 'Agents Preview');
}
2018-09-10 08:32:49 +00:00
return;
2018-09-11 09:52:54 +00:00
}
}