0

Using JConsole someone can access to the metrics that were gathered by default for OS like memory, CPU load and ..., in addition to process specific metrics. My question is can we add some OS customized metrics, like the usage of some directory using Java Files API or checking if a specific port is responsive? I gather so-called metrics using remote SSH and the commands like du -sh /directory that has so many delays and I want to get it using JMX so it could run faster.

This question talked about adding spring metrics.

Soheil Pourbafrani
  • 3,249
  • 3
  • 32
  • 69

1 Answers1

0

As the linked question shows it is easy to expose a Java class as an MBean, so you could certainly write a class that collects the metrics you need. Implementing du in Java is not difficult. However, I'm not sure that it will solve your problem. The example of du -sh /directory is probably slow because it needs to recursively measure the size of a directory hierarchy. That will be just as slow (probably slower!) in Java.

As a side note I would normally use collectd or Telegraf for that kind of thing, but again the I/O cost for finding disk usage would be the same.

I would suggest adding some logs with times to your current script so that you can see where it spends time. If it takes less than a second to connect with SSH and 15 seconds to determine the directory size, for example, moving from SSH to JMX won't help.

ewramner
  • 5,810
  • 2
  • 17
  • 33
  • Is there any faster approach?, I need to get metric data in less than a second! – Soheil Pourbafrani Jun 23 '18 at 09:25
  • There are two dimensions. 1) How long time does it take to fetch the data? 2) How long time does it take to prepare (measure) the data? If it takes more than a second to scan the file system and determine how large a directory tree is you have a problem. I would split the metrics and cache some results. You could measure cheap ones every second, but for the expensive metrics (such as directory size) you could cache the first result and return it again. After a certain time you flush the cache and measure. You don't need disk usage updates every second, right? Works in Java and in scripts. – ewramner Jun 23 '18 at 11:16