Skip to content

Commit

Permalink
First commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
vesln committed Jan 11, 2012
0 parents commit 97726a1
Show file tree
Hide file tree
Showing 10 changed files with 520 additions and 0 deletions.
13 changes: 13 additions & 0 deletions .gitignore
@@ -0,0 +1,13 @@
node_modules/
*._
*.tmp
.monitor
*.diff
*.err
*.orig
*.rej
*.swo
*.swp
*.vi
*~
.DS_Store
9 changes: 9 additions & 0 deletions Makefile
@@ -0,0 +1,9 @@
TESTS = $(shell find test/ -iname \*.test.js)

test:
@NODE_ENV=test ./node_modules/.bin/mocha \
--require should \
--reporter spec \
$(TESTS)

.PHONY: test
105 changes: 105 additions & 0 deletions Readme.md
@@ -0,0 +1,105 @@
[![Build Status](https://secure.travis-ci.org/vesln/moni.png)](http://travis-ci.org/vesln/moni)

# word - Text this.

![screenshot](http://img254.imageshack.us/img254/3719/monilogo.png)


http://github.com/vesln/moni

## Description

Process monitoring with node.

## Synopsis

### Pid:

```js

var Moni = require('moni');
var watcher = new Moni(44444);

watcher.on('end', function() {
console.log('dead');
});

watcher.watch(1000);

```

### Group of pids:

```js

var Moni = require('moni');
var watcher = new Moni([44444, 33333, 12345]);

watcher.on('end', function() {
console.log('dead');
});

watcher.watch(1000);

```

### Command:

```js

var Moni = require('moni');
var watcher = new Moni('redis-server');

watcher.on('start', function() {
console.log('up');
});

watcher.on('end', function() {
console.log('dead');
});

watcher.watch(1000);

```

## Requirements

- NPM (http://npmjs.org/)
- Node.js 0.6 (http://nodejs.org/)

## Install

```
$ npm install moni
```

## Tests

```
$ npm install
$ make test
```

## License

MIT License

Copyright (C) 2012 Veselin Todorov

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
18 changes: 18 additions & 0 deletions examples/index.js
@@ -0,0 +1,18 @@
/*!
* moni - Process monitoring with node.
*
* Veselin Todorov <hi@vesln.com>
* MIT License.
*/
var Watcher = require('../');
var watcher = new Watcher('redis-server');

watcher.on('start', function() {
console.log('redis is up.');
});

watcher.on('end', function() {
console.log('redis is down.');
});

watcher.watch(100);
22 changes: 22 additions & 0 deletions lib/moni.js
@@ -0,0 +1,22 @@
/*!
* moni - Process monitoring with node.
*
* Veselin Todorov <hi@vesln.com>
* MIT License.
*/

/**
* Dependencies.
*/
var package = require('package')(module);
var Watcher = require('./watcher');

/**
* Exposing `Watcher`.
*/
module.exports = Watcher;

/**
* Exposing version.
*/
module.exports.version = package.version;
170 changes: 170 additions & 0 deletions lib/watcher.js
@@ -0,0 +1,170 @@
/*!
* moni - Process monitoring with node.
*
* Veselin Todorov <hi@vesln.com>
* MIT License.
*/

/**
* Dependencies.
*/
var events = require('events');
var exec = require('child_process').exec;

/**
* toString alias.
*
* @type {Function}
*/
var toString = Object.prototype.toString;

/**
* Watcher constructor.
*
* @param {String|Number|Array} pid or process name.
*/
function Watcher(pids) {
this._processes = {};
this.pids(pids);
};

/**
* Extends EventEmitter.
*/
Watcher.prototype.__proto__ = events.EventEmitter.prototype;

/**
* Parses pids.
*
* @param {String|Number|Array} pid or process name.
* @param {Function} callback.
*/
Watcher.prototype.parse = function(pids, cb) {
var self = this;

switch(true) {
case Array.isArray(pids):
cb.call(this, this.pids(pids));
break;

case toString.call(pids) === '[object Number]':
cb.call(this, this.pids([pids]));
break;

default:
this.pidof(pids, function(pid) {
cb.call(self, self.pids(pid));
});
}
};

/**
* Pids accessor.
*
* @param {Array} pids
* @returns {Mixed}
*/
Watcher.prototype.pids = function(pids) {
if (arguments.length === 1) {
if (!Array.isArray(pids)) pids = [pids];
this._pids = pids;
}
return this._pids;
};

/**
* Interval accessor.
*
* @param {Array} pids
* @returns {Mixed}
*/
Watcher.prototype.interval = function(interval) {
if (arguments.length === 1) {
this._interval = interval;
}
return this._interval;
};

/**
* Find pidof process.
*
* @param {String} process
* @param {Function} cb
*/
Watcher.prototype.pidof = function(str, cb) {
var pids = null;
var pid = null;
exec('pidof ' + str, function (error, stdout, stderr) {
pids = stdout.split(/\s+/);
pids.forEach(function(pid, i) {
if (!pid) pids.splice(i, 1);
});
if (pids[0]) pid = pids[0];
cb(pid);
});
};

/**
* Starts to monitor processes.
*
* @param {Number} interval.
*/
Watcher.prototype.watch = function(interval) {
var self = this;
this.interval(interval);

this.pids().forEach(function(pid) {
self.check(pid, function(live) {
self._processes[pid] = live;
self.start(pid, interval);
});
});
};

/**
* Initializes the continious monitoring.
*
* @parm {Number} pid
* @param {Number} interval
*/
Watcher.prototype.start = function(pid, interval) {
var self = this;
setInterval(function() {
self.check(pid, function(live) {
if (live !== self._processes[pid]) {
self._processes[pid] = live;
self.notify(pid, live);
}
});
}, interval);
};

/**
* Emits a start/end event.
*
* @parm {Number} pid
* @param {Boolean} state
*/
Watcher.prototype.notify = function(pid, state) {
var name = (state) ? 'start' : 'end';
this.emit(name, pid);
};

/**
* Checks if process is live.
*
* @param {Number} pid
* @param {Function} cb
*/
Watcher.prototype.check = function(pid, cb) {
this.parse(pid, function(p) {
exec('kill -0 ' + p, function(error, stdout, stderr) {
cb(!(stderr));
});
});
};

/**
* Exposing `Watcher`.
*/
module.exports = Watcher;
26 changes: 26 additions & 0 deletions package.json
@@ -0,0 +1,26 @@
{
"name": "moni"
, "version": "0.0.1"
, "description": "Process monitoring with node."
, "keywords": ["monitoring", "process monitoring"]
, "author": "Veselin Todorov <hi@vesln.com>"
, "dependencies": {
"package": "1.0.0"
}
, "devDependencies": {
"mocha": "0.3.3"
, "should": "0.3.2"
}
, "repository" : {
"type" : "git"
, "url" : "http://github.com/vesln/moni.git"
}
, "homepage": "http://github.com/vesln/moni"
, "scripts": {
"test": "make test"
}
, "main": "./lib/moni"
, "engines": {
"node": ">= 0.6.0 < 0.7.0"
}
}

0 comments on commit 97726a1

Please sign in to comment.