Jul 26 2008

mtop – MySQL top

Category: LinuxBipin Balakrishnan @ 9:47 pm

mtop or MySQL top command shows the MySQL commands consuming the greatest time.It is a command similar as top which provides a dynamic real-time view of a running system. Top can display system summary information as well as a list of tasks currently being managed by the Linux kernel.Difference is that mtop show only the MySQL tasks.

Normally, run as a console program this will allow you to see slow or badly optimized queries as they will stay on the screen for a while. However, if you are hunting for short lived queries, running in the manualrefresh mode with a short refresh time will allow you to catch short lived queries as well.
Installing mtop

sudo apt-get install mtop

Tags: , ,


Jul 24 2008

PHP command line agrument

Category: PHPBipin Balakrishnan @ 12:31 pm

php.gif

We can pass command line argument to a PHP script as in any other programming language when we run it in the cli mode.For example make script called test.php in your editor.Copy paste content as follows.

<?php
echo $argv[0];
echo $argv[1];
echo $argv[2];
?>

Run the program as follows with command line argument as follows.

bipin@bipin-laptop:~/php$ php  test.php 10 22

Here the command line arguments are 10 and 22.The PHP script will take the argument as an array argv[] with the first element in the array (agrv[0]) as the program name and the arguments passed being held after that.

Tags: , ,


Jul 18 2008

Asterisk Applications with Ruby On Rails

Category: AsteriskBipin Balakrishnan @ 12:15 pm

rails.jpg

Recently while surfing in google i found that we can develop nice web+telephony applications by the combination of Asterisk and Ruby on Rails.All we required is knowledge in Asterisk and any Framework architecture.I am not much familiar in ruby still managed to develop my first app with it.

The system is built using the following components:
Asterisk – the open source PBX which is used to interface to VOIP/PSTN provider over SIP to place real telephone calls
Ruby on Rails – the super-productive web application framework built in the Ruby programming language
MySQL – database is used to store the records associated with the application data model. All database access is handles by Ruby on Rails.
RAGI – Ruby Asterisk Gateway Interface, an API for building Asterisk telephony applications in Ruby (and Ruby on Rails).

It is assumed that the you are already familiar with:

Asterisk
How to install
How to setup
Basics of what AGI is
Ruby on Rails
How to install
Rails app structure (controllers, views, models, etc)
MySQL
How to install
Basic SQL operations and queries

First Create a Web App with Ruby On Rails

mkdir    ragi_testapp
cd  ragi_testapp
rails tutorial

Create a database called ‘ragidb’

CREATE TABLE `deliveries` (
 `id` int(11) NOT NULL auto_increment,
 `user_id` int(11) NOT NULL default '0',
 `tracking_number` varchar(64) NOT NULL default '',
 `delivery_status` varchar(255) default NULL,
 `updated_on` datetime default NULL,
 `created_on` datetime default NULL,
 PRIMARY KEY (`id`)
 );
CREATE TABLE `users` (
 `id` int(11) NOT NULL auto_increment,
 `phone_number` varchar(10) NOT NULL default '',
 `updated_on` datetime default NULL,
 `created_on` datetime default NULL,
 PRIMARY KEY (`id`) );

Insert some values to the user table.

Database setup for rails

vim ragi_testapp/tutorial/config/database.yml
development:
 adapter: mysql
 database: ragidb
 socket: /var/run/mysqld/mysqld.sock
 timeout: 5000
 username: root
 password:

Enter into the tutorial directory

cd ragi_testapp/tutorial/
 ruby script/generate model User
 ruby script/generate model Delivery
 ruby script/generate controller User

Start Webbrick Server

ruby script/server

Enter into ‘controller’ directory of project

Edit user_controller.rb

class UserController < ApplicationController
 #scaffold :user
 def index
 @users = User.find(:all)
 respond_to do |format|
 format.html # index.html.erb
    end
 end
 end

Create file “index.html.erb” in veiw/user folder of the project

<h1>Listing Users</h1>
<table>
 <tr>
 <th>Phone Number</th>
 <th>Updated date</th>
 <% for user in @users %>
 <tr>
 <td><%=h user.phone_number %></td>
 <td><%=h user.updated_on %></td>
 </tr>
 <% end %>
 </tr>
 </table>

Run http://localhost:3000/user/ in the browser we can see the rails app running fetching the values in the user table.

Now the Asterisk part.

Install RAGI. This is easy, because RAGI is packaged as a “gem”. Just type the following from our command prompt:

gem install ragi

Include the configuration below in the “environment.rb” in the config folder in the project.

#The following code tells Rails to start a Ragi server as a separate thread.
Dependencies.mechanism = :require
# Simple server that spawns a new thread for the server
class SimpleThreadServer < WEBrick::SimpleServer
     def SimpleThreadServer.start(&block)
     Thread.new do block.call
    end
  end
end
require 'ragi/call_server'
RAGI::CallServer.new(:ServerType => SimpleThreadServer)

In this step, we’ll tell Asterisk where are RAGI server is so that the appropriate calls can be routed into our new application.

Now edit extension.conf of asterisk add

[ragi]
 exten => 9999,1,Set(RAGI_SERVER="localhost:4573")
 exten => 9999,2,NoOp(${RAGI_SERVER})
 exten => 9999,3,Answer()
 exten => 9999,4,AGI(agi://${RAGI_SERVER}/tutorial/dialup)
 exten => 9999,5,Hangup

When we call 9999 the call will be routed to RAGI server which can be a local system or a remote system.

Create a directory called “handlers” in your Rails app directory at the same level in your directory structure as controllers, models and views. This is where you will put the class that implements.

Create a new file called “ tutorial_handler.rb” and open that file to edit.

require 'ragi/call_handler'
class TutorialHandler < RAGI::CallHandler
 def dialup
 say_digits('12345')
 say_digits(User.count.to_s)
  end
 end

This is basically your “hello world” call handler. The class is “TutorialHandler” and is an implementation of the RAGI “CallHandler” class. The method “dialup” is automatically called when a phone call is routed by Asterisk through RAGI to this handler.Configure a sip phone and make call to 99999 and can see Ruby application answering the call.

And lots we can play :)

Tags: , , ,


Jul 10 2008

Securing Web folder with htaccess

Category: LinuxBipin Balakrishnan @ 3:12 pm

seo.JPG

Htaccess is a password protection scheme used by Apache Web servers.The web server will ask an authentication to enter if we set a password for it.Here the steps explains how to set an authentication for the web folder.

  1. Create a file called .htaccess in your web folder which you want to protect.
  2. Open the file .htaccess in your favorite editor.
  3. Paste the lines below.
  4. AuthUserFile /var/www/.htpasswd
    AuthGroupFile /dev/null
    AuthName "Protected files"
    AuthType Basic
    require user bipin
  5. Change the path of .htpasswd in AuthUserFile where you are going to place the .htpasswd file.
  6. Change AuthName to from “Protected files” to any name you would like.
  7. Change require user to any user name you would like to give (This will be the authentication user name it will ask).
  8. issue command htpasswd -c <htpasswd path> <username> for password setting . For me it is htpasswd -c /var/www/.htpasswd bipin
  9. Edit httpd.conf file or the web configuration file of apache.It should be like this.
<Directory /var/www/>
   Options Indexes FollowSymLinks MultiViews
  AllowOverride AuthConfig
 #AllowOverride none
  Order allow,deny
  allow from all
</Directory>

Uncomment AllowOverride AuthConfig if it got commented.

Now onwards it will ask an authentication to enter into it.