【问题标题】:How to draw a live chart by reading the /proc/net/dev file in Ubuntu [closed]如何通过阅读 Ubuntu 中的 /proc/net/dev 文件来绘制实时图表 [关闭]
【发布时间】:2014-10-10 16:42:27
【问题描述】:

我想通过读取ubuntu中的/proc/net/dev文件来绘制图表,以显示wlan、eth和lo中接收到的字节和发送的字节。目前,这些详细信息以文本格式显示并实时更新。所以我想通过图表展示这些实时更新。此刻,我将我的文本显示为如下代码所示。

package throughput;

import java.awt.*;
import java.io.*;
import java.util.*;
import javax.swing.*;

public class Throughput extends JPanel implements Runnable {

    private ArrayList<String> lines;

    public Throughput() {
    lines = new ArrayList<String>();
    new Thread(this).start();
}

@Override
public void run() {
    try {
        while (true) {
            String sContents = readFile();
            StringTokenizer tok = new StringTokenizer(sContents, "\n");
            lines = new ArrayList<String>();
            int x = 0;

            while (tok.hasMoreTokens()) {
                String line = tok.nextToken();
                if (x++ < 2) {//skip headers
                    continue;
                }
                lines.add(line);
            }
            repaint();
            Thread.sleep(500);//half a second
        }
    } catch (Exception exc) {
        exc.printStackTrace();
    }
}

@Override
public void paint(Graphics gr) {
    super.paint(gr);
    Graphics2D g = (Graphics2D) gr;
    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g.setFont(new Font(Font.DIALOG_INPUT, Font.PLAIN, 14));
    g.drawString("Interface: receivedBytes/receivedPackets, sentBytes/sentPackets", 10, 20);
    for (int i = 0; i < lines.size(); i++) {
        drawLine(g, lines.get(i), i);
    }
}

private static final String readFile() {
    try {
        File file = new File("/proc/net/dev");
        StringBuilder sb = new StringBuilder();
        byte[] bytes = new byte[1024];
        int read = 0;
        InputStream in = new FileInputStream(file);
        while ((read = in.read(bytes)) != -1) {
            sb.append(new String(bytes, 0, read));
        }
        in.close();
        return sb.toString();
    } catch (Exception exc) {
        exc.printStackTrace();
    }
    return null;
}

private void drawLine(Graphics2D g, String line, int iLineIndex) {
    String[] words = line.split("\\s+");
    String sInterface = words[words[0].length() > 0 ? 0 : 1];
    int index = sInterface.indexOf(':');
    boolean jump = index != sInterface.length()-1;
    String receivedBytes = jump ? sInterface.substring(index+1) : words[2];
    String receivedPackets = jump ? words[2] : words[3];
    String sentBytes = jump ? words[9] : words[10];
    String sentPackets = jump ? words[10] : words[11];
    if(index > -1) {
        sInterface = sInterface.substring(0, index);
    }
    int x = 10, y = 50 + iLineIndex * 20;
    String sReceived = receivedBytes + "/" + receivedPackets;
    String sSent = sentBytes + "/" + sentPackets;
    g.drawString(sInterface + ":", x, y);
    g.drawString(sReceived, x+100, y);
    g.drawString(sSent, x+250, y);
}

}

package throughput;

import javax.swing.*;

public class Main extends JFrame{

    public static void main(String[] args) {
        new Main();
    }

    public Main() {
        setSize(550, 200);
        setTitle("Network Monitor");
        setContentPane(new Throughput());
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
    }
}

【问题讨论】:

  • 有什么问题?你被困在哪里了?
  • 我想通过读取ubuntu中的/proc/net/dev文件来绘制图表,以显示wlan、eth和lo中接收到的字节和发送的字节。目前,这些详细信息仅以文本格式显示并实时更新。这就是上面的代码。上面的代码不是问题。我想用上面的输出画一个实时图表。请帮忙!
  • 所以您希望有人为您编写代码?这不是 SO 的工作方式。您需要展示您的尝试,然后在遇到困难时提出具体问题。
  • 是的,我自己写了上面的代码。我想将其转换为以图形格式显示。我真的不知道如何完成它。我读到了 jfreechart 之类的东西。但不知道用它来绘制图表。如果你熟悉这个,请帮忙,因为我的作业需要这个。
  • 谢谢。你真的很有帮助。

标签: java ubuntu graph draw jfreechart


【解决方案1】:

为避免阻塞event dispatch thread,您需要在后台使用SwingWorker 读取(虚拟)文件,如从/dev/random 读取的example 所示。在process() 的实现中,更新图表的模型,通常是Dataset,封闭的ChartPanel 将看到图表自行更新。一个完整的例子见here

【讨论】:

  • 非常感谢。我现在试试这个。
猜你喜欢
  • 2018-04-28
  • 2012-02-15
  • 1970-01-01
  • 2016-01-04
  • 2010-09-10
  • 2016-06-28
  • 2011-05-25
  • 2023-04-06
  • 2014-01-11
相关资源
最近更新 更多