Tuesday 17 November 2015

PERL SCRIPT for killing CLOSE_WAIT

Some time our application gets chocked because of large number of connection in COSE WAIT state, In such case there is only way to get rid of it by closing application, but in production we cann't kill our application this frequently, so here is a hack I come accross for it.

Ultimate solution :: optimise and find reason in your application.

#!/bin/bash
netstat -tulnap | grep CLOSE_WAIT | awk '{print $4,$5}' |  sed 's/:/ /g' | awk '{print $2, $3, $5, $6}'|  while read line; do
        echo $line
        perl kill_cw.pl $line;
done


wget http://apt.sw.be/redhat/el6/en/x86_64/rpmforge/RPMS/perl-Net-RawIP-0.25-1.el6.rf.x86_64.rpm
wget http://apt.sw.be/redhat/el6/en/x86_64/rpmforge/RPMS/perl-Net-Pcap-0.16-1.el6.rf.x86_64.rpm
wget http://www6.atomicorp.com/channels/atomic/centos/6/i386/RPMS/perl-NetPacket-0.41.1-1.el6.art.noarch.rpm

rpm -Uvh  perl-Net-RawIP-0.25-1.el6.rf.x86_64.rpm
rpm -Uvh perl-Net-Pcap-0.16-1.el6.rf.x86_64.rpm
yum install perl-Net-Pcap

rpm -Uvh  perl-NetPacket-0.41.1-1.el6.art.noarch.rpm

