Tag: snippet

  • Run MD5 check sum against all files in a directory

    Couple snippets that allow us to run checksum and get unique md5 checksums.

    This is two step process. First, we obtain our md5 checksum for all files

    find -type f -exec md5sum "{}" + > /opt/checklist.chk
    

    This produces file with following contents

    71cc452a8ac5a27c32a83e6a0909e7ae  ./PID_190_7344_0_47710322.tif6712032974632727465.tiff
    71cc452a8ac5a27c32a83e6a0909e7ae  ./PID_190_7344_0_47710322.tif174464329785828524.tiff
    71cc452a8ac5a27c32a83e6a0909e7ae  ./PID_190_7344_0_47710322.tif6775939766281585264.tiff
    71cc452a8ac5a27c32a83e6a0909e7ae  ./PID_190_7344_0_47710322.tif7205305688614612348.tiff
    71cc452a8ac5a27c32a83e6a0909e7ae  ./PID_190_7344_0_47710322.tif3909999865608008175.tiff
    

    Next we parse and get only unique checksums.

    cat  /opt/checklist.chk | awk '{split($0, a, " "); if(!seen[a[1]]++) print a[1]}'
    

    This produces our distinct checksums

    71cc452a8ac5a27c32a83e6a0909e7ae
    
  • Grepping for multiple strings in a file

    We will use egrep which accepts a regular expression to grep for multiple strings.

    tail -f localhost_access_log.txt | egrep "\" 404|\" 500" 
    

    Here our example looks at logs to see if we got 404 or 500 request.

     "GET /favicon.ico HTTP/1.1" 404 973
     "GET /login.html  HTTP/1.1" 500 1230
     "GET /favicon.ico HTTP/1.1" 404 973
    
  • Apache 408 Connection timedout

    nohup tail -f access.log | grep ‘408’ –line-buffered | awk ‘{split($0,a,” “); print a[1]; fflush()}’ | tee -a bad-408.txt

  • Starting jetty via command line an nohup

    Somehow I am getting problems starting Jetty via

    service jetty start
    

    We will be using unix command called nohup
    “Nohup is a unix command, used to start another program, in such a way that it does not terminate when the parent process is terminated.”

    I have opted out for using this

    nohup java -jar start.jar -Djetty.port=8085
    

    while this works it shown an message

    nohup: ignoring input and appending output to `nohup.out'
    

    to fix that up we need to redirect in put and output to /dev/null

     nohup java -jar start.jar -Djetty.port=8085  /dev/null &
    
  • Taking heap dump of java process on linux and windows

    Taking a heap dump from console when Java VisualVM and JMX is not available to us.
    We will use following tools

      • jmap
      • jps
      • ps

    Dumping heap requires two steps
    1) Obtaining target process id
    2) Dumping heap for given pid

    First we need to obtain the target process id we would like to dump, here I will show couple ways I like to use.

    ps aux | grep 'java'
    -----
    userx     29901  6.7 47.0 25418812 3848276 ?    Sl   Mar23  85:42 /opt/java/bin/java -Djava.util.logging.config.
    

    Here second column indicates our process id (pid)

    Second method that is quite useful to obtain pid for java processes

    uxserx@WS4:/opt/java/bin# ./jps -l
    4281 sun.tools.jps.Jps
    29901 org.apache.catalina.startup.Bootstrap
    

    As we see both methods returned us pid of 29901
    Npw to perform the dump we issue our second command

    userx@WS4:/opt/java/bin# ./jmap -dump:format=b,file=/tmp/heapdump-001.hprof 29901
    Dumping heap to /tmp/heapdump-001.hprof ...
    

    At this point we have our heap dump that is ready to be analyzed, for my analysis I use two tools. Eclipse Memory Analyzer (MAT) and Java Visual VM

  • Accessing data of leptonica PIX data

    This is mainly as a reference

    
    /**
     * Get Pixel value at given  point
     */
    l_uint32 pixAtGet(PIX* pix, int_t x, int_t y)
    {
        l_int32 wpl    = pixGetWpl(pix);
        l_uint32* data = pixGetData(pix);
        l_uint32* line = data + y * wpl;
        l_uint32 value = GET_DATA_BYTE(line, x);
        return value;
    }
    
    

    To set a pixel value we can use this

    /**
     * Set Pixel value at given  point
     */
    void pixAtSet(PIX* pix, int_t x, int_t y, byte_t value)
    {
    	l_int32 wpl     = pixGetWpl(pix);
    	l_uint32* data  = pixGetData(pix);
    	l_uint32* line  = data + y * wpl;
    	SET_DATA_BYTE(line, x, value);
    }
    
  • Tokenizing/splitting string in c++

    This method uses strtok to tokeninze our string given a specific delimeter, results of that are put into supplied vector. There are few other ways we can do this but this one is straight forward.

    #include 
    #include 
    #include 
    
    #include 
    #include 
    #include 
    #include 
    #include 
    
    using namespace std;
    
    void split(vector& out, const string& in, const string& delim)
    {
      char* lc = (char*) malloc(in.size());
      strcpy(lc, in.c_str());
      strtok(lc, delim.c_str());
      while (lc)
        {
          string s = lc;
          out.push_back(s);
          lc = strtok(NULL, delim.c_str());
        }
      free(lc);
    }
    
    int main(int argc, char* args[])
    {
      string str = "apple,organge,cherry";
      vector o1;
      split(o1, str, ",");
    
      for (int i = 0; i < o1.size(); ++i)
      {
         cout << "token = " << o1[i] <
    

    Results

    Supplied string : apple,organge,cherry
    Delemeter : ","
    Output

    • apple
    • organge
    • cherry