Kudithipudi.Org

March 14, 2012

HOW TO : Increasing number of processes that can be run by a user in Linux

Filed under: HOWTO,Linux — Vinay @ 10:15 pm

By default, most of the Linux distros limit the number of processes that a user can spawn. This is put in place to limit (un)intended cases when a process might just fork off processes without a limit and bring down a server.

For RHEL (and CentOS), the default is 1024 processes per user. In some cases, you do need to increase the number of processes that a particular user can spawn. For example if you are running a database or an application server, you definitely want to tweak this number because these apps tend to create a lot of threads.

As a side note, if you run into this limitation on a machine running jboss, you typically see an error with the following string in your server logs

java.lang.OutOfMemoryError: unable to create new native thread.

. Looking at the error, one would think it is related to memory issues :) .

OK.. back to the subject at hand. Here is the process for identifying your limits and then tweaking them as required in RHEL or CentOS.

  • Check the current limits on the number of processes a user can run by executing
    ulimit -u
  • Edit the /etc/security/limits.conf file and add the required limits. You can get all the possible options by running man limits.conf. For example, if I wanted all the users to have a soft limit of 2000 and a hard limit of 4000, my limits.conf file wold look like this
    # Increase the number of threads per process
    *       soft    nproc   200
    *       hard    nproc   4000 
  • Edit the /etc/security/limits.d/90-nproc.conf file and update it to have the same soft limits. By default, it has 1024 as the limit. So an updated file with my new limits as in the example above would look like this
    # Default limit for number of user's processes to prevent
    # accidental fork bombs.
    # See rhbz #432903 for reasoning.
    
    *          soft    nproc     2000
  • Restart the server. The updated settings won’t take affect until this is done
  • Check if you have the new limits by running
    ulimit -u

You can also check the limits of a particular user by finding a process ID being executed by that user and running

sudo cat /proc/PROCESS_ID/limits 

March 13, 2012

HOW TO : Configure Jboss for writing web access logs

Filed under: HOWTO,Technology,Web — Vinay @ 10:08 pm

One of the capabilities of Jboss is that it can serve HTTP traffic. By default Jboss does not log any of the HTTP traffic in it’s log files. Here is a quick howto on enabling this logging. This post is specific to Jboss 4.x (ancient!!) and I will post another one soon on how do it in version 5.x and newer.

Edit the server.xml file located in $JBOSS_HOME/servers/$PROFILE/deploy/jboss-web.deployer and replace the commented out access logger section as such

FROM

<!--
<Valve className="org.apache.catalina.valves.AccessLogValve"
prefix="localhost_access_log." suffix=".log"
pattern="common" directory="${jboss.server.log.dir}"
resolveHosts="false" />
--> 

TO

<Valve className="org.apache.catalina.valves.AccessLogValve"
prefix="localhost_access_log." suffix=".log"
pattern="common" directory="${jboss.server.log.dir}"
resolveHosts="false" /> 

This will start creating a file with the format localhost_access_log.CURRENT_DATE.log in the $JBOSS_HOME/server/$PROFILE/log folder

But it isn’t fun if you just leave the default logging right :) . The pattern formats of common and combined are similar to the standard apache logging options. But if you wanted to have certain content and format in the log files, you have a lot of options. Jboss community has documented all the data that is exposed through this valve at http://docs.jboss.org/jbossweb/latest/api/org/apache/catalina/valves/AccessLogValve.html

So say, I want to log the referrer header, user agent and the value of a cookie called JSESSONID and log all this data into a file called jboss_web_access_log, I setup the options as such

<Valve className="org.apache.catalina.valves.AccessLogValve"
prefix="jboss_web_access_log." suffix=".log"
pattern="%h %p %l %u %t %r %s %b '%{Referer}i' '%{User-Agent}i' '%{JSESSIONID}c'"
directory="${jboss.server.log.dir}"
resolveHosts="false" /> 

March 12, 2012

HOW TO : Check the number of threads spawned (opened) by a process

Filed under: HOWTO,Linux,Technology — Vinay @ 5:28 pm

