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

196 lines
7.1 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-06-15 10:42:57 +00:00
import { uiModules } from 'ui/modules'
2018-06-04 09:09:40 +00:00
import * as FileSaver from '../services/file-saver'
2018-06-15 10:42:57 +00:00
const app = uiModules.get('app/wazuh', []);
2016-09-16 18:44:04 +00:00
app.controller('agentsPreviewController', function ($scope, $rootScope, $routeParams, genericReq, apiReq, appState, Agents, $location, errorHandler, csvReq, shareAgent) {
2018-06-07 11:51:42 +00:00
$scope.isClusterEnabled = appState.getClusterInfo() && appState.getClusterInfo().status === 'enabled'
2017-10-30 09:08:42 +00:00
$scope.loading = true;
$scope.agents = Agents;
$scope.status = 'all';
$scope.osPlatform = 'all';
$scope.version = 'all'
2017-10-30 09:08:42 +00:00
$scope.osPlatforms = [];
$scope.versions = [];
$scope.groups = [];
2018-06-07 11:51:42 +00:00
$scope.nodes = [];
$scope.node_name = 'all';
2017-10-30 09:08:42 +00:00
$scope.mostActiveAgent = {
name: '',
id : ''
2017-10-30 09:08:42 +00:00
};
2016-09-16 18:44:04 +00:00
2018-03-20 15:20:50 +00:00
// Load URL params
if ($routeParams.tab){
$scope.submenuNavItem = $routeParams.tab;
}
// Watcher for URL params
$scope.$watch('submenuNavItem', () => {
$location.search('tab', $scope.submenuNavItem);
});
let tmpUrl, tmpUrl2;
if (appState.getClusterInfo().status === 'enabled') {
tmpUrl = `/api/wazuh-elastic/top/cluster/${appState.getClusterInfo().cluster}/agent.name`;
tmpUrl2 = `/api/wazuh-elastic/top/cluster/${appState.getClusterInfo().cluster}/agent.id`;
} else {
tmpUrl = `/api/wazuh-elastic/top/manager/${appState.getClusterInfo().manager}/agent.name`;
tmpUrl2 = `/api/wazuh-elastic/top/manager/${appState.getClusterInfo().manager}/agent.id`;
}
2016-09-16 18:44:04 +00:00
$scope.applyFilters = filter => {
2017-12-21 10:22:38 +00:00
if(filter.includes('Unknown')){
$scope.agents.addFilter('status','Never connected');
2018-03-20 15:20:50 +00:00
/** Pending API implementation */
//} else if(filter.includes('group-')){
// $scope.agents.addFilter('group',filter.split('group-')[1]);
2018-03-20 15:20:50 +00:00
2018-06-07 11:51:42 +00:00
} else if(filter.includes('node-')){
$scope.agents.addFilter('node',filter.split('node-')[1]);
} else if(filter.includes('version-')) {
$scope.agents.addFilter('version',filter.split('version-')[1]);
2017-12-21 10:22:38 +00:00
} else {
const platform = filter.split(' - ')[0];
const version = filter.split(' - ')[1];
2017-12-21 10:32:05 +00:00
$scope.agents.addMultipleFilters([
{ name: 'os.platform', value: platform },
{ name: 'os.version', value: version }
]);
2017-12-21 10:22:38 +00:00
}
}
// Retrieve os list
const retrieveList = agents => {
2018-06-07 11:51:42 +00:00
for(const agent of agents){
if(agent.id === '000') continue;
if(agent.group && !$scope.groups.includes(agent.group)) $scope.groups.push(agent.group);
2018-06-07 11:51:42 +00:00
if(agent.node_name && !$scope.nodes.includes(agent.node_name)) $scope.nodes.push(agent.node_name);
if(agent.version && !$scope.versions.includes(agent.version)) $scope.versions.push(agent.version);
2018-06-07 11:51:42 +00:00
if(agent.os && agent.os.name){
const exists = $scope.osPlatforms.filter((e) => e.name === agent.os.name && e.platform === agent.os.platform && e.version === agent.os.version);
if(!exists.length){
$scope.osPlatforms.push({
name: agent.os.name,
platform: agent.os.platform,
version: agent.os.version
});
}
2017-10-30 09:08:42 +00:00
}
}
}
2018-04-30 11:16:30 +00:00
$scope.downloadCsv = async () => {
try {
2018-06-04 09:09:40 +00:00
errorHandler.info('Your download should begin automatically...', 'CSV')
2018-04-30 11:16:30 +00:00
const currentApi = JSON.parse(appState.getCurrentAPI()).id;
const output = await csvReq.fetch('/agents', currentApi, $scope.agents ? $scope.agents.filters : null);
2018-06-04 09:09:40 +00:00
const blob = new Blob([output], {type: 'text/csv'});
FileSaver.saveAs(blob, 'agents.csv');
2018-06-04 09:09:40 +00:00
return;
2018-04-30 11:16:30 +00:00
} catch (error) {
errorHandler.handle(error,'Download CSV');
}
2018-06-04 15:48:41 +00:00
return;
2018-04-30 11:16:30 +00:00
}
const load = async () => {
try{
const data = await Promise.all([
$scope.agents.nextPage(),
apiReq.request('GET', '/agents/summary', { }),
2018-06-12 15:02:52 +00:00
genericReq.request('GET', tmpUrl)
]);
// Agents summary
if(parseInt(data[1].data.data['Never connected']) > 0){
$scope.osPlatforms.push({
name: 'Unknown',
platform: 'Unknown',
2017-12-21 10:22:38 +00:00
version: ''
});
}
2018-06-01 13:06:28 +00:00
// Once Wazuh core fixes agent 000 issues, this should be adjusted
2018-06-01 13:06:05 +00:00
const active = data[1].data.data.Active - 1;
const total = data[1].data.data.Total - 1;
$scope.agentsCountActive = active;
$scope.agentsCountDisconnected = data[1].data.data.Disconnected;
$scope.agentsCountNeverConnected = data[1].data.data['Never connected'];
2018-06-01 13:06:05 +00:00
$scope.agentsCountTotal = total;
$scope.agentsCoverity = (active / total) * 100;
2016-09-16 18:44:04 +00:00
// tmpUrl y tmpUrl2
if (data[2].data.data === '') {
$scope.mostActiveAgent.name = appState.getClusterInfo().manager;
2017-10-30 09:08:42 +00:00
$scope.mostActiveAgent.id = '000';
} else {
$scope.mostActiveAgent.name = data[2].data.data;
const info = await genericReq.request('GET', tmpUrl2);
if (info.data.data === '' && $scope.mostActiveAgent.name !== '') {
$scope.mostActiveAgent.id = '000';
} else {
$scope.mostActiveAgent.id = info.data.data;
}
2017-10-30 09:08:42 +00:00
}
2018-06-12 15:02:52 +00:00
// Fetch agents sorting by -dateAdd and using pagination
const agents = [];
const total_items = data[1].data.data.Total;
let offset = 0;
const limit = 1000;
while(agents.length < total_items){
const page = await apiReq.request('GET', '/agents', { sort:'-dateAdd', limit, offset });
agents.push(...page.data.data.items)
offset += limit;
}
// Last agent
2018-06-12 15:02:52 +00:00
$scope.lastAgent = agents[0];
2018-06-12 15:02:52 +00:00
retrieveList(agents);
$scope.loading = false;
2018-01-26 10:21:50 +00:00
if(!$scope.$$phase) $scope.$digest();
return;
} catch (error) {
errorHandler.handle(error,'Agents Preview');
}
2018-06-04 15:48:41 +00:00
return;
2016-09-16 18:44:04 +00:00
};
$scope.goGroup = agent => {
shareAgent.setAgent(agent);
$location.search('tab', 'groups');
$location.path('/manager');
};
$scope.showAgent = agent => {
shareAgent.setAgent(agent);
2018-03-20 15:20:50 +00:00
$location.path('/agents');
};
2016-09-16 18:44:04 +00:00
//Load
load();
2018-03-20 15:20:50 +00:00
2016-09-16 18:44:04 +00:00
//Destroy
2017-10-30 09:08:42 +00:00
$scope.$on("$destroy", () => $scope.agents.reset());
});