Wednesday, December 9, 2009

How (& Why) To Install i686 Kernel In Fedora Core 6 in 7 Steps

Anaconda has a well documented defect where it installs i586 based kernel in i686 machines. It causes problems with several drivers including nvidia drivers. Also you will gain performance improvement by moving to i686 kernel. Let's upgrade the kernel in just 7 steps.

First you must verify that you do have a i686 cpu and you have i586 kernel.

Note: All the following commands must be run as root.

1. Run the following from terminal to know your cpu architecture:
arch

2. Now run the following to find out the kernel installed:
yum list kernel

You may get something like this at the end:
kernel.i586 2.6.18-1.2798.fc6 installed
Note the i586. It shows that you have the wrong kernel installed. Let's go to the next step.
Note: If it shows i686 then you needn't proceed further.

3. First save a copy of your grub.conf. You may need it.
cd
cp /boot/grub/ .

4. Next remove the existing kernels.
yum remove kernel
Confirm the removal.
This will remove the kernel and all iuts dependencies. Note the dependencies.You will have to install them later.

You may get a grub error message like:
grubby fatal error: unable to find a suitable template
grubby: doing this would leave no kernel entries. Not writing out new config.

Ignore it.

5. Now install the i686 kernel:
yum install kernel.i686
This will force installation of i686 kernel. Confirm the installation when asked.

You may get grub error like this:
grubby fatal error: unable to find a suitable template

This one is dangerous. If you get it then first fine the exact version of kernel installed as follows:
yum list kernel
You will get a version string like: 2.6.20-1.2933.fc6
Write it down.
Then open grub.conf in any text editor like gedit. I did:
gedit /boot/grub/grub.conf &
Note the line starting with kernel /vmlinuz. Change the version string of vmlinuz to the string you copied before. Double check to ensure there are no typos. Then repeat the process for the line starting with initrd /initrd. Save the file and exit.

6. Now install the dependencies you removed earlier. I did the following:
yum install compiz gnome-volume-manager systemtap gnome-session pcmciautils

7. Reboot and you are done:
reboot

Note: You may get an error while rebooting if you previously ignored the "unable to find suitable template" error or incorrectly modified grub.conf. You will get a second chance to modify it now to make it boot. However if you do have to modify then ensure that you make the same changes in grub.conf so you don't have to type it again and again while rebooting.

Monitoring MySQL Queries Using Bash Script

Monitoring MySQL queries is a favorite pastime of MySQL administrators especially for performance reasons. Here is a simple bash script to monitor long running MySQL queries in realtime using the ubiquitous 'show processlist'. The best part about this script is that you can use it to log your queries over time for later evaluation.


#!/bin/bash
while [ 1 ]
do
mysql -N -u root -ppassword -e 'show processlist' |grep -v 'show processlist'
sleep 2
done



Note: Replace password with your actual password.
Note: -N removes column headers.

This script excludes the show processlist thread itself. You may also exclude the Sleeping threads with the following modification:
mysql -N -u root -ppassword -e 'show processlist' |egrep -v 'Sleep|show processlist'

Note: This is not the only or best solution in market. There is mtop script with more functionality but written in perl. I am alergic to perl and also couldn't get it working in my only attempt. So here is a simple solution in bash. Personally I don't like hacking perl scripts.

Note: You can use slow query log too but in my experience a running top like display is better at finding bottlenecks. Slow query log doesn't indicate why a query took long time. It could be because it was waiting on another query to complete.





How to Change MySQL Password for Other Users

Do you get this error message when you try to change the root password?

mysqladmin: connect to server at 'localhost' failed
error: 'Access denied for user 'root'@'localhost' (using password: YES)'

Or may be you need to change the MySQL password for other users. So what do you do then?

To change a normal user password you need to type:
$ mysqladmin -u user-name -p oldpassword newpass

How to Update or Change Password for a Single User in MySQL and Reload Privileges

MySQL stores usernames and passwords in the user table inside the MySQL database. You can directly update a password using the following method to update or change passwords:

1) Login to the MySQL server, type the following command at the shell prompt:

