使用 Java 进行简单的服务器监控

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1404986/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 16:25:51  来源:igfitidea点击:

Simple Server Monitoring with Java

javalinuxmonitoringhealth-monitoring

提问by MSR

I'm trying to find a solution that allows me to monitor resource consumption of a server. Preferably, the metrics I'm wanting to obtain are network utilisation IO, and if possible CPU usage/load average and disk IO.

我试图找到一种解决方案,让我能够监控服务器的资源消耗。最好,我想要获得的指标是网络利用率 IO,如果可能的话,还有 CPU 使用率/平均负载和磁盘 IO。

The only other requirement I have is that this information be obtainable by Java so it can be manipulated, and at least work on Linux (Fedora).

我唯一的另一个要求是这些信息可以通过 Java 获取,以便可以对其进行操作,并且至少可以在 Linux (Fedora) 上工作。

I've heard about a few monitoring tools but I'm just not sure of the best way of going about this. I would probably want to be gathering the information about every 30 seconds.

我听说过一些监控工具,但我不确定解决这个问题的最佳方式。我可能希望每 30 秒收集一次信息。

Thanks

谢谢

Update: Just to re-iterate, I am referring to system-wide monitoring NOT Java specific monitoring. I just want to use Java to access to these metrics

更新:重申一下,我指的是系统范围的监控,而不是 Java 特定的监控。我只想使用 Java 来访问这些指标

回答by Pascal Thivent

You could choose to delegate the monitoring to a dedicated tool like Cacti, Centreon, or Zenossbut this might be a bit overkill for a single application.

您可以选择将监控委托给CactiCentreonZenoss等专用工具,但这对于单个应用程序来说可能有点矫枉过正。

For a simple solution, JMX might indeed be a better solution. As starting point, I suggest reading the following article: Monitoring Local and Remote Applications Using JMX 1.2 and JConsole. Then, have a look at Using JConsole to Monitor Applications, a very detailed article that shows how to use JConsole to access several core monitoring and management functionalities provided by the Java platform including:

对于一个简单的解决方案,JMX 可能确实是一个更好的解决方案。作为起点,我建议阅读以下文章:使用 JMX 1.2 和 JConsole 监控本地和远程应用程序。然后,看看Using JConsole to Monitor Applications,一篇非常详细的文章,展示了如何使用 JConsole 访问 Java 平台提供的几个核心监控和管理功能,包括:

  • Detect low memory
  • Enable or disable GC and class loading verbose tracing
  • Detect deadlocks
  • Control the log level of any loggers in an application
  • Access OS resources—Sun's platform extension
  • Manage an application's Managed Beans (MBeans)
  • 检测内存不足
  • 启用或禁用 GC 和类加载详细跟踪
  • 检测死锁
  • 控制应用程序中任何记录器的日志级别
  • 访问操作系统资源——Sun 的平台扩展
  • 管理应用程序的托管 Bean (MBean)

But, AFAIK, JMX won't give you access to network IO so you might need a combination of these tools. Luckily, many tools (e.g. Cacti, SmokePing) use the RDD format that you can easily manipulate with Java APIs like JRobinor rdd4j.

但是,AFAIK,JMX 不会让您访问网络 IO,因此您可能需要这些工具的组合。幸运的是,许多工具(例如 Cacti、SmokePing)都使用 RDD 格式,您可以使用JRobinrdd4j等 Java API 轻松操作这些格式。

回答by SimonJ

I'm growing fond of collectd, a modular C daemon focusing on monitoring (rather than graphing) with a multitude of plugins:

我越来越喜欢collectd,这是一个模块化的 C 守护进程,专注于监控(而不是绘图)和大量插件

There would seem to be several options for getting metrics into your Java code:

似乎有几个选项可以将指标输入到您的 Java 代码中:

  • in-process, using the aforementioned Java pluginto register a write callback to receive data from the various other plugins
  • over the network, by embedding jcollectd(a Java implementation of collectd's protocol) into your app
  • indirectly, by writing to a CSV or RRD file and using one of the various RRD implementations for Java
  • 在进程中,使用前面提到的Java 插件注册一个写回调来接收来自各种其他插件的数据
  • 通过网络,通过将jcollectdcollectd协议的 Java 实现)嵌入到您的应用程序中
  • 间接地,通过写入 CSV 或 RRD 文件并使用 Java 的各种 RRD 实现之一

回答by Heiko Rupp

Just want to throw RHQ (http://rhq-project.org/ ) in the mix :-)

只是想把 RHQ (http://rhq-project.org/) 放在一起:-)

回答by Adamski

You could look into using the Gangliamonitoring tool. It uses XML for its data representation and so I imagine it would be fairly straightforward to access the data from Java, and there are the added advantages that it's been designed to be highly scaleable, with the potential to record server metrics across a large number of machines.

您可以考虑使用Ganglia监控工具。它使用 XML 作为其数据表示,因此我认为从 Java 访问数据会相当简单,并且还有其他优势,即它被设计为高度可扩展的,有可能记录大量服务器指标机器。

回答by Eamorr

What's wrong with Runtime.exec("some command")?

Runtime.exec("some command") 有什么问题?

回答by nelsonslament

Your best bet would probably look in /proc for all your systems resource usage /proc/cpuinfo and /proc/net/ are a good place to start.

您最好的选择可能是在 /proc 中查看所有系统资源使用情况 /proc/cpuinfo 和 /proc/net/ 是一个很好的起点。

回答by Kevin

I am not sure if this would assist in your query. IMHO Cacti and Centreon seem to be too complex to invest time into for a requirement as simple as yours. There are simpler tools out there for server monitoring such as New Relicand SeaLion. SeaLion is the my current favourite. It gives simple default commands which cover most basic requirements (will probably cover yours as well). It's also extremely simple to setup and is free. You could try these out.

我不确定这是否有助于您的查询。恕我直言,Cacti 和 Centreon 似乎太复杂了,无法为像您这样简单的要求投入时间。有更简单的服务器监控工具,例如New RelicSeaLion。SeaLion 是我目前的最爱。它提供了简单的默认命令,涵盖了大多数基本要求(也可能涵盖您的要求)。它的设置也非常简单并且是免费的。你可以试试这些。