0

My current project in c# requires getting the number of under replicated partitions from a kafka server. I can view this in jconsole under the mbeans section, but I need to get the value in a c# program. I tried using NetMX with this code to make the initial connection.

AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
INetMXConnector connector = NetMXConnectorFactory.Connect(new Uri("http://<myserver>:<jmxport>"), null);    
IMBeanServerConnection remoteServer = connector.MBeanServerConnection;

A "Section not found" error is thrown at the second line and I was wondering if anyone could help with this please?

tobygriffin
  • 5,339
  • 4
  • 36
  • 61
Rish
  • 1
  • 3
  • It was not in .net, but I used jolokia for some basic monitoring and it works ok. Never tried INetMXConnector, but looking at github code, your error comes that you need an App.Config for it to work - see samples here https://github.com/SzymonPobiega/NetMX/tree/master/Samples/JmxClientDemo https://github.com/SzymonPobiega/NetMX/blob/master/NetMX.Default/MBeanServer.cs#L36 – Treziac Aug 30 '17 at 16:37

3 Answers3

0

I don't think NetMX will connect to a Java JMX endpoint. It's an "independent" implementation specifically for DotNet.

Your best bet is to load the Jolokia java-agent into your target Kafka servers. Jolokia exposes JMX via an HTTP/REST interface which you can access via C#.

You could try IKVM (a java byte code to CLR which will provide you a Java JMX compatible DLL you can invoke against. IKVM is a compiler that converts Java byte code to .NET byte code. The standard JMX remoting works fine from a C# client.

Nicholas
  • 15,916
  • 4
  • 42
  • 66
  • Hey, so I installed the Jolokia war agent into the webapp folder of tomcat. However when i make requests to get the kafka metrics, it gives an error. Furthermore, I used the list request and that also did not show anything about kafka, but it does show the standard java stuff like "java.lang". However, when I use jconsole on my computer to connect to the jmx port on the server, I can see all the kafka metrics. Have you come across this before? – Rish Aug 31 '17 at 10:59
  • I would use the jolokia java agent instead of the war. – Nicholas Aug 31 '17 at 11:15
  • I tried running this as stated in the documentation: java -javaagent:jolokia-jvm-1.3.7-agent.jar=port=7777,host=localhost I get this exception: Exception in thread "main" java.lang.reflect.InvocationTargetException Caused by: java.lang.NumberFormatException: For input string: "7777 host=localhost" FATAL ERROR in native method: processing of -javaagent failed. I have left out most of the stack trace. – Rish Aug 31 '17 at 12:07
0

In reference to your Jolokia configuration, perhaps you need to fully qualify the path of the jar. Mine looks like this and works:

export JOLOKIA_HOME=/libs/java/jolokia/1.3.7
export JOLOKIA_JAR=$JOLOKIA_HOME/jolokia-jvm-1.3.7-agent.jar
export KAFKA_OPTS="-javaagent:$JOLOKIA_JAR=port=7778,host=* $KAFKA_OPTS"

When I start Kafka in non-daemon mode, it prints this:

I> No access restrictor found, access to any MBean is allowed
Jolokia: Agent started with URL http://10.8.36.121:7778/jolokia/

Then I point my browser to http://localhost:7778/jolokia/search/: and I get:

{
  "request": {
    "mbean": "*:*",
    "type": "search"
  },
  "value": [
    "kafka.network:name=ResponseQueueTimeMs,request=ListGroups,type=RequestMetrics",
    "kafka.server:delayedOperation=topic,name=PurgatorySize,type=DelayedOperationPurgatory",
    "kafka.server:delayedOperation=Fetch,name=NumDelayedOperations,type=DelayedOperationPurgatory",
    "kafka.network:name=RemoteTimeMs,request=Heartbeat,type=RequestMetrics",
<-- SNIP -->
    "kafka.network:name=LocalTimeMs,request=Offsets,type=RequestMetrics"
  ],
  "timestamp": 1504188793,
  "status": 200
}
Nicholas
  • 15,916
  • 4
  • 42
  • 66
  • Thanks. I am actually running kafka on a windows server. So I set up an environment variable for "KAKFA_OPS" and restarted kafka yet it still does not work. Do I need to have something else to run the agent jar? FYI I have the JRE installed – Rish Aug 31 '17 at 18:46
0

1) Use jolokia jar to convert JMX to HTTP by adding:

KAFKA_OPTS: javaagent:/usr/share/java/kafka/jolokia-jvm-1.6.0-agent.jar -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Djava.rmi.server.hostname=localhost -Dcom.sun.management.jmxremote.rmi.port=9999 -Djava.security.auth.login.config=/var/private/sasl_acl/kafka.server.jaas.config.

2) You will get a http endpoint now you can try a sample get request to check whether it works http://localhost:8778/jolokia/read//java.lang:type=Memory/HeapMemoryUsage

3) Use standard rest api libraries to consume above endpoint.

Iskuskov Alexander
  • 4,077
  • 3
  • 23
  • 38
Vinayak Mishra
  • 341
  • 4
  • 11