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

188 lines
5.0 KiB
JavaScript
Raw Normal View History

2017-10-30 09:08:42 +00:00
let app = require('ui/modules').get('app/wazuh', []);
2017-01-16 12:29:48 +00:00
app.controller('agentsController',
2017-11-28 16:37:31 +00:00
function ($scope, $location, $rootScope, Notifier, appState, genericReq, apiReq, AgentsAutoComplete) {
$rootScope.page = 'agents';
$scope.extensions = appState.getExtensions().extensions;
2017-10-30 09:08:42 +00:00
$scope.agentsAutoComplete = AgentsAutoComplete;
2017-11-28 16:37:31 +00:00
const notify = new Notifier({ location: 'Agents' });
2017-10-20 19:04:22 +00:00
2017-11-28 16:37:31 +00:00
// Check the url hash and retriew the tabView information
2017-10-30 09:08:42 +00:00
if ($location.search().tabView){
2017-10-20 19:04:22 +00:00
$scope.tabView = $location.search().tabView;
2017-11-28 16:37:31 +00:00
} else { // If tabView doesn't exist, default it to 'panels' view
2017-10-20 19:04:22 +00:00
$scope.tabView = "panels";
$location.search("tabView", "panels");
}
2017-11-28 16:37:31 +00:00
// Check the url hash and retrivew the tab information
if ($location.search().tab){
$scope.tab = $location.search().tab;
2017-11-28 16:37:31 +00:00
} else { // If tab doesn't exist, default it to 'general' view
$scope.tab = "general";
$location.search("tab", "general");
// Now we initialize the implicitFilter
$rootScope.currentImplicitFilter = "";
}
// Object for matching nav items and Wazuh groups
2017-11-17 16:01:16 +00:00
let tabFilters = {
2017-11-28 16:37:31 +00:00
"general": {
2017-11-17 16:01:16 +00:00
"group": ""
2017-10-30 09:08:42 +00:00
},
"fim": {
"group": "syscheck"
},
2017-11-28 16:37:31 +00:00
"pm": {
2017-10-30 09:08:42 +00:00
"group": "rootcheck"
},
"oscap": {
"group": "oscap"
},
"audit": {
"group": "audit"
},
"pci": {
"group": "pci_dss"
2017-10-30 09:08:42 +00:00
}
};
// Switch subtab
$scope.switchSubtab = (subtab) => {
$scope.tabView = subtab;
};
2017-10-30 09:08:42 +00:00
$scope.switchTab = (tab) => {
2017-11-28 16:37:31 +00:00
// Deleting app state traces in the url
2017-11-17 16:01:16 +00:00
$location.search('_a', null);
$scope.tabView = 'panels';
};
2017-11-17 16:01:16 +00:00
// Watchers
2017-11-17 16:01:16 +00:00
// We watch the resultState provided by the discover
$scope.$watch('tabView', () => $location.search('tabView', $scope.tabView));
$scope.$watch('tab', () => {
$location.search('tab', $scope.tab);
2017-11-17 16:01:16 +00:00
// Update the implicit filter
2017-11-28 16:37:31 +00:00
if (tabFilters[$scope.tab].group === "") $rootScope.currentImplicitFilter = "";
else $rootScope.currentImplicitFilter = tabFilters[$scope.tab].group;
});
2017-11-28 16:37:31 +00:00
// Agent data
2017-10-30 09:08:42 +00:00
$scope.getAgentStatusClass = (agentStatus) => agentStatus === "Active" ? "green" : "red";
2017-01-16 12:29:48 +00:00
2017-10-30 09:08:42 +00:00
$scope.formatAgentStatus = (agentStatus) => {
let condition = (agentStatus !== "Active" || agentStatus === "Disconnected");
return (condition) ? "Never connected" : agentStatus;
};
const calculateMinutes = (start,end) => {
let time = new Date(start);
let endTime = new Date(end);
2017-11-28 16:37:31 +00:00
let minutes = ((endTime - time) / 1000) / 60;
return minutes;
}
const validateRootCheck = () => {
2017-11-28 16:37:31 +00:00
$scope.agent.rootcheck.duration = 'Unknown';
if ($scope.agent.rootcheck.end && $scope.agent.rootcheck.start) {
let minutes = calculateMinutes($scope.agent.rootcheck.start, $scope.agent.rootcheck.end);
$scope.agent.rootcheck.duration = window.Math.round(minutes);
} else {
2017-11-28 16:37:31 +00:00
if (!$scope.agent.rootcheck.end) {
$scope.agent.rootcheck.end = 'Unknown';
}
2017-11-28 16:37:31 +00:00
if (!$scope.agent.rootcheck.start){
$scope.agent.rootcheck.start = 'Unknown';
}
}
}
const validateSysCheck = () => {
2017-11-28 16:37:31 +00:00
$scope.agent.syscheck.duration = 'Unknown';
if ($scope.agent.syscheck.end && $scope.agent.syscheck.start) {
let minutes = calculateMinutes($scope.agent.syscheck.start, $scope.agent.syscheck.end);
$scope.agent.syscheck.duration = window.Math.round(minutes);
} else {
2017-11-28 16:37:31 +00:00
if (!$scope.agent.syscheck.end) {
$scope.agent.syscheck.end = 'Unknown';
}
2017-11-28 16:37:31 +00:00
if (!$scope.agent.syscheck.start){
$scope.agent.syscheck.start = 'Unknown';
}
}
}
$scope.getAgent = newAgentId => {
let id = null;
// They passed an id
if (newAgentId) {
id = newAgentId;
$location.search('agent', id);
} else {
if ($location.search().agent) { // There's one in the url
id = $location.search().agent;
} else { // We pick the one in the rootScope
id = $rootScope.globalAgent;
$location.search('agent', id);
delete $rootScope.globalAgent;
}
}
2017-11-28 16:37:31 +00:00
Promise.all([
apiReq.request('GET', `/agents/${id}`, {}),
apiReq.request('GET', `/syscheck/${id}/last_scan`, {}),
apiReq.request('GET', `/rootcheck/${id}/last_scan`, {})
2017-11-28 16:37:31 +00:00
])
.then(data => {
// Agent
$scope.agent = data[0].data.data;
if ($scope.agent.os) {
$scope.agentOS = $scope.agent.os.name + ' ' + $scope.agent.os.version;
}
else { $scope.agentOS = "Unkwnown" };
2017-11-28 16:37:31 +00:00
// Syscheck
$scope.agent.syscheck = data[1].data.data;
2017-11-28 16:37:31 +00:00
validateSysCheck();
// Rootcheck
$scope.agent.rootcheck = data[2].data.data;
2017-11-28 16:37:31 +00:00
validateRootCheck();
$scope.$digest();
})
.catch(error => notify.error(error.message));
2017-10-30 09:08:42 +00:00
};
2017-01-16 12:29:48 +00:00
2017-10-30 09:08:42 +00:00
//Load
try {
2017-11-28 16:37:31 +00:00
$scope.getAgent();
$scope.agentsAutoComplete.nextPage('');
2017-10-30 09:08:42 +00:00
} catch (e) {
notify.error('Unexpected exception loading controller');
}
2017-10-20 19:04:22 +00:00
2017-10-30 09:08:42 +00:00
//Destroy
$scope.$on("$destroy", () => $scope.agentsAutoComplete.reset());
2017-11-28 16:37:31 +00:00
//PCI tab
2017-10-30 09:08:42 +00:00
let tabs = [];
genericReq.request('GET', '/api/wazuh-api/pci/all')
.then((data) => {
for(let key in data.data){
2017-10-30 09:08:42 +00:00
tabs.push({
"title": key,
"content": data.data[key]
2017-10-30 09:08:42 +00:00
});
}
})
.catch(error => notify.error(error.message));
2017-10-20 19:04:22 +00:00
2017-10-30 09:08:42 +00:00
$scope.tabs = tabs;
$scope.selectedIndex = 0;
});