I have enjoyed using Zapcat with Zabbix. The standard Tomcat export from Zapcat and import from Zabbix works well and I can actually see the MBean attributes in Zabbix for all the MBean attributes I set up for the Tomcat host that gets created for you during the import process.
I have also tried the Zapcat Trapper and it works great from a command line Java program. I can pick up MBean attributes as well other key/value pairs that you wish to send. However, I have had trouble using the Trapper from a web application. I built the application using Maven and deployed the generated WAR to Tomcat 6. I built the code with Java 6. The code always dies around here (outlined in red):
public ZabbixTrapper(final String zabbixServer, final String host) throws UnknownHostException
{
final String server = System.getProperty(SERVER_PROPERTY, zabbixServer);
final String serverPort = System.getProperty(PORT_PROPERTY, Integer
.toString(DEFAULT_PORT));
final String serverHost = System.getProperty(HOST_PROPERTY, host);
sender = new Sender(queue, InetAddress.getByName(server), Integer
.parseInt(serverPort), serverHost);
sender.start();
}
Do you have any ideas as to how I can get the Trapper working in a web application? It is a very, very simple proof-of-concept web application. As I mentioned above, I've got this working in a regular command-line Java program running from IntelliJ IDEA. From this command-line program I have no problem pushing traps to Zabbix. I see these values in Zabbix soon after. I'm wondering whether the fact that Zapcat creates dmon threads causes problems in a managed (Tomcat) environment?
The servlet code is simple and is shown hereunder. Bear in mind that this is only a proof-of-concept. I just need to show that the Trapper works in a managed environment:
package com.real.research;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.UnknownHostException;
import java.util.Date;
import java.util.concurrent.TimeUnit;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.kjkoster.zapcat.Trapper;
import org.kjkoster.zapcat.zabbix.ZabbixTrapper;
/**
* ${NAME}
*
* @author Viraf Karai
*/
public class Viraf extends HttpServlet {
private static final String IP_ADS_TO_MONITOR = "172.21.121.249"; // "172.27.128.98"; //"192.168.0.150";
private static final String HOST_TO_MONITOR = "SEAD2vkarai"; // mark-app01.dev.real.com"; //""mac.kjkoster.org";
@Override
public void init() {
sendZabbixTrap();
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
final PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>Hola</title>");
out.println("</head>");
out.println("<body bgcolor=\"white\">");
out.println(String.format("<h1>Trapper message was sent to Zabbix at: %s</h1>", new Date().toString()));
out.println("</body>");
out.println("</html>");
out.flush();
for (int i = 0; i < 20; ++i) {
sendZabbixTrap();
try {
Thread.sleep(3000L);
}
catch(InterruptedException ie) {
}
}
}
private void sendZabbixTrap() {
Trapper trapper = null;
try {
trapper = new ZabbixTrapper(IP_ADS_TO_MONITOR, HOST_TO_MONITOR);
final long currTime = System.currentTimeMillis();
trapper.send("java.version", System.getProperty("java.version") + "_" + currTime);
trapper.send("java.loaded.classcount", "java.lang:type=ClassLoading", "LoadedClassCount");
trapper.send("viraf.networth", currTime);
/*trapper.send("free.physical.mem", "java.lang:type=OperatingSystem", "FreePhysicalMemorySize");
trapper.send("compiler.time", "java.lang:type=Compilation", "TotalCompilationTime");*/
/* trapper.every(30, TimeUnit.SECONDS, "free.physical.mem", "java.lang:type=OperatingSystem", "FreePhysicalMemorySize");
trapper.every(30, TimeUnit.SECONDS, "compiler.time", "java.lang:type=Compilation", "TotalCompilationTime");
// simulate lots of important work being done
Thread.sleep(Long.MAX_VALUE);*/
}
catch (Exception e) {
e.printStackTrace();
}
/* catch (InterruptedException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} */
finally {
if (trapper != null) {
trapper.stop();
}
}
}
}
I have also tried the Zapcat Trapper and it works great from a command line Java program. I can pick up MBean attributes as well other key/value pairs that you wish to send. However, I have had trouble using the Trapper from a web application. I built the application using Maven and deployed the generated WAR to Tomcat 6. I built the code with Java 6. The code always dies around here (outlined in red):
public ZabbixTrapper(final String zabbixServer, final String host) throws UnknownHostException
{
final String server = System.getProperty(SERVER_PROPERTY, zabbixServer);
final String serverPort = System.getProperty(PORT_PROPERTY, Integer
.toString(DEFAULT_PORT));
final String serverHost = System.getProperty(HOST_PROPERTY, host);
sender = new Sender(queue, InetAddress.getByName(server), Integer
.parseInt(serverPort), serverHost);
sender.start();
}
Do you have any ideas as to how I can get the Trapper working in a web application? It is a very, very simple proof-of-concept web application. As I mentioned above, I've got this working in a regular command-line Java program running from IntelliJ IDEA. From this command-line program I have no problem pushing traps to Zabbix. I see these values in Zabbix soon after. I'm wondering whether the fact that Zapcat creates dmon threads causes problems in a managed (Tomcat) environment?
The servlet code is simple and is shown hereunder. Bear in mind that this is only a proof-of-concept. I just need to show that the Trapper works in a managed environment:
package com.real.research;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.UnknownHostException;
import java.util.Date;
import java.util.concurrent.TimeUnit;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.kjkoster.zapcat.Trapper;
import org.kjkoster.zapcat.zabbix.ZabbixTrapper;
/**
* ${NAME}
*
* @author Viraf Karai
*/
public class Viraf extends HttpServlet {
private static final String IP_ADS_TO_MONITOR = "172.21.121.249"; // "172.27.128.98"; //"192.168.0.150";
private static final String HOST_TO_MONITOR = "SEAD2vkarai"; // mark-app01.dev.real.com"; //""mac.kjkoster.org";
@Override
public void init() {
sendZabbixTrap();
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
final PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>Hola</title>");
out.println("</head>");
out.println("<body bgcolor=\"white\">");
out.println(String.format("<h1>Trapper message was sent to Zabbix at: %s</h1>", new Date().toString()));
out.println("</body>");
out.println("</html>");
out.flush();
for (int i = 0; i < 20; ++i) {
sendZabbixTrap();
try {
Thread.sleep(3000L);
}
catch(InterruptedException ie) {
}
}
}
private void sendZabbixTrap() {
Trapper trapper = null;
try {
trapper = new ZabbixTrapper(IP_ADS_TO_MONITOR, HOST_TO_MONITOR);
final long currTime = System.currentTimeMillis();
trapper.send("java.version", System.getProperty("java.version") + "_" + currTime);
trapper.send("java.loaded.classcount", "java.lang:type=ClassLoading", "LoadedClassCount");
trapper.send("viraf.networth", currTime);
/*trapper.send("free.physical.mem", "java.lang:type=OperatingSystem", "FreePhysicalMemorySize");
trapper.send("compiler.time", "java.lang:type=Compilation", "TotalCompilationTime");*/
/* trapper.every(30, TimeUnit.SECONDS, "free.physical.mem", "java.lang:type=OperatingSystem", "FreePhysicalMemorySize");
trapper.every(30, TimeUnit.SECONDS, "compiler.time", "java.lang:type=Compilation", "TotalCompilationTime");
// simulate lots of important work being done
Thread.sleep(Long.MAX_VALUE);*/
}
catch (Exception e) {
e.printStackTrace();
}
/* catch (InterruptedException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} */
finally {
if (trapper != null) {
trapper.stop();
}
}
}
}
Comment