- 论坛徽章:
- 0
|
基于JRobin的磁盘IO监控
作者:终南
先列以下以前写过的文章作为参考:
1。
JRobin简介
2。
基于JRobin的网络监控管理
,以及
基于JRobin的CPU使用率监控
3。
磁盘IO性能监控(Linux 和 Windows)
这些文章介绍了JRobin、利用JRobin以及WIndows脚本技术监控网络和CPU使用率,以及如何获取Linux以及Windows下磁盘IO性能数据。将这些结合在一起,于是就有了使用Java语言写成的基于JRobin的磁盘IO监控代码。
监控程序的代码与利用ping监控网络和监控CPU基本类似,但是还是有一些不同:
1。由于可能有多个逻辑磁盘,因此需要先获取磁盘列表,然后才即每个磁盘的IO数据。
2。出于性能考虑,特别是在Windows下,在获取磁盘IO数据时,可以在执行脚本时一次性采集这些数据,根据磁盘名称保存,不用分别获取,这样可以提高性能。
3。数据的单位和显示在图形上的格式不同。
4。JRobin对数据源名称及其程度有要求,因此需要对磁盘名称进行一定的变更。
成果:
![]()
Java代码:
import java.awt.Color;
import java.awt.Font;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import java.util.logging.Logger;
import org.jrobin.core.RrdDb;
import org.jrobin.core.RrdDef;
import org.jrobin.core.Sample;
import org.jrobin.graph.RrdGraph;
import org.jrobin.graph.RrdGraphDef;
public class DiskIoMonitor {
String[] disks = null;
public static String[] execute(String[] commands) {
String[] strs = null;
File scriptFile = null;
try {
List cmdList = new ArrayList();
String osName = System.getProperty("os.name");
if (osName.indexOf("Windows") > -1) {
scriptFile = File.createTempFile("monitor", ".vbs");
cmdList.add("CMD.EXE");
cmdList.add("/C");
cmdList.add("CSCRIPT.EXE");
cmdList.add("//NoLogo");
} else {
scriptFile = File.createTempFile("monitor", ".sh");
cmdList.add("/bin/bash");
}
String fileName = scriptFile.getCanonicalPath();
PrintWriter writer = new PrintWriter(scriptFile);
for (int i = 0; i stdoutList = new ArrayList();
while ((line = stdout.readLine()) != null) {
stdoutList.add(line);
}
BufferedReader stderr = new BufferedReader(new InputStreamReader(p
.getErrorStream()));
List stderrList = new ArrayList();
while ((line = stderr.readLine()) != null) {
stderrList.add(line);
}
strs = stdoutList.toArray(new String[0]);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (scriptFile != null)
scriptFile.delete();
}
return strs;
}
private String dataFormat = "%5.1lf%sB";
private Logger logger = Logger.getLogger(this.getClass().getName());
private String monitorName = "diskio";
private String dataDir = ".";
private int step = 10;
private String rrdPath = "";
private Timer timer = new Timer();
private long timeStart = 0;
protected int width = 600;
protected int height = 150;
public DiskIoMonitor() {
this(null);
}
public DiskIoMonitor(String diskName) {
this.rrdPath = this.dataDir + File.separator + monitorName + ".rrd";
disks = getDisks();
if (diskName != null) {
for (int i = 0; i ");
gDef.setPoolUsed(false);
gDef.setImageFormat("png");
gDef.setSmallFont(new Font("Monospaced", Font.PLAIN, 11));
gDef.setLargeFont(new Font("SansSerif", Font.BOLD, 14));
// gDef.setAltYMrtg(true);
// create graph finally
RrdGraph graph = new RrdGraph(gDef);
// logger.info(graph.getRrdGraphInfo().dump());
logger.info("Graph created");
return graph.getRrdGraphInfo().getFilename();
} catch (Exception e) {
logger.warning("Error in generating graph: " + e.getMessage());
} finally {
if (rrdDb != null)
try {
rrdDb.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
/**
* Return a HashMap which contains the current value of each data source.
*
* @return the current value of each data source.
*/
public HashMap getValues() {
String osName = System.getProperty("os.name");
if (osName.indexOf("Windows") > -1) {
return getWinDSValues();
} else {
return getLinuxDSValues();
}
}
/**
* Initialization.
*/
public void initialize() throws Exception {
RrdDb rrdDb = null;
try {
rrdDb = new RrdDb(rrdPath);
} catch (Exception e) {
}
if (rrdDb == null) {
logger.info("RRD data is not located in " + rrdPath
+ ", create a new one");
RrdDef rrdDef = new RrdDef(rrdPath, timeStart - 1, step);
for (int i = 0; i lastUpdateTime) {
rrdDb.setInfo("T=" + t);
Sample sample = rrdDb.createSample();
sample.setTime(t);
HashMap hm = getValues();
System.out.println(hm);
for (int i = 0; i -1) {
scripts = new String[] {
"strComputer = \".\"",
"Set objWMIService = GetObject(\"winmgmts:\" _",
" & \"{impersonationLevel=impersonate}!\\\\\" & strComputer & \"
\\root\\cimv2\
")",
"Const HARD_DISK = 3",
"Set colDisks = objWMIService.ExecQuery _",
" (\"Select * from Win32_LogicalDisk Where DriveType = \" & HARD_DISK)",
"For Each objDisk in colDisks",
" Wscript.Echo objDisk.DeviceID", "Next" };
} else {
scripts = new String[] { "df | grep \"^/\" | awk '{print $1}'" };
}
String[] strs = execute(scripts);
if (strs == null) {
logger.fine("not get disk information");
} else {
logger.fine(strs.length + " disks:");
for (int i = 0; i getWinDSValues() {
HashMap dsValues = new HashMap();
String[] scripts = new String[] {
"strComputer = \".\"",
"Set objWMIService = GetObject(\"winmgmts:\" _",
" & \"{impersonationLevel=impersonate}!\\\\\" & strComputer & \"
\\root\\cimv2\
")",
"set objRefresher = CreateObject(\"WbemScripting.SWbemRefresher\")",
"Set colDisks = objRefresher.AddEnum(objWMIService, \"Win32_PerfFormattedData_PerfDisk_LogicalDisk\").objectSet",
"objRefresher.Refresh",
"Wscript.Sleep 2000",
"objRefresher.Refresh",
"For Each objDisk in colDisks",
" Wscript.Echo objDisk.Name & \" \" & objDisk.DiskReadBytesPerSec & \" \" & objDisk.DiskWriteBytesPerSec",
"Next" };
String[] strs = execute(scripts);
if (strs != null && strs.length > 0) {
for (int i = 0; i getLinuxDSValues() {
HashMap dsValues = new HashMap();
String[] scripts = new String[] { "iostat -d -k 2 2" };
String[] strs = execute(scripts);
int count = 0;
if (strs != null && strs.length > 0) {
for (int i = 0; i
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u2/67646/showart_1792343.html |
|