If you ever wanted to check how many threads a particular process spawned off in Linux, you can use

  1. Find the PID of the process by running
     ps -ef | grep -i PROCES_NAME  
  2. Check the number of threads opened by this process by running
     <code>ps uH p <PID_OF_PROCESS> | wc -l 

March 9, 2012

HOW TO : Clear unused swap memory in Linux

Filed under: HOWTO,Linux,Technology — Vinay @ 10:15 am

Inspired by a G+  post by Thomas Weeks .

swap memory is something used by the OS to essentially swap data to and forth if the main memory is not available. It is several times slower than RAM, since it uses hard disk to store the memory. And if you are constantly swapping, your system performance is going to be impacted quite a lot. You should always ensure that  your system is not swapping by adding the required RAM and/or stopping your application(s) from using so much memory. At times, because of spike in utilization, the OS might briefly use swap. And when it does, it doesn’t release the memory from swap. So from an analysis prospective, it makes it difficult to check (quickly) if your system is using swap or not. This is similar to errors on an interface in a router. Unless you clear them and monitor, you don’t know when the errors happened.

I was not aware that you could turn off swap devices while the OS is running and then enable them again. So here are the commands to do that in Linux

swapoff -a

This essentially disables swap on all devices configured for swap in /etc/fstab

swapon -a

This does the opposite of the first command. Enabled swap on all devices that have swap configured.

Tom put this into a nice alias by doing the following

alias unswap='sudo swapoff -a && sudo swapon -a'

Thx Tom…

March 8, 2012

HOW TO : Clear screen based on OS in python scripts

Filed under: HOWTO,Programming,Technology — Vinay @ 9:59 am

I like shiny new toys :) . Even though perl is pretty powerful and more than enough for the simple tasks I get to automate from time to time, I want to start learning python and find out first hand, why the whole geek community is raving about it.

As I start to write new scripts in python, I wanted to document how I used to do some things in perl and how I implemented them in python.

One of the standard features of any script I write is to “clear” the screen before starting to send output to the console. Here is the comparison between perl and python

perl

system $^O eq 'MSWin32' ? 'cls' : 'clear'; 

python


# Clear screen, based on the OS
if (os.name == 'nt'):
os.system("cls")
else:
os.system("clear")

March 7, 2012

Project Uptime : Progress Report – 1

Filed under: HOWTO,Linux — Vinay @ 4:05 am

Here is the first update on Project Uptime. I spun up a new server (doesn’t that sound so odd.. spun up a new server!! :) ) with 512MB of RAM running Ubuntu 11.1o (Oneiric Ocelot). First order of business after spinning up the server?

  • Update to the latest and greatest patches
 sudo apt-get update 
  • Update to the latest kernel.
    • First check the version of kernel you are running
 uname -r 
    • Check the repository for latest version
 apt-cache search linux-image 
    • Install latest version
 sudo apt-get install linux-image-LATEST-VERSION 
    • Restart server
 sudo init 6 

March 6, 2012

HOW TO : Find size of directories in current directory

Filed under: HOWTO,Linux — Vinay @ 10:02 am

Quick note for self. Simple bash loop to find out the size of each directory in the existing directory. This script is useful if you are running our of disk space and want to quickly find out the offending directory.

for dir in $(find ./ -maxdepth 1 -type d); do echo ${dir}; du -ch ${dir} | grep -i total; done 

Breaking this down

  • The find command prints out a list of directories. You can modify it to do recursive lookups by just removing the -maxdepth option. This output is fed into the bash loop
  • du gets the size of all the files (and sub directories) in the directory and grepping it for total gives you the total size of the directory

 

March 5, 2012

Project : Uptime

Filed under: HOWTO,Linux,Networking,Technology,Web — Vinay @ 5:13 am

