Skip to content

Commit 17b9751

Browse files
committed
Initial commit
0 parents  commit 17b9751

File tree

10 files changed

+231
-0
lines changed

10 files changed

+231
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules
2+
logs
3+
.DS_Store

activity.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
var client = require('./lib/client.js');
2+
3+
var values = (function(){
4+
var args=process.argv.splice(2);
5+
6+
if(args.length==0){
7+
console.log('Usage: ./activity.js -t <timeout> -m "<message>"');
8+
process.exit(1);
9+
}
10+
var arg_parser = {
11+
"-t" : function(val){
12+
var unit = val.charAt(val.length-1);
13+
var nval = parseInt(val.substring(0,val.length-1));
14+
switch(unit){
15+
case 's':
16+
return nval*1000;
17+
case 'm':
18+
return nval*1000*60;
19+
case 'h':
20+
return nval*1000*60*60;
21+
default:
22+
throw Error('Could not parse timeout! '+val);
23+
}
24+
},
25+
"-m" : function(val){
26+
return val;
27+
}
28+
};
29+
30+
var vals = {
31+
"-t" : 0,
32+
"-m" : "?"
33+
};
34+
35+
args.forEach(function(v,i){
36+
if(v.charAt(0)=='-'){
37+
if(arg_parser[v]!==undefined){
38+
if(args.length>=i+2){
39+
vals[v]=arg_parser[v](args[i+1]);
40+
}else{
41+
new Error('No argument for '+v);
42+
}
43+
}else{
44+
throw Error('Could not parse arguments '+v);
45+
}
46+
}
47+
});
48+
return vals;
49+
}());
50+
51+
process.on('uncaughtException', function (err) {
52+
console.log('Caught exception: ' + err);
53+
});
54+
55+
var updater = new client.UpdateClient({
56+
hmmac : require('./etc/hmmac-config.json'),
57+
pusher_cred : require('./etc/pusher-key.json'),
58+
api_cred : require('./etc/api-key.json'),
59+
host : "localhost",
60+
port : "3000"
61+
});
62+
63+
updater.activity(values['-m'],values['-t'], function(res) {
64+
console.log('STATUS: ' + res.statusCode);
65+
res.setEncoding('utf8');
66+
res.on('data', function (chunk) {
67+
console.log(chunk);
68+
});
69+
});
70+
71+

bin/activity

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/sh
2+
3+
node ../activity.js $*

bin/cpu-daemon

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/sh
2+
3+
node ../cpu-daemon.js &> ../logs/cpu-daemon.log &

cpu-daemon.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
var monitor = require('./lib/iostat'),
2+
Pusher = require('node-pusher'),
3+
fs = require('fs');
4+
5+
process.on('uncaughtException', function (err) {
6+
console.log('Caught uncaught exception: ' + err);
7+
});
8+
9+
var cred = require('./etc/pusher-key.json');
10+
var pusher = new Pusher(cred);
11+
12+
monitor.cpu(3)
13+
.on('update',function(val){
14+
pusher.trigger('macbook','cpu',val);
15+
});

etc/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
api-key.json
2+
pusher-key.json

etc/hmmac-config.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"headerPrefix": "Hmmac",
3+
"hash": "sha1",
4+
"serviceLabel": "HMAC"
5+
}

lib/client.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
var http = require("http"),
2+
Hmmac = require('hmmac'),
3+
crypto = require('crypto'),
4+
Pusher = require('node-pusher');
5+
6+
7+
var default_config = {
8+
host : "leightonturner.org",
9+
port : 80,
10+
hmmac : {
11+
headerPrefix:'Hmmac',
12+
hash: 'sha1',
13+
serviceLabel: 'HMAC'
14+
},
15+
pusher_cred : {},
16+
api_cred : {}
17+
};
18+
19+
var UpdateClient = exports.UpdateClient = function(config){
20+
var cfg = {};
21+
for(var m in default_config){
22+
config[m] ? cfg[m] = config[m] : cfg[m] = default_config[m];
23+
}
24+
this.config = cfg;
25+
this.hmmac = new Hmmac(this.config.hmmac);
26+
}
27+
28+
UpdateClient.prototype.activity = function(msg, timeout,fn){
29+
30+
var data = JSON.stringify({
31+
msg : msg,
32+
timeout : timeout
33+
});
34+
35+
var hmac_cred = {
36+
accessKeyId : this.config.api_cred.user,
37+
accessKeySecret : this.config.api_cred.secret
38+
};
39+
40+
var opts = {
41+
host : this.config.host,
42+
port : this.config.port,
43+
path : "/activity",
44+
method : 'PUT',
45+
data : data,
46+
headers : {
47+
'Content-Type' : 'application/json',
48+
'Content-MD5' : crypto.createHash('md5').update(data || '').digest('hex')
49+
}
50+
};
51+
52+
var signed_request = this.hmmac.signHttpRequest(hmac_cred, opts);
53+
//console.log(signed_request);
54+
55+
console.log("Attempting to update server activity cache");
56+
var req = http.request(signed_request,fn);
57+
req.write(data);
58+
req.end();
59+
60+
var pusher = new Pusher(this.config.pusher_cred);
61+
console.log('Pushing message to connected clients');
62+
pusher.trigger('macbook','activity',{
63+
msg : msg,
64+
timeout : timeout
65+
});
66+
67+
}
68+

lib/iostat.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
2+
var EventEmitter = require('events').EventEmitter,
3+
spawn = require('child_process').spawn;
4+
5+
var CPU = exports.CPU = function(interval){
6+
7+
var self = this;
8+
9+
this.proc = spawn('iostat',['-w',interval]);
10+
11+
this.proc.stdout.setEncoding('utf8');
12+
this.proc.stdout.on('data',function(data){
13+
try {
14+
self.update(data);
15+
} catch (err) {
16+
self.kill();
17+
throw err;
18+
}
19+
});
20+
21+
this.proc.stderr.setEncoding('utf8');
22+
this.proc.stderr.on('data', function (data) {
23+
self.kill();
24+
self.emit('error',data);
25+
});
26+
27+
this.proc.on('exit', function (code) {
28+
console.log('CPU monitoring process exited: ' + code);
29+
});
30+
31+
process.on('exit',function(){
32+
console.log("CPU has recognised that the parent process is exiting");
33+
self.kill();
34+
});
35+
}
36+
37+
CPU.prototype.__proto__ = EventEmitter.prototype;
38+
39+
CPU.prototype.update = function(data){
40+
var tokens = data.split(/[ ]+/);
41+
//Subtract the idle cpu percent from 100 and add two random decimal places for effect.
42+
this.emit('update',100-tokens[tokens.length-4]+(Math.floor((Math.random()*100)+1)/100));
43+
};
44+
45+
CPU.prototype.kill = function(){
46+
this.proc.kill();
47+
}
48+
49+
var cpu = exports.cpu = function(interval){
50+
return new CPU(interval);
51+
}
52+

package.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "ltorg-client"
3+
, "version": "0.0.1"
4+
, "private": true
5+
, "dependencies": {
6+
"node-pusher" : "*",
7+
"hmmac" : "0.1.1"
8+
}
9+
}

0 commit comments

Comments
 (0)