rename some commands & update readme

This commit is contained in:
Sami Samhuri 2013-06-08 22:26:03 -07:00
parent f8a09636d1
commit 36930d666d
5 changed files with 76 additions and 54 deletions

View file

@ -11,28 +11,27 @@ npm install -g kwikemon
## Usage ## Usage
$ kwikemond & $ kwikemond &
$ curl -s localhost/nginx_status | grep Active | kwikemon nginx-connections $ curl -s localhost/nginx_status | grep Active | kwikemon write nginx-connections
$ curl localhost:1111/nignx-connections $ curl localhost:1111/nignx-connections
Active connections: 316 Active connections: 316
$ kwikemon foo bar $ kwikemon set foo bar
$ curl localhost:1111/ $ curl localhost:1111/
foo: bar foo: bar
nginx-connections: Active connections: 316 nginx-connections: Active connections: 316
Here's how it works: Here's how it works:
- call `kwikemon thing status` to set the text for the monitor named "thing" - call `kwikemon set thing status` to set the text for the monitor named "thing"
- fire up the server, `kwikemond`, that serves up these monitors in a big list or individually - fire up the server, `kwikemond`, that serves up these monitors in a big list or individually
Alternatively: Alternatively:
- continuously pipe data to `kwikemon <name of thing you are watching>` on stdin - continuously pipe data to `kwikemon write <name of thing you are watching>` on stdin
- every time a full line of text is received on stdin it becomes the new status for <name of thing you are watching> - every time a full line of text is received on stdin it becomes the new status for <name of thing you are watching>
To see everything `kwikemon` can do run it without arguments. To see everything `kwikemon` can do run `kwikemon help`.
# or with -h or --help $ kwikemon help
$ kwikemon
This is very much a work in progress. This is very much a work in progress.
@ -44,12 +43,12 @@ You can use kwikemon as a library.
var kwikemon = require('kiwkemon'); var kwikemon = require('kiwkemon');
kwikemon.set('foo', 'bar', function(err) { kwikemon.set('foo', 'bar', function(err) {
kwikemon.fetch('foo', function(err, text) { kwikemon.get('foo', function(err, text) {
console.log('foo = ' + text); console.log('foo = ' + text);
}); });
}); });
kwikemon.fetchAll(function(err, monitors) { kwikemon.getAll(function(err, monitors) {
Object.keys(monitors).forEach(function (name) { Object.keys(monitors).forEach(function (name) {
console.log(name + ' = ' + monitors[name]); console.log(name + ' = ' + monitors[name]);
}); });
@ -70,28 +69,14 @@ A monitor named `nginx` stores its data in the hash `kwikemon:monitor:nginx`. Ha
are: are:
- text - text
- expire
- created - created
- modified - modified
- updates - updates
The list of all monitors is a set stored at `kwikemon:monitors`. The list of all monitors is a set stored at `kwikemon:monitors`.
#### List #### Set
This is when you should clean out expired entries.
names = redis.smembers("kwikemon:monitors")
monitors = {}
for name in names:
if redis.exists("kwikemon:monitor:$name"):
monitors[name] = redis.hget("kwikemon:monitor:$name", "text")
return monitors
#### Read
redis.hgetall("kwikemon:monitor:nginx")
#### Update
exists = redis.exists("kwikemon:monitor:nginx") exists = redis.exists("kwikemon:monitor:nginx")
if exists: if exists:
@ -111,11 +96,48 @@ This is when you should clean out expired entries.
# optional # optional
redis.expire("kwikemon:monitor:nginx", <ttl>) redis.expire("kwikemon:monitor:nginx", <ttl>)
#### Delete #### Get
redis.hgetall("kwikemon:monitor:nginx")
#### Remove
redis.del("kwikemon:monitor:nginx") redis.del("kwikemon:monitor:nginx")
redis.srem("kwikemon:monitors", "nginx") redis.srem("kwikemon:monitors", "nginx")
#### Sweep
Clean out expired monitors. Call this before anything that relies on counting or iterating through all monitors.
for name in redis.smembers("kwikemon:monitors"):
if not redis.exists("kwikemon:monitor:$name"):
remove(name)
#### Count
Sweep before running a count.
sweep()
redis.scard("kwikemon:monitors")
#### List names
Sweep before listing.
sweep()
redis.smembers("kwikemon:monitors")
#### Get all
Sweep before geting all.
sweep()
monitors = {}
for name in list():
if redis.exists("kwikemon:monitor:$name"):
monitors[name] = get(name)
return monitors
## License ## License

View file

@ -64,7 +64,7 @@ function expire(name, ttl) {
function get(name) { function get(name) {
if (name) { if (name) {
kwikemon.fetch(name, function(err, mon) { kwikemon.get(name, function(err, mon) {
if (mon) { if (mon) {
console.log(mon.text); console.log(mon.text);
process.exit(0); process.exit(0);
@ -82,7 +82,7 @@ function get(name) {
} }
function list() { function list() {
kwikemon.fetchAll(function(err, monitors) { kwikemon.getAll(function(err, monitors) {
Object.keys(monitors).forEach(function(name) { Object.keys(monitors).forEach(function(name) {
console.log(name + ':', monitors[name].text); console.log(name + ':', monitors[name].text);
}); });

View file

@ -8,11 +8,11 @@ module.exports = {
// read // read
, exists: callbackOptional(exists) , exists: callbackOptional(exists)
, fetch: callbackOptional(fetch) , get: callbackOptional(get)
, ttl: callbackOptional(ttl) , ttl: callbackOptional(ttl)
, list: list
, fetchAll: fetchAll
, count: count , count: count
, list: list
, getAll: getAll
// remove // remove
, remove: callbackOptional(remove) , remove: callbackOptional(remove)
@ -56,15 +56,15 @@ function redis(newRedis) {
// that accepts the callback is returned, with the // that accepts the callback is returned, with the
// rest of the arguments fixed (like bind). // rest of the arguments fixed (like bind).
// //
// function fetch(id, cb) { db.fetch(id, cb); } // function get(id, cb) { db.get(id, cb); }
// fetch = callbackOptional(fetch); // get = callbackOptional(get);
// //
// function print(err, x) { if (err) throw err; console.log(x); } // function print(err, x) { if (err) throw err; console.log(x); }
// //
// fetch(1, print); // get(1, print);
// //
// var fetch1 = fetch(1); // var get1 = get(1);
// fetch1(print); // get1(print);
function callbackOptional(fn, ctx) { function callbackOptional(fn, ctx) {
return function() { return function() {
var args = Array.prototype.slice.call(arguments); var args = Array.prototype.slice.call(arguments);
@ -135,7 +135,7 @@ function writer(name) {
return le; return le;
} }
function fetch(name, cb) { function get(name, cb) {
redis().hgetall(k(name), cb); redis().hgetall(k(name), cb);
} }
@ -202,20 +202,20 @@ function list(cb) {
}); });
} }
function fetchAll(cb) { function getAll(cb) {
var monitors = {}; var monitors = {};
list(function(err, names) { list(function(err, names) {
if (err) return cb(err); if (err) return cb(err);
var fetchers = names.sort().map(function(name) { var geters = names.sort().map(function(name) {
return function(done) { return function(done) {
fetch(name, function(err, text) { get(name, function(err, text) {
if (err) return done(err); if (err) return done(err);
monitors[name] = text; monitors[name] = text;
done(); done();
}); });
}; };
}); });
async.parallel(fetchers, function(err, _) { async.parallel(geters, function(err, _) {
if (err) return cb(err); if (err) return cb(err);
cb(null, monitors) cb(null, monitors)
}); });

View file

@ -40,7 +40,7 @@ function handleRequest(req, res) {
name = name.replace(RegExp('\.' + type + '$'), ''); name = name.replace(RegExp('\.' + type + '$'), '');
} }
if (name) { if (name) {
kwikemon.fetch(name, function(err, text) { kwikemon.get(name, function(err, text) {
if (err) { if (err) {
res.end('error: ' + (err.message || 'unknown')); res.end('error: ' + (err.message || 'unknown'));
return; return;
@ -50,7 +50,7 @@ function handleRequest(req, res) {
} }
// all // all
else { else {
kwikemon.fetchAll(function(err, monitors) { kwikemon.getAll(function(err, monitors) {
if (err) { if (err) {
res.end('error: ' + (err.message || 'unknown')); res.end('error: ' + (err.message || 'unknown'));
return; return;

22
test.js
View file

@ -19,7 +19,7 @@ describe("kwikemon", function() {
describe("#set", function() { describe("#set", function() {
it("should set text", function(done) { it("should set text", function(done) {
kwikemon.set('foo', 'bar', function(err) { kwikemon.set('foo', 'bar', function(err) {
kwikemon.fetch('foo', function(err, mon) { kwikemon.get('foo', function(err, mon) {
assert(mon.text == 'bar'); assert(mon.text == 'bar');
done(); done();
}); });
@ -28,7 +28,7 @@ describe("kwikemon", function() {
it("should overwrite text", function(done) { it("should overwrite text", function(done) {
kwikemon.set('foo', 'baz', function(err) { kwikemon.set('foo', 'baz', function(err) {
kwikemon.fetch('foo', function(err, mon) { kwikemon.get('foo', function(err, mon) {
assert(mon.text == 'baz'); assert(mon.text == 'baz');
done(); done();
}); });
@ -92,12 +92,12 @@ describe("kwikemon", function() {
}); });
}); });
describe("#fetch", function() { describe("#get", function() {
it("should fetch the last text monitored", function(done) { it("should get the last text monitored", function(done) {
async.series([ async.series([
kwikemon.set('foo', 'bar') kwikemon.set('foo', 'bar')
, kwikemon.set('foo', 'marcellus') , kwikemon.set('foo', 'marcellus')
, kwikemon.fetch('foo') , kwikemon.get('foo')
], ],
function(err, results) { function(err, results) {
var mon = results[2]; var mon = results[2];
@ -107,8 +107,8 @@ describe("kwikemon", function() {
); );
}); });
it("should fetch null for non-existent monitors", function(done) { it("should get null for non-existent monitors", function(done) {
kwikemon.fetch('non-existent', function(err, mon) { kwikemon.get('non-existent', function(err, mon) {
assert(mon == null); assert(mon == null);
done(); done();
}); });
@ -116,7 +116,7 @@ describe("kwikemon", function() {
}); });
describe("#ttl", function() { describe("#ttl", function() {
it("should fetch the last TTL set", function(done) { it("should get the last TTL set", function(done) {
kwikemon.set('foo', 'bar', { ttl: 300 }, function(err) { kwikemon.set('foo', 'bar', { ttl: 300 }, function(err) {
kwikemon.ttl('foo', function(err, ttl) { kwikemon.ttl('foo', function(err, ttl) {
assert(ttl <= 300); assert(ttl <= 300);
@ -144,13 +144,13 @@ describe("kwikemon", function() {
}); });
}); });
describe("#fetchAll", function() { describe("#getAll", function() {
it("should fetch all monitors", function(done) { it("should get all monitors", function(done) {
async.series([ async.series([
kwikemon.set('a', '1') kwikemon.set('a', '1')
, kwikemon.set('b', '2') , kwikemon.set('b', '2')
, kwikemon.set('c', '3') , kwikemon.set('c', '3')
, kwikemon.fetchAll , kwikemon.getAll
], ],
function(err, results) { function(err, results) {
var monitors = results.pop() var monitors = results.pop()