Blog

  • Here is what happens when I recovery from surgery.

    After being told that I would need to take five days to recovery from a recent surgery and being informed that they would prefer me not to work on any of the system at work well under the influence of pain killers, I started looking for thing to kill time.

    Well trying to find something to do for 5 days and not having much luck, I was surfing the web using Google’s Chrome Web Browser, I started looking at their “New Tab Apps Feature” or whatever it’s called. I started thinking that would be really simple to create a web based version of the application that did roughly the something, but wasn’t dependent on the web browser. It would also be handy for all those table devices that have web browsers. It’s kind of a pain to hit those little links with fat fingers like mine. After doing a quick design in my head, I figured what the heck; I got nothing better to do.

    I decided to go old school LAMP on it with Linux, Apache, MySQL, and PERL. I decided to use mod_perl and MASON as my frame work, MASON being a throw back from my short stent at Amazon.com. I went with the old MVC architecture, since it’s the easiest for a one node system and because of my state of the art Dual Pentium Pro 180 MHZ, with 256G of RAM, and 14G 5400 RPM Hard Drive for the server that I was building it on.

    After 5 days, a few additional week nights and Saturdays, here is what I came up withhttp://www.myapplinks.com. It is still in the Alpha/Beta stage by not a bad start.

  • Using CURL to manage Tomcat

    The other day I and a few of my colleges were talking about a easy way to deploy and undeploy war files from the command line like you could through the Tomcat Web Application Manager portal and being on a python kick, I started writing it in python. After an hour or two I realized that I had made this way more complex then I need to.  I had been reading theApache Tomcat 6.0 Manager App HOW-TO and I was using curl to test all the commands from localhost.

    shell> curl –anyauth -u admin:password http://localhost:8080/manager/start?path=/myapp

    So now after slapping myself in forehead and saying “duh!”. I decided I could write this as shell script and have it knock out in 20 minutes.

    So here what I came up with tomcat-cli.sh.

    –Cheers

     

  • Simple HTTP Server with Python

    Ever needed a quick web server to share something with a Windows user from you Linux box.  Python has really easy to use embedded HTTP Server. Just try the following.shell> python -m SimpleHTTPServer 9001

    And point you web browser at http://localhost:9001 and see what happens.

    — Cheers

     

  • Slacking over the Holidays

    So December was an interesting month for me, with a few changes in my personal career and the holidays. I take a week off. Which of course lead me spending two weeks catching up on work and to top it off my boss or someone above him decided that we need to take look at a our service architecture. So of course I am now in research mode, which should hopefully lead to some good blog post. Stay tuned.

  • Connecting PHP to An Oracle Instance on RedHat or CentOS 5

    Here lately it seems that everyone wants to connect to Oracle, but I have to admit this was the first time someone asked me to get PHP to talk to Oracle. It was a lot less painful then I thought it would be, so here is what I did.

    A long with the standard PHP RPMs you need to install a couple of additional RPMs from Oracle. These are oracle-instantclient-basic and oracle-instantclient-devel which can be downloaded from http://www.oracle.com/technetwork/database/features/instant-client/index-100365.html. You will also need php-oci8 RPM which can be download fromhttp://oss.oracle.com/projects/php/files/EL5/.

    So after you have downloaded the RPMs go a head and install the packages and create the symlink for libcIntsh.so.

    $ rpm -Uvh oracle-instantclient-basic-##.#.#.rpm
    $ rpm -Uvh oracle-instantclient-devel-##.#.#.rpm
    $ cd /usr/include/oracle/##.#/[client|client64]
    $ ln –s libclntsh.so.##.# libclntsh.so

    Now you are going to want to setup you environment settings, It is important to set all Oracle environment variables before starting Apache or running a PHP script,  so that the OCI8 process environment is correctly initialized. Setting environment variables in PHP scripts can lead to obvious or non-obvious problems. You can also add Instant Client library path to /etc/ld.so.conf.

    $ LD_LIBRARY_PATH=/usr/lib/oracle/##.#/[client|client64]/lib:${LD_LIBRARY_PATH}
    $ export LD_LIBRARY_PATH

    And now for the big finish. Here is a simple connection script to test it all out.

    <?php
    $c = oci_connect( ‘USERNAME’,
    ‘PASSWORD’,
    ‘SERVERNAME:PORT/SERVICE_NAME’, INSTANCE_NAME’ );

    if( $c ) {

    $s = oci_parse( $c, ‘SELECT TABLE_NAME FROM all_tables’ );

    oci_execute($s) ;

    while($res = oci_fetch_array( $s, OCI_ASSOC) ) {

    echo $res[‘TABLE_NAME’] . “\n”;
    }

    }
    ?>

    For a complete list of function and additional install resources check out the following sites:

    http://php.net/manual/en/book.oci8.php
    http://wiki.oracle.com/page/PHP
    http://www.oracle.com/technetwork/articles/technote-php-instant-084410.html

  • Quick Start To Using MongoDB with Python on Linux

    With it’s rapid growth in popularity MongoDB is quickly becoming one of the top NoSQL Databases out there and with Python being one of the top ten programming languages according to Tiobe Software’s Programming Community Index. I’ve decided to write a quick how-to to show you just how easy it is to get started with MongoDB and Python.

     

    Assuming that you all ready have Python installed on your system. We’ll start with downloading and installing MongoDB. The first that you will need to do is download the appropriate package from http://www.mongodb.org/downloads to your /tmp directory.

     

    $ curl http://fastdl.mongodb.org/linux/mongodb-linux-i686-1.6.3.tgz > /tmp/mongo.tgz

     

    Once you have downloaded the correct package, find a suitable directory to unpack it and move it to, such as /opt/mongodb.

     

    $ cd /tmp

    $ tar -zxf mongo.tgz

    $ sudo mv mongodb-linux-i686-1.6.3 /opt/mongodb

     

    Now you will need to create the data directory. By default, MongoDB stores it’s data in “/data/db”, but if for some reason you need to change the location of the data directory you will need to use the “–dbpath” option when starting the server. However I am sticking with the default directory.

     

    $ sudo mkdir -p /data/db

    $ sudo chown -R owerid /data/db

     

    You can start MongoDB with the following command.

     

    $ /opt/mongodb/bin/mongod

     

    Now, test it out by using the MongoDB shell to connect to the server as follows.

     

    $ /opt/mongodb/bin/mongo

    MongoDB shell version: 1.6.3

    connecting to: test

    > db.foo.save( { Message : “Hello World” } )

    > db.foo.find()

    { “_id” : ObjectId(“4cdd92bc4f6fb75dd2a7642d”), “Message” : “Hello World” }

    >

     

    Once you have verified that MongoDB is working you will need to download the Python driver for it which are called “PyMongo”. If you have the Python “setuptools” installed you should be able to do “easy_install pymongo” to install the drivers. Otherwise you can download the project source from http://pypi.python.org/pypi/pymongo/ to install the MongoDB drivers.

     

    $ curl http://pypi.python.org/packages/source/p/pymongo/pymongo-1.9.tar.gz  > /tmp/pymongo-1.9.tar.gz

    $ tar -zxf pymongo-1.9.tar.gz

    $ cd pymongo-1.9/

    $ python setup.py install

     

    After everything is installed it’s time to test it all out, so just pop open your favor Python IDE or the Python Console and give it a go. Here is a simple example.

     

    $ python

    Python 2.4.3 (#1, Nov 11 2010, 13:34:43)

    [GCC 4.1.2 20080704 (Red Hat 4.1.2-48)] on linux2

    Type “help”, “copyright”, “credits” or “license” for more information.

    >>> import pymongo

    >>> connection = pymongo.Connection( “localhost”, 27017 )

    >>> db = connection.test

    >>> db.foo.save({ “Message” : “Hello World 2” })

    ObjectId(‘4cdd95bfe1382330b5000000′)

    >>> for message in db.foo.find():

    … print message

    {u’Message’: u’Hello World’, u’_id’: ObjectId(‘4cdd92bc4f6fb75dd2a7642d’)}

    {u’Message’: u’Hello World 2′, u’_id’: ObjectId(‘4cdd95bfe1382330b5000000’)}

    >>>

    $

     

    For more information on the PyMongo check out the website athttp://api.mongodb.org/python/1.9%2B/index.html.

     

  • Connecting Python to Oracle.

    In the current world of heterogeneous networks it seems that you need to be able to connect to ever type of database system out there. In a recent project, I was asked, to take an existing MySQL and Python system and get it to connect to an existing Oracle Data Warehouse system. To be honest, I hadn’t actually ever connected Python to an Oracle Database before and from experience this would be either very painful or pretty straight forward.

    Of course like everything Python it was pretty straight forward. The first thing you will need to get is the correct cx_Oracle module from http://cx-oracle.sourceforge.net/. The cx_Oracle module allows you to connect to Oracle databases and it conforms to the Python database API specification. Which makes life easier for everyone. After you have installed the module the rest is just a matter having the right permission to connect to the Oracle Database and writing the code.

    Here is quick script showing you how to connect to Oracle with Python.

    #!/usr/bin/python

    import cx_Oracle

    connstr=’scott/tiger’
    conn = cx_Oracle.connect(connstr)
    curs = conn.cursor()
    curs.arraysize=50
    curs.execute(‘select 2+2 “aaa” ,3*3 from dual’)
    print curs.description
    print curs.fetchone()
    conn.close()

    For more information check out the Python Programming Language – Official Website

  • Why You Should Looking at Python.

    Python is an interpreted, interactive, object-oriented programming language. Python includes modules, classes, exceptions, very high level dynamic data types and dynamic typing. Python has also been one of the top 10 programming languages, since May of 2004 according toTiobe Software’s Programming Community Index. Python is being used by some big named companies such as Google, Yahoo, Walt Disney, and Industrial Light & Magic to name a few. Above all else the learning curve is simple.

    Here is a quick script that show you how easy it is to connect to a MySQL Database

    #!/usr/bin/python
    # Import the MySQL Module
    import MySQLdb
    #Create the database connection
    conn = MySQLdb.connect (host = “localhost”, user = “rhosto”, passwd = “biteme”, db = “test”)
    #create and execute the sql query
    cursor = conn.cursor()
    cursor.execute (“SELECT VERSION()”)
    #get and print out the results
    row = cursor.fetchone ()
    print “server version:”, row[0]
    #close you database connection
    cursor.close ()
    conn.close ()

  • Calculating The Percentage of Max Connections in with MySQL 5.1.9 and higher

    For longest time it had bugged me that there wasn’t an easy way to put “SHOW STATUS”, “SHOW VARIABLES”, or “SHOW PROCESSLIST” variables into queries, so I could easily calculate stuff like max connections percentage. Then MySQL 5.1.9 came out and you could final start to do SELECT statements on information_schema.GLOBAL_STATUS, information_schema.SESSION_STATUS, information_schema.GLOBAL_VARIABLES, information_schema.SESSION_VARIABLES and information_schema.PROCESSLIST.

    The STATUS and VARIABLES tables have basically two columns VARIABLE_NAME and VARIABLE_VALUE. These correspond with the “SHOW STATUS” and “SHOW VARIABLES” command’s Variable_name and Value. The PROCESSLIST table has all the same columns as the “SHOW PROCESSLIST” command.

    So now with that being said here is a quick little query that returns the Max Connection Limit, Connection Count, and Connect Percentage of max allowed connections.

    /* requires MySQL 5.1.19 or higher */

    SELECT v.VARIABLE_VALUE as “Max Connections”,
    s.processcount as “Connection Count”,
    ((s.processcount/v.VARIABLE_VALUE)*100) as “% of max connections allowed”
    FROM information_schema.GLOBAL_VARIABLES v,
    (SELECT (@proc_connect:=count(*)) processcount FROM information_schema.PROCESSLIST) s
    WHERE v.VARIABLE_NAME = ‘max_connections’;

     

  • MySQL Functions for Converting IP Address to an Integer and Back Again

    So after my MySQL server-id idea. I was asked to come up with a function to Convert IP Address. So here it is.

    use test;
    DROP FUNCTION IF EXISTS IpToInteger;
    DELIMITER //
    CREATE FUNCTION IpToInteger ( ipAddress CHAR(15) )
    RETURNS INT UNSIGNED
    DETERMINISTIC
    BEGIN
    DECLARE o1,o2,o3,o4 char(3);
    DECLARE IpInteger INT UNSIGNED;
    SET o1 = REPLACE(SUBSTRING(SUBSTRING_INDEX( ipAddress, ‘.’, 1), LENGTH(SUBSTRING_INDEX( ipAddress, ‘.’, 1 – 1) ) + 1), ‘.’, ” );
    SET o2 = REPLACE(SUBSTRING(SUBSTRING_INDEX( ipAddress, ‘.’, 2), LENGTH(SUBSTRING_INDEX( ipAddress, ‘.’, 2 – 1) ) + 1), ‘.’, ” );
    SET o3 = REPLACE(SUBSTRING(SUBSTRING_INDEX( ipAddress, ‘.’, 3), LENGTH(SUBSTRING_INDEX( ipAddress, ‘.’, 3 – 1) ) + 1), ‘.’, ” );
    SET o4 = REPLACE(SUBSTRING(SUBSTRING_INDEX( ipAddress, ‘.’, 4), LENGTH(SUBSTRING_INDEX( ipAddress, ‘.’, 4 – 1) ) + 1), ‘.’, ” );
    SELECT ( o1 << 24 ) + ( o2 << 16 ) + ( o3 << 8 ) + o4 INTO IpInteger;
    RETURN IpInteger;
    END//
    DELIMITER ;

    use test;
    DROP FUNCTION IF EXISTS IntegerToIp;
    DELIMITER //
    CREATE FUNCTION IntegerToIp( ipInteger INT UNSIGNED )
    RETURNS CHAR(16) DETERMINISTIC
    BEGIN
    DECLARE o1,o2,o3,o4 INT UNSIGNED;
    SET o1 = ( ipInteger >> 24 );
    SET o2 = ( ipInteger >> 16 << 16 )-( ipInteger >> 24 << 24) >> 16;
    SET o3 = (ipInteger >> 8 << 8 ) – ( ipInteger >> 16 << 16 ) >> 8;
    SET o4 = ipInteger – ( ipInteger >> 8 << 8 );
    RETURN CONCAT( o1, “.” , o2, “.” , o3, “.”, o4 );
    END//
    DELIMITER ;

    mysql> SELECT IpToInteger ( '10.8.208.30' ); +-------------------------------+ | IpToInteger ( '10.8.208.30' ) | +-------------------------------+ | 168349726 | +-------------------------------+ 1 row in set (0.00 sec) mysql> SELECT IntegerToIp( 168349726 ); +--------------------------+ | IntegerToIp( 168349726 ) | +--------------------------+ | 10.8.208.30 | +--------------------------+ 1 row in set (0.00 sec) mysql>