$ mysql -u root -p

2) Use the mysql database (type commands at the mysql> prompt):

mysql> use mysql;

3) Change password for a user:

mysql> update user set password=PASSWORD("newpass") where User='YOUR-USER-NAME';

4) Reload privileges:

mysql> flush privileges;
mysql> quit


*the above script works only with PHP or pearl scripting*

4 How To Change MySQL Password

Let's look at all the ways to change MySQL password, for root and other users:

In MySQL the default password is empty. This is inherently unsafe and should be immediately changed. Here is how you can change MySQL default root password:

mysqladmin -u root password NEWPASSWORD

Here is how you can change OLDPASSWORD to NEWPASSWORD

mysqladmin -u LOGIN -p OLDPASSWORD NEWPASSWORD

Replace LOGIN with your login name, OLDPASSWORD with your current password and NEWPASSWORD with the new password.


enjoy!

How to Set Up Root Password for Your MySQL Server

If you have never set a root password for MySQL, the server does not require a password at all for connecting as root. To set up a root password for the first time, use the mysqladmin command at the shell prompt as follows:

$ mysqladmin -u root password newpass

If you want to change (or update) a root password, then you need to use the following command:

$ mysqladmin -u root -p oldpassword newpass

I hope this will work for you perfectly.

How to Recover MySQL Root Password

Do you want to recover the MySQL root password. its by no means, easy. But its quite simple if you follow the procedure. You will have to follow this step-by-step processes

Step 1: Stop the MySQL server process.

Step 2: Start the MySQL (mysqld) server/daemon process with the –skip-grant-tables option so that it will not prompt for a password.

Step 3: Connect to the MySQL server as the root user

Step 4: Set a new root password

Step 5: Exit and restart the MySQL server.


Here are the commands you need to type for each step (log in as the root user):


Step 1 : Stop the MySQL service:

# /etc/init.d/mysql stop


Output:

Stopping MySQL database server: mysqld.


Step 2: Start the MySQL server w/o password:

# mysqld_safe --skip-grant-tables &

Output:

[1] 5988
Starting mysqld daemon with databases from /var/lib/mysql
mysqld_safe[6025]: started


Step 3: Connect to the MySQL server using the MySQL client:

# mysql -u root

Output:

Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1 to server version: 4.1.15-Debian_1-log

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql>


Step 4: Set a new MySQL root user password:

mysql> use mysql;
mysql> update user set password=PASSWORD("NEW-ROOT-PASSWORD") where User='root';
mysql> flush privileges;
mysql> quit


Step 5: Stop the MySQL server:

# /etc/init.d/mysql stop

Output:

Stopping MySQL database server: mysqld
STOPPING server from pid file /var/run/mysqld/mysqld.pid
mysqld_safe[6186]: ended

[1]+ Done mysqld_safe –skip-grant-tables


Now Start the MySQL server and test it:

# /etc/init.d/mysql start
# mysql -u root -p


phew done now enjoy programming


Sunday, December 6, 2009

The Real Ajay Bhatt of Intel's 2009 Ad Campaign

Here's the picture of Real Ajay Bhatt


You must have seen this Intel's ad on your television many times, here's what you don't know about this ad.
The tagline of the advertisement is ''our rock stars are different from your rock stars.'' The ad is great but the only person missing in the ad is the Real Ajay Bhatt. Yes, the ad is played by some actor instead of Real Ajay Bhatt.

