Archive for the ‘Scripting’ Category

NodeJS How Fun.

Wednesday, September 8th, 2010

I have been playing around with nodejs for a few weeks now and I have to say its pretty amazing what one can do with it. I have been working on a node server that will host real-time queue information using web sockets. Yes I know your going to say web-sockets are only supported in Chrome, Firefox and Safari, Well I have to tell you if your still using IE for your internet experience you really missing out. With nodejs you can attach to child process and execute commands on the system and display them back to the browser. A great example of this is using iostat on linux/unix system. Below is the code for it.

var sys		= require("sys"),
	http	= require("http"),
	ws		= require('./deps/node-websocket-server/lib/ws'),
	fs		= require("fs");

var iostat = require("child_process").spawn("iostat",["-w 1"]);
var httpServer = http.createServer();
var server = ws.createServer({debug:true},httpServer);

// Format iostat date
function format (data){
	var output_data = data.toString();
	console.log(output_data);
	header = 'disk0            cpu       load average';
	if (output_data.match(header)){
		console.log("ignore header");
	}else{
		var output_array = output_data.replace(/^\s+|\s+$/g,"").split(/\s+/);
		for (var i=0; i < output_array.length; i++){
			output_array[i] = parseFloat( output_array[i]);
		};
		output_hash = {
			date:new Date(),
				 disk:{
					kpt:output_array[0],
					tps:output_array[1],
					mbs:output_array[2]
				},
				cpu:{
					us:output_array[3],
					sy:output_array[4],
					id:output_array[5]
				},
				load_average:{
					m1:output_array[6],
					m5:output_array[7],
					m15:output_array[8]
				}
		}
		return JSON.stringify(output_hash);
	}
}
server.addListener("connection",function(conn){
		console.log("opened connection: "+conn.id);
		server.send(conn.id,"connected as:"+conn.id);
		iostat.stdout.on('data',function(data){
			console.log(typeof(data));
			console.log('stdout: '+data);
			console.log('stdout: '+format(data));
			server.send(conn.id, format(data));
		});
		server.send(conn.id, "Connected as: "+conn.id);
});
server.addListener("close",function(conn){
		console.log("closed connection: "+conn.id);
		conn.broadcast("<"+conn.id+"> disconnected");
});
server.listen(9000);

This code allows you to stream iostat’s output over a http server on port 9000 to read by a client html file and js script. Its pretty neat because you can pass that information in to a graphing library like Protovis Graphing. With the addition of web-sockets that means you can have real time CPU information streaming to a web browser. Works great when you have systems where you really want to know how say a MySQL query is effecting the system itself. You should really give it a try.

  • Share/Bookmark

Procmailrc Auto Reply with out Caching the email

Thursday, February 11th, 2010

The other day at work I had to figure out how to accept a email give a reply to anyone that ever sends a email no matter how many times they email the address. Everything I could find had caching in it, with a little digging and poking around the web I found some information that lead me to this solution:

# Auto Reply without caching
# Creator: Richard Genthner
# Date 11-FEB-2010
#
# SENDER the email you want it to look like it came from
# SUBJECT What you want to change the subject to
# FILE the name of the auto reply message
SENDER=bob@example.com
SUBJECT="My Contact information has changed"
FILE=.autoreply_mesg

# Processor this will send the incoming message to /dev/null after sending a auto reply
:0 h c
* !^FROM_DAEMON
* !^FROM_MAILER
* !^X-Loop: $SENDER
| (formail -r -A"FROM:$SENDER" -A"X-Loop: $SENDER" -I"Subject:$SUBJECT";\
cat $HOME/$FILE) | $SENDMAIL -t
:0
/dev/null

Lets break this done in to sections. First we have is this:

SENDER=help@example.com
SUBJECT="Our Support information has changed"
FILE=.autoreply_mesg

In this section we are setting the SENDER which is the email address that we want the message to look like. SUBJECT is the Subject we want to replace the RE: blah blah with something that is informational. FILE is the name if the file containing our message. In the next section we have the following bits:

:0 h c
* !^FROM_DAEMON
* !^FROM_MAILER
* !^X-Loop: $SENDER
| (formail -r -A"FROM:$SENDER" -A"X-Loop: $SENDER" -I"Subject:$SUBJECT";\
cat $HOME/$FILE) | $SENDMAIL -t

:0
/dev/null

So at first this looks cryptic but lets break it down line by line. So frist we have the :0 to accept anything then h and c tell procmail to keep a copy of the email just in case we need to process it more then in just this one block. * !^FROM_DAEMON says if its not from a daemon then proceed tot he next step. * !^FROM_MAILER means if not from a auto mailer aka mailing lists then proceed to the next step. * !^X-Loop: this mean is that if not a mail loop then proceed to next step. Which is the formail command. Let break this down by options, first we have -r which mean Generate an auto-reply header. This will normally throw away all the existing fields, then we have -A which allows us to apply custom information to the header fields, -I that any existing similar fields are simply removed. then we are cat’ing the auto reply file in the body of the message and then passing it off to the sendmail to send the file.

  • Share/Bookmark