これは、インターネットから検索した Java コードです: http://avery-leo.iteye.com/blog/298724
その目標は、Linux システムの CPU 使用率とメモリ情報を取得することです。Eclipse でコンパイルしたところ、次の 2 つのエラーが見つかりました。
private Config config=Config.getInstance();
SnmpUtil util=new SnmpUtil();
また、メモ部分に太字でマークします。
Config
これら 2 つのエラーは classとの不足が原因だと思います。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());
}
}