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

247 lines
8.8 KiB
JavaScript
Raw Normal View History

2016-06-29 01:43:18 +00:00
// Require config
var app = require('ui/modules').get('app/wazuh', []);
2016-08-23 14:27:42 +00:00
app.controller('agentsController', function ($scope, DataFactory, $mdToast) {
2016-06-29 01:43:18 +00:00
//Initialisation
$scope.load = true;
2016-07-20 17:34:15 +00:00
$scope.agentInfo = [];
$scope.$parent.submenuNavItem = 'overview';
2016-06-29 01:43:18 +00:00
var objectsArray = [];
var loadWatch;
2016-06-29 01:43:18 +00:00
//Print Error
var printError = function (error) {
2016-07-19 10:38:29 +00:00
$mdToast.show({
template: '<md-toast>' + error.html + '</md-toast>',
position: 'bottom left',
hideDelay: 5000,
2016-07-19 10:38:29 +00:00
});
2016-07-28 15:58:25 +00:00
if ($scope.blocked) {
$scope.blocked = false;
}
2016-07-19 10:38:29 +00:00
};
2016-06-29 01:43:18 +00:00
//Functions
2016-07-18 14:54:06 +00:00
2016-06-29 01:43:18 +00:00
$scope.getAgentStatusClass = function (agentStatus) {
if (agentStatus == "Active")
return "green"
else if (agentStatus == "Disconnected")
return "red";
else
return "red";
};
$scope.fetchAgent = function (agent) {
DataFactory.getAndClean('get', '/agents/' + agent.id, {})
.then(function (data) {
2016-07-20 17:34:15 +00:00
$scope.agentInfo = data.data;
if (agent.id != '000') {
DataFactory.getAndClean('get', '/agents/' + agent.id + '/key', {})
.then(function (data) {
2016-07-20 17:34:15 +00:00
$scope.agentInfo.key = data.data;
2016-08-23 14:27:42 +00:00
$scope.load = false;
}, printError);
}
}, printError);
2016-08-23 14:27:42 +00:00
$scope.fetchFim(agent);
$scope.fetchRootcheck(agent);
2016-06-29 01:43:18 +00:00
};
$scope.fetchFim = function (agent) {
DataFactory.getAndClean('get', '/syscheck/' + agent.id + '/files', { 'offset': 0, 'limit': 5 })
.then(function (data) {
2016-07-20 17:34:15 +00:00
$scope.agentInfo.syscheckEvents = data.data.items;
}, printError);
};
$scope.fetchRootcheck = function (agent) {
DataFactory.getAndClean('get', '/rootcheck/' + agent.id, { 'offset': 0, 'limit': 5 })
.then(function (data) {
2016-07-20 17:34:15 +00:00
$scope.agentInfo.rootcheckEvents = data.data.items;
}, printError);
};
2016-06-29 01:43:18 +00:00
$scope.restart = function (agent) {
2016-08-23 14:27:42 +00:00
$mdToast.show({
template: '<md-toast>Restarting agent...</md-toast>',
position: 'bottom left',
hideDelay: 5000,
2016-06-29 01:43:18 +00:00
});
2016-08-23 14:27:42 +00:00
DataFactory.getAndClean('put', '/agents/' + agent.id + '/restart', {})
2016-06-29 01:43:18 +00:00
.then(function (data) {
2016-08-23 14:27:42 +00:00
$mdToast.show({
template: '<md-toast>Restarted successfully.</md-toast>',
position: 'bottom left',
hideDelay: 5000,
});
2016-06-29 01:43:18 +00:00
}, printError);
};
2016-08-24 17:27:40 +00:00
$scope.getDiscoverByAgent = function (agent) {
var _urlStr = '/app/kibana#/discover?_g=(refreshInterval:(display:Off,pause:!f,value:0),time:(from:now-7d,mode:quick,to:now))&_a=(columns:!(_source),filters:!((\'$state\':(store:appState),meta:(alias:!n,disabled:!f,index:\'ossec-*\',key:AgentName,negate:!f,value:\'';
var _urlStrSf = '\'),query:(match:(AgentName:(query:\'';
var _urlStrSSf = '\',type:phrase))))),index:\'ossec-*\',interval:auto,query:(query_string:(analyze_wildcard:!t,query:\'*\')),sort:!(\'@timestamp\',desc),vis:(aggs:!((params:(field:AgentName,orderBy:\'2\',size:20),schema:segment,type:terms),(id:\'2\',schema:metric,type:count)),type:histogram))&indexPattern=ossec-*&type=histogram';
return _urlStr + agent.name + _urlStrSf + agent.name + _urlStrSSf;
}
2016-06-29 01:43:18 +00:00
$scope.addAgent = function () {
if ($scope.newName == undefined) {
notify.error('Error adding agent: Specify an agent name');
}
else if ($scope.newIp == undefined) {
notify.error('Error adding agent: Specify an IP address');
}
else {
DataFactory.getAndClean('post', '/agents', {
name: $scope.newName,
ip: $scope.newIp
}).then(function (data) {
$mdToast.show($mdToast.simple().textContent('Agent added successfully.'));
2016-06-29 13:44:36 +00:00
$scope.agentsGet();
2016-06-29 01:43:18 +00:00
}, printError);
}
};
//Load
loadWatch = $scope.$watch(function () {
return $scope.$parent._agent;
}, function () {
$scope.fetchAgent($scope.$parent._agent);
});
2016-06-29 01:43:18 +00:00
//Destroy
$scope.$on("$destroy", function () {
angular.forEach(objectsArray, function (value) {
2016-08-23 14:27:42 +00:00
DataFactory.clean(value)
});
loadWatch();
2016-06-29 01:43:18 +00:00
});
2016-08-03 03:15:37 +00:00
});
2016-09-01 13:03:33 +00:00
app.controller('agentsPreviewController', function ($scope, DataFactory, $mdToast) {
//Initialisation
$scope.load = true;
$scope.agents = [];
$scope._status = 'all';
var objectsArray = [];
//Print Error
var printError = function (error) {
$mdToast.show({
template: '<md-toast>' + error.html + '</md-toast>',
position: 'bottom left',
hideDelay: 5000,
});
if ($scope.blocked) {
$scope.blocked = false;
}
};
//Functions
$scope.getAgentStatusClass = function (agentStatus) {
if (agentStatus == "Active")
return "green"
else if (agentStatus == "Disconnected")
return "red";
else
return "red";
};
$scope.setSort = function (field) {
if ($scope._sort === field) {
if ($scope._sortOrder) {
$scope._sortOrder = false;
$scope._sort = '';
DataFactory.filters.unset(objectsArray['/agents'], 'filter-sort');
} else {
$scope._sortOrder = true;
DataFactory.filters.set(objectsArray['/agents'], 'filter-sort', field);
}
} else {
$scope._sortOrder = false;
$scope._sort = field;
DataFactory.filters.set(objectsArray['/agents'], 'filter-sort', '-' + field);
}
}
$scope.agentSearchFilter = function (search) {
if (search) {
DataFactory.filters.set(objectsArray['/agents'], 'search', search);
} else {
DataFactory.filters.unset(objectsArray['/agents'], 'search');
}
};
$scope.agentStatusFilter = function (status) {
if (status == 'all') {
DataFactory.filters.unset(objectsArray['/agents'], 'status');
} else {
DataFactory.filters.set(objectsArray['/agents'], 'status', status);
}
};
$scope.agentsObj = {
//Obj with methods for virtual scrolling
getItemAtIndex: function (index) {
if ($scope.blocked) {
return null;
}
var _pos = index - DataFactory.getOffset(objectsArray['/agents']);
if (DataFactory.filters.flag(objectsArray['/agents'])) {
$scope.blocked = true;
DataFactory.scrollTo(objectsArray['/agents'], 50)
.then(function (data) {
$scope.agents.length = 0;
$scope.agents = data.data.items;
DataFactory.filters.unflag(objectsArray['/agents']);
$scope.blocked = false;
}, printError);
} else if ((_pos > 70) || (_pos < 0)) {
$scope.blocked = true;
DataFactory.scrollTo(objectsArray['/agents'], index)
.then(function (data) {
$scope.agents.length = 0;
$scope.agents = data.data.items;
$scope.blocked = false;
}, printError);
} else {
return $scope.agents[_pos];
}
},
getLength: function () {
return DataFactory.getTotalItems(objectsArray['/agents']);
},
};
var load = function () {
DataFactory.initialize('get', '/agents', {}, 100, 0)
.then(function (data) {
objectsArray['/agents'] = data;
DataFactory.get(objectsArray['/agents'])
.then(function (data) {
$scope.agents = data.data.items;
DataFactory.filters.register(objectsArray['/agents'], 'search', 'string');
DataFactory.filters.register(objectsArray['/agents'], 'status', 'string');
DataFactory.filters.register(objectsArray['/agents'], 'filter-sort', 'string');
$scope.load = false;
}, printError);
}, printError);
};
//Load
load();
//Destroy
$scope.$on("$destroy", function () {
angular.forEach(objectsArray, function (value) {
DataFactory.clean(value)
});
$scope.agents.length = 0;
});
});