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

306 lines
8.2 KiB
JavaScript
Raw Normal View History

import rison from 'rison-node';
2017-01-16 12:29:48 +00:00
// Require config
2017-10-30 09:08:42 +00:00
let app = require('ui/modules').get('app/wazuh', []);
2017-01-16 12:29:48 +00:00
2017-10-20 19:04:22 +00:00
app.controller('agentsController',
function ($scope, $q, $routeParams, $route, $location, $rootScope, Notifier, appState, genericReq, apiReq, AgentsAutoComplete) {
2017-10-30 09:08:42 +00:00
const notify = new Notifier({ location: 'Agents' });
$rootScope.page = 'agents';
$scope.submenuNavItem = 'preview';
$scope.agentsAutoComplete = AgentsAutoComplete;
2017-10-20 19:04:22 +00:00
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-10-30 09:08:42 +00:00
} else {
2017-10-20 19:04:22 +00:00
$scope.tabView = "panels";
$location.search("tabView", "panels");
}
2017-10-30 09:08:42 +00:00
$scope.hideRing = (items) => $(".vis-editor-content").length >= items;
// Object for matching nav items and Wazuh groups
2017-10-30 09:08:42 +00:00
let tabGroups = {
"overview": {
"group": "*"
},
"fim": {
"group": "syscheck"
},
"policy_monitoring": {
"group": "rootcheck"
},
"oscap": {
"group": "oscap"
},
"audit": {
"group": "audit"
},
"pci": {
"group": "*"
}
};
2017-10-30 09:08:42 +00:00
$scope.switchTab = (tab) => {
$scope.loading = true;
2017-10-20 19:04:22 +00:00
$scope.submenuNavItem = tab;
2017-10-30 09:08:42 +00:00
$scope.checkAlerts($scope._agent.id)
.then((data) => {
$scope.results = data;
$scope.loading = false;
})
.catch(() => {
$scope.results = false;
$scope.loading = false;
});
};
2017-10-30 09:08:42 +00:00
//Print Error
const printError = (error) => notify.error(error.message);
2017-01-16 12:29:48 +00:00
// Check if there are any alert.
2017-10-30 09:08:42 +00:00
$scope.checkAlerts = (agent_id) => {
let group = tabGroups[$scope.submenuNavItem].group;
let payload = {};
let fields = {
"fields": [{
"field": "rule.groups",
"value": group
}, {
"field": "agent.id",
"value": agent_id
}]
};
// No filter needed for general/pci
2017-10-30 09:08:42 +00:00
if (group === '*'){
fields = {
"fields": [{
"field": "agent.id",
"value": agent_id
}]
};
}
let clusterName = {
"cluster": appState.getClusterInfo().cluster
};
let timeInterval = {
"timeinterval": {
"gte": $scope.timeGTE,
"lt": $scope.timeLT
}
};
angular.extend(payload, fields, clusterName, timeInterval);
2017-10-30 09:08:42 +00:00
let deferred = $q.defer();
genericReq.request('POST', '/api/wazuh-elastic/alerts-count/', payload)
.then((data) => {
if (data.data.data !== 0){
deferred.resolve(true);
2017-10-30 09:08:42 +00:00
} else {
deferred.resolve(false);
2017-10-30 09:08:42 +00:00
}
});
2017-10-30 09:08:42 +00:00
return deferred.promise;
};
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;
};
2017-10-30 09:08:42 +00:00
$scope.extensionStatus = (extension) => appState.getExtensions().extensions[extension];
2017-10-30 09:08:42 +00:00
$scope.applyAgent = (agent) => {
if (agent) {
$scope.loading = true;
2017-10-20 19:04:22 +00:00
$scope.submenuNavItem = 'overview';
2017-10-30 09:08:42 +00:00
$scope.agentInfo = {};
// Get Agent Info
2017-10-30 09:08:42 +00:00
apiReq.request('GET', `/agents/${agent.id}`, {})
.then((data) => {
2017-10-06 15:40:02 +00:00
$scope.agentInfo = data.data.data;
2017-08-21 07:11:05 +00:00
$rootScope.agent = $scope.agentInfo;
if (typeof $scope.agentInfo.version === 'undefined'){
$scope.agentInfo.version = "Unknown";
}
2017-10-30 09:08:42 +00:00
if (typeof $scope.agentInfo.os === 'undefined') {
$scope.agentOs = "Unknown";
2017-10-30 09:08:42 +00:00
} else {
if (typeof $scope.agentInfo.os.name !== 'undefined') {
$scope.agentOs = `${$scope.agentInfo.os.name} ${$scope.agentInfo.os.version}`;
} else {
if (typeof $scope.agentInfo.os.uname !== 'undefined') {
$scope.agentOs = $scope.agentInfo.os.uname;
2017-10-30 09:08:42 +00:00
} else {
$scope.agentOs = "Unknown";
}
}
}
2017-10-30 09:08:42 +00:00
if (typeof $scope.agentInfo.lastKeepAlive === 'undefined'){
$scope.agentInfo.lastKeepAlive = "Unknown";
2017-10-30 09:08:42 +00:00
}
2017-08-22 16:36:00 +00:00
2017-10-06 15:40:02 +00:00
$scope._agent = data.data.data;
$scope.search = data.data.data.name;
$location.search('id', $scope._agent.id);
2017-10-30 09:08:42 +00:00
$scope.checkAlerts($scope._agent.id)
.then((data) => {
2017-08-22 16:36:00 +00:00
$scope.results = data;
2017-10-20 19:04:22 +00:00
$scope.loading = false;
});
2017-08-22 16:36:00 +00:00
2017-10-30 09:08:42 +00:00
apiReq.request('GET', `/syscheck/${agent.id}/last_scan`, {})
.then((data) => {
$scope.agentInfo.syscheck = data.data.data;
$scope.agentInfo.syscheck.duration = "Unknown";
2017-10-30 09:08:42 +00:00
if (!$scope.agentInfo.syscheck.end && !$scope.agentInfo.syscheck.start) {
let syscheckTime = new Date($scope.agentInfo.syscheck.start);
let syscheckEndTime = new Date($scope.agentInfo.syscheck.end);
let minutes = ((syscheckEndTime - syscheckTime) / 1000) / 60;
2017-08-22 16:36:00 +00:00
$scope.agentInfo.syscheck.duration = window.Math.round(minutes);
2017-10-30 09:08:42 +00:00
} else if (!$scope.agentInfo.syscheck.end) {
$scope.agentInfo.syscheck.end = "Unknown";
2017-10-30 09:08:42 +00:00
} else {
$scope.agentInfo.syscheck.start = "Unknown";
}
2017-10-30 09:08:42 +00:00
})
.catch((err) => printError(err));
2017-08-22 16:36:00 +00:00
// Get rootcheck info
2017-10-30 09:08:42 +00:00
apiReq.request('GET', `/rootcheck/${agent.id}/last_scan`, {})
.then((data) => {
$scope.agentInfo.rootcheck = data.data.data;
$scope.agentInfo.rootcheck.duration = "Unknown";
2017-10-30 09:08:42 +00:00
if ($scope.agentInfo.rootcheck.end && $scope.agentInfo.rootcheck.start) {
let rootcheckTime = new Date($scope.agentInfo.rootcheck.start);
let rootcheckEndTime = new Date($scope.agentInfo.rootcheck.end);
let minutes = ((rootcheckEndTime - rootcheckTime) / 1000) / 60;
2017-08-22 16:36:00 +00:00
$scope.agentInfo.rootcheck.duration = window.Math.round(minutes);
2017-10-30 09:08:42 +00:00
} else if ($scope.agentInfo.rootcheck.end) {
$scope.agentInfo.rootcheck.end = "Unknown";
2017-10-30 09:08:42 +00:00
} else {
$scope.agentInfo.rootcheck.start = "Unknown";
2017-08-22 16:36:00 +00:00
}
2017-10-30 09:08:42 +00:00
})
.catch((err) => printError(err));
})
.catch((err) => printError(err));
}
};
2017-01-16 12:29:48 +00:00
// Copy agent from groups tab
if($rootScope.comeFromGroups){
let tmpAgent = Object.assign($rootScope.comeFromGroups);
delete $rootScope.comeFromGroups;
$scope.applyAgent(tmpAgent);
}
// Watchers
2017-10-30 09:08:42 +00:00
$scope.$watch('tabView', () => $location.search('tabView', $scope.tabView));
// Watch for timefilter changes
$scope.$on('$routeUpdate', () => {
2017-10-30 09:08:42 +00:00
if ($location.search()._g && $location.search()._g !== '()') {
let currentTimeFilter = rison.decode($location.search()._g);
// Check if timefilter has changed and update values
2017-10-30 09:08:42 +00:00
let gParameter;
if ($route.current.params._g.startsWith("h@")) {
gParameter = sessionStorage.getItem($route.current.params._g);
2017-10-30 09:08:42 +00:00
} else {
gParameter = $route.current.params._g;
}
2017-10-30 09:08:42 +00:00
if (gParameter != "()" && (
$scope.timeGTE != currentTimeFilter.time.from ||
$scope.timeLT != currentTimeFilter.time.to)
) {
$scope.timeGTE = currentTimeFilter.time.from;
2017-10-30 09:08:42 +00:00
$scope.timeLT = currentTimeFilter.time.to;
//Check for present data for the selected tab
2017-10-30 09:08:42 +00:00
if ($scope.submenuNavItem !== "preview") {
if(!('_agent' in $scope)){
console.log('Waiting for an agent...');
} else {
$scope.checkAlerts($scope._agent.id)
2017-10-30 09:08:42 +00:00
.then((data) => $scope.results = data)
.catch(() => $scope.results = false);
}
}
}
}
// Check if tab is empty, then reset to preview
2017-10-30 09:08:42 +00:00
if (typeof $location.search().tab === 'undefined' &&
typeof $location.search().id === 'undefined') {
$scope.submenuNavItem = "preview";
delete $scope._agent;
$scope.search = "";
}
});
2017-10-30 09:08:42 +00:00
//Load
try {
$scope.agentsAutoComplete.nextPage('')
.then(() => $scope.loading = false);
} 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-10-20 19:04:22 +00:00
});
2017-01-16 12:29:48 +00:00
2017-10-30 09:08:42 +00:00
app.controller('agentsOverviewController', function ($scope) {});
2017-10-20 19:04:22 +00:00
2017-10-30 09:08:42 +00:00
app.controller('fimController', ($scope) => $scope._fimEvent = 'all');
2017-10-20 19:04:22 +00:00
2017-10-30 09:08:42 +00:00
app.controller('pmController', function ($scope) {});
2017-10-20 19:04:22 +00:00
2017-10-30 09:08:42 +00:00
app.controller('auditController', function ($scope) {});
app.controller('oscapController', function ($scope) {});
2017-10-20 19:04:22 +00:00
app.controller('PCIController', function ($scope, genericReq) {
2017-10-30 09:08:42 +00:00
let tabs = [];
genericReq.request('GET', '/api/wazuh-api/pci/all')
.then((data) => {
angular.forEach(data.data, (value, key) => {
tabs.push({
"title": key,
"content": value
});
});
2017-10-20 19:04:22 +00:00
});
2017-10-30 09:08:42 +00:00
$scope.tabs = tabs;
$scope.selectedIndex = 0;
2017-10-20 19:04:22 +00:00
2017-10-30 09:08:42 +00:00
$scope.addTab = (title, view) => {
view = view || title + " Content View";
tabs.push({
title: title,
content: view,
disabled: false
});
};
2017-10-20 19:04:22 +00:00
2017-10-30 09:08:42 +00:00
$scope.removeTab = (tab) => {
let index = tabs.indexOf(tab);
tabs.splice(index, 1);
};
});