The uptime of this blog has been really bad recently. I switched to hosting it on a Rackspace virtual server last year and went with the cheapest option. A 256MB Linux virtual server that was costing me ~$12/month. I never got around to tuning the OS, so the server was always using swap and would go down pretty much every day. Last week, I upgraded the plan and moved to a 512MB server. But the uptime hasn’t been any better. Here’s a report from Pingdom (which by the way is a great service to track the uptime and responsiveness of your website) showing the availability of the site over the last year 96%!!.. And for someone that has been working in the operations and infrastructure world, that is unacceptable :) . So my new goal is to maintain at least 99.5% uptime. Here is my plan to achieve this

  1. Move to a fresh VM with the latest kernel
  2. Upgrade to the latest version of Apache. Initially, I wanted to move to nginx or lighttpd, but with the recent Apache upgrade, I hear good things about Apache working well in low memory situations.
  3. Upgrade to latest version of MySQL and tune it for memory usage
  4. Configure cloudflare to serve a static version of front page, in case the server goes down. Design the static page to point people to my other digital presences (Google+, LinkedIn, Flickr etc)

I plan to blog the progress and learnings as I implement this plan.

March 4, 2012

HOW TO : Search and Replace text in a file with Perl

Filed under: HOWTO,Technology — Vinay @ 2:10 am

There are tons of sites (and tons of different ways to do this) about this information.. But wanted to note this down for my personal records. If you ever wanted to search for and replace certain text in a file, you can do it with perl with this quick one liner

perl  -p -i -e 's/ORIGINAL_STRING/NEW_STRING/g' FILE_NAME 

March 1, 2012

Demonstrating the power of perl

Filed under: Programming,Technology — Vinay @ 1:26 am

I haven’t scripted in perl for quite some time (disadvantages of moving into management :) ). Today, we had to analyze some log files at work and thought I would dust off my scripting skills..

The source data is Apache web logs and we had to find out the number of hits from a unique IP address for a particular scenario.

Pretty simple right, grep will do the job very well. As demonstrated in this blog post. But we had to analyze the data for a ton of servers and I really didn’t want to repeat the same command again and again. Did you know that laziness is the mother of invention :) . So I wrote a simple perl script to do the job for me. The biggest advantage of writing this perl script was not that it helped reduce the copy/paste job, but the speed that the script took to run. Details of the comparison below

HOW 99% OF ENGINEERS WOULD DO IT

The analysis consisted of getting web logs for the last week (and some of these log files were already rotated/compressed). Concatenating them to create one large file and then getting the number of hits by IP for a certain condition. This can be done very simply by using a couple of commands that come standard with any *nix system

  • cp
  • cat
  • grep for each day we needed the data

The final grep command would look like this

 grep -i "\[20/Feb/2012" final_log | grep -i "splash.do" | grep -i productcode | cut -d' ' -f 1 -| sort |uniq -c | sort -rn > ~/2_20_2012_ip_report.log 

Timing this command showed that it took ~1 min and 22 seconds to run it.

HOW THE 1% DO IT:)

I wrote this perl script (disclaimer : I am not a programmer :) , so pls excuse the hack code).


#!/usr/bin/perl
# Modules to load
# use strict;
use warnings;

# Variables
my $version = 0.1;

# Clear the screen
system $^O eq 'MSWin32' ? 'cls' : 'clear';

# Create one large file to parse
`cp /opt/apache/logs/access_log ~/access_log`;
`cp /opt/apache/logs/access_log.1.gz ~/access_log.1.gz`;
`cp /opt/apache/logs/access_log.2.gz ~/access_log.2.gz`;

`gunzip access_log.1.gz`;
`gunzip access_log.2.gz`;

`cat access_log.2 access_log.1 access_log > final_access_log`;

# Hostname
$hostName=`hostname`;
chomp($hostName);

print "The Hostname of the server is : $hostName \n";

# Process the log file file, one line at a time
open(INPUTFILE,"< final_access_log") || die "Couldn't open log file, exiting $!\n";

