2018-04-22 20:16:38 +00:00
|
|
|
/*
|
|
|
|
* 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-04-21 11:31:47 +00:00
|
|
|
|
2018-06-15 10:42:57 +00:00
|
|
|
const app = uiModules.get('app/wazuh', []);
|
2016-09-16 18:44:04 +00:00
|
|
|
|
2018-06-04 14:31:42 +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';
|
2018-06-07 11:00:01 +00:00
|
|
|
$scope.version = 'all'
|
2017-10-30 09:08:42 +00:00
|
|
|
$scope.osPlatforms = [];
|
2018-06-07 11:00:01 +00:00
|
|
|
$scope.versions = [];
|
2018-03-13 12:35:58 +00:00
|
|
|
$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 = {
|
2018-01-29 11:37:29 +00:00
|
|
|
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);
|
|
|
|
});
|
|
|
|
|
2017-12-03 14:30:47 +00:00
|
|
|
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
|
|
|
|
2017-12-21 09:39:54 +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
|
|
|
|
2018-03-13 12:35:58 +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]);
|
2018-06-07 11:00:01 +00:00
|
|
|
} 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
|
|
|
}
|
2017-12-21 09:39:54 +00:00
|
|
|
}
|
|
|
|
|
2017-11-24 16:18:04 +00:00
|
|
|
// Retrieve os list
|
2017-12-21 09:33:15 +00:00
|
|
|
const retrieveList = agents => {
|
2018-06-07 11:51:42 +00:00
|
|
|
for(const agent of agents){
|
|
|
|
if(agent.id === '000') continue;
|
2018-03-13 12:35:58 +00:00
|
|
|
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);
|
2018-06-07 11:00:01 +00:00
|
|
|
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);
|
2017-11-24 16:18:04 +00:00
|
|
|
if(!exists.length){
|
|
|
|
$scope.osPlatforms.push({
|
|
|
|
name: agent.os.name,
|
|
|
|
platform: agent.os.platform,
|
|
|
|
version: agent.os.version
|
|
|
|
});
|
2017-11-02 09:22:15 +00:00
|
|
|
}
|
2017-10-30 09:08:42 +00:00
|
|
|
}
|
2017-11-24 16:18:04 +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-10 21:13:20 +00:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-01-29 11:37:29 +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)
|
2018-01-29 11:37:29 +00:00
|
|
|
]);
|
2017-11-24 16:18:04 +00:00
|
|
|
|
|
|
|
// Agents summary
|
|
|
|
if(parseInt(data[1].data.data['Never connected']) > 0){
|
2017-11-08 15:00:58 +00:00
|
|
|
$scope.osPlatforms.push({
|
|
|
|
name: 'Unknown',
|
|
|
|
platform: 'Unknown',
|
2017-12-21 10:22:38 +00:00
|
|
|
version: ''
|
2017-11-08 15:00:58 +00:00
|
|
|
});
|
|
|
|
}
|
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;
|
2017-11-24 16:18:04 +00:00
|
|
|
$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
|
|
|
|
2017-11-24 16:18:04 +00:00
|
|
|
// tmpUrl y tmpUrl2
|
|
|
|
if (data[2].data.data === '') {
|
2017-12-18 12:00:00 +00:00
|
|
|
$scope.mostActiveAgent.name = appState.getClusterInfo().manager;
|
2017-10-30 09:08:42 +00:00
|
|
|
$scope.mostActiveAgent.id = '000';
|
2017-11-24 16:18:04 +00:00
|
|
|
} else {
|
|
|
|
$scope.mostActiveAgent.name = data[2].data.data;
|
2018-01-29 11:37:29 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-11-24 16:18:04 +00:00
|
|
|
// Last agent
|
2018-06-12 15:02:52 +00:00
|
|
|
$scope.lastAgent = agents[0];
|
2017-12-21 09:33:15 +00:00
|
|
|
|
2018-06-12 15:02:52 +00:00
|
|
|
retrieveList(agents);
|
2017-11-24 16:18:04 +00:00
|
|
|
|
|
|
|
$scope.loading = false;
|
2018-01-26 10:21:50 +00:00
|
|
|
if(!$scope.$$phase) $scope.$digest();
|
2018-01-29 11:37:29 +00:00
|
|
|
return;
|
|
|
|
} catch (error) {
|
2018-01-29 14:39:21 +00:00
|
|
|
errorHandler.handle(error,'Agents Preview');
|
2018-01-29 11:37:29 +00:00
|
|
|
}
|
2018-06-04 15:48:41 +00:00
|
|
|
return;
|
2016-09-16 18:44:04 +00:00
|
|
|
};
|
|
|
|
|
2018-03-13 12:35:58 +00:00
|
|
|
$scope.goGroup = agent => {
|
2018-06-04 14:31:42 +00:00
|
|
|
shareAgent.setAgent(agent);
|
2018-03-13 12:35:58 +00:00
|
|
|
$location.search('tab', 'groups');
|
|
|
|
$location.path('/manager');
|
|
|
|
};
|
|
|
|
|
2017-11-28 15:42:00 +00:00
|
|
|
$scope.showAgent = agent => {
|
2018-06-04 14:47:34 +00:00
|
|
|
shareAgent.setAgent(agent);
|
2018-03-20 15:20:50 +00:00
|
|
|
$location.path('/agents');
|
2017-11-28 15:42:00 +00:00
|
|
|
};
|
|
|
|
|
2016-09-16 18:44:04 +00:00
|
|
|
//Load
|
2018-01-29 11:37:29 +00:00
|
|
|
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());
|
2017-11-20 18:20:27 +00:00
|
|
|
});
|