使用 Java 在 Linux 中监控网络使用情况 [英] Network Usage Monitoring in Linux using Java

查看:45
本文介绍了使用 Java 在 Linux 中监控网络使用情况的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于 Java 的媒体服务器,可将内容流式传输到网页和 Android 设备.它的设置可以供多个用户使用,还可以与其他服务器交谈并与他们共享媒体.

我希望能够通过我的媒体服务器中的服务监控总网络流量 (kb/s),这样我就可以通过我的 android 设备或网页查看它,而无需查看我的服务器.

是否有使用 Java 获取此信息的简单方法?某种linux资源文件?我通过谷歌搜索查看了 netstat 和其他几个,但它们似乎并没有以明显的方式向我提供我想要的东西.我只需要总流量,我不需要知道谁在和我说话.

感谢您的帮助.:)

pps:我使用的是最新版本的 OpenSUSE,但也有在 Fedora 上运行的版本(最新版本).

解决方案

您可以从 /proc/net/dev 读取网络统计信息——请参阅 http://linuxdevcenter.com/pub/a/linux/2000/11/16/LinuxAdmin.html 了解详情.

您会看到如下内容:

<前>间-|接收 |发送face |字节数据包错误丢弃 fifo 帧压缩多播|字节数据包错误丢弃 fifo colls 载波压缩lo: 2504 30 0 0 0 0 0 0 2504 30 0 0 0 0 0 0eth0:33769785 2548318 0 0 0 0 0 0 4146854 215379 0 0 0 0 0 0eth1:0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

因此您需要打开文件,阅读每一行并检查第一列以找到您关心的接口,然后提取接收和/或发送的字节和/或数据包.如果您跟踪读取之间的时间,则可以这样计算速率:


b0 = t0
b1 = t1 时的字节数,晚于 t0.

然后你可以计算速率,r,像这样:
r = (b1 - b0)/(t1 - t0)

I've got a Java based media server which streams content to both a webpage and an android device. It's set up so it can be used by multiple users and also talk with other servers and share media with them.

I'd like to be able to monitor the total network traffic (kb/s) via a service in my media server, so I can see it via my android device or webpage without having to look at my server.

Is there an easy way to get this information with Java? Some kind of linux resource file? I've looked at netstat and few others from googling and they don't seem to deliver me what I want in an obvious fashion. I just need the total traffic, I don't need to know who is talking to me.

Thanks for any help. :)

pps: I'm using the most recent release of OpenSUSE, but also have working versions on Fedora (most recent).

解决方案

You can read network statistics from /proc/net/dev -- see http://linuxdevcenter.com/pub/a/linux/2000/11/16/LinuxAdmin.html for details.

You'll see something like this:

Inter-|   Receive                                                |  Transmit
 face |bytes    packets errs drop fifo frame compressed multicast|bytes    packets errs drop fifo colls carrier compressed
    lo:    2504      30    0    0    0     0          0         0     2504      30    0    0    0     0       0          0
  eth0:33769785 2548318    0    0    0     0          0         0  4146854  215379    0    0    0     0       0          0
  eth1:       0       0    0    0    0     0          0         0        0       0    0    0    0     0       0          0

So you'll need to open the file, read each line and check the first column to find the interface(s) you care about, and then extract the received and/or sent bytes and/or packets. If you keep track of the time between reads, you can calculate the rate like this:

Let
b0 = bytes at time t0
b1 = bytes at time t1, some time later than t0.

Then you can calculate the rate, r, like this:
r = (b1 - b0) / (t1 - t0)

这篇关于使用 Java 在 Linux 中监控网络使用情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