Access JMX via Golang

I need to access the running process JMX via TCP. I cannot install third-party utilities on a machine, such as Jolokia, to make JMX accessible via HTTP. Is there a library that allows Golang to speak JMX or Java RMI? Was the JMX client implemented in any language other than the JVM that I can use as inspiration for the Golang port? Read and write access will be preferable, but I agree to read only.

+5
source share
1 answer

Unfortunately, you cannot directly access JMX from languages ​​other than Java; which means you cannot write.

There are two ways to pull MBean data from other languages, although not very intuitively.

Bridge method

This method involves creating a Java client bridge to extract the MBeans JMX and then exchange it back to Go.

Fortunately, I really did it; specially for Go; you can get the code here . If you need recording capabilities, feel free to change this code and send me a PR.

Then you can use a tool like go-bindata to insert the jar into the Go binary; you would use os.Exec to run; for example (obviously, you first need to read go-bindata docs first on how to manage these assets):

 // RunJar runs the embedded mjb.jar returning the output from STDOUT func RunJar(host string, port int) (string, error) { nport := strconv.Itoa(port) _, err := os.Stat("/tmp/mjb.jar") if err != nil { return "", err } // Check that /usr/bin/java exists _, err = os.Stat("/usr/bin/java") if err != nil { return "", err } cmd := exec.Command("/usr/bin/java", "-jar", "/tmp/mjb.jar", host, nport) var out bytes.Buffer var erro bytes.Buffer cmd.Stdout = &out cmd.Stderr = &erro err = cmd.Run() if err != nil { return "", fmt.Errorf("%s %s", err.Error(), out.String()) } return out.String(), nil } 

Jolokia Method

This method includes embedding Jolokia in your Java application; obviously this only works for the java applications you support; and will not work for Java applications such as Cassandra or Kafka.

+2
source

Source: https://habr.com/ru/post/1264778/


All Articles