I would like to introduce a small project that I developed at @plugtree in the last months. It’s a project to monitor and persist internal metrics of Drools Knowledge Session’s. This will allow you to gather internal metrics information of the knowledge base and sessions registered into the JVM (local or remote) using the Drools JMX API and persist this information into a database (right now it’s using Hibernate as the ORM but it’s pluggable enough to use another persistence framework, which should be the ideal because of the huge amount of data to be stored). As you can imagine it’s really useful to use to create your custom dashboard to monitor how healthy are your knowledge session´s, to made predictive analysis, etc.
It currently supports the following features, but there are more coming:
- knowledge base auto discovery.
- knowledge sessions auto discovery.
- configurable knowledge scanners.
- pluggable persistence framework.
- spring framework configuration friendly.
Brief Architecture explanation
It was developed in three simple modules:
- drools-metrics-api: contains listener extensions to gather custom information, and is actually in development.
- drools-metrics-model: the metrics domain model.
- drools-metrics-persistence: the engine to capture and store the metrics.
The next graphic can help you to get a general picture about how interacts the internal components. In this example drools-server instances are being monitored.
Each JVM to be monitored has to have enabled the Drools JMX Management, which will expose the MBean created when the internal MBean Server is created. Once the drools-metrics-persistence is started, a scanner agent is created by each configured JVM. These scanner agents create a connection to the JVM, start to discover the registered knowledge resources and create the metrics scanner for each of these resources, that are going to gather the metrics in the configured time interval. Also, each scanner agent is going to be assigned one persistence scheduler that will consume the internally buffered metrics from the metrics scanner and persist them in bulk mode.
There are two preliminary steps to start using the application, one is regarding to enable the JMX monitoring in your drools application and the another one is related to JVM JMX configuration
- Enable JMX Management and register your knowledge resources in your Drools application: Use these snippet code to register the knowledge base and session that you want to be monitored.
// enable the JMX management DroolsManagementAgent kmanagement = DroolsManagementAgent.getInstance(); // registering a Knowledge Base kmanagement.registerKnowledgeBase((ReteooRuleBase) ((KnowledgeBaseImpl)kbase).getRuleBase()); // registering a Stateful Knowledge Session kmanagement.registerKnowledgeSession(((StatefulKnowledgeSessionImpl)ksession).getInternalWorkingMemory());
- Configure the JVM to accept remote JMX connections adding these arguments
-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=3000 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false
The current implementation is using JPA/Hibernate as the default persistence engine, and the the default configuration should be modified in the hibernate.cfg.xml file stored internally the application package. Remember that you can implement your own persistence framework, and even a contribution should be great.
How to configure
At this moment the configuration is internally stored in a configuration.xml file.
<?xml version="1.0" encoding="UTF-8"?> <configuration> <connections> <jvm name="jvm1" address="localhost" port="3000" scanInterval="1000" persistenceInterval="10000"/> <jvm name="jvm2" address="localhost" port="3003" scanInterval="1000" persistenceInterval="10000"/> </connections> </configuration>
As you can see, you can configure local or remote JVM’s to be monitored with several parameters. Let’s take a piece of the configuration to see how configure one JVM.
<jvm name="jvm1" address="localhost" port="3000" scanInterval="1000" persistenceInterval="10000"/>
The configuration of one JVM has 5 parameter, and only three of them are required.
- name: it should be an unique JVM identificator (required)
- address: the IP address where the JVM is located (required)
- port: the port where the JVM is waiting for remote connections (required)
- scanInterval: interval to scan the registered knowledge resources. default: 1s (optional)
- persistenceInterval: interval to do a bulk store into the data storage. default: 60s (optional)
How to use
Once you have downloaded the distribution or compiled the project yourself, you only have to execute the mvn assembly:assembly goal in the drools-metrics-persistence project to generate a distribution with all the dependencies needed to execute it and also a bash script.
type sh start.sh and happy monitoring!
There are other modules under construction to consume the persisted metrics and display the information in a portal application, using portlets. If you have interest on it you can take a look on the drools-metrics-monitor submodules and give us some feedback, or even improve them.
You can clone the code from the github repository, fork or whatever you want from https://github.com/plugtree/drools-metrics