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

223 lines
6.2 KiB
JavaScript
Raw Normal View History

/*
* Wazuh app - Agents preview controller
2019-01-14 16:36:47 +00:00
* 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.
*/
2018-09-10 08:32:49 +00:00
import * as FileSaver from '../../services/file-saver';
import { timefilter } from 'ui/timefilter';
2018-11-02 18:59:51 +00:00
export class AgentsPreviewController {
/**
2018-12-13 10:02:53 +00:00
* Class constructor
* @param {Object} $scope
* @param {Object} genericReq
* @param {Object} appState
* @param {Object} $location
* @param {Object} errorHandler
* @param {Object} csvReq
* @param {Object} shareAgent
* @param {Object} wzTableFilter
*/
2018-09-11 09:52:54 +00:00
constructor(
$scope,
genericReq,
appState,
$location,
errorHandler,
csvReq,
shareAgent,
wzTableFilter,
commonData
2018-09-11 09:52:54 +00:00
) {
this.$scope = $scope;
this.genericReq = genericReq;
this.appState = appState;
this.$location = $location;
this.errorHandler = errorHandler;
this.csvReq = csvReq;
this.shareAgent = shareAgent;
this.wzTableFilter = wzTableFilter;
this.commonData = commonData;
2018-09-10 08:32:49 +00:00
}
2018-12-12 12:07:30 +00:00
/**
* On controller loads
*/
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-12-18 15:19:05 +00:00
if ((loc || {}).agent && (loc || {}).agent !== '000') {
this.commonData.setTimefilter(timefilter.getTime());
2018-09-28 13:23:47 +00:00
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.osPlatforms = [];
this.versions = [];
this.groups = [];
this.nodes = [];
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
});
2019-03-18 12:32:29 +00:00
this.$scope.$on('wazuhFetched', (ev, parameters) => {
this.$scope.showNoAgents = !parameters.items.length > 0 && !parameters.filters.length;
});
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-13 10:02:53 +00:00
* Searches by a query and term
* @param {String} query
* @param {String} search
*/
2018-12-04 09:27:59 +00:00
query(query, search) {
this.$scope.$broadcast('wazuhQuery', { query, search });
2018-11-02 07:42:47 +00:00
}
/**
2018-12-13 10:02:53 +00:00
* Selects an agent
* @param {String} agent
*/
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-12-13 10:02:53 +00:00
* Exports the table in CSV format
*/
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-12-12 12:07:30 +00:00
/**
* On controller loads
*/
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 = {
2019-03-05 10:46:01 +00:00
name: [],
2018-12-13 10:02:53 +00:00
status: ['Active', 'Disconnected', 'Never connected'],
group: unique.groups,
version: unique.versions,
'os.platform': unique.osPlatforms.map(x => x.platform),
'os.version': unique.osPlatforms.map(x => x.version),
2018-12-13 10:02:53 +00:00
'os.name': unique.osPlatforms.map(x => x.name)
};
2019-01-17 09:05:47 +00:00
if (clusterInfo.status === 'enabled' && unique.nodes) {
this.searchBarModel.node_name = unique.nodes;
}
2019-01-17 09:05:47 +00:00
2018-12-13 10:02:53 +00:00
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
}
reloadList() {
2019-02-06 08:55:25 +00:00
this.$scope.$broadcast('wazuhSearch', { term: '' });
}
2018-09-11 09:52:54 +00:00
}