Thursday, February 19, 2015

Taking thread dump on CPU High usage in WSO2 products

Please follow the commands provided here to take the thread dump.

 

First you need to create test.sh script using following command.
     vi test.sh

Then paste following script content.

    #!/bin/bash
    # 1: ['command\ name' or PID number(,s)] 2: MAX_CPU_PERCENT
    [[ $# -ne 2 ]] && exit 1
    PID_NAMES=$1
    # get all PIDS as nn,nn,nn
    if [[ ! "$PID_NAMES" =~ ^[0-9,]+$ ]] ; then
        PIDS=$(pgrep -d ',' -x $PID_NAMES)
    else
        PIDS=$PID_NAMES
    fi
    # echo "$PIDS $MAX_CPU"
    MAX_CPU="$2"
    MAX_CPU="$(echo "($MAX_CPU+0.5)/1" | bc)"
    LOOP=1
    while [[ $LOOP -eq 1 ]] ; do
        sleep 0.3s
        # Depending on your 'top' version and OS you might have
        # to change head and tail line-numbers
        LINE="$(top -b -d 0 -n 1 -p $PIDS | head -n 8 \
            | tail -n 1 | sed -r 's/[ ]+/,/g' | \
            sed -r 's/^\,|\,$//')"
        # If multiple processes in $PIDS, $LINE will only match\
        # the most active process
        CURR_PID=$(echo "$LINE" | cut -d ',' -f 1)
        # calculate cpu limits
        CURR_CPU_FLOAT=$(echo "$LINE"| cut -d ',' -f 9)
        CURR_CPU=$(echo "($CURR_CPU_FLOAT+0.5)/1" | bc)
        echo "PID $CURR_PID: $CURR_CPU""%"
        if [[ $CURR_CPU -ge $MAX_CPU ]] ; then
            now="$(date)"
            echo "PID $CURR_PID ($PID_NAMES) went over $MAX_CPU""%" on $now
            jstack $CURR_PID > ./$now+jlog.txt
            echo "[[ $CURR_CPU""% -ge $MAX_CPU""% ]]"
            LOOP=0
            break
        fi
    done
    echo "Stopped"



Then we need to get process id of running WSO2 server by running following command.
    sanjeewa@sanjeewa-ThinkPad-T530:~/work$ jps
    30755 Bootstrap
    8543 Jps
    4892 Main


Now we know carbon server running with process ID 30755. Then we can start our script by providing init parameters(process ID and CPU limit). So it will keep printing CPU usage in terminal and once it reached limit it will take thread dump using jstack command. It will create new file with with embedding current date time and push Jstack output to it.

We can start scritp like this.
    sanjeewa@sanjeewa-ThinkPad-T530:~/work$ sh test.sh 30755 99

As you can see when CPU goes above 99% it will create log file and append thread dump.

 

Public sample wadl urls for testing

Public sample wadl urls for testing: https://developer.dnb.com/docs/v1/1.0/rest

 

Tuesday, February 17, 2015

Installing certificates into trust store

openssl s_client -showcerts -connect www.trimblemaps.com:443 </dev/null 2>/dev/null|openssl x509 -outform PEM > /tmp/trimblemaps.pem

 

keytool -importcert -alias trimblemaps -keystore client-truststore.jks --file /tmp/trimblemaps.pem

Installing Mongo in Linux using yum

Installing Mongo in linux using yum.

The Mongo DB downloads repository contains two packages:

  • mongo-10gen-server

This package contains the mongod and mongos daemons from the latest stable release and associated configuration and init scripts. Additionally, you can use this package to install daemons from a previous release of MongoDB.

  • mongo-10gen

This package contains all MongoDB tools from the latest stable release. Additionally, you can use this package to install tools from a previous release of MongoDB. Install this package on all production MongoDB hosts and optionally on other systems from which you may need to administer MongoDB systems.

Install MongoDB

Configure Package Management System (YUM)

Create a /etc/yum.repos.d/mongodb.repo file to hold the following configuration information for the MongoDB repository:

TIP

For production deployments, always run MongoDB on 64-bit systems.

If you are running a 64-bit system, use the following configuration:

[mongodb]

name=MongoDB Repository

baseurl=http://downloads-distro.mongodb.org/repo/redhat/os/x86_64/

gpgcheck=0

enabled=1

If you are running a 32-bit system, which is not recommended for production deployments, use the following configuration:

[mongodb]

name=MongoDB Repository

baseurl=http://downloads-distro.mongodb.org/repo/redhat/os/i686/

gpgcheck=0

enabled=1

Install Packages

Issue the following command (as root or with sudo) to install the latest stable version of MongoDB and the associated tools:

yum install mongo-10gen mongo-10gen-server

When this command completes, you have successfully installed MongoDB!

Manage Installed Versions

You can use the mongo-10gen and mongo-10gen-server packages to install previous releases of MongoDB. To install a specific release, append the version number, as in the following example:

yum install mongo-10gen-2.2.3 mongo-10gen-server-2.2.3

This installs the mongo-10gen and mongo-10gen-server packages with the 2.2.3 release. You can specify any available version of MongoDB; however yum will upgrade the mongo-10gen and mongo-10gen-server packages when a newer version becomes available. Use the following pinningprocedure to prevent unintended upgrades.

To pin a package, add the following line to your /etc/yum.conf file:

exclude=mongo-10gen,mongo-10gen-server

 

Wednesday, February 11, 2015

How to get the url params in custom sequences in wso2 API Manager - 1.7.0

Here is the solution:

 

Example requesst :

GET http://localhost:8280/foo?a=1&b=2

 

and to log values of a and b

 

<sequence xmlns="http://ws.apache.org/ns/synapse" name="URL--PARAMS --In">

            <property name="VAL_a" expression="$url:a"/>

            <property name="VAL_b" expression="$url:b"/>

</sequence>

 

Thursday, February 5, 2015

How to swap the headers in a request in wso2 API Manager

File Name: /_system/governance/apimgt/customsequences/in/auth_token_swapper.xml

Content: 
<sequence xmlns="http://ws.apache.org/ns/synapse" name="AuthToken--Swapper--In">
         <property name="Backend-Authorization" expression="$trp:Backend-Authorization" scope="default" type="STRING"/>
         <property name="TPaaS-Authorization" expression="$trp:Authorization" scope="default" type="STRING"/>
    <filter xpath="fn:string-length(get-property('Backend-Authorization')) > 0">
          <then>
              <property name="TPaaS-Authorization" expression="get-property('TPaaS-Authorization')" scope="transport"/>
              <property name="Authorization" expression="get-property('Backend-Authorization')" scope="transport"/>
              <log level="custom">
                  <property name="TRACE" value="Global Mediation Extension by Guru Authorization is there."/>
              </log>
          </then>
          <else>
              <log level="custom">
                  <property name="TRACE" value="Global Mediation Extension by Guru Authorization is not there."/>
              </log>
          </else>
     </filter>
</sequence>

How to remove header from a request using wso2 API Manager

File Name: /_system/governance/apimgt/customsequences/in/auth_token_remover.xml

Content:
<sequence xmlns="http://ws.apache.org/ns/synapse" name="AuthToken--Remover--In">
    <header name="Authorization" scope="transport" action="remove"/>
    <log level="custom">
        <property name="TRACE" value="Global Mediation Extension by Krishna for Authorization remove."/>
    </log>
</sequence>