-4

这是我从互联网上搜索的一个 java 代码:http: //avery-leo.iteye.com/blog/298724

它的目标是获取 Linux 系统中的 CPU 使用率和内存信息。我在eclipse中编译,发现两个错误如下:

private Config config=Config.getInstance();
SnmpUtil util=new SnmpUtil();  

我也在注释部分用粗体标记。

我认为这两个错误是由于缺少class Configand引起的SnmpUtil,我尝试从网上搜索下载一个config.jar并将其添加到lib中,但仍然无法正常工作!我需要你的帮助!!

import java.io.BufferedReader;  
import java.io.File;  
import java.io.FileInputStream;  
import java.io.IOException;  
import java.io.InputStreamReader;  
import java.net.InetAddress;  
import java.util.StringTokenizer;  

import org.apache.log4j.Logger;  

/** 
 * To get the cpu usage and memory in Linux system
 *  
 * <p> 
 */  
public final class LinuxSystemTool implements Runnable{  
    private Logger log = Logger.getLogger(LinuxSystemTool.class);  
    private Config config=Config.getInstance();  //**Error when compiled**
    /** 
     * get memory by used info 
     *  
     * @return int[] result 
     *                      
     * result.length==4; int[0]=MemTotal;int[1]=MemFree;                  
     * int[2]=SwapTotal;int[3]=SwapFree; 
     * @throws IOException    
     * @throws InterruptedException 
     */  
public void run() {  
    // TODO Auto-generated method stub  

    while (true) {  

        try {  
            exec();  
            Thread.sleep(config.getThreadTime());  
        } catch (Exception e) {  
            // TODO Auto-generated catch block  
            log.error("Performance Monitoring error:"+e.getMessage());  
            e.printStackTrace();  
        }  
    }  
}  
public void exec() throws Exception {  
//      ServerStatus ss = new ServerStatus();  

    InetAddress inet = InetAddress.getLocalHost();  
    System.out.println("Performance Monitoring ip:"+inet.toString());  

    String ip=inet.toString().substring(inet.toString().indexOf("/")+1);  
    log.info("Performance Monitoring ip:"+ip);  

    int[] memInfo = LinuxSystemTool.getMemInfo();  
    System.out.println("MemTotal:" + memInfo[0]);  
    System.out.println("MemFree:" + memInfo[1]);  

    SnmpUtil util=new SnmpUtil();  //**Error when compiled**
    util.setCPU(getCpuInfo());  
//  util.setDISK(1);  
    util.setMEM(memInfo[0]/memInfo[1]);  
    util.setIP(ip);  

}  
public static int[] getMemInfo() throws IOException, InterruptedException {  
    File file = new File("/proc/meminfo");  
    BufferedReader br = new BufferedReader(new InputStreamReader(  
            new FileInputStream(file)));  
    int[] result = new int[4];  
    String str = null;  
    StringTokenizer token = null;  
    while ((str = br.readLine()) != null) {  
        token = new StringTokenizer(str);  
        if (!token.hasMoreTokens())  
            continue;  

        str = token.nextToken();  
        if (!token.hasMoreTokens())  
            continue;  

        if (str.equalsIgnoreCase("MemTotal:"))  
            result[0] = Integer.parseInt(token.nextToken());  
        else if (str.equalsIgnoreCase("MemFree:"))  
            result[1] = Integer.parseInt(token.nextToken());  
        else if (str.equalsIgnoreCase("SwapTotal:"))  
            result[2] = Integer.parseInt(token.nextToken());  
        else if (str.equalsIgnoreCase("SwapFree:"))  
            result[3] = Integer.parseInt(token.nextToken());  
    }  

    return result;  
}  

/** 
 * get memory by used info 
 *  
 * @return float efficiency 
 * @throws IOException 
 * @throws InterruptedException 
 */  
public static float getCpuInfo() throws IOException, InterruptedException {  
    File file = new File("/proc/stat");  
    BufferedReader br = new BufferedReader(new InputStreamReader(  
            new FileInputStream(file)));  
    StringTokenizer token = new StringTokenizer(br.readLine());  
    token.nextToken();  
    int user1 = Integer.parseInt(token.nextToken());  
    int nice1 = Integer.parseInt(token.nextToken());  
    int sys1 = Integer.parseInt(token.nextToken());  
    int idle1 = Integer.parseInt(token.nextToken());  

    Thread.sleep(1000);  

    br = new BufferedReader(  
            new InputStreamReader(new FileInputStream(file)));  
    token = new StringTokenizer(br.readLine());  
    token.nextToken();  
    int user2 = Integer.parseInt(token.nextToken());  
    int nice2 = Integer.parseInt(token.nextToken());  
    int sys2 = Integer.parseInt(token.nextToken());  
    int idle2 = Integer.parseInt(token.nextToken());  

    return (float) ((user2 + sys2 + nice2) - (user1 + sys1 + nice1))  
            / (float) ((user2 + nice2 + sys2 + idle2) - (user1 + nice1  
                    + sys1 + idle1));  
}  

/** 
 *  
 * <p> 
 *  
 * @author 
 * </p> 
 * @date 
 */  

public static void main(String[] args) throws Exception {  
    int[] memInfo = LinuxSystemTool.getMemInfo();  
    System.out.println("MemTotal:" + memInfo[0]);  
    System.out.println("MemFree:" + memInfo[1]);  
    System.out.println("SwapTotal:" + memInfo[2]);  
    System.out.println("SwapFree:" + memInfo[3]);  

    System.out.println("CPU use ratio:" + LinuxSystemTool.getCpuInfo());  
}  
}  
4

1 回答 1

3

您需要 snmp4j jar 用于 snmp 相关的 java 开发。你可以从这里得到。

希望这对你有用。

于 2012-05-13T14:33:17.240 回答