// Require config var app = require('ui/modules').get('app/wazuh', []); app.controller('osseclogController', function ($scope, DataFactory, $sce, $interval, $mdToast, errlog) { //Initialisation $scope.load = true; $scope.text = []; $scope.realtime = false; $scope.$parent.state.setManagerState('logs'); var objectsArray = []; var _category = 'all'; var _level = 'all'; var _promise; //Print Error var printError = function (error) { $mdToast.show({ template: '' + error.html + '', position: 'bottom left', hideDelay: 5000, }); if ($scope.blocked) { $scope.blocked = false; } } //Functions $scope.textObj = { //Obj with methods for virtual scrolling getItemAtIndex: function (index) { if ($scope.blocked) { return null; } var _pos = index - DataFactory.getOffset(objectsArray['/manager/logs']); if (DataFactory.filters.flag(objectsArray['/manager/logs'])) { $scope.blocked = true; DataFactory.scrollTo(objectsArray['/manager/logs'], 75) .then(function (data) { $scope.text.length = 0; $scope.text = data.data.items; DataFactory.filters.unflag(objectsArray['/manager/logs']); $scope.blocked = false; }, printError); } else if ((_pos > 225) || (_pos < 0)) { $scope.blocked = true; DataFactory.scrollTo(objectsArray['/manager/logs'], index) .then(function (data) { $scope.text.length = 0; $scope.text = data.data.items; $scope.blocked = false; }, printError); } else { return $scope.text[_pos]; } }, getLength: function () { if ($scope.realtime) { return 120; } var tmpLength = DataFactory.getTotalItems(objectsArray['/manager/logs']); if (tmpLength === 0) { return 120; } return tmpLength; }, }; $scope.categoryFilter = function (category) { if (category) { DataFactory.filters.set(objectsArray['/manager/logs'], 'category', category); } else { DataFactory.filters.unset(objectsArray['/manager/logs'], 'category'); } }; $scope.levelFilter = function (level) { if (level) { DataFactory.filters.set(objectsArray['/manager/logs'], 'type_log', level); } else { DataFactory.filters.unset(objectsArray['/manager/logs'], 'type_log'); } }; var loadSummary = function () { DataFactory.getAndClean('get', '/manager/logs/summary', {}) .then(function (data) { $scope.summary = data.data; $scope.load = false; }, printError); }; $scope.colorLine = function (line) { if ((!line) || (line == null)) { return $sce.trustAsHtml('-'); } line = line.replace('INFO', 'INFO'); line = line.replace('ERROR', 'ERROR'); line = line.replace('ossec-remoted', 'ossec-remoted'); line = line.replace('ossec-testrule', 'ossec-testrule'); line = line.replace('wazuh-moduled', 'wazuh-moduled'); line = line.replace('ossec-monitord', 'ossec-monitord'); line = line.replace('ossec-logcollector', 'ossec-logcollector'); line = line.replace('ossec-execd', 'ossec-execd'); line = line.replace('ossec-rootcheck', 'ossec-rootcheck'); line = line.replace('rootcheck', 'rootcheck'); line = line.replace('ossec-syscheckd', 'ossec-syscheckd'); line = line.replace('ossec-analysisd', 'ossec-analysisd'); line = line.replace('ossec-maild', 'ossec-maild'); line = line.replace(line.substring(0, 19), '' + line.substring(0, 19) + ''); return $sce.trustAsHtml(line); }; $scope.playRealtime = function () { $scope.realtime = !$scope.realtime; if (!$scope.realtime) { $interval.cancel(_promise); } else { _promise = $interval(function () { DataFactory.get(objectsArray['/manager/logs']) .then(function (data) { $scope.text.length = 0; $scope.text = data.data.items; }, printError); }, 1000); } }; var load = function () { DataFactory.initialize('get', '/manager/logs', {}, 250, 0) .then(function (data) { objectsArray['/manager/logs'] = data; DataFactory.filters.register(objectsArray['/manager/logs'], 'category', 'string'); DataFactory.filters.register(objectsArray['/manager/logs'], 'type_log', 'string'); DataFactory.get(data).then(function (data) { $scope.text = data.data.items; loadSummary(); }, printError); }, printError); } //Load try { load(); } catch (e) { $mdToast.show({ template: ' Unexpected exception loading controller ', position: 'bottom left', hideDelay: 5000, }); errlog.log('Unexpected exception loading controller', e); } //Destroy $scope.$on("$destroy", function () { $interval.cancel(_promise); angular.forEach(objectsArray, function (value) { DataFactory.clean(value) }); }); });