while (defined ($line = <INPUTFILE>)) {
 chomp $line;
 if ($line =~ m/\[20\/Feb\/2012/)
 {
 open(OUTPUTFILE, ">> 2_20_2012_log_file") || die "Couldn't open log file, exiting $!\n";
 print OUTPUTFILE "$line\n";
 close(OUTPUTFILE);
 next;
 }
 if ($line =~ m/\[21\/Feb\/2012/)
 {
 open(OUTPUTFILE, ">> 2_21_2012_log_file") || die "Couldn't open log file, exiting $!\n";
 print OUTPUTFILE "$line\n";
 close(OUTPUTFILE);
 next;
 }
 if ($line =~ m/\[22\/Feb\/2012/)
 {
 open(OUTPUTFILE, ">> 2_22_2012_log_file") || die "Couldn't open log file, exiting $!\n";
 print OUTPUTFILE "$line\n";
 close(OUTPUTFILE);
 next;
 }
 if ($line =~ m/\[23\/Feb\/2012/)
 {
 open(OUTPUTFILE, ">> 2_23_2012_log_file") || die "Couldn't open log file, exiting $!\n";
 print OUTPUTFILE "$line\n";
 close(OUTPUTFILE);
 next;
 }
 if ($line =~ m/\[24\/Feb\/2012/)
 {
 open(OUTPUTFILE, ">> 2_24_2012_log_file") || die "Couldn't open log file, exiting $!\n";
 print OUTPUTFILE "$line\n";
 close(OUTPUTFILE);
 next;
 }
 if ($line =~ m/\[25\/Feb\/2012/)
 {
 open(OUTPUTFILE, ">> 2_25_2012_log_file") || die "Couldn't open log file, exiting $!\n";
 print OUTPUTFILE "$line\n";
 close(OUTPUTFILE);
 next;
 }

if ($line =~ m/\[26\/Feb\/2012/)
 {
 open(OUTPUTFILE, ">> 2_26_2012_log_file") || die "Couldn't open log file, exiting $!\n";
 print OUTPUTFILE "$line\n";
 close(OUTPUTFILE);
 next;
 }
 if ($line =~ m/\[27\/Feb\/2012/)
 {
 open(OUTPUTFILE, ">> 2_27_2012_log_file") || die "Couldn't open log file, exiting $!\n";
 print OUTPUTFILE "$line\n";
 close(OUTPUTFILE);
 next;
 }
 if ($line =~ m/\[28\/Feb\/2012/)
 {
 open(OUTPUTFILE, ">> 2_28_2012_log_file") || die "Couldn't open log file, exiting $!\n";
 print OUTPUTFILE "$line\n";
 close(OUTPUTFILE);
 next;
 }
}

`rm final_access_log`;
`rm access_log`;
`rm access_log.1`;
`rm access_log.2`;

for ($day=0; $day < 9; $day++)
 {
 $outputLog = $hostName."_2_2".$day."_2012.txt";
 $inputLog = "2_2".$day."_2012_log_file";

$dateString = "\\[2".$day."/Feb/2012";

print "Running the aggregator with following data\n";
 print "Input File : $inputLog\n";
 print "Output Log : $outputLog\n";
 print "Date String: $dateString\n";

`grep -i "splash.do" | grep -i productcode | cut -d' ' -f 1 -| sort |uniq -c | sort -rn > ~/$outputLog`;

# Cleanup after yourself
 `rm $inputLog`;
 }

I wrote a smaller script to do the same job as the command line hack that I tried earlier and compared the time. First, here is the smaller script


#!/usr/bin/perl
# Modules to load
# use strict;
use warnings;

# Variables
my $version = 0.1;
# Clear the screen
system $^O eq 'MSWin32' ? 'cls' : 'clear';
open (TEMPFILE,"< final_log");

# Match date and write to another log file
while (defined ($line = <TEMPFILE>)) {
 chomp $line;
 if ($line =~ m/\[20\/Feb\/2012/)
 {
 open(OUTPUTFILE, ">> perl_speed_test_output.log");
 print OUTPUTFILE "$line\n";
 close(OUTPUTFILE);
 next;
 }
}

`grep -i "splash.do" perl_speed_test_output.log | grep -i productcode | cut -d' ' -f 1 -| sort |uniq -c | sort -rn > ~/perl_speed_test_output_ip.log`;

Timing this script, showed that it took 21 seconds to run it.  > 300% improvement in speed and more importantly, less load (RAM utilization) on the system

One has to love technology :) .

« Newer PostsOlder Posts »

Powered by WordPress