Ajay V. Bhatt is an Indian-American computer architect. He has been instrumental in driving definition and development of broadly adopted technologies such as USB (Universal Serial Bus), AGP (Accelerated Graphics Port), PCI Express, Platform Power management architecture and various chipset enhancements. Ajay Bhatt rose to global celebrity as the co-inventor of USB through an Intel 2009 TV advertisement, where he was portrayed by actor Sunil Narkar.
The ad was created by the agency Venables Bell & Partners.
After completing his graduation from the Maharaja Sayajirao University of Baroda, India, Bhatt received his master's degree from The City University of New York. Ajay Bhatt is an Intel Fellow and Chief Client Platform Architect for the Intel Architecture Group at Intel Corporation. Bhatt leads definition and development of the next-generation Client Platform architecture. He is primarily focused on the novel advances in platform hardware and software by working with key internal and external technology partners to develop the future Client Platform Architectures and Technologies. As a lead Client Platform Architect, Ajay also works with key business and planning groups to be at the forefront of the future Client Platform innovation areas by setting Intel-wide and industry-impacting strategies.
In addition, Bhatt continues to hold a position of Intel's Chief I/O architect. In his role as a Chief I/O architect, he is responsible for the platform and I/O interconnects directions for Intel; leading the definition of next-generation platform architectures and I/O technologies across the market segments internally and within the industry.
Bhatt is an industry-recognized expert in the area of I/O technologies. At Intel, Bhatt has been instrumental in driving definition and development of broadly adopted technologies such as USB, Accelerated Graphics Port, PCI Express, Platform Power management architecture and various chipset enhancements. Bhatt joined Intel in 1990 as a senior staff architect on the chipset architecture team in Folsom.
Bhatt received his master's degree from The City University of New York. He holds nine U.S. patents with several in various stages of filing. In 1998, 2003 and 2004 Bhatt was nominated to take part in a Distinguished Lecture Series at leading universities in the United States and Asia. He received an Achievement in Excellence Award for his contribution in PCI Express specification development in 2002.

Friday, December 4, 2009

Log4j MDC (Mapped Diagnostic Context) : What and Why

I hope that Log4j does not need any introduction and I assume that you already have basic understanding of Log4j. If not, I’ll recommend you to first read read my previous post to get an understanding.


That said, now I’ll start with MDC or Mapped Diagnostic Context. Don’t get scared with this name! MDC is not that tough. It’s a simple yet useful concept. Before I explain what is MDC, lets assume that we are going to develop a simple web application with one servlet ‘MyServlet’ servicing requests from multiple clients. And, this servlet uses log4j framework for logging. A file appender has been defined for this servlet, so all the log messages will be logged into a text file.

With the above said configuration, all the log messages from MyServlet will go into a single log file. And when this servlet is serving more than one clients at the same time, the log statements will be mixed and there’s no way to differentiate which log statement is belongs to which client’s processing. This’ll make it difficult to trace and debug if any processing error occured in MyServlet life cycle.

How to differentiate log statements with respective to each clients?:-
To avoid the log statements mix-in, we could add a user name (or some other data which will be unique to each client) to our log statements. To do this, we have to make sure that we pass this user name data explicitley to each and every log statements, which is a tedious and repetitive work. But, no need to worry! Log4j has an excellent way to overcome this. It’s called as MDC or Mapped Diagnostic Context.

So, What is Log4j MDC (Mapped Diagnostic Context):-
To put is simple, the MDC is a map which stores the context data of the particular thread where the context is running. To explain it, come back to our simple application – every client request will be served by different thread of the MyServlet. So, if you use log4j for logging, then each thread can have it’s own MDC which is global to the entire thread. Any code which is part of that thread can easily access the values that are present in thread’s MDC.


So, how do we make MDC to differentiate logging statements from multiple clients? Simple : Before starting any business process in your code, get the user name (for our Servlet, we can get it from request object) and put that into MDC. Now the user name will be available to the further processing. In your log4j.properties while defining the ‘conversionPattern’, add a pattern %X{key} to retrievce the values that are present in the MDC. The key will be ‘userName’ in our example. It’s like getting a value from a Session object.

In my next post, I’ll give the source code for the same example that I used in this post. So, keep Watching!

Thursday, December 3, 2009

Log4j Tutorial : Adding Log4j logging to your project

