Showing posts with label carbon. Show all posts
Showing posts with label carbon. Show all posts

Thursday, June 4, 2015

How to write systemd service to start stop wso2 server in RHEL7 CentOS7



In rhel7/centos7 we use systemd services.

One of the major changes in RHEL  / CentOS 7.0 is the swtich to systemd, a system and service manager, that replaces SysV and Upstart used in previous releases of Red Hat Enterprise Linux. 

Ex.
#systemctl start myservice.service

Here I explain how to write systemd service in rhel7 to work with any wso2 (carbon) server.
Before creating systemd service we have create a service call file in your wso2Home/bin directory (if not exist a service file),

#cd <wso2HOME>/bin
#vi myService.sh

Then add below code to the file,

#!bin/sh
USER="user"           
PRODUCT_CODE="DSS"                               
CARBON_HOME="wso2HOME"               
LOCK_FILE="${CARBON_HOME}/wso2carbon.lck"
PID_FILE="${CARBON_HOME}/wso2carbon.pid"
CMD="${CARBON_HOME}/bin/wso2server.sh"  
JAVA_HOME="/opt/jdk1.7.0_60"          

export JAVA_HOME=$JAVA_HOME

# Status the service
status() {
 if [ -f $PID_FILE ]
     then
  PID=`cat $PID_FILE`
  ps -fp $PID > /dev/null 2>&1
  PIDVAL=$?
     else
  PIDVAL=3
 fi

 if [ $PIDVAL -eq 0 ]
     then
  echo "WSO2 $PRODUCT_CODE server is running ..."
     else
  echo "WSO2 $PRODUCT_CODE server is stopped."
 fi
 return $PIDVAL
}

# Start the service
start() {
 if [ -f $PID_FILE ]
     then
  PID=`cat $PID_FILE`
  ps -fp $PID > /dev/null 2>&1
  PIDVAL=$?
     else
  PIDVAL=3
 fi

 if [ $PIDVAL -eq 0 ]
     then
        echo "WSO2 $PRODUCT_CODE server is running ..."
     else
        echo -n "Starting WSO2 $PRODUCT_CODE server: "
        touch $LOCK_FILE
        su - $USER -c "$CMD start > /dev/null 2>&1 &"
        sleep 5
        if [ -f $PID_FILE ]
     then
   PID=`cat $PID_FILE`
   ps -fp $PID > /dev/null 2>&1
   PIDVAL=$?
   if [ $PIDVAL -eq 0 ]
       then
    echo "success"
       else
    echo "failure"
   fi
     else
   echo "failure"
   PIDVAL=2
        fi
 fi
 echo
 return $PIDVAL
}

# Restart the service
restart() {
 echo -n "Restarting WSO2 $PRODUCT_CODE server: "
 touch $LOCK_FILE
 su - $USER -c "$CMD restart > /dev/null 2>&1 &"
 sleep 15
 if [ -f $PID_FILE ]
     then
  PID=`cat $PID_FILE`
  ps -fp $PID > /dev/null 2>&1
  PIDVAL=$?
  if [ $PIDVAL -eq 0 ]
      then
   echo "success"
      else
   echo "failure"
  fi
     else
  echo "failure"
  PIDVAL=2
 fi
 echo
 return $PIDVAL
}

# Stop the service
stop() {
 if [ -f $PID_FILE ]
     then
  PID=`cat $PID_FILE`
  ps -fp $PID > /dev/null 2>&1
  PIDVAL=$?
  if [ $PIDVAL -eq 0 ]
      then
   echo -n "Stopping WSO2 $PRODUCT_CODE server: "
   su - $USER -c "$CMD stop > /dev/null 2>&1 &"
   rm -f $LOCK_FILE
   sleep 10
   PID=`cat $PID_FILE`
   ps -fp $PID > /dev/null 2>&1
   PIDVAL=$?
   if [ $PIDVAL -eq 0 ]
       then
    echo "failure"
    PIDVAL=2
       else
    echo "success"
    PIDVAL=0
   fi
      else
         echo "WSO2 $PRODUCT_CODE server is not running."
         PIDVAL=0
  fi
     else
        echo "WSO2 $PRODUCT_CODE server is not running."
        PIDVAL=0
 fi
 echo
 return $PIDVAL
}

### main logic ###
case "$1" in
start)
    start
    ;;
stop)
    stop
    ;;
status)
    status
    ;;
restart|reload|condrestart)
    restart
    ;;
*)
   echo $"Usage: $0 {start|stop|restart|reload|status}"
   exit 1
esac
exit $?

Change first 7 lines with your data. Reffere exmples in comments.

Now let’s create systemd service,

Systemd services are located in /etc/systemd/system directory.go to the directory,

#cd /etc/systemd/system

Create a file with .service extention(this will be service you are going to use),

#vi myservice.service

Then add below code to your created file,

[Unit]
Description=Wso2 Identity Server
After=syslog.target network.target

[Service]
Type=oneshot
ExecStart=/<wso2serverHOME>/bin/myService.sh start
ExecStop=/<wso2serverHOME>/bin/ myService.sh stop
RemainAfterExit=yes
StandardOutput=syslog
StandardError=syslog

[Install]
WantedBy=multi-user.target

That’s all. Now you have successfully created the systemd service.

Check the service using below commands and accessing wso2 product consoles,

#systemctl start myservice.service
#systemctl start myservice.service

** note that if you face any error starting stopping the service, check privileges granted to the service.

Sunday, February 22, 2015

WSO2 ESB Monitoring – logs, tail, file size



Logs

In wso2 esb there are different kind of log file that help you to audit services.
Before view log files you can set logging in your proxy services, in here you can use full logs or your own custom log.
Also you can enable statistics and enable tracing in the service level. To do that , List > specific service > enable tracing/enable statistics.
For that refer this link,

Now you can view logs. For that go <wso2Home>/repository/logs
Here simple description about each log file type,

·         http_access_management_console.log – this file logs about all https calls. Mainly about management console get and post calls.

·         Wso2carbon.log – this is the mail log file of wso2 esb. In here you can find all information regarding the system. As I previously mentioned, if you set logs in your proxy service, you can view thos logs in here. **** Keep remind that if you use full log in your proxy service, .log file may become bigger. So keep track these files. Also there is a feature inbuilt that will version all log files daily.

·         Patches.log – using this you can find all patch information.

·          Wso2_esb-errors.log – you can find all error messages in this place.

·         Wso2_esb_service.log – in here you can find all service invocations. Information about called services.

·         Wso2_esb_trace.log – as I mentioned previously, if you were activated tracing you can find all service traces, how the mediation flow going, who invoked services (client ips), what are the endpoints called. 

If you are using linux, you can tail your log files using tail command, this will help you to audit in an efficient way. To use tail use like below,

>tail -1000f  <esbHome>/repository/logs/wso2carbon.log

Also if you are using jconsole, remember to keep in touch with memory usage. It will show the sequence of memory usage and other all server information.