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

262 lines
6.9 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,
2019-03-19 16:50:41 +00:00
commonData,
wazuhConfig,
2019-05-17 08:13:46 +00:00
$window,
timeService
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;
2019-03-19 16:50:41 +00:00
this.wazuhConfig = wazuhConfig;
this.errorInit = false;
this.$window = $window;
2019-05-17 08:13:46 +00:00
this.timeService = timeService;
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;
this.api = JSON.parse(this.appState.getCurrentAPI()).id;
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: ''
};
2019-08-05 06:17:40 +00:00
this.prevSearch = false;
2018-09-11 09:52:54 +00:00
// 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', evt => {
evt.stopPropagation();
2019-03-18 12:32:29 +00:00
});
2018-09-11 09:52:54 +00:00
2019-08-07 08:31:59 +00:00
this.registerAgentsProps = {
addNewAgent: flag => this.addNewAgent(flag)
};
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 });
2019-08-05 06:17:40 +00:00
this.prevSearch = search || false;
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 output = await this.csvReq.fetch(
2018-09-10 08:32:49 +00:00
'/agents',
this.api,
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 {
this.errorInit = false;
2019-03-19 16:50:41 +00:00
const configuration = this.wazuhConfig.getConfig();
this.$scope.adminMode = !!(configuration || {}).admin;
2018-09-11 09:52:54 +00:00
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([
this.genericReq.request('GET', '/api/agents-unique/' + this.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.summary = unique.summary;
2019-08-07 08:31:59 +00:00
if (!this.lastAgent || !this.lastAgent.id) {
this.addNewAgent(true);
}
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
}
} catch (error) {
this.errorInit = this.errorHandler.handle(error, false, false, true);
}
this.loading = false;
this.$scope.$applyAsync();
2018-09-10 08:32:49 +00:00
return;
2018-09-11 09:52:54 +00:00
}
2019-08-07 08:31:59 +00:00
addNewAgent(flag) {
this.addingNewAgent = flag;
2019-03-19 16:50:41 +00:00
}
reloadList() {
this.refreshAgentsStats();
}
async refreshAgentsStats() {
try {
2019-08-06 08:38:30 +00:00
const data = await this.genericReq.request(
'GET',
'/api/agents-unique/' + this.api,
{}
);
this.summary = ((data.data || {}).result || {}).summary || {};
} catch (error) {
this.errorHandler.handle('Error refreshing agents stats');
}
2019-08-07 08:31:59 +00:00
this.$scope.$broadcast('reloadSearchFilterBar', {});
}
openRegistrationDocs() {
this.$window.open(
'https://documentation.wazuh.com/current/user-manual/registering/index.html',
'_blank'
);
}
2018-09-11 09:52:54 +00:00
}