If you are new to Log4j, here is a short description on what is it and why should we care about it:
Log4j is a flexible logging library, an open source project from Apache. Using Log4j, we can replace the debugging print line statements, like System.out.println(”Value is ” + someVariable), with a configurable logging statement like logger.debug(”Value is ” + someVariable). The advantage in using Log4j is, if you do not want to print the debugging print lines in production application, you can easily switch them off using Log4j configuration file (which we will see it how, shortly).
How to add Log4j support to your project?
Follow these steps if you want to add Log4j logging support to your Java project.
Download the latest Log4j distribution from Apache website (at the time of this article is written, the latest version of Log4j is 1.2) and put the log4j-xxx.jar in your project class path. For Java web application, you can place the jar file in WEB-INF/lib folder. For Java applications, you can place the jar in any folder, but remember to add the folder to your classpath.
Next we need to configure the Log4j library to our requirements. Log4j reads its configurations from log4j.properties file (or log4j.xml) placed in the CLASSPATH. Every log4j.properties file defines the following three things, mainly:
an Appender – this is the class file which does the actual logging. It could be a simple Console appender which dumps the log messages to stdout (screen) or a file appender, which sends the log messages to a log file.
a Layout – is nothing but how the log message is formatted. This format is very simlar to C languages’s printf function formatting.
and a Logger – a logger ties the appender with the log messages that are coming from the Java application. Basically, a logger tells that the log messages from these packages should go to some appender which will log the message in the defined layout.
Below is a sample log4j.properties for configuring the console appender for your project.


#define the console appender
log4j.appender.consoleAppender = org.apache.log4j.ConsoleAppender

# now define the layout for the appender
log4j.appender.consoleAppender.layout =
org.apache.log4j.PatternLayout
log4j.appender.consoleAppender.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n

# now map our console appender as a root logger, means all log messages will go to this appender
log4j.rootLogger = DEBUG, consoleAppender

In the above properties file, we are first defining a Console appender and a layout pattern (refer this complete documentation to know what’s the weired symbols in the conversion patterns means). Then we are mapping all the DEBUG level log messages from the entire application to go into the consoleAppender, since we added the consoleAppender to the rootLogger.

In any of your Java file, add the below lines, in order to start logging:

private static Logger logger = Logger.getLogger(Myclassname.class);
logger.debug("this is a sample log message.");

Now you are done. Run your application and you should be seeing the log messages coming in your console window.

Monday, November 30, 2009

Finding files in java archives by their content

While doing a bug hunting i needed to find a file containing a certain string and did not know where to look for it. At one stage of "file hunting" i needed to be able to search for the file in jars (Java Archives). Thus in searching for it i came up with a shell script using my good old friend venkys help
#!/bin/sh
2 echo Params: "$#" - \("$1"\) \("$2"\) \("$3"\) \("$4"\)
3 if test "$#" -ne 4
4 then
5 echo "Command takes exactly 4 params - Jar_name_pattern search_location file_name_pattern search_string
6 Use - for Jar_name_pattern to search for within all jars.
7 Use - for search_file_name to find with file_name_pattern only and avoid content search"
8 exit 1
9 fi
10
11 if test "$1" = "-"
12 then
13 jar_param="*.jar"
14 else
15 jar_param="$1"
16 fi
17 if test "$4" = "-"
18 then
19 search_content=1
20 else
21 search_content=0
22 fi
23 echo Executing: find "$2" -name "$jar_param"
24 file_list=`find "$2" -name "$jar_param"`
25 current_dir=`pwd`
26 cd /tmp/
27 mkdir search_file rw_not=1
28 if test -n "$rw_not"
29 then
30 echo "/tmp/ is not writeable by current user thus can search file content and exiting"
31 exit 1
32 fi
33 cd search_file
34 echo "Search Result:"
35 for jar_file in $file_list
36 do
37 inner_file_list=`jar tf "$jar_file" grep "$3"`
38 if test "$search_content" = "0"
39 then
40 if test -n "$inner_file_list"
41 then
42 jar xf "$jar_file" $inner_file_list
43 for inner_file in $inner_file_list
44 do
45 has_content=`cat $inner_file grep "$4"`
46 if test -n "$has_content"
47 then
48 echo Found in "$inner_file" @ "$jar_file"
49 fi
50 done
51 rm -rf *
52 fi
53 else
54 test -n "$inner_file_list" && echo "$jar_file"
55 fi
56 done
57 cd ..
58 rm -rf ./search_file
59 cd "$current_dir"
using this script not only can we find jars containing certain filename but also search file content to get results. Lets take three use cases - "I want to find jars that contain .properties file", "I want to find jars containing .properties file containing the word com.smartitengineering" and "I want to find jars, whose name has smart, containing .properties file containing the word com.smartitengineering". Using the script all these use cases can be achieved by using the following commands respectively:
./search-jar.sh - /home/user/ .properties -
./search-jar.sh - /home/user/ .properties com.smartitengineering
./search-jar.sh *smart*.jar /home/user/ .properties com.smartitengineering
The command takes exactly 4 arguments - jar_filename_pattern, search_dir_path, search_file_name and content_to_search. If jar_filename is "-" than it will search within all jars within search_dir_path. If content_to_search is "-" it will not search for content, but rather jars with certain filename.
As the script is in open domain I would appreciate if users would make their own edit and submit or request for features through this post's comments.

