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

118 lines
4.3 KiB
JavaScript
Raw Normal View History

2017-10-30 09:08:42 +00:00
let app = require('ui/modules').get('app/wazuh', []);
2016-09-16 18:44:04 +00:00
app.controller('agentsPreviewController', function ($scope,$rootScope, genericReq, apiReq, appState, Agents, $location, errorHandler) {
2017-10-30 09:08:42 +00:00
$scope.loading = true;
$scope.agents = Agents;
$scope.status = 'all';
$scope.osPlatform = 'all';
$scope.osPlatforms = [];
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
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
$scope.agents.filters = [];
if(filter.includes('Unknown')){
$scope.agents.addFilter('status','Never connected');
} 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 => {
for(let agent of agents){
if('os' in agent && 'name' in agent.os){
let 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
}
}
}
const load = async () => {
try{
const data = await Promise.all([
$scope.agents.nextPage(),
apiReq.request('GET', '/agents/summary', { }),
genericReq.request('GET', tmpUrl),
apiReq.request('GET', '/agents', { sort:'-date_add', limit:9999999 })
]);
// 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: ''
});
}
$scope.agentsCountActive = data[1].data.data.Active;
$scope.agentsCountDisconnected = data[1].data.data.Disconnected;
$scope.agentsCountNeverConnected = data[1].data.data['Never connected'];
$scope.agentsCountTotal = data[1].data.data.Total;
$scope.agentsCoverity = (data[1].data.data.Active / data[1].data.data.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
}
// Last agent
$scope.lastAgent = data[3].data.data.items[0];
retrieveList(data[3].data.data.items);
$scope.loading = false;
2018-01-26 10:21:50 +00:00
if(!$scope.$$phase) $scope.$digest();
return;
} catch (error) {
errorHandler.handle(error,'Agents Preview');
if(!$rootScope.$$phase) $rootScope.$digest();
}
2016-09-16 18:44:04 +00:00
};
$scope.showAgent = agent => {
$rootScope.globalAgent = agent.id;
2017-11-28 15:46:10 +00:00
$rootScope.comeFrom = 'agentsPreview';
$location.path('/agents');
};
2016-09-16 18:44:04 +00:00
//Load
load();
2016-09-16 18:44:04 +00:00
//Destroy
2017-10-30 09:08:42 +00:00
$scope.$on("$destroy", () => $scope.agents.reset());
});