(http://pkgs.org/centos-6/atomic-i386/perl-NetPacket-0.41.1-1.el6.art.noarch.rpm.html)

Friday 20 December 2013

Install PhantomJS on Ubuntu




cd /usr/local/share
sudo wget http://phantomjs.googlecode.com/files/phantomjs-1.9.2-linux-x86_64.tar.bz2
sudo tar xjf phantomjs-1.9.2-linux-x86_64.tar.bz2
sudo ln -s /usr/local/share/phantomjs-1.9.2-linux-x86_64/bin/phantomjs /usr/local/share/phantomjs
sudo ln -s /usr/local/share/phantomjs-1.9.2-linux-x86_64/bin/phantomjs /usr/local/bin/phantomjs
sudo ln -s /usr/local/share/phantomjs-1.9.2-linux-x86_64/bin/phantomjs /usr/bin/phantomjs


on this,
phantomjs -v
it will give
1.9.2

Tuesday 22 October 2013

Some learning on Cassandra - I

In this post we will not talk about  Cassandra – Data Model, There are many blogs on them and one of the best describe is by Animesh Kumar, oh yup Animesh sir is my collage(ISM) senior.



poor approach:
Serialize the Object into JSON.
Now store this JSON as a string into Cassandra.
When need to get the object, read JSON string, deserialize the JSON.
The above approach is fine and works well, but It have its own disadvantage. Say we have
Object(as JSON):
{
  name: "nijju"
  country: "IN"
  email: "niraj.nijju@gmail.com"
}

and we store it into a column into cassandra( as string).
When we need to update a field say country then we need to read the whole JSON string, then we need to deserialize it, and then to change the object attribute and again serialize the resulting object into JSON and storing this JSON at that key.

As in cassandra write is much faster than read operation, so making a read before write will not be a good approach.
Even if your application is mostly write-once data then for reading a single field will make to read all.

In conclusion, we can better performance by storing all the fields into a separate column. For making an update on a field we only need to re-write the corresponding column.




It totally depends on our read behavior, as rows will be store on random disk, so reading from various rows at same time will be slow.
and If rows will be too long then these columns will be stored together on same node so we may have scalability challenges.

                                                       Let me know your thoughts


Saturday 28 September 2013

Python script for get the files_from_directory, using_grep_on_file, on_day_increment


How can I find all files in directory with the extension .txt in python?


http://stackoverflow.com/questions/3964681/find-all-files-in-directory-with-extension-txt-with-python

import glob
import os
#os.chdir("/mydir")
for files in glob.glob("*.txt"):
    print files


python grep on file

for line in open("file_name"):
 if "search_string" in line:
   print line



Python increment day in datetime:
import datetime
today = datetime.datetime.today()
today += datetime.timedelta(days=1)













Sunday 14 April 2013

Google Code Jam Problem C. Fair and Square


Below is my solution for Problem C. Fair and Square

Well I qualified for Google Code Jam 2013 with only 60 points.

* Still to add method to handle Large Input 2.





Input is taken from a file(C-small-practice.in)



Method 1:


/*
* @author niraj.nijju
*/


package gcj.TwelveThirteen;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;

public class SmallOfFairAndSquare {

public static void main(String [] args){
try{
// System.out.println(Double.MAX_VALUE);
// System.exit(0);

  FileInputStream fstream = new FileInputStream("C-small-practice.in");
  DataInputStream in = new DataInputStream(fstream);
  BufferedReader br = new BufferedReader(new InputStreamReader(in));
  String strLine;
  strLine = br.readLine();
  int T = Integer.valueOf(strLine);
  System.out.println("T: "+T);
  int i=1;
  while ((strLine = br.readLine()) != null)   {
//   System.out.println (strLine);
  String [] str = strLine.split(" ");
  double x = Double.valueOf(str[0].trim() );
  double y = Double.valueOf(str[1].trim() );
  int count= getFairSqur(x,y);
  System.out.println ("Case #"+i++ +": "+count);
  }
  in.close();
}catch (Exception e){//Catch exception if any
  System.err.println("Error: " + e.getMessage());
  e.printStackTrace();
}
}


public static int getFairSqur(double x, double y){
int count=0;
long x1 = (long)Math.ceil(Math.sqrt(x));
long y1 = (long)Math.sqrt(y);

for(long i=x1; i<=y1; i++ ){
if(checkPalindrom(i)){
long sq=i*i;
if(checkPalindrom(sq)){
// System.out.println("("+i+")");
count++;
}
}
}


return count;
}



    public static Boolean checkPalindrom(long n)
    {
     String normal = String.valueOf(n);
    //reverse the letters in string
    int length = normal.length(); //length of string
    StringBuffer result = new StringBuffer(length);
    int i;
    for(i = length - 1; i>=0; i--)
    {
    result.append(normal.charAt(i));
    }
    long r = Long.valueOf(result.toString());

    if(r==n)
     return true;
    return false;
    }


}




Method 2:





/*

* @author niraj.nijju

*/



package gcj.TwelveThirteen;



import java.io.BufferedReader;

import java.io.DataInputStream;
import java.io.FileInputStream;

import java.io.InputStreamReader;


import org.omg.CORBA.INTERNAL;

public class CopyOfFairAndSquare {

public static long [] array = new long[10000010];
public static void main(String [] args){
fillArray();
try{
// System.out.println(Double.MAX_VALUE);
// System.exit(0);
  FileInputStream fstream = new FileInputStream("C-small-practice.in");
  DataInputStream in = new DataInputStream(fstream);
  BufferedReader br = new BufferedReader(new InputStreamReader(in));
  String strLine;
  strLine = br.readLine();
  int T = Integer.valueOf(strLine);
//   System.out.println("T: "+T);
  int i=1;
  while ((strLine = br.readLine()) != null)   {
//   System.out.println (strLine);
  String [] str = strLine.split(" ");
  double x = Double.valueOf(str[0].trim() );
  double y = Double.valueOf(str[1].trim() );
  //int count= getFairSqur(x,y);
  long count=0;
  int xr = (int)Math.sqrt(x);
  int yr = (int)Math.sqrt(y);
  count = array[yr]-array[xr];
  if(xr*xr == (int)x){
  if(checkPalindrom(xr) &&  checkPalindrom(xr*xr)){
  count++;
  }
  }
  
  
  System.out.println ("Case #"+i++ +": "+count);
  }
  in.close();
}catch (Exception e){//Catch exception if any
  System.err.println("Error: " + e.getMessage());
  e.printStackTrace();
}
}

public static void fillArray(){
long count = 0;
for(int i=1;i<array.length;i++){
long j = (long)i;
if(!checkPalindrom(j))
array[i]= count;
else if(!checkPalindrom(j*j))
array[i]= count;
else{
count++;
array[i]= count;
}
}
}

    public static Boolean checkPalindrom(long n)
    {
     String normal = String.valueOf(n);
    //reverse the letters in string
    int length = normal.length(); //length of string
    StringBuffer result = new StringBuffer(length);
    int i;
    for(i = length - 1; i>=0; i--)
    {
    result.append(normal.charAt(i));
    }
    long r=0;
    try{
     r= Long.valueOf(result.toString());
    }catch(Exception e){
     e.printStackTrace();
     System.out.println("i:"+i+"\tr:"+r+"\tn:"+n);
    }

    if(r==n)
     return true;
    return false;
    }


    
    
}



Google Code Jam Problem B. Lawnmower


Below is my solution for Problem B: Lawnmower

Well I qualified for Google Code Jam 2013 with only 60 points.