Stickies on Ubuntu

It seems that Wine is an excellent tool to run Windows application on Ubuntu. Recently I had to use Stickies and tried it on Ubuntu. So first I had to install Ubuntu. I executed the following commands to install Wine on Ubuntu 7.04 Fiesty

sudo wget http://wine.budgetdedicated.com/apt/sources.list.d/feisty.list -O /etc/apt/sources.list.d/winehq.list

sudo apt-get update

sudo apt-get install wine

For other platforms you can have a look at http://www.winehq.org/site/download. Then you have to simply download the stickies.exe and then execute

wine {PATH-TO-STICKIES}/stickies.exe

But hold on the installation is not done, you need get the following the installation steps mentioned in
http://appdb.winehq.org/appview.php?iVersionId=5169&iTestingId=4356

scroll down to Install Note and follow that. The only change will be installing the MSI file, instead of the one mentioned there use the following to install the MSI file

wine msiexec /i/{PATH-TO-MSI}MSISetup.MSI

Now try to execute

wine /.wine/{PATH-TO-STICKIES-INSTALLATION}/stickies.exe

It might say that MFC42.dll is missing, if it says so please get it from a Windows installation and copy it to /.wine/drive_c/windows/system32/ and try to execute it again

Hopefully that will be enough for Stickies Installation :).Now to get the Stickies running you can execute the following command:

sudo wine ~/.wine/drive_c/Program\ Files/stickies/stickies.exe > {PATH-TO-LOG}
2>&1 &
Though this is for Ubuntu but I guess excepting the installation procedure rest should be same across Linux platform. Let me know if any further changes were required.

Killing a process in linux

As a software enthusiast one thing I have to do several times is killing a process, some people will term it as ugly or bad, but general software developers often come across situation where killing a process is handy or necessary. I will mention some use cases later in the blog.Killing a process usually involves 2 steps. Finding the process ID that is intended to be terminated and terminating it with a kill statement. Step one for general users (like myself) again involves a ps command with some grepping and then manual lookup. I have to do these almost every time I kill a process. I have simply put this steps into the following script so that I can do them in a single command.
#!/bin/bash
if [ -z $3 ]; then
USER_STR=`whoami`
else
USER_STR=$3
fi
echo User: $USER_STR
process_str=`ps -eopid,user,cmd grep $USER_STR grep $1 grep $2`
pid=`echo $process_str cut -d' ' -f0-1`
if [ -z $pid ]; then
pid=`echo $process_str cut -d' ' -f1-2`
if [ -z $pid ]; then
pid=`echo $process_str cut -d' ' -f2-3`
if [ -z $pid ]; then
pid=`echo $process_str cut -d' ' -f3-4`
else
echo "Retrieved PID: " $pid
fi
else
echo "Retrieved PID: " $pid
fi
else
echo "Retrieved PID: " $pid
fi
if [ -z $pid ]; then
echo 'Could not execute kill. PID: ' $pid
else
kill -9 $pid
fi

