Wazuh monitoring (WIP)

This commit is contained in:
jesmg 2016-09-02 12:16:53 +00:00
parent 85ad1e014f
commit 4f0857ded0
51 changed files with 2033 additions and 11 deletions

View File

@ -1,4 +1,4 @@
module.exports = function (server, options) {
require('./routes/wazuh-api.js')(server, options);
require('./server/routes/wazuh-api.js')(server, options);
require('./server/wazuh-monitoring.js')(server, options);
};

1
node_modules/node-cron/.covignore generated vendored Normal file
View File

@ -0,0 +1 @@
/node_modules/

3
node_modules/node-cron/.hound.yml generated vendored Normal file
View File

@ -0,0 +1,3 @@
javascript:
config_file: .jshintrc
ignore_file: .jshintignore

3
node_modules/node-cron/.jshintignore generated vendored Normal file
View File

@ -0,0 +1,3 @@
node_modules/
coverage/
covreporter/

34
node_modules/node-cron/.jshintrc generated vendored Normal file
View File

@ -0,0 +1,34 @@
{
"node": true,
"bitwise": true,
"curly": true,
"eqeqeq": true,
"forin": true,
"freeze": true,
"funcscope": false,
"globals": false,
"latedef": true,
"maxcomplexity": 8,
"maxparams": 4,
"nonbsp": true,
"nonew": true,
"quotmark": "single",
"shadow": false,
"strict": true,
"undef": true,
"unused": true,
"asi": false,
"boss": false,
"debug": false,
"eqnull": false,
"evil": false,
"noyield": true,
"predef": [
"beforeEach",
"afterEach",
"describe",
"it"
]
}

37
node_modules/node-cron/.npmignore generated vendored Normal file
View File

@ -0,0 +1,37 @@
# Created by https://www.gitignore.io/api/node
### Node ###
# Logs
logs
*.log
npm-debug.log*
# Runtime data
pids
*.pid
*.seed
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
covreporter
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# node-waf configuration
.lock-wscript
# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release
# Dependency directory
node_modules
# Optional npm cache directory
.npm
# Optional REPL history
.node_repl_history

7
node_modules/node-cron/.travis.yml generated vendored Normal file
View File

@ -0,0 +1,7 @@
language: node_js
node_js:
- stable
before_script:
- npm install
script:
- npm run check

7
node_modules/node-cron/LICENSE.md generated vendored Normal file
View File

@ -0,0 +1,7 @@
## ISC License
Copyright (c) 2016, Lucas Merencia \<lucas.merencia@gmail.com\>
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

194
node_modules/node-cron/README.md generated vendored Normal file
View File

