Archive for September, 2007

12
Sep
07

xargs.. Life can be much easier!

Any Linux admin has a special love story with xargs, It’s like a true friend. It doesn’t always appear to be handy until you stuck looking for a quick and nifty solution. One of these situations is when you need to filter a certain directory that contains a lot of files and you need also to manipulate those files.

xargs is an execution command, you pass your arguments and it will take care of the execution based on the given commands. For instance, I need to get the number of lines in each file within the current path.

ls | xargs wc -l

ls0.png

Simple, Isn’t it?

The first part of this command is very basic, you list the contents of the directory. Which apparently shows the following output:

basicxargs0.png

The second part is the real crux! Here, each line of the ls output will be piped as a standard stream to the xargs which will be process each line accordingly. This means the wc -l will be executed for each line, as if you would write it this way:

wc -l hello.c hello.ko hello.mod.c hello.mod.o hello.o
.
..

And so on!

Internally, xargs command tears up its Stdin input to separate lines based on a delimited character. Most likely, It’s a whitespaces, tabs, new lines and EOF.

xargs-internals.png

A very useful way to check and debug the execution pipeline within xargs is to add the option -t:

ls | xargs -t wc -l

The result of adding the -t option would be:

wc -l 1 anaconda-ks.cfg apache_1.3.37 apache_1.3.37.tar.gz Desktop hello.c hello.ko hello.mod.c hello.mod.o hello.o index.html install.log install.log.syslog InstallShield JavaApplication1 jdk1.5.0_09 jdk-1_5_0_09-nb-5_5-linux.bin lighttpd-1.4.16 lighttpd-1.4.16.tar.gz lynx-2.8.3-1.i386-cygwin.rpm lynx-debuginfo-2.8.5-18.0.2.i386.rpm Makefile mysql-5.0.41.tar.gz php-4.4.6.tar.gz rpm temp temp.c

I guess it’s pretty much meaningful now, Isn’t it?

Let’s heat this up a little bit:

You can use the blackslah “\” to write commands in multiple lines, the same example can be written as follows:

lsbackslash.png

“Don’t worry about .serverauth.XXXX files, just an XServer thing. It’s OK!” 

A powerful aspect of xargs is the ability to control the input data “stdin” from the pipe per invocation. As an example, we can limit the number of delimited strings to accept only two arguments “Again, per invocation”:

xargs-n.png

In shell world, you’re the king! xargs can ask for you permission for any execution “y = affirmative response, others are negative!. How easy!!”. The option “-p” takes care of this:

xargs-p2.png

If you feel playful a little bit, I guess it’s your time to enjoy. Manipulation moments are pretty much exciting! As you have formerly seen, xargs depends largely on the stdin “Standard Input” and if you don’t have enough control over that, xargs would be vain. Luckily, the option “-I” is a drastic option to pass the pipe’s output to the execution command within xargs. To make things more comprehensible, suppose that we need to copy all files within a certain directory to a different directory as a backup files “.bak”.

xargs-icp.png

Let’s break down that command, the “-I” option will determine the replacement string “‘%’”. A neat and tricky question can be raised right here:

Does the “-I” option include the single quote in the replacement string as a variable or is it considered as a escaping character?

The answer can simply be: YES! anything after “-I” and before a single/double unquoted whitespace is considered a replacement string. So, we may have the following valid strings:
‘%’
“‘%’”
%

But please don’t try this one:
HI!

At work I use “Linux~GNU”, but at home I use “*BSD~BSD”… In anyway, take care and note that GNU xargs uses “-i” and BSD version uses “-I”.

06
Sep
07

ISAPI in a nutshell

ISAPI stands for Internet Server Application Programming Interface, In simple words it’s a way to gain the highest performance that you’d ever dream from IIS. ISAPI is “up to a certain level” similar to CGI but also overcomes many of CGI’s problems. One of the biggest differences between ISAPI and CGI is the process handling. In CGI, a new process is created “spawned” for each request. ISAPI uses threads to solve this issue “I wish to know a problem that hasn’t been solved by threading!”.

