Page tree
Skip to end of metadata
Go to start of metadata

If you have multiple Java Processes you might want to monitor them with the javaagent light in a very fast plug-and-play way. This recipe explains how.

This recipes works with MoSKito 2.8.0, MoSKito Javaagent lite 2.8.0 and MoSKito Control 1.1.1. All three can be downloaded from the download page: https://www.moskito.org/download.html or directly: https://www.moskito.org/download/moskito-2.8.0.tgz



Preparation

Download the MoSKito package from the above link (or https://www.moskito.org/download/moskito-2.8.0.tgz if you are lazy) and unpack it in a directory of your choice. We will call this directory MOSKITO_HOME. You will obviously need Java and Maven, so install them if you don't have yet.

Setting up example.

To present the usage of the javaagent we need some running java processes. For demonstration purposes the moskito-demo project contains an example we will work with. The example code is located here:

https://github.com/anotheria/moskito-demo/tree/master/daemon/agentlight


The project contains a running Java Daemon and some configuration.

Code

Our code is plain simple it doesn't actually do a lot, except running in background:

Example code
public class Daemon {
   public static void main(String args[]) throws InterruptedException{
      Logger log = LoggerFactory.getLogger(Daemon.class);
      DaemonThread daemon = new DaemonThread();
      daemon.start();

      //wait forever.
      daemon.join();
   }

   static class DaemonThread extends Thread{
      public void run(){
         System.out.println("Daemon started. Will print something out every 60 seconds or so.");
         Random rnd = new Random(System.nanoTime());
         int sleepCounter = 0;
         long tasks = 0L;
         Worker worker = new Worker();
         while(true){
            try{
               Thread.sleep(20000);
            }catch(InterruptedException ignored){}

            int tasksThisTurn = rnd.nextInt(10);
            for (int i=0; i<tasksThisTurn; i++){
               worker.doSomeWork();
               tasks++;
            }

            sleepCounter++;
            if (sleepCounter==3){
               sleepCounter = 0;
               System.out.println("I am still alive, and I performed "+tasks+" tasks.");
            }
         }
      }
   }
}

public class Worker {
   public void doSomeWork(){
      //lets assume they do some work.
   }
}


Building 

To build the code simply checkout the project and build with maven:

git clone git@github.com:anotheria/moskito-demo.git
cd moskito-demo/
mvn install


After couple of seconds the demo project is build.


Running

To run the example change into the agentlight directory:

cd daemon/agentlight/


There is a start script there, called start.sh. Open it in an editor of your choice and change the first line:

export PATH_TO_JAVAAGENT<MOSKITO_HOME>/moskito-javaagent-lite/moskito-javaagent-lite-2.8.0.jar
echo starting javaagent light example with agent $PATH_TO_JAVAAGENT and port $1
java -javaagent:$PATH_TO_JAVAAGENT -DmoskitoAgentPort=$1 -cp target/moskito-demo-daemon-agentlight-1.0.0-SNAPSHOT-jar-with-dependencies.jar org.moskito.demo.daemon.agentlight.Daemon

Change the value of MOSKITO_HOME to the directory to which you downloaded MoSKito previously. 

You can now start any amount of instances with the start script by providing a port: ./start.sh 9415 

For convenience we added three start scripts with separate ports. So open three terminal windows/tabs and start ./start1.sh in the first, ./start2.sh in second and ./start3.sh in the third one.

The output will be something like this:

starting javaagent light example with agent ../../../msk-downloads/moskito/moskito-javaagent-lite/moskito-javaagent-lite-2.8.0.jar and port 9416
...
930 2017-06-14 18:18:16,719 [main] INFO   o.m.c.e.r.generated.AgentServer:119 - Getting local registry
931 2017-06-14 18:18:16,720 [main] INFO   o.m.c.e.r.generated.AgentServer:129 - Registering org_moskito_controlagent_endpoints_rmi_AgentService locally.
933 2017-06-14 18:18:16,722 [main] INFO   o.m.j.LightTransformationAgent:86 - Started Moskito-backend on 9416 port!
Daemon started. Will print something out every 60 seconds or so.

The process will run in the terminal and print out something like this:

I am still alive, and I performed 1083 tasks.

every minute.


Using MoSKito Tools with the Example.

MoSKito Inspect

MoSKito Inspect allows you to inspect a single JVM instance of your application. 

You downloaded MoSKito Inspect previously in the Preparations section. 

Change in MOSKITO_HOME/moskito-inspect/tomcat directory and start moskito inspect:  bin/startup.sh


Open a browser and navigate to http://localhost:8088/moskito/moskito-inspect/mskDashboard

You should see something like this:


In the left lower corner there is a Form called QuickConnect:


Enter host localhost and port 9415 (or whatever port you used to start your daemon in previous step) and press Connect.

You are now connected to your daemon and can now access all the data. You can always check your connection status in the top area of MoSKito Inspect:


MoSKito Control

MoSKito Inspect gives you insights on one JVM, but if you want to monitor a cluster you will need MoSKito Control. It is also included in the download package you downloaded earlier.

In a terminal window goto MOSKITO_HOME/moskito-control/webapps/moskito-control/WEB-INF/classes and open file named moskitocontrol.json for editing.

Add following content:

{
    "mailNotificationEnabled" : false,
    "notificationsMutingTime": 60,
    "defaultApplication" : "DAEMONS",

    "@applications": [
        {
        "name": "DAEMONS",
        "@components": [
            {
                "name": "D1",
                "category": "daemon",
                "connectorType": "RMI",
                "location": "localhost:9415"
            },
            {
                "name": "D2",
                "category": "daemon",
                "connectorType": "RMI",
                "location": "localhost:9416"
            },
            {
                "name": "D3",
                "category": "daemon",
                "connectorType": "RMI",
                "location": "localhost:9417"
            }
            ],
         "@charts": [
            {
               "name": "CPU Time 1m",
               "limit": 100,
               "@lines": [
                     {
                        "component": "D1",
                            "accumulator": "CPU Time 1m"
                     },
                     {
                        "component": "D2",
                            "accumulator": "CPU Time 1m"
                     },
                     {
                        "component": "D3",
                            "accumulator": "CPU Time 1m"
                     }
                    ]
               },
              {
                "name": "Process CPU Load 1m",
                "limit": 100,
                "@lines": [
                  {
                    "component": "D1",
                    "accumulator": "Process CPU Load 1m"
                  },
                  {
                    "component": "D2",
                    "accumulator": "Process CPU Load 1m"
                  },
                  {
                    "component": "D3",
                    "accumulator": "Process CPU Load 1m"
                  }
                ]
              },
              {
                "name": "System CPU Load 1m",
                "limit": 100,
                "@lines": [
                  {
                    "component": "D1",
                    "accumulator": "System CPU Load 1m"
                  },
                  {
                    "component": "D2",
                    "accumulator": "System CPU Load 1m"
                  },
                  {
                    "component": "D3",
                    "accumulator": "System CPU Load 1m"
                  }
                ]
              },

               {
                  "name": "Process and System CPU Load 1m in D1",
                  "limit": 100,
                  "@lines": [
                        {
                           "component": "D1",
                               "accumulator": "Process CPU Load 1m"
                        },
                        {
                           "component": "D1",
                               "accumulator": "System CPU Load 1m"
                        }
                        

                    ]
               }
            /*,
              {
                "name": "Threads in D1",
                "limit": 100,
                "@lines": [
                  {
                    "component": "D1",
                    "accumulator": "ThreadCount"
                  },
                  {
                    "component": "D1",
                    "accumulator": "ThreadStateBlocked-1m"
                  },
                  {
                    "component": "D1",
                    "accumulator": "ThreadStateRunnable-1m"
                  },
                  {
                    "component": "D1",
                    "accumulator": "ThreadStateTimedWaiting-1m"
                  },
                  {
                    "component": "D1",
                    "accumulator": "ThreadStateWaiting-1m"
                  }
                ]
              }*/

         ]
        }
      ],


    "@connectors": [
        {
            "type": "HTTP",
            "className": "org.moskito.control.connectors.HttpConnector"
        },
        {
            "type": "RMI",
            "className": "org.moskito.control.connectors.RMIConnector"
        },
        {
            "type": "JDBC",
            "className": "org.moskito.control.connectors.JDBCConnector"
        },
        {
            "type": "MONGO",
            "className": "org.moskito.control.connectors.MongoConnector"
        },
        {
            "type": "URL",
            "className": "org.moskito.control.connectors.HttpURLConnector"
        }


    ],

    "@statusUpdater": {
        "checkPeriodInSeconds": 10,
        "threadPoolSize": 50,
        "timeoutInSeconds": 60,
        "enabled": true
    },

    "@chartsUpdater": {
        "checkPeriodInSeconds": 40,
        "threadPoolSize": 30,
        "timeoutInSeconds": 60,
        "enabled": true
    },
   "@pluginsConfig":{
       "@plugins": [
       ]
   }

}


Now return to MOSKITO_HOME/moskito-control and start the tool (which is btw just a packaged tomcat) bin/startup.sh

After a short period of time MoSKito Control will be up. If you point your browser to http://localhost:8999/moskito-control/control/main you will see all your daemons (D1, D2 and D3) and their data:


You will be also able to 'look inside' the daemons and see thresholds and charts in each of the applications.







  • No labels