Like us on Facebook!

Monday, 15 April 2013

zeroMQ with PHP and next challenge



So this time round I wanted to experiment with PHP so that I can implement the front end for my little messaging application.

Why PHP? Because it's something that I already know and zeroMQ supports it :)

On the same server I have had to install a few things to get it working.

1] phpize5 - check that you have it available (you could also use phpinfo())
# phpize5 --help
Usage: /usr/bin/phpize5 [--clean|--help|--version|-v]

2] if not then you probably don't have php5-dev 
# apt-get install php5-dev

3] clone git repo for php-zmq
# git clone https://github.com/mkoppanen/php-zmq.git
# cd php-zmq.git

4] then follow the guide to finish it off ...
# phpize
# ./configure
# make && make install
# make test

5] add extension=zmq.so to php.ini
6] lastly
# apache2ctl graceful



So below is my PHP code. There's nothing special about it.

  1. Firstly I create my context
  2. subscribe to the socket on port 5556 since this is the port that the sender server sends messages out on
  3. created a small loop of 100 and each time reading from the socket

<html>
  <body>
    <?php
       $context = new ZMQContext();
       //  Socket to talk to server
       $subscriber = new ZMQSocket($context, ZMQ::SOCKET_SUB);
       $subscriber->connect("tcp://localhost:5556");
       $subscriber->setSockOpt(ZMQ::SOCKOPT_SUBSCRIBE, "");
       $i = 0; 
           while( $i < 100 ) {
                $string = $subscriber->recv();
                echo $i."-".$string."<br>";
                $i++;
           }
     ?>
   </body>
</html>


<snip>
...
10- docho: dog:1363660548
11- docho: dog:1363660549
12- docho: dog:1363660550
13- docho: dog:1363660551
14- docho: dog:1363660552
15- docho: dog:1363660553
...
</snip>


Challenge

Right now I am creating a random loop to read messages from the queue.

What I need to do is let the web front know when there is a message to display.

I have a couple of ideas on how to do this ...

  1. memcached - submit every message in memory and let the client loop. But doesn't solve the trigger problem
  2. add the messages to database and let the client look for updates rows. Not so efficient. 
  3. HTML5 webworks? The Web Workers specification defines an API for spawning background scripts in your web application. Web Workers allow you to do things like fire up long-running scripts to handle computationally intensive tasks, but without blocking the UI or other scripts to handle user interactions. They're going to help put and end to that nasty 'unresponsive script' dialog that we've all come to love. So the worker will connect to the port and update the div that contains the message holder. Thoughts? http://www.html5rocks.com/en/tutorials/workers/basics/ 

Saturday, 13 April 2013

Is Python the right language for Sys Administrators?


Yesterday at work the big boss told me to give up on Perl and to start doing everything in Python!

So this took me a few years back when I did all my sys stuff in bash before I got exposed to Perl. I found it easy to pick up and the online community helped quite a bit! 

My natural reaction when I get asked "hey, I need you to do this and it's urgent" is to do it using Perl because >

a) I feel comfortable 
b) it does the job 
c) it's urgent. 

It has pretty much all the modules that I need to get by - web programming, database programming, OO programming, and general stuff like threading, MIME handling ...

So I have used Perl in pretty much everything that I do - log parsing, cgi, reporting, system checks and now automation.

I spoke to a few guys at work and they have said good things about Python and it's ability to use C libraries. 

So what is your experience with Python? Is it the right tool for Sys Admin? Should I stop using Perl? :)

"Python is a programming language that lets you work more quickly and integrate your systems more effectively. You can learn to use Python and see almost immediate gains in productivity and lower maintenance costs."

Python lets you work more quickly ... how? I was told that parsing large logs takes quite a bit. They say Python is easy but Perl is a pain. It's partially true about Perl but maybe that's because they haven't read the doc very well ... this was the case for me :)

How does it integrate with your system *more* effectively?

http://silicainsilico.wordpress.com/2012/03/26/switching-from-perl-to-python-speed/ [Speed Test]
http://strombergers.com/python/ [Python is Cool (and Perl is not), Especially for C/C++ Programmers]
http://www.revolves.net/perl-vs-python-the-final-battle/



handy line to lookup country using IP address/hostname





Here is a handy lookup tool for all admins out there ... You can use both IP and FQDN (fully qualified domain name). Use of this tool is when you have a few servers scattered all over the globe and you want to know where it is attached ...




geoiplookup(1) - Linux man page


Name

geoiplookup - look up country using IP Address or hostname

Synopsis

geoiplookup [-d directory] [-f filename] [-v] <ipaddress|hostname>

Description

geoiplookup uses the GeoIP library and database to find the Country that an IP address or hostname originates from.
For example
geoiplookup 80.60.233.195
will find the Country that 80.60.233.195 originates from, in the following format:
NL, Netherlands

Options

-fSpecify a custom path to a single GeoIP datafile.
-d
Specify a custom directory containing GeoIP datafile(s). By default geoiplookup looks in /usr/share/GeoIP
-v
Lists the date and build number for the GeoIP datafile(s).

Author

Written by T.J. Mather