If someone has any improvements please inform me about it.The shell script expects 3 arguments, of which 2 are mandatory and 1 is optional. The first 2 are grep params to identify the process. For example, to identify a Tomcat running Escenic Content Engine I use java and escenic.root to identify the process. The third param is the user to search it with. If it is not provided then whoami will be used to determine the user executing the shell.This shell script has enabled me to sync, build, deploy my projects all with a single command and this helps me save some sustainable amount of time. Now without a build server I can still achieve what a build server like Hudson can do. Actually I use this script from within Hudson and thus I almost "never" have to be concerned about deploying applications to my demo server. I hope this is useful to all

SIP application structure


As mentioned in my previous blog I feel that SIP applications are the next generation of applications for Desktop, Web and Mobile. By the will of Almighty, today I will be discussing the structure of SIP applications.We will see the main layers of the application and discuss them in bottom up approach. Any SIP application will contain 3 layers. Topmost is the Application Layer - where all the logics of the application resides and which takes care of the SIP packets. Then comes SIP Stack it self, which is responsible for abstracting the SIP packets from the application developer by providing an API to do the low level SIP stuffs. Lastly comes the Transport layer that is responsible for carrying the SIP packets and sending them to the intended destination; the SIP stack is very closely binded with this layer as it will provide transport for common protocols and developers has to extends the transport API to add more transport layer protocols.Almost any transport layer protocol can be used with SIP as long as the SIP stack recognizes it. The most commonly used transport layer protocol is UDP; on some occasions TCP and TLS are also used. We all know that all networks are converging towards IP backbone and as those protocols are IP based it should be more convenient for developers in future as they will not have to write SIP transport layers for different protocols. One challenge would be from Cell Phone to BTS; so SIP stack providers for Cell phone will have to implement transport layer for GSM and CDMA phones.IMHO the most important layer of the application structure is SIP stack. This will abstract the SIP packets and Transport layer hassles from the developers and will provide them API's to call and use them to make an SIP application. There are quite a few SIP stacks available such as reSIProcate, Jain SIP (JSR-32) and SIP Servlet (JSR-000116). Among them SIP Servlet Application (archives with sar extensions) is usually deployed in Java EE Container (BEA Weblogic, IBM Websphere, GlassFish-SailFin) or SIP Container implementing SIP Servlet. Personally I have used Jain SIP and its simplicity in usage and good API structure has created liking in me for it. In my blogs related to SIP I will be using this stack to develop examples. I will also discuss about SailFin.Application layer is where everybody gets to exercise their innovations. Now is the time to generates SIP centric ideas and implementing them. All SIP application can be categorized in four categories -
Registrar Server - A server responsible to register a SIP user and its location
Proxy Server - A server responsible to act as the main server where as it is nothing but a proxy and knows which is the real server
Redirect Server - A server that redirects request to another server; an example of this functionality would be load balancing of requests
User Agent - This is a client side entity responsible for communicating with the server and server the Client's request. Anything could be user agent such as PC Software, Cell Phone, Refrigerator, Television, Microwave Owen etc.Developers can develop applications that serve any of these purposes.By the will of Almighty, I will soon make some postings about what could be SIP applications

SIP-For next generation communication

I have been introduced to SIP (Session Initiation Protocol) during my college courses. From then it appealed a lot to me. As timed passed by I came to realize that with the rate in which everything is getting globalized it is a matter of time when VoIP will rule the world of communication. As internet is already accepted as the global information highway, communication is also bound to be built on that. SIP is IETF engineered protocol and open for everybody's suggestion.Recently SIP related activities are very visible; especially, with reSIProcate, Jain-SIP protocol stacks popularity and major Java EE Container vendors supporting SIP Servlets it is easily comprehensable that industry is expecting a increase in use of SIP in business applications, as more and more applications want to include web based voice/video chat.Recently, GlassFish project has introduced the SailFin project, which is a SIP Servlet container. Hopefully, by the will of Almighty, it will also include support for easy portability for Jain-SIP based server applications into its container as well.If I were to bet on a technology of next generation I would place my bet on SIP based applications. Hopefully, by the will of Almighty, I hope to bring more writeups about SIP applications - mostly server side.