@ -0,0 +1,194 @@
# Node Cron
[![npm](https://img.shields.io/npm/l/node-cron.svg)](https://github.com/merencia/node-cron/blob/master/LICENSE.md)
[![npm](https://img.shields.io/npm/v/node-cron.svg)](https://img.shields.io/npm/v/node-cron.svg)
[![Coverage Status](https://coveralls.io/repos/github/merencia/node-cron/badge.svg?branch=master)](https://coveralls.io/github/merencia/node-cron?branch=master)
[![Code Climate](https://codeclimate.com/github/merencia/node-cron/badges/gpa.svg)](https://codeclimate.com/github/merencia/node-cron)
[![Build Status](https://travis-ci.org/merencia/node-cron.svg?branch=master)](https://travis-ci.org/merencia/node-cron)
[![Dependency Status](https://david-dm.org/merencia/node-cron.svg)](https://david-dm.org/merencia/node-cron)
[![devDependency Status](https://david-dm.org/merencia/node-cron/dev-status.svg)](https://david-dm.org/merencia/node-cron#info=devDependencies)
The node-cron module is tiny task scheduler in pure JavaScript for node.js based on [GNU crontab](https://www.gnu.org/software/mcron/manual/html_node/Crontab-file.html). This module allows you to schedule task in node.js using full crontab syntax.
## Getting Started
Install node-cron using npm:
```console
$ npm install --save node-cron
```
Import node-cron and schedule a task:
```javascript
var cron = require('node-cron');
cron.schedule('* * * * *', function(){
console.log('running a task every minute');
});
```
## Cron Syntax
This is a quick reference to cron syntax and also shows the options supported by node-cron.
### Allowed fields
```
# ┌────────────── second (optional)
# │ ┌──────────── minute
# │ │ ┌────────── hour
# │ │ │ ┌──────── day of month
# │ │ │ │ ┌────── month
# │ │ │ │ │ ┌──── day of week
# │ │ │ │ │ │
# │ │ │ │ │ │
# * * * * * *
```
### Allowed values
| field | value |
|--------------|---------------------|
| second | 0-59 |
| minute | 0-59 |
| hour | 0-23 |
| day of month | 1-31 |
| month | 1-12 (or names) |
| day of week | 0-7 (or names, 0 or 7 are sunday) |
#### Using multiples values
You may use multiples values separated by comma:
```javascript
var cron = require('node-cron');
cron.schedule('1,2,4,5 * * * *', function(){
console.log('running every minute 1, 2, 4 and 5');
});
```
#### Using ranges
You may also define a range of values:
```javascript
var cron = require('node-cron');
cron.schedule('1-5 * * * *', function(){
console.log('running every minute to 1 from 5');
});
```
#### Using step values
Step values can be used in conjunction with ranges, following a range with '/' and a number. e.g: `1-10/2` that is the same as `2,4,6,8,10`. Steps are also permitted after an asterisk, so if you want to say “every two minutes”, just use `*/2`.
```javascript
var cron = require('node-cron');
cron.schedule('*/2 * * * *', function(){
console.log('running a task every two minutes');
});
```
#### Using names
For month and week day you also may use names or short names. e.g:
```javascript
var cron = require('node-cron');
cron.schedule('* * * January,September Sunday', function(){
console.log('running on Sundays of January and September');
});
```
Or with short names:
```javascript
var cron = require('node-cron');
cron.schedule('* * * Jan,Sep Sun', function(){
console.log('running on Sundays of January and September');
});
```
## Cron methods
### Schedule
Schedules given task to be executed whenever the cron expression ticks.
Arguments:
- !string expression - Cron expression
- !Function func - Task to be executed
- boolean? immediateStart - Whether to start scheduler immediately after create.
## ScheduledTask methods
### Start
Starts the scheduled task.
```javascript
var cron = require('node-cron');
var task = cron.schedule('* * * * *', function() {
console.log('immediately started');
}, false);
task.start();
```
### Stop
The task won't be executed unless re-started.
```javascript
var cron = require('node-cron');
var task = cron.schedule('* * * * *', function() {
console.log('will execute every minute until stopped');
});
task.stop();
```
### Destroy
The task will be stopped and completely destroyed.
```javascript
var cron = require('node-cron');
var task = cron.schedule('* * * * *', function() {
console.log('will not execute anymore, nor be able to restart');
});
task.destroy();
```
## Issues
Feel free to submit issues and enhancement requests [here](https://github.com/merencia/node-cron/issues).
## Contributors
In general, we follow the "fork-and-pull" Git workflow.
- Fork the repo on GitHub;
- Commit changes to a branch in your fork;
- Pull request "upstream" with your changes;
NOTE: Be sure to merge the latest from "upstream" before making a pull request!
Please do not contribute code you did not write yourself, unless you are certain you have the legal ability to do so. Also ensure all contributed code can be distributed under the ISC License.
## License
node-cron is under [ISC License](https://github.com/merencia/node-cron/blob/master/LICENSE.md).

64
node_modules/node-cron/package.json generated vendored Normal file
View File

@ -0,0 +1,64 @@
{
"name": "node-cron",
"version": "1.1.2",
"description": "A simple cron-like task scheduler for Node.js",
"author": {
"name": "Lucas Merencia"
},
"license": "ISC",
"homepage": "http://merencia.com/node-cron/",
"main": "src/node-cron.js",
"scripts": {
"test": "./node_modules/mocha/bin/mocha --recursive",
"coverage": "istanbul cover _mocha -- --recursive",
"coveralls": "cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js",
"check": "npm run coverage && npm run coveralls"
},
"repository": {
"type": "git",
"url": "git+https://github.com/merencia/node-cron.git"
},
"keywords": [
"cron",
"scheduler",
"schedule",
"task",
"job"
],
"bugs": {
"url": "https://github.com/merencia/node-cron/issues"
},
"devDependencies": {
"coveralls": "^2.11.6",
"expect.js": "^0.3.1",
"istanbul": "^0.4.2",
"mocha": "^3.0.2",
"sinon": "^1.17.3"
},
"gitHead": "c89903c6bee2e9f6cca6455674eacb3775a27fbc",
"_id": "node-cron@1.1.2",
"_shasum": "6f7f6e14e05f3885f8ebc6eee2882a6e17e6b9a7",
"_from": "node-cron@latest",
"_npmVersion": "2.15.1",
"_nodeVersion": "4.4.4",
"_npmUser": {
"name": "merencia",
"email": "lucas.merencia@gmail.com"
},
"dist": {
"shasum": "6f7f6e14e05f3885f8ebc6eee2882a6e17e6b9a7",
"tarball": "https://registry.npmjs.org/node-cron/-/node-cron-1.1.2.tgz"
},
"maintainers": [
{
"name": "merencia",
"email": "lucas.merencia@gmail.com"
}
],
"_npmOperationalInternal": {
"host": "packages-16-east.internal.npmjs.com",
"tmp": "tmp/node-cron-1.1.2.tgz_1472731069711_0.17741610133089125"
},
"directories": {},
"_resolved": "https://registry.npmjs.org/node-cron/-/node-cron-1.1.2.tgz"
}

View File

@ -0,0 +1,21 @@
'use strict';
module.exports = (function() {
function convertAsterisk(expression, replecement){
if(expression.indexOf('*') !== -1){
return expression.replace('*', replecement);
}
return expression;
}
function convertAsterisksToRanges(expressions){
expressions[0] = convertAsterisk(expressions[0], '0-59');
expressions[1] = convertAsterisk(expressions[1], '0-59');
expressions[2] = convertAsterisk(expressions[2], '0-23');
expressions[3] = convertAsterisk(expressions[3], '1-31');
expressions[4] = convertAsterisk(expressions[4], '1-12');
expressions[5] = convertAsterisk(expressions[5], '0-6');
return expressions;
}
return convertAsterisksToRanges;
}());

47
node_modules/node-cron/src/convert-expression/index.js generated vendored Normal file
View File

@ -0,0 +1,47 @@
'use strict';
var monthNamesConversion = require('./month-names-conversion');
var weekDayNamesConversion = require('./week-day-names-conversion');
var convertAsterisksToRanges = require('./asterisk-to-range-conversion');
var convertRanges = require('./range-conversion');
var convertSteps = require('./step-values-conversion');
module.exports = (function() {
function appendSeccondExpression(expressions){
if(expressions.length === 5){
return ['0'].concat(expressions);
}
return expressions;
}
/*
* The node-cron core allows only numbers (including multiple numbers e.g 1,2).
* This module is going to translate the month names, week day names and ranges
* to integers relatives.
*
* Month names example:
* - expression 0 1 1 January,Sep *
* - Will be translated to 0 1 1 1,9 *
*
* Week day names example:
* - expression 0 1 1 2 Monday,Sat
* - Will be translated to 0 1 1 1,5 *
*
* Ranges example:
* - expression 1-5 * * * *
* - Will be translated to 1,2,3,4,5 * * * *
*/
function interprete(expression){
var expressions = expression.split(' ');
expressions = appendSeccondExpression(expressions);
expressions[4] = monthNamesConversion(expressions[4]);
expressions[5] = weekDayNamesConversion(expressions[5]);
expressions = convertAsterisksToRanges(expressions);
expressions = convertRanges(expressions);
expressions = convertSteps(expressions);
return expressions.join(' ');
}
return interprete;
}());

View File

@ -0,0 +1,22 @@
'use strict';
module.exports = (function() {
var months = ['january','february','march','april','may','june','july',
'august','september','october','november','december'];
var shortMonths = ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug',
'sep', 'oct', 'nov', 'dec'];
function convertMonthName(expression, items){
for(var i = 0; i < items.length; i++){
expression = expression.replace(new RegExp(items[i], 'gi'), parseInt(i, 10) + 1);
}
return expression;
}
function interprete(monthExpression){
monthExpression = convertMonthName(monthExpression, months);
monthExpression = convertMonthName(monthExpression, shortMonths);
return monthExpression;
}
return interprete;
}());

View File

@ -0,0 +1,33 @@
'use strict';
module.exports = (function() {
function replaceWithRange(expression, text, init, end) {
var numbers = [];
var last = parseInt(end);
for(var i = parseInt(init); i <= last; i++) {
numbers.push(i);
}
return expression.replace(new RegExp(text, 'gi'), numbers.join());
}
function convertRange(expression){
var rangeRegEx = /(\d+)\-(\d+)/;
var match = rangeRegEx.exec(expression);
while(match !== null && match.length > 0){
expression = replaceWithRange(expression, match[0], match[1], match[2]);
match = rangeRegEx.exec(expression);
}
return expression;
}
function convertAllRanges(expressions){
for(var i = 0; i < expressions.length; i++){
expressions[i] = convertRange(expressions[i]);
}
return expressions;
}
return convertAllRanges;
}());

View File

@ -0,0 +1,27 @@
'use strict';
module.exports = (function() {
function convertSteps(expressions){
var stepValuePattern = /^(.+)\/(\d+)$/;
for(var i = 0; i < expressions.length; i++){
var match = stepValuePattern.exec(expressions[i]);
var isStepValue = match !== null && match.length > 0;
if(isStepValue){
var values = match[1].split(',');
var setpValues = [];
var divider = parseInt(match[2], 10);
for(var j = 0; j <= values.length; j++){
var value = parseInt(values[j], 10);
if(value % divider === 0){
setpValues.push(value);
}
}
expressions[i] = setpValues.join(',');
}
}
return expressions;
}
return convertSteps;
}());

View File

@ -0,0 +1,21 @@
'use strict';
module.exports = (function() {
var weekDays = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday',
'friday', 'saturday'];
var shortWeekDays = ['sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat'];
function convertWeekDays(expression){
expression = expression.replace('7', '0');
expression = convertWeekDayName(expression, weekDays);
return convertWeekDayName(expression, shortWeekDays);
}
function convertWeekDayName(expression, items){
for(var i = 0; i < items.length; i++){
expression = expression.replace(new RegExp(items[i], 'gi'), parseInt(i, 10));
}
return expression;
}
return convertWeekDays;
}());

26
node_modules/node-cron/src/node-cron.js generated vendored Normal file
View File

@ -0,0 +1,26 @@
'use strict';
var Task = require('./task'),
ScheduledTask = require('./scheduled-task');
module.exports = (function() {
/**
* Creates a new task to execute given function when the cron
* expression ticks.
*
* @param {string} expression - cron expression.
* @param {Function} func - task to be executed.
* @param {boolean} immediateStart - whether to start the task immediately.
* @returns {ScheduledTask} update function.
*/
function createTask(expression, func, immediateStart) {
var task = new Task(expression, func);
return new ScheduledTask(task, immediateStart);
}
return {
schedule: createTask
};
}());

78
node_modules/node-cron/src/pattern-validation.js generated vendored Normal file
View File

@ -0,0 +1,78 @@
'use strict';
var convertExpression = require('./convert-expression');
module.exports = ( function(){
function isValidExpression(expression, min, max){
var options = expression.split(',');
var regexValidation = /^\d+$|^\*$|^\*\/\d+$/;
for(var i = 0; i < options.length; i++){
var option = options[i];
var optionAsInt = parseInt(options[i], 10);
if(optionAsInt < min || optionAsInt > max || !regexValidation.test(option)) {
return false;
}
}
return true;
}
function isInvalidSecond(expression){
return !isValidExpression(expression, 0, 59);
}
function isInvalidMinute(expression){
return !isValidExpression(expression, 0, 59);
}
function isInvalidHour(expression){
return !isValidExpression(expression, 0, 23);
}
function isInvalidDayOfMonth(expression){
return !isValidExpression(expression, 1, 31);
}
function isInvalidMonth(expression){
return !isValidExpression(expression, 1, 12);
}
function isInvalidWeekDay(expression){
return !isValidExpression(expression, 0, 7);
}
function validate(pattern){
var patterns = pattern.split(' ');
var executablePattern = convertExpression(pattern);
var executablePatterns = executablePattern.split(' ');
if(patterns.length === 5){
patterns = ['0'].concat(patterns);
}
if (isInvalidSecond(executablePatterns[0])) {
throw patterns[0] + ' is a invalid expression for second';
}
if (isInvalidMinute(executablePatterns[1])) {
throw patterns[1] + ' is a invalid expression for minute';
}
if (isInvalidHour(executablePatterns[2])) {
throw patterns[2] + ' is a invalid expression for hour';
}
if (isInvalidDayOfMonth(executablePatterns[3])) {
throw patterns[3] + ' is a invalid expression for day of month';
}
if (isInvalidMonth(executablePatterns[4])) {
throw patterns[4] + ' is a invalid expression for month';
}
if (isInvalidWeekDay(executablePatterns[5])) {
throw patterns[5] + ' is a invalid expression for week day';
}
}
return validate;
}());

60
node_modules/node-cron/src/scheduled-task.js generated vendored Normal file
View File

@ -0,0 +1,60 @@
'use strict';
module.exports = (function() {
/**
* Creates a new scheduled task.
*
* @param {Task} task - task to schedule.
* @param {boolean} immediateStart - whether to start the task immediately.
*/
function ScheduledTask(task, immediateStart) {
this.task = function() {
task.update(new Date());
};
this.tick = null;
if (immediateStart !== false) {
this.start();
}
}
/**
* Starts updating the task.
*
* @returns {ScheduledTask} instance of this task.
*/
ScheduledTask.prototype.start = function() {
if (this.task && !this.tick) {
this.tick = setInterval(this.task, 1000);
}
return this;
};
/**
* Stops updating the task.
*
* @returns {ScheduledTask} instance of this task.
*/
ScheduledTask.prototype.stop = function() {
if (this.tick) {
clearInterval(this.tick);
this.tick = null;
}
return this;
};
/**
* Destoys the scheduled task.
*/
ScheduledTask.prototype.destroy = function() {
this.stop();
this.task = null;
};
return ScheduledTask;
}());

44
node_modules/node-cron/src/task.js generated vendored Normal file
View File

@ -0,0 +1,44 @@
'use strict';
var convertExpression = require('./convert-expression');
var validatePattern = require('./pattern-validation');
module.exports = (function(){
function matchPattern(pattern, value){
if( pattern.indexOf(',') !== -1 ){
var patterns = pattern.split(',');
return patterns.indexOf(value.toString()) !== -1;
}
return pattern === value.toString();
}
function mustRun(task, date){
var runInSecond = matchPattern(task.expressions[0], date.getSeconds());
var runOnMinute = matchPattern(task.expressions[1], date.getMinutes());
var runOnHour = matchPattern(task.expressions[2], date.getHours());
var runOnDayOfMonth = matchPattern(task.expressions[3], date.getDate());
var runOnMonth = matchPattern(task.expressions[4], date.getMonth() + 1);
var runOnDayOfWeek = matchPattern(task.expressions[5], date.getDay());
return runInSecond && runOnMinute && runOnHour && runOnDayOfMonth &&
runOnMonth && runOnDayOfWeek;
}
function Task(pattern, execution){
validatePattern(pattern);
this.pattern = convertExpression(pattern);
this.execution = execution;
this.expressions = this.pattern.split(' ');
}
Task.prototype.update = function(date){
if(mustRun(this, date)){
try {
this.execution();
} catch(err) {
console.error(err);
}
}
};
return Task;
}());

View File

@ -0,0 +1,12 @@
'use strict';
var expect = require('expect.js');
var conversion = require('../../src/convert-expression/asterisk-to-range-conversion');
describe('asterisk-to-range-conversion.js', function() {
it('shuld convert * to ranges', function() {
var expressions = '* * * * * *'.split(' ');
var expression = conversion(expressions).join(' ');
expect(expression).to.equal('0-59 0-59 0-23 1-31 1-12 0-6');
});
});

View File

@ -0,0 +1,18 @@
'use strict';
var expect = require('expect.js');
var conversion = require('../../src/convert-expression');
describe('month-names-conversion.js', function() {
it('shuld convert month names', function() {
var expression = conversion('* * * * January,February *');
var expressions = expression.split(' ');
expect(expressions[4]).to.equal('1,2');
});
it('shuld convert week day names', function() {
var expression = conversion('* * * * * Mon,Sun');
var expressions = expression.split(' ');
expect(expressions[5]).to.equal('1,0');
});
});

View File

@ -0,0 +1,16 @@
'use strict';
var expect = require('expect.js');
var conversion = require('../../src/convert-expression/month-names-conversion');
describe('month-names-conversion.js', function() {
it('shuld convert month names', function() {
var months = conversion('January,February,March,April,May,June,July,August,September,October,November,December');
expect(months).to.equal('1,2,3,4,5,6,7,8,9,10,11,12');
});
it('shuld convert month names', function() {
var months = conversion('Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec');
expect(months).to.equal('1,2,3,4,5,6,7,8,9,10,11,12');
});
});

View File

@ -0,0 +1,18 @@
'use strict';
var expect = require('expect.js');
var conversion = require('../../src/convert-expression/range-conversion');
describe('range-conversion.js', function() {
it('shuld convert ranges to numbers', function() {
var expressions = '0-3 0-3 0-2 1-3 1-2 0-3'.split(' ');
var expression = conversion(expressions).join(' ');
expect(expression).to.equal('0,1,2,3 0,1,2,3 0,1,2 1,2,3 1,2 0,1,2,3');
});
it('shuld convert ranges to numbers', function() {
var expressions = '0-3 0-3 8-10 1-3 1-2 0-3'.split(' ');
var expression = conversion(expressions).join(' ');
expect(expression).to.equal('0,1,2,3 0,1,2,3 8,9,10 1,2,3 1,2 0,1,2,3');
});
});

View File

@ -0,0 +1,14 @@
'use strict';
var expect = require('expect.js');
var conversion = require('../../src/convert-expression/step-values-conversion');
describe('step-values-conversion.js', function() {
it('shuld convert step values', function() {
var expressions = '1,2,3,4,5,6,7,8,9,10/2 0,1,2,3,4,5,6,7,8,9/5 * * * *'.split(' ');
expressions = conversion(expressions);
console.log(expressions);
expect(expressions[0]).to.equal('2,4,6,8,10');
expect(expressions[1]).to.equal('0,5');
});
});

View File

@ -0,0 +1,21 @@
'use strict';
var expect = require('expect.js');
var conversion = require('../../src/convert-expression/week-day-names-conversion');
describe('week-day-names-conversion.js', function() {
it('shuld convert week day names names', function() {
var weekDays = conversion('Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday');
expect(weekDays).to.equal('1,2,3,4,5,6,0');
});
it('shuld convert short week day names names', function() {
var weekDays = conversion('Mon,Tue,Wed,Thu,Fri,Sat,Sun');
expect(weekDays).to.equal('1,2,3,4,5,6,0');
});
it('shuld convert 7 to 0', function() {
var weekDays = conversion('7');
expect(weekDays).to.equal('0');
});
});

27
node_modules/node-cron/test/defer-start-test.js generated vendored Normal file
View File

@ -0,0 +1,27 @@
'use strict';
var expect = require('expect.js');
var sinon = require('sinon');
var cron = require('../src/node-cron');
describe('defer a task', function() {
beforeEach(function() {
this.clock = sinon.useFakeTimers();
});
afterEach(function() {
this.clock.restore();
});
it('should defer start of a task', function() {
var executed = 0,
task = cron.schedule('* * * * *', function() {
executed++;
}, false);
this.clock.tick(1000 * 60);
task.start();
this.clock.tick(1000 * 60);
expect(executed).to.equal(1);
});
});

30
node_modules/node-cron/test/destroy-task-test.js generated vendored Normal file
View File

@ -0,0 +1,30 @@
'use strict';
var expect = require('expect.js');
var sinon = require('sinon');
var cron = require('../src/node-cron');
describe('destroying a task', function() {
beforeEach(function() {
this.clock = sinon.useFakeTimers();
});
afterEach(function() {
this.clock.restore();
});
it('should destroy the task', function() {
var executed = 0,
task = cron.schedule('* * * * *', function() {
executed++;
});
this.clock.tick(1000 * 60);
task.destroy();
this.clock.tick(1000 * 60);
task.start();
this.clock.tick(1000 * 60);
expect(executed).to.equal(1);
});
});

37
node_modules/node-cron/test/multiples-values-test.js generated vendored Normal file
View File

@ -0,0 +1,37 @@
'use strict';
var expect = require('expect.js');
var sinon = require('sinon');
var cron = require('../src/node-cron');
describe('scheduling with multiples values', function(){
beforeEach(function(){
this.clock = sinon.useFakeTimers();
});
afterEach(function(){
this.clock.restore();
});
it('should accept multiples values in minute', function() {
var initialDate = new Date();
initialDate.setMinutes(0);
var executed = 0;
cron.schedule('2,3,4 * * * *', function(){
executed += 1;
});
this.clock.tick(7000 * 60);
expect(executed).to.equal(3);
});
it('should accept multiples values in hour', function() {
var initialDate = new Date();
initialDate.setMinutes(0);
var executed = 0;
cron.schedule('2,3,4 * * * *', function(){
executed += 1;
});
this.clock.tick(7000 * 60);
expect(executed).to.equal(3);
});
});

37
node_modules/node-cron/test/range-values-test.js generated vendored Normal file
View File

@ -0,0 +1,37 @@
'use strict';
var expect = require('expect.js');
var sinon = require('sinon');
var cron = require('../src/node-cron');
describe('scheduling with range values', function(){
beforeEach(function(){
this.clock = sinon.useFakeTimers();
});
afterEach(function(){
this.clock.restore();
});
it('should accept range values in minute', function() {
var initialDate = new Date();
initialDate.setMinutes(0);
var executed = 0;
cron.schedule('2-4 * * * *', function(){
executed += 1;
});
this.clock.tick(7000 * 60);
expect(executed).to.equal(3);
});
it('should accept range values in hour', function() {
var initialDate = new Date();
initialDate.setMinutes(0);
var executed = 0;
cron.schedule('0 2-4 * * *', function(){
executed += 1;
});
this.clock.tick(7000 * 60 * 60);
expect(executed).to.equal(3);
});
});

30
node_modules/node-cron/test/restart-task-test.js generated vendored Normal file
View File

@ -0,0 +1,30 @@
'use strict';
var expect = require('expect.js');
var sinon = require('sinon');
var cron = require('../src/node-cron');
describe('restarting a task', function() {
beforeEach(function() {
this.clock = sinon.useFakeTimers();
});
afterEach(function() {
this.clock.restore();
});
it('should restart a task', function() {
var executed = 0,
task = cron.schedule('* * * * *', function() {
executed++;
});
this.clock.tick(1000 * 60);
task.stop();
this.clock.tick(1000 * 60);
task.start();
this.clock.tick(1000 * 60);
expect(executed).to.equal(2);
});
});

72
node_modules/node-cron/test/scheduling-test.js generated vendored Normal file
View File

@ -0,0 +1,72 @@
'use strict';
var expect = require('expect.js');
var sinon = require('sinon');
var cron = require('../src/node-cron');
describe('scheduling on minutes', function(){
beforeEach(function(){
this.clock = sinon.useFakeTimers();
});
afterEach(function(){
this.clock.restore();
});
it('should execute a task every minute', function() {
var executed = 0;
cron.schedule('* * * * *', function(){
executed += 1;
});
this.clock.tick(3000 * 60);
expect(executed).to.equal(3);
});
it('should execute a task on minute 1', function() {
var initialDate = new Date();
initialDate.setMinutes(0);
this.clock = sinon.useFakeTimers(initialDate.getTime());
var executed = 0;
cron.schedule('1 * * * *', function(){
executed += 1;
});
this.clock.tick(3000 * 60);
expect(executed).to.equal(1);
});
it('should execute a task on minutes multiples of 5', function() {
var initialDate = new Date();
initialDate.setMinutes(0);
this.clock = sinon.useFakeTimers(initialDate.getTime());
var executed = 0;
cron.schedule('*/5 * * * *', function(){
executed += 1;
});
this.clock.tick(1000 * 60 * 23);
expect(executed).to.equal(4);
});
it('should execute a task every minute from 10 to 20', function() {
var initialDate = new Date();
initialDate.setMinutes(0);
this.clock = sinon.useFakeTimers(initialDate.getTime());
var executed = 0;
cron.schedule('10-20 * * * *', function(){
executed += 1;
});
this.clock.tick(1000 * 60 * 30);
expect(executed).to.equal(11);
});
it('should execute a task every minute from 10 to 20 and from 30 to 35', function() {
var initialDate = new Date();
initialDate.setMinutes(0);
this.clock = sinon.useFakeTimers(initialDate.getTime());
var executed = 0;
cron.schedule('10-20,30-35 * * * *', function(){
executed += 1;
});
this.clock.tick(1000 * 60 * 40);
expect(executed).to.equal(17);
});
});

38
node_modules/node-cron/test/step-value-test.js generated vendored Normal file
View File

@ -0,0 +1,38 @@
'use strict';
var expect = require('expect.js');
var sinon = require('sinon');
var cron = require('../src/node-cron');
describe('scheduling with divided values', function(){
beforeEach(function(){
this.clock = sinon.useFakeTimers();
});
afterEach(function(){
this.clock.restore();
});
it('should accept * divided by 2 for minutes', function() {
var initialDate = new Date();
initialDate.setMinutes(0);
var executed = 0;
cron.schedule('*/2 * * * *', function(){
executed += 1;
});
this.clock.tick(5000 * 60);
expect(executed).to.equal(2);
});
it('should accept 0-10 divided by 2 for minutes', function() {
var initialDate = new Date();
initialDate.setMinutes(0);
var executed = 0;
cron.schedule('0-10/2 * * * *', function(){
executed += 1;
});
this.clock.tick(15000 * 60);
expect(executed).to.equal(5);
});
});

27
node_modules/node-cron/test/stop-task-test.js generated vendored Normal file
View File

@ -0,0 +1,27 @@
'use strict';
var expect = require('expect.js');
var sinon = require('sinon');
var cron = require('../src/node-cron');
describe('stopping a task', function() {
beforeEach(function() {
this.clock = sinon.useFakeTimers();
});
afterEach(function() {
this.clock.restore();
});
it('should stop a task', function() {
var executed = 0,
task = cron.schedule('* * * * *', function() {
executed++;
});
this.clock.tick(1000 * 60);
task.stop();
this.clock.tick(1000 * 60);
expect(executed).to.equal(1);
});
});

73
node_modules/node-cron/test/task-day-of-month-test.js generated vendored Normal file
View File

@ -0,0 +1,73 @@
'use strict';
var expect = require('expect.js');
var Task = require('../src/task');
describe('Task', function(){
describe('day of day', function(){
it('should run on day', function(){
var task = new Task('* * * * *', function(){
this.executed += 1;
});
task.executed = 0;
var date = new Date(2016, 1, 1);
date.setDate(0);
task.update(date);
date.setDate(15);
task.update(date);
date.setDate(50);
task.update(date);
expect(3).to.equal(task.executed);
});
it('should run only on day 9', function(){
var task = new Task('* * 9 * *', function(){
this.executed += 1;
});
task.executed = 0;
var date = new Date(2016, 1, 1);
date.setDate(3);
task.update(date);
date.setDate(9);
task.update(date);
date.setDate(11);
task.update(date);
expect(1).to.equal(task.executed);
});
it('should run only on day 4, 6 and 12 ', function(){
var task = new Task('* * 4,6,12 * *', function(){
this.executed += 1;
});
task.executed = 0;
var date = new Date(2016, 1, 1);
date.setDate(1);
task.update(date);
date.setDate(4);
task.update(date);
date.setDate(6);
task.update(date);
date.setDate(8);
task.update(date);
date.setDate(12);
task.update(date);
expect(3).to.equal(task.executed);
});
it('should run in even day', function(){
var task = new Task('* * */2 * *', function(){
this.executed += 1;
});
task.executed = 0;
var date = new Date(2016, 1, 1);
date.setDate(2);
task.update(date);
date.setDate(15);
task.update(date);
date.setDate(20);
task.update(date);
expect(2).to.equal(task.executed);
});
});
});

25
node_modules/node-cron/test/task-fail-test.js generated vendored Normal file
View File

@ -0,0 +1,25 @@
'use strict';
var expect = require('expect.js');
var sinon = require('sinon');
var cron = require('../src/node-cron');
describe('scheduling a task with exception', function(){
beforeEach(function(){
this.clock = sinon.useFakeTimers();
});
afterEach(function(){
this.clock.restore();
});
it('should not stop on task exception', function() {
var executed = 0;
cron.schedule('* * * * *', function(){
executed += 1;
throw 'exception!';
});
this.clock.tick(3000 * 60);
expect(executed).to.equal(3);
});
});

90
node_modules/node-cron/test/task-hour-test.js generated vendored Normal file
View File

@ -0,0 +1,90 @@
'use strict';
var expect = require('expect.js');
var Task = require('../src/task');
describe('Task', function(){
describe('hour', function(){
it('should run a task on hour', function(){
var task = new Task('* * * * *', function(){
this.executed += 1;
});
task.executed = 0;
var date = new Date(2016, 1, 1);
date.setHours(0);
task.update(date);
date.setHours(15);
task.update(date);
date.setHours(50);
task.update(date);
expect(3).to.equal(task.executed);
});
it('should run only on hour 12', function(){
var task = new Task('0 12 * * *', function(){
this.executed += 1;
});
task.executed = 0;
var date = new Date(2016, 1, 1);
date.setHours(3);
task.update(date);
date.setHours(12);
task.update(date);
date.setHours(15);
task.update(date);
expect(1).to.equal(task.executed);
});
it('should run only on hours 20, 30 and 40 ', function(){
var task = new Task('0 5,10,20 * * *', function(){
this.executed += 1;
});
task.executed = 0;
var date = new Date(2016, 1, 1);
date.setHours(5);
task.update(date);
date.setHours(10);
task.update(date);
date.setHours(13);
task.update(date);
date.setHours(20);
task.update(date);
date.setHours(22);
task.update(date);
expect(3).to.equal(task.executed);
});
it('should run in even hours', function(){
var task = new Task('* */2 * * *', function(){
this.executed += 1;
});
task.executed = 0;
var date = new Date(2016, 1, 1);
date.setHours(0);
task.update(date);
date.setHours(15);
task.update(date);
date.setHours(50);
task.update(date);
expect(2).to.equal(task.executed);
});
it('should run every hour on range', function(){
var task = new Task('* 8-20 * * *', function(){
this.executed += 1;
});
task.executed = 0;
var date = new Date(2016, 1, 1);
date.setHours(0);
task.update(date);
date.setHours(8);
task.update(date);
date.setHours(19);
task.update(date);
date.setHours(21);
task.update(date);
expect(2).to.equal(task.executed);
});
});
});

73
node_modules/node-cron/test/task-minute-test.js generated vendored Normal file
View File

@ -0,0 +1,73 @@
'use strict';
var expect = require('expect.js');
var Task = require('../src/task');
describe('Task', function(){
describe('minute', function(){
it('should run a task on minute', function(){
var task = new Task('* * * * *', function(){
this.executed += 1;
});
task.executed = 0;
var date = new Date(2016, 1, 1);
date.setMinutes(0);
task.update(date);
date.setMinutes(15);
task.update(date);
date.setMinutes(50);
task.update(date);
expect(3).to.equal(task.executed);
});
it('should run only on minute 33', function(){
var task = new Task('33 * * * *', function(){
this.executed += 1;
});
task.executed = 0;
var date = new Date(2016, 1, 1);
date.setMinutes(3);
task.update(date);
date.setMinutes(33);
task.update(date);
date.setMinutes(32);
task.update(date);
expect(1).to.equal(task.executed);
});
it('should run only on minutes 20, 30 and 40 ', function(){
var task = new Task('20,30,40 * * * *', function(){
this.executed += 1;
});
task.executed = 0;
var date = new Date(2016, 1, 1);
date.setMinutes(20);
task.update(date);
date.setMinutes(30);
task.update(date);
date.setMinutes(33);
task.update(date);
date.setMinutes(40);
task.update(date);
date.setMinutes(50);
task.update(date);
expect(3).to.equal(task.executed);
});
it('should run in even minutes', function(){
var task = new Task('*/2 * * * *', function(){
this.executed += 1;
});
task.executed = 0;
var date = new Date(2016, 1, 1);
date.setMinutes(0);
task.update(date);
date.setMinutes(15);
task.update(date);
date.setMinutes(50);
task.update(date);
expect(2).to.equal(task.executed);
});
});
});

82
node_modules/node-cron/test/task-month-test.js generated vendored Normal file
View File

@ -0,0 +1,82 @@
'use strict';
var expect = require('expect.js');
var Task = require('../src/task');
describe('Task', function(){
describe('month', function(){
it('should run a task on month', function(){
var task = new Task('* * * * *', function(){
this.executed += 1;
});
task.executed = 0;
var date = new Date(2016, 1, 1);
date.setMonth(0);
task.update(date);
date.setMonth(10);
task.update(date);
date.setMonth(12);
task.update(date);
expect(3).to.equal(task.executed);
});
it('should run only on month 9', function(){
var task = new Task('* * * 9 *', function(){
this.executed += 1;
});
task.executed = 0;
var date = new Date(2016, 3, 1);
date.setMonth(3);
task.update(date);
date.setMonth(8);
task.update(date);
date.setMonth(10);
task.update(date);
expect(1).to.equal(task.executed);
});
it('should run only on months 2, 4 and 6 ', function(){
var task = new Task('* * * 2,4,6 *', function(){
this.executed += 1;
});
task.executed = 0;
var date = new Date(2016, 1, 1);
date.setMonth(0);
task.update(date);
date.setMonth(1);
task.update(date);
date.setMonth(3);
task.update(date);
date.setMonth(5);
task.update(date);
date.setMonth(7);
task.update(date);
expect(3).to.equal(task.executed);
});
it('should run in even months', function(){
var task = new Task('* * * */2 *', function(){
this.executed += 1;
});
task.executed = 0;
var date = new Date(2016, 1, 1);
date.setMonth(1);
task.update(date);
date.setMonth(9);
task.update(date);
date.setMonth(8);
task.update(date);
expect(2).to.equal(task.executed);
});
it('should run on September', function(){
var task = new Task('* * * Sep *', function(){
this.executed += 1;
});
task.executed = 0;
task.update(new Date(2016, 8, 1));
expect(1).to.equal(task.executed);
});
});
});

73
node_modules/node-cron/test/task-second-test.js generated vendored Normal file
View File

@ -0,0 +1,73 @@
'use strict';
var expect = require('expect.js');
var Task = require('../src/task');
describe('Task', function(){
describe('second', function(){
it('should run a task on second', function(){
var task = new Task('* * * * * *', function(){
this.executed += 1;
});
task.executed = 0;
var date = new Date(2016, 1, 1);
date.setSeconds(0);
task.update(date);
date.setSeconds(15);
task.update(date);
date.setSeconds(50);
task.update(date);
expect(3).to.equal(task.executed);
});
it('should run only on second 33', function(){
var task = new Task('33 * * * * *', function(){
this.executed += 1;
});
task.executed = 0;
var date = new Date(2016, 1, 1);
date.setSeconds(3);
task.update(date);
date.setSeconds(33);
task.update(date);
date.setSeconds(32);
task.update(date);
expect(1).to.equal(task.executed);
});
it('should run only on seconds 20, 30 and 40 ', function(){
var task = new Task('20,30,40 * * * * *', function(){
this.executed += 1;
});
task.executed = 0;
var date = new Date(2016, 1, 1);
date.setSeconds(20);
task.update(date);
date.setSeconds(30);
task.update(date);
date.setSeconds(33);
task.update(date);
date.setSeconds(40);
task.update(date);
date.setSeconds(50);
task.update(date);
expect(3).to.equal(task.executed);
});
it('should run in even seconds', function(){
var task = new Task('*/2 * * * * *', function(){
this.executed += 1;
});
task.executed = 0;
var date = new Date(2016, 1, 1);
date.setSeconds(0);
task.update(date);
date.setSeconds(15);
task.update(date);
date.setSeconds(50);
task.update(date);
expect(2).to.equal(task.executed);
});
});
});

82
node_modules/node-cron/test/task-week-day-test.js generated vendored Normal file
View File

@ -0,0 +1,82 @@
'use strict';
var expect = require('expect.js');
var Task = require('../src/task');
describe('Task', function(){
describe('week day', function(){
it('should run on week day', function(){
var task = new Task('* * * * *', function(){
this.executed += 1;
});
task.executed = 0;
var date = new Date(2016, 0, 1);
date.setDate(4);
task.update(date);
date.setDate(5);
task.update(date);
date.setDate(6);
task.update(date);
expect(3).to.equal(task.executed);
});
it('should run only on week day 3', function(){
var task = new Task('* * * * 3', function(){
this.executed += 1;
});
task.executed = 0;
var date = new Date(2016, 0, 1);
date.setDate(5);
task.update(date);
date.setDate(6);
task.update(date);
date.setDate(9);
task.update(date);
expect(1).to.equal(task.executed);
});
it('should run only on week days 2, 3 and 5 ', function(){
var task = new Task('* * * * 2,3,5', function(){
this.executed += 1;
});
task.executed = 0;
var date = new Date(2016, 0, 1);
date.setDate(4);
task.update(date);
date.setDate(5);
task.update(date);
date.setDate(6);
task.update(date);
date.setDate(7);
task.update(date);
date.setDate(8);
task.update(date);
expect(3).to.equal(task.executed);
});
it('should run in even week days', function(){
var task = new Task('* * * * */2', function(){
this.executed += 1;
});
task.executed = 0;
var date = new Date(2016, 0, 1);
date.setDate(4);
task.update(date);
date.setDate(5);
task.update(date);
date.setDate(7);
task.update(date);
expect(2).to.equal(task.executed);
});
it('should run on monday', function(){
var task = new Task('* * * * Monday', function(){
this.executed += 1;
});
task.executed = 0;
task.update(new Date(2016, 0, 4));
expect(1).to.equal(task.executed);
});
});
});

34
node_modules/node-cron/test/validate-day-test.js generated vendored Normal file
View File

@ -0,0 +1,34 @@
'use strict';
var expect = require('expect.js');
var validate = require('../src/pattern-validation');
describe('pattern-validation.js', function(){
describe('validate day of month', function(){
it('should fail with invalid day of month', function(){
expect(function(){
validate('* * 32 * *');
}).to.throwException(function(e){
expect('32 is a invalid expression for day of month').to.equal(e);
});
});
it('should not fail with valid day of month', function(){
expect(function(){
validate('0 * * 15 * *');
}).to.not.throwException();
});
it('should not fail with * for day of month', function(){
expect(function(){
validate('* * * * * *');
}).to.not.throwException();
});
it('should not fail with */2 for day of month', function(){
expect(function(){
validate('* * */2 * *');
}).to.not.throwException();
});
});
});

40
node_modules/node-cron/test/validate-hours-test.js generated vendored Normal file
View File

@ -0,0 +1,40 @@
'use strict';
var expect = require('expect.js');
var validate = require('../src/pattern-validation');
describe('pattern-validation.js', function(){
describe('validate hour', function(){
it('should fail with invalid hour', function(){
expect(function(){
validate('* 25 * * *');
}).to.throwException(function(e){
expect('25 is a invalid expression for hour').to.equal(e);
});
});
it('should not fail with valid hour', function(){
expect(function(){
validate('* 12 * * *');
}).to.not.throwException();
});
it('should not fail with * for hour', function(){
expect(function(){
validate('* * * * * *');
}).to.not.throwException();
});
it('should not fail with */2 for hour', function(){
expect(function(){
validate('* */2 * * *');
}).to.not.throwException();
});
it('should accept range for hours', function(){
expect(function(){
validate('* 3-20 * * *');
}).to.not.throwException();
});
});
});

34
node_modules/node-cron/test/validate-minute-test.js generated vendored Normal file
View File

@ -0,0 +1,34 @@
'use strict';
var expect = require('expect.js');
var validate = require('../src/pattern-validation');
describe('pattern-validation.js', function(){
describe('validate minutes', function(){
it('should fail with invalid minute', function(){
expect(function(){
validate('63 * * * *');
}).to.throwException(function(e){
expect('63 is a invalid expression for minute').to.equal(e);
});
});
it('should not fail with valid minute', function(){
expect(function(){
validate('30 * * * *');
}).to.not.throwException();
});
it('should not fail with *', function(){
expect(function(){
validate('* * * * *');
}).to.not.throwException();
});
it('should not fail with */2', function(){
expect(function(){
validate('*/2 * * * *');
}).to.not.throwException();
});
});
});

48
node_modules/node-cron/test/validate-month-test.js generated vendored Normal file
View File

@ -0,0 +1,48 @@
'use strict';
var expect = require('expect.js');
var validate = require('../src/pattern-validation');
describe('pattern-validation.js', function(){
describe('validate month', function(){
it('should fail with invalid month', function(){
expect(function(){
validate('* * * 13 *');
}).to.throwException(function(e){
expect('13 is a invalid expression for month').to.equal(e);
});
});
it('should fail with invalid month name', function(){
expect(function(){
validate('* * * foo *');
}).to.throwException(function(e){
expect('foo is a invalid expression for month').to.equal(e);
});
});
it('should not fail with valid month', function(){
expect(function(){
validate('* * * 10 *');
}).to.not.throwException();
});
it('should not fail with valid month name', function(){
expect(function(){
validate('* * * September *');
}).to.not.throwException();
});
it('should not fail with * for month', function(){
expect(function(){
validate('* * * * *');
}).to.not.throwException();
});
it('should not fail with */2 for month', function(){
expect(function(){
validate('* * * */2 *');
}).to.not.throwException();
});
});
});

34
node_modules/node-cron/test/validate-second-test.js generated vendored Normal file
View File

@ -0,0 +1,34 @@
'use strict';
var expect = require('expect.js');
var validate = require('../src/pattern-validation');
describe('pattern-validation.js', function(){
describe('validate seconds', function(){
it('should fail with invalid second', function(){
expect(function(){
validate('63 * * * * *');
}).to.throwException(function(e){
expect('63 is a invalid expression for second').to.equal(e);
});
});
it('should not fail with valid second', function(){
expect(function(){
validate('30 * * * * *');
}).to.not.throwException();
});
it('should not fail with * for second', function(){
expect(function(){
validate('* * * * * *');
}).to.not.throwException();
});
it('should not fail with */2 for second', function(){
expect(function(){
validate('*/2 * * * * *');
}).to.not.throwException();
});
});
});

48
node_modules/node-cron/test/validate-week-day-test.js generated vendored Normal file
View File

@ -0,0 +1,48 @@
'use strict';
var expect = require('expect.js');
var validate = require('../src/pattern-validation');
describe('pattern-validation.js', function(){
describe('validate week day', function(){
it('should fail with invalid week day', function(){
expect(function(){
validate('* * * * 9');
}).to.throwException(function(e){
expect('9 is a invalid expression for week day').to.equal(e);
});
});
it('should fail with invalid week day name', function(){
expect(function(){
validate('* * * * foo');
}).to.throwException(function(e){
expect('foo is a invalid expression for week day').to.equal(e);
});
});
it('should not fail with valid week day', function(){
expect(function(){
validate('* * * * 5');
}).to.not.throwException();
});
it('should not fail with valid week day name', function(){
expect(function(){
validate('* * * * Friday');
}).to.not.throwException();
});
it('should not fail with * for week day', function(){
expect(function(){
validate('* * * * *');
}).to.not.throwException();
});
it('should not fail with */2 for week day', function(){
expect(function(){
validate('* * * */2 *');
}).to.not.throwException();
});
});
});

View File

@ -0,0 +1,23 @@
'use strict';
var expect = require('expect.js');
var sinon = require('sinon');
var cron = require('../src/node-cron');
describe('validate cron on task schaduling', function(){
beforeEach(function(){
this.clock = sinon.useFakeTimers();
});
afterEach(function(){
this.clock.restore();
});
it('should fail with a invalid cron expression', function(){
expect(function(){
cron.schedule('65 * * * *', function(){});
}).to.throwException(function(e){
expect(e).to.equal('65 is a invalid expression for minute');
});
});
});

View File

@ -6,7 +6,11 @@
"scripts": {
"install": "npm install"
},
"keywords": ["kibana", "wazuh", "ossec"],
"keywords": [
"kibana",
"wazuh",
"ossec"
],
"author": "Wazuh",
"license": "GPL-2.0",
"repository": {
@ -34,7 +38,8 @@
"angular-aria": "1.4.7",
"angular-material": "1.1.0-rc.5",
"angular-md5": "^0.1.10",
"bootstrap": "3.3.6",
"needle": "^1.0.0",
"bootstrap": "3.3.6"
"node-cron": "^1.1.2"
}
}

View File

@ -621,31 +621,31 @@ module.exports = function (server, options) {
* Returns the agent with most alerts
*
**/
server.route({
/*server.route({
method: 'GET',
path: '/api/wazuh-stats/top/agent',
handler: statsTopAgent
});
});*/
/*
* GET /api/wazuh-stats/overview/alerts
* Returns overview stats about alerts
*
**/
server.route({
/*server.route({
method: 'GET',
path: '/api/wazuh-stats/overview/alerts',
handler: statsOverviewAlerts
});
});*/
/*
* GET /api/wazuh-stats/overview/syscheck
* Returns overview stats about syscheck
*
**/
server.route({
/*server.route({
method: 'GET',
path: '/api/wazuh-stats/overview/syscheck',
handler: statsOverviewSyscheck
})
})*/
};

132
server/wazuh-monitoring.js Normal file
View File

@ -0,0 +1,132 @@
module.exports = function (server, options) {
const config = server.config();
var _elurl = config.get('elasticsearch.url');
var _eluser = config.get('elasticsearch.username');
var _elpass = config.get('elasticsearch.password');
var api_user;
var api_pass;
var api_url;
var api_insecure;
var cron = require('node-cron');
var needle = require('needle');
var agentsArray = [];
/*cron.schedule('* 0,30 * * * *', function () {
agentsArray.length = 0;
getConfig(loadCredentials);
}, true);*/
//UNCOMMENT IN ORDER TO DEBUG
/*cron.schedule('0 * * * * *', function () {
agentsArray.length = 0;
getConfig(loadCredentials);
}, true);*/
var loadCredentials = function (json) {
if (json.error) {
console.log('Error getting wazuh-api data: ' + json.error);
return;
}
api_user = json.user;
api_pass = json.password;
api_url = json.url;
api_insecure = json.insecure;
checkAndSaveStatus();
}
var checkAndSaveStatus = function () {
var payload = {
'offset': 0,
'limit': 1,
};
var options = {
headers: { 'api-version': 'v1.2' },
username: api_user,
password: api_pass,
rejectUnauthorized: !api_insecure
};
needle.request('get', api_url + '/agents', payload, options, function (error, response) {
if (!error) {
checkStatus(response.body.data.totalItems);
} else {
console.log('Could not get data from Wazuh api. Reason: ' + response.body.message);
return;
}
});
};
var checkStatus = function (maxSize, offset) {
if (!maxSize) {
console.log('You must provide a max size');
}
var payload = {
'offset': offset ? offset : 0,
'limit': (10 < maxSize) ? 10 : maxSize
};
var options = {
headers: { 'api-version': 'v1.2' },
username: api_user,
password: api_pass,
rejectUnauthorized: !api_insecure
};
needle.request('get', api_url + '/agents', payload, options, function (error, response) {
if (!error) {
agentsArray = agentsArray.concat(response.body.data.items);
if ((payload.limit + payload.offset) < maxSize) {
checkStatus(response.body.data.totalItems, payload.limit + payload.offset);
} else {
saveStatus();
}
} else {
console.log('Could not get data from Wazuh api. Reason: ' + response.body.message);
return;
}
});
};
var saveStatus = function () {
//Save the data to wazuh-monitoring index pattern, and do it well formatted!
console.log(agentsArray);
};
//This method should be reused from wazuh-api
var getConfig = function (callback) {
var needle = require('needle');
if (_eluser && _elpass) {
var options = {
username: _eluser,
password: _elpass,
rejectUnauthorized: false
};
} else {
var options = {
rejectUnauthorized: false
};
}
var elasticurl = _elurl + '/.kibana/wazuh-configuration/1';
needle.get(elasticurl, options, function (error, response) {
if (!error) {
if (response.body.found) {
callback({ 'user': response.body._source.api_user, 'password': new Buffer(response.body._source.api_password, 'base64').toString("ascii"), 'url': response.body._source.api_url, 'insecure': response.body._source.insecure });
} else {
callback({ 'error': 'no credentials', 'error_code': 1 });
}
} else {
callback({ 'error': 'no elasticsearch', 'error_code': 2 });
}
});
};
}