ISAPI extensions, the heart and soul!

ISAPI has two major types. ISAP extension applications and ISAPI Filters. ISAPI extensions are the heart of IIS. For instance, ASP.dll is an ISAPI extension that processes ASP pages, perlis.dll is another ISAPI extension works as a Perl script engine that runs .pl and .plx file extensions “You can obtain it from ActiveState“. Don’t conflict between the Perl ISAPI extension “perlis.dll and Perl CGI “Perl.exe”.

appconf.png

If you’re using IIS 6.0 or above, you will notice a different Application Configuration form. Have a look:
ISAPI Configuration in IIS 6.0+

Wildcard Application Maps

Wildcard Application Maps contain a list of ISAPI Applications (ISAPIA) that will be processed before any HTTPS request, will make these applications available for all file extensions “regardless of whatever the file extension is”.

ISAPI filters

On the other side, ISAPI filters can be used to enhance an existing functionality provided by IIS. Any incoming/outgoing request can be processed and modified by those filters. As an example, you can provide your own custom operations (authentication, logging, etc…)

isap-filters.png

You may conclude now that ISAPI filters (ISAPIF) are kinda similar to the Wildcard Application Maps (WAM). I also agree with you, except that the WAM has many advantages over using ISAPIF. One of the chief differences is that WAM performs the customization operations “remember: encryption, logging, etc.” using the ISAPI extensions (ISAPIE) through IIS 6. And thereby you can overcome many limitations in ISAPIF, such as:

  • ISAPIF are limited to parse the HTTP headers and cannot be used to process information contained in the body of an HTTP request “forget about processing HTTP Post”.
  • ISAPIF can only configured per website, not per virtual directory.
  • ISAPIF are synchronous.

ISAPIF are synchronous. “I know, that was informative!”

That last point is a very interesting one! Generally, a synchronous call blocks a process till the operation completes “Blocking Call”. In contrary, an asynchronous call is non-blocking and can only initiate the process “Send-And-Pray”. ISAPIF executes synchronously because of it’s execution on a core IIS thread. For example, If you’re involved in a network latency scenario “multiple agents”, ISAPIF are not of your choice. Most likely, It will cause serious problems. In ISAPIE “Again, ISAPIA -> WAM”, the concept is totally different, it has its own thread and thereby it executes asynchronously.

05
Sep
07

netstat: the indispensable tool!

One of my best ever troubleshooting tool, sometimes I stuck in many difficult situations that are related to remote access or internal services/daemons healthiness. You can easily figure out what the problem is from this straightforward command.

netstat-an.png

As you can see, If you have many connections on you PC it will be very difficult to maintain all the result. Fortunately, you can paginate the results using “more” command.

netstat -an | more

In the preceding example, the FTP service runs on a TCP port 21. This means that the service is waiting for a connection request from any remote TCP.

You can also filter the given results using either find or findstr. By the way, findstr is much better than find since you can provide a regular expression for your search.

Checking a certain port number

So, let’s start with a simple scenario. I need to check the MS SQL Server default TCP port (1433):

netstat -an | find “1433″

Find command will filter the results from the netstat command and grabs only the specified string.

PID Concatenation

A serious problem arises from unknown ports, sometimes you modify a port number for a certain service “SSH for example” and after awhile you forgot what that port is, though you can open the *.conf file and snatch the actual values, I really prefer techie and neat solutions. In this case, I usually display the associated process ID with each connection.

netstat-ano.png

Now, you can easily seize that process and figure out where that hidden process is. Simply, by matching the same PID in the Windows Task Manager “taskmgr.exe”. Another good-looking idea is to use the tasklist in the command prompt:

tasklist /svc | find “3306″

This is not all about netstat, there are many other ideas. Grouping the statistics per-protocol, or listing for the new requests.