`
writemist
  • 浏览: 7506 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
文章分类
社区版块
存档分类
最新评论

java 获得计算机的cpu使用率,物理内存等信息

 
阅读更多
public interface IMonitorService {
public MonitorInfoBean getMonitorInfoBean() throws Exception;
}
实现类
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Enumeration;
import java.util.StringTokenizer;
//import sun.management.*;
//import com.sun.management.*;
import sun.management.ManagementFactory;
import com.sun.management.OperatingSystemMXBean;
/*采集系统采集数据实现类*/
/**
* @author Administrator
*
*/
public class MonitorServiceImpl implements IMonitorService {
private static final int CPUTIME = 30;
private static final int PERCENT = 100;
private static final int FAULTLENGTH = 10;
private static String osVersion = null;
private String mIpAddress = null;
private String dDateTime = null;
private float totalMemorySize = 0.0f;
private float buffersMemory = 0.0f;
private float cachedMemory = 0.0f;
private float usedMemory = 0.0f;
private float memoryRatio;
/**
  * 获得当前的监控对象.
  *
  * @return 返回构造好的监控对象
  * @throws Exception
*/
public MonitorInfoBean getMonitorInfoBean() throws Exception {
  int kb = 1024;
  CountDate ddate = new CountDate();
  osVersion = System.getProperty("os.version");
  OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory
    .getOperatingSystemMXBean();
  // 操作系统
  String osName = System.getProperty("os.name");
  // 主机IP
  if (osName.toLowerCase().startsWith("windows")) {
   mIpAddress = this.getWindowsIp();
  } else {
   mIpAddress = this.getLinuxIP();
  }
  if (osName.toLowerCase().startsWith("windows")) {
   // 总的物理内存
   float totalPhysicalMemorySize = osmxb.getTotalPhysicalMemorySize()
     / kb;
   float usedPhysicalMemorySize = (osmxb.getTotalPhysicalMemorySize() - osmxb
     .getFreePhysicalMemorySize())
     / kb;
   totalMemorySize = Float.parseFloat(String.format("%.1f",
     totalPhysicalMemorySize));
   // 已使用的物理内存
   usedMemory = Float.parseFloat(String.format("%.1f",
     usedPhysicalMemorySize));
   // windows内存使用率
   memoryRatio = Float.parseFloat(String.format("%.1f",
     (usedMemory / totalMemorySize) * 100));
  } else {
   float[] result = null;
   result = getLinuxMemInfo();
   totalMemorySize = Float
     .parseFloat(String.format("%.1f", result[0]));
   buffersMemory = Float.parseFloat(String.format("%.1f", result[1]));
   cachedMemory = Float.parseFloat(String.format("%.1f", result[2]));
   usedMemory = totalMemorySize - result[3];
   // linux内存使用率
   memoryRatio = Float
     .parseFloat(String
       .format(
         "%.1f",
         ((usedMemory - (cachedMemory + buffersMemory)) / totalMemorySize) * 100));
  }
  // 获得cpu频率
  double cpuRatio = 0;
  if (osName.toLowerCase().startsWith("windows")) {
   cpuRatio = this.getCpuRatioForWindows();
  } else {
   cpuRatio = this.getCpuRateForLinux();
  }
  /* 取得数据时间 */
  dDateTime = ddate.getCurrentYMDHMS();
  // 构造返回对象
  MonitorInfoBean infoBean = new MonitorInfoBean();
  infoBean.setOsName(osName);
  infoBean.setCpuRatio(cpuRatio);
  infoBean.setMIpAddress(mIpAddress);
  infoBean.setDDateTime(dDateTime);
  infoBean.setBuffersMemory(buffersMemory);
  infoBean.setCachedMemory(cachedMemory);
  infoBean.setUsedMemory(usedMemory);
  infoBean.setTotalMemorySize(totalMemorySize);
  infoBean.setMemoryRatio(memoryRatio);
  return infoBean;
}
/**
  * 获得Linux下IP地址.
  *
  * @return 返回Linux下IP地址
*/
public String getLinuxIP() {
  String ip = "";
  try {
   Enumeration<?> e1 = (Enumeration<?>) NetworkInterface
     .getNetworkInterfaces();
   while (e1.hasMoreElements()) {
    NetworkInterface ni = (NetworkInterface) e1.nextElement();
    if (!ni.getName().equals("eth0")) {
     continue;
    } else {
     Enumeration<?> e2 = ni.getInetAddresses();
     while (e2.hasMoreElements()) {
      InetAddress ia = (InetAddress) e2.nextElement();
      if (ia instanceof Inet6Address)
       continue;
      ip = ia.getHostAddress();
     }
     break;
    }
   }
  } catch (SocketException e) {
   e.printStackTrace();
   System.exit(-1);
  }
  return ip;
}
public String getWindowsIp() {
  String ip = "";
  try {
   ip = InetAddress.getLocalHost().getHostAddress();
  } catch (UnknownHostException e) {
   e.printStackTrace();
  }
  return ip;
}
/**
  * 获得Linux下CPU使用率.
  *
  * @return 返回Linux下cpu使用率
  */
private double getCpuRateForLinux() {
  InputStream is = null;
  InputStreamReader isr = null;
  BufferedReader brStat = null;
  StringTokenizer tokenStat = null;
  try {
   Process process = Runtime.getRuntime().exec("top -b -n 1");
   is = process.getInputStream();
   isr = new InputStreamReader(is);
   brStat = new BufferedReader(isr);
   if (osVersion.startsWith("2.4")) {
    brStat.readLine();
    brStat.readLine();
    brStat.readLine();
    brStat.readLine();
    tokenStat = new StringTokenizer(brStat.readLine());
    tokenStat.nextToken();
    tokenStat.nextToken();
    String user = tokenStat.nextToken();
    tokenStat.nextToken();
    String system = tokenStat.nextToken();
    tokenStat.nextToken();
    String nice = tokenStat.nextToken();
    user = user.substring(0, user.indexOf("%"));
    system = system.substring(0, system.indexOf("%"));
    nice = nice.substring(0, nice.indexOf("%"));
    float userUsage = new Float(user).floatValue();
    float systemUsage = new Float(system).floatValue();
    float niceUsage = new Float(nice).floatValue();
    return (userUsage + systemUsage + niceUsage) / 100;
   } else {
    brStat.readLine();
    brStat.readLine();
    tokenStat = new StringTokenizer(brStat.readLine());
    tokenStat.nextToken();
    tokenStat.nextToken();
    tokenStat.nextToken();
    tokenStat.nextToken();
    String cpuUsage = tokenStat.nextToken();
    Float usage = new Float(cpuUsage.substring(0, cpuUsage
      .indexOf("%")));
    return ((1 - usage.floatValue() / 100) * 100);
   }
  } catch (IOException ioe) {
   System.out.println(ioe.getMessage());
   freeResource(is, isr, brStat);
   return 1;
  } finally {
   freeResource(is, isr, brStat);
  }
}
private static void freeResource(InputStream is, InputStreamReader isr,
   BufferedReader br) {
  try {
   if (is != null)
    is.close();
   if (isr != null)
    isr.close();
   if (br != null)
    br.close();
  } catch (IOException ioe) {
   System.out.println(ioe.getMessage());
  }
}
/**
  * 获得windows下CPU使用率.
  *
  * @return 返回windows下cpu使用率
  */
private double getCpuRatioForWindows() {
  try {
   mIpAddress = InetAddress.getLocalHost().getHostAddress();
   String procCmd = System.getenv("windir")
     + " process get Caption,CommandLine,"
     + "KernelModeTime,ReadOperationCount,ThreadCount,UserModeTime,WriteOperationCount";
   // 取进程信息
   long[] c0 = readCpu(Runtime.getRuntime().exec(procCmd));
   Thread.sleep(CPUTIME);
   long[] c1 = readCpu(Runtime.getRuntime().exec(procCmd));
   if (c0 != null && c1 != null) {
    long idletime = c1[0] - c0[0];
    long busytime = c1[1] - c0[1];
    return Double.valueOf(
      PERCENT * (busytime) / (busytime + idletime))
      .doubleValue();
   } else {
    return 0.0;
   }
  } catch (Exception ex) {
   ex.printStackTrace();
   return 0.0;
  }
}
/**
  *
  * 读取CPU信息.
  *
  * @param proc
  * @return
  */
private long[] readCpu(final Process proc) {
  long[] retn = new long[2];
  try {
   proc.getOutputStream().close();
   InputStreamReader ir = new InputStreamReader(proc.getInputStream());
   LineNumberReader input = new LineNumberReader(ir);
   String line = input.readLine();
   if (line == null || line.length() < FAULTLENGTH) {
    return null;
   }
   int capidx = line.indexOf("Caption");
   int cmdidx = line.indexOf("CommandLine");
   int rocidx = line.indexOf("ReadOperationCount");
   int umtidx = line.indexOf("UserModeTime");
   int kmtidx = line.indexOf("KernelModeTime");
   int wocidx = line.indexOf("WriteOperationCount");
   long idletime = 0;
   long kneltime = 0;
   long usertime = 0;
   while ((line = input.readLine()) != null) {
    if (line.length() < wocidx) {
     continue;
    }
    // 字段出现顺序:Caption,CommandLine,KernelModeTime,ReadOperationCount,
    // ThreadCount,UserModeTime,WriteOperation
    String caption = line.substring(capidx, cmdidx - 1)
      .trim();
    String cmd = line.substring( cmdidx, kmtidx - 1).trim();
    if (cmd.indexOf("wmic.exe") >= 0) {
     continue;
    }
    // log.info("line="+line);
    if (caption.equals("System Idle Process")
      || caption.equals("System")) {
     idletime += Long.valueOf(
       line.substring(kmtidx, rocidx - 1).trim())
       .longValue();
     idletime += Long.valueOf(
       line.substring(umtidx, wocidx - 1).trim())
       .longValue();
     continue;
    }
    kneltime += Long.valueOf(
      line.substring(kmtidx, rocidx - 1).trim())
      .longValue();
    usertime += Long.valueOf(
      line.substring(umtidx, wocidx - 1).trim())
      .longValue();
   }
   retn[0] = idletime;
   retn[1] = kneltime + usertime;
   return retn;
  } catch (Exception ex) {
   ex.printStackTrace();
  } finally {
   try {
    proc.getInputStream().close();
   } catch (Exception e) {
    e.printStackTrace();
   }
  }
  return null;
}
public float[] getLinuxMemInfo() {
  File file = new File("/proc/meminfo");
  float result[] = new float[4];
  try {
   BufferedReader br = new BufferedReader(new InputStreamReader(
     new FileInputStream(file)));
   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] = Long.parseLong(token.nextToken());
    }
    if (str.equalsIgnoreCase("Buffers:")) {
     result[1] = Long.parseLong(token.nextToken());
    }
    if (str.equalsIgnoreCase("Cached:")) {
     result[2] = Long.parseLong(token.nextToken());
    }
    if (str.equalsIgnoreCase("MemFree:")) {
     result[3] = Long.parseLong(token.nextToken());
    }
   }
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
  return result;
}
/**
  * 测试方法.
  *
  * @param args
  * @throws Exception
  */
public static void information() {
  MonitorInfoBean monitorInfo = null;
  IMonitorService service = new MonitorServiceImpl();
  try {
   monitorInfo = service.getMonitorInfoBean();
  } catch (Exception e) {
   e.printStackTrace();
  }
  String osName = monitorInfo.getOsName();
  String cpuRatio = String.valueOf(monitorInfo.getCpuRatio());
  String mIpAddress = monitorInfo.getMIpAddress();
  String memoryRatio = String.valueOf(monitorInfo.getMemoryRatio());
  String dDateTime = monitorInfo.getDDateTime();
  System.out.println("操作系统名字:" + osName);
  System.out.println("cpu使用频率:" + cpuRatio);
  System.out.println("内存使用率:" + memoryRatio);
  System.out.println("总的物理内存:" + monitorInfo.getTotalMemorySize());
  System.out.println("主机IP地址:" + mIpAddress);
  System.out.println("获取数据时间:" + dDateTime);
}
public static void main(String[] args) {
  MonitorServiceImpl.information();
}
}
用于日期计算
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/*该类用于数据库日期的运算*/
public class CountDate {
/**
*/
private String currentMonth;
private String nextMonth;
private String currentYear;
private String nextYear;
private String currentYM;
private String currentYMDHMS;
private Date currentYMDHMSs;
private String nextYMDHMS;
/* 根据系统日期获得当月 */
public String getCurrentMonth() {
  Calendar rightNow = Calendar.getInstance();
  if (String.valueOf(rightNow.get(Calendar.MONTH) + 1).length() > 1) {
   this.currentMonth = String
     .valueOf(rightNow.get(Calendar.MONTH) + 1);
   return currentMonth;
  }
  this.currentMonth = "0"
    + String.valueOf(rightNow.get(Calendar.MONTH) + 1);
  return currentMonth;
}
/* 根据系统日期获得当年 */
public String getCurrentYear() {
  Calendar rightNow = Calendar.getInstance();
  this.currentYear = String.valueOf(rightNow.get(Calendar.YEAR));
  return currentYear;
}
/* 根据系统日期获得数据库日期 */
public String getCurrentYMDHMS() {
  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  Timestamp t = new Timestamp(Calendar.getInstance().getTimeInMillis());
  this.currentYMDHMS = sdf.format(t);
  return currentYMDHMS;
}
public Date getCurrentYMDHMSs() {
  Timestamp t = new Timestamp(Calendar.getInstance().getTimeInMillis());
  currentYMDHMSs = t;
  return currentYMDHMSs;
}
/* 根据系统日期获得下个月 */
public String getNextMonth() {
  Calendar rightNow = Calendar.getInstance();
  if ((rightNow.get(Calendar.MONTH) + 1) == 12) {
   this.nextMonth = "01";
   this.nextYear = String.valueOf(rightNow.get(Calendar.YEAR) + 1);
   return nextMonth;
  }
  if (String.valueOf(rightNow.get(Calendar.MONTH) + 2).length() > 1) {
   this.nextMonth = String.valueOf(rightNow.get(Calendar.MONTH) + 2);
   return nextMonth;
  }
  this.nextMonth = "0" + String.valueOf(rightNow.get(Calendar.MONTH) + 2);
  return nextMonth;
}
/* 根据系统日期获得下个月的年分 */
public String getNextYear() {
  Calendar rightNow = Calendar.getInstance();
  if ((rightNow.get(Calendar.MONTH) + 1) == 12) {
   this.nextMonth = "01";
   this.nextYear = String.valueOf(rightNow.get(Calendar.YEAR) + 1);
   return nextYear;
  }
  this.nextYear = String.valueOf(rightNow.get(Calendar.YEAR));
  return nextYear;
}
/* 根据系统日期获得下个月的数据库日期 */
public String getNextYMDHMS() {
  nextYear = getNextYear();
  nextMonth = getNextMonth();
  this.nextYMDHMS = nextYear + "-" + nextMonth + "-" + "01 00:00:00";
  return nextYMDHMS;
}
/* 根据给定的月参数获得当月日期 */
public String getCurrentMonth(int pcurrentMonth) {
  if (pcurrentMonth % 12 > 1 || (pcurrentMonth % 12 == 1)) {
   if (String.valueOf((pcurrentMonth % 12)).length() > 1) {
    this.currentMonth = String.valueOf((pcurrentMonth % 12));
   } else {
    this.currentMonth = "0" + String.valueOf((pcurrentMonth % 12));
    return currentMonth;
   }
  }
  if (String.valueOf(pcurrentMonth).length() > 1) {
   this.currentMonth = String.valueOf(pcurrentMonth);
  } else {
   this.currentMonth = "0" + String.valueOf(pcurrentMonth);
  }
  return currentMonth;
}
/* 根据给定的月参数和年参数获得当年 */
public String getCurrentYear(int pcurrentMonth, int pcurrentYear) {
  if (pcurrentMonth % 12 > 1 || (pcurrentMonth % 12 == 1)) {
   if (String.valueOf((pcurrentMonth % 12)).length() > 1) {
    this.currentMonth = String.valueOf((pcurrentMonth % 12));
   } else {
    this.currentMonth = "0" + String.valueOf((pcurrentMonth % 12));
   }
   this.currentYear = String
     .valueOf(pcurrentMonth / 12 + pcurrentYear);
   return currentYear;
  }
  return this.currentYear = String.valueOf(pcurrentYear);
}
/* 根据给定的月参数和年参数获得数据库日期 */
public String getCurrentYMDHMS(int pcurrentMonth, int pcurrentYear) {
  if ((pcurrentMonth % 12 > 1) || (pcurrentMonth % 12 == 1)) {
   if (String.valueOf((pcurrentMonth % 12)).length() > 1) {
    this.currentMonth = String.valueOf((pcurrentMonth % 12));
   } else {
    this.currentMonth = "0" + String.valueOf((pcurrentMonth % 12));
   }
   this.currentYear = String
     .valueOf(pcurrentMonth / 12 + pcurrentYear);
   this.currentYMDHMS = currentYear + "-" + currentMonth + "-"
     + "01 00:00:00";
   return currentYMDHMS;
  }
  if (String.valueOf(pcurrentMonth).length() > 1) {
   this.currentMonth = String.valueOf(pcurrentMonth);
  } else {
   this.currentMonth = "0" + String.valueOf(pcurrentMonth);
  }
  this.currentYear = String.valueOf(pcurrentYear);
  this.currentYMDHMS = currentYear + "-" + currentMonth + "-"
    + "01 00:00:00";
  return currentYMDHMS;
}
public Date getCurrentYMDHMSs(int pcurrentMonth, int pcurrentYear) {
  if (pcurrentMonth % 12 > 1 || (pcurrentMonth % 12 == 1)) {
   if (String.valueOf((pcurrentMonth % 12)).length() > 1) {
    this.currentMonth = String.valueOf((pcurrentMonth % 12));
   } else {
    this.currentMonth = "0" + String.valueOf((pcurrentMonth % 12));
   }
   this.currentYear = String
     .valueOf(pcurrentMonth / 12 + pcurrentYear);
   this.currentYMDHMSs = Timestamp.valueOf(currentYear + "-"
     + currentMonth + "-" + "01 00:00:00.0");
   return currentYMDHMSs;
  }
  if (String.valueOf(pcurrentMonth).length() > 1) {
   this.currentMonth = String.valueOf(pcurrentMonth);
  } else {
   this.currentMonth = "0" + String.valueOf(pcurrentMonth);
  }
  this.currentYear = String.valueOf(pcurrentYear);
  this.currentYMDHMSs = Timestamp.valueOf(currentYear + "-"
    + currentMonth + "-" + "01 00:00:00.0");
  return currentYMDHMSs;
}
/* 根据给定的月参数和年参数获得下个月的月份和年份 */
public String getNextYMDHMS(int pcurrentMonth, int pcurrentYear) {
  if ((pcurrentMonth % 12 > 1) || (pcurrentMonth % 12 == 1)) {
   if (String.valueOf((pcurrentMonth % 12)).length() > 1) {
    this.currentMonth = String.valueOf((pcurrentMonth % 12));
   } else {
    this.currentMonth = "0" + String.valueOf((pcurrentMonth % 12));
   }
   this.currentYear = String
     .valueOf(pcurrentMonth / 12 + pcurrentYear);
   if (String.valueOf(Integer.parseInt(currentMonth) + 1).length() > 1) {
    this.nextMonth = String
      .valueOf(Integer.parseInt(currentMonth) + 1);
   } else {
    this.nextMonth = "0"
      + String.valueOf(Integer.parseInt(currentMonth) + 1);
   }
   this.nextYear = currentYear;
   this.nextYMDHMS = nextYear + "-" + nextMonth + "-" + "01 00:00:00";
   return nextYMDHMS;
  }
  if (pcurrentMonth == 12) {
   this.nextMonth = "01";
   this.nextYear = String.valueOf(pcurrentYear + 1);
   this.nextYMDHMS = nextYear + "-" + nextMonth + "-" + "01 00:00:00";
   return nextYMDHMS;
  }
  if (String.valueOf(pcurrentMonth + 1).length() > 1) {
   this.nextMonth = String.valueOf(pcurrentMonth + 1);
   this.nextYear = String.valueOf(pcurrentYear);
   this.nextYMDHMS = nextYear + "-" + nextMonth + "-" + "01 00:00:00";
   return nextYMDHMS;
  }
  this.nextMonth = "0" + String.valueOf(pcurrentMonth + 1);
  this.nextYear = String.valueOf(pcurrentYear);
  this.nextYMDHMS = nextYear + "-" + nextMonth + "-" + "01 00:00:00";
  return nextYMDHMS;
}
/* 根据系统日期获得下个月的年份和月份 */
public String getCurrentYM() {
  SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月");
  Timestamp t = new Timestamp(Calendar.getInstance().getTimeInMillis());
  this.currentYM = sdf.format(t);
  return currentYM;
}
/* 根据系统日期获得当月的年份和月份 */
public String getCurrentYM(int pcurrentMonth, int pcurrentYear) {
  if (pcurrentMonth % 12 > 1 || (pcurrentMonth % 12 == 1)) {
   if (String.valueOf(pcurrentMonth % 12).length() > 1) {
    this.currentMonth = String.valueOf((pcurrentMonth % 12));
   } else {
    this.currentMonth = "0" + String.valueOf((pcurrentMonth % 12));
   }
   this.currentYear = String
     .valueOf(pcurrentMonth / 12 + pcurrentYear);
   this.currentYM = currentYear + "年" + currentMonth + "月";
   return currentYM;
  }
  if (String.valueOf(pcurrentMonth).length() > 1) {
   this.currentMonth = String.valueOf(pcurrentMonth);
  } else {
   this.currentMonth = "0" + String.valueOf(pcurrentMonth);
  }
  this.currentYear = String.valueOf(pcurrentYear);
  this.currentYM = currentYear + "年" + currentMonth + "月";
  return currentYM;
}
/*
  * public static void main(String args[]){ CountDate c = new CountDate();
  *
  * System.out.println(c.getCurrentYMDHMSs(12, 2029)); }
  */
}
保存信息的javaBean
import java.sql.Timestamp;
/*采集系统存取数据JavaBean*/
public class MonitorInfoBean implements Comparable<MonitorInfoBean> {
/** 操作系统. */
private String osName;
/** 总的物理内存. */
private float totalMemorySize;
/** 已使用的物理内存. */
private float usedMemory;
/** cpu使用率. */
private double cpuRatio;
/** 主机IP地址 */
private String mIpAddress;
/** 数据存储时间 */
private String dDateTime;
/** 内存使用率 */
private float memoryRatio;
/** linux下Buffers内存 */
private float buffersMemory;
/** linux下Cached内存 */
private float cachedMemory;
public float getBuffersMemory() {
  return buffersMemory;
}
public float getCachedMemory() {
  return cachedMemory;
}
public String getDDateTime() {
  return dDateTime;
}
public void setDDateTime(String dateTime) {
  dDateTime = dateTime;
}
public String getMIpAddress() {
  return mIpAddress;
}
public void setMIpAddress(String ipAddress) {
  mIpAddress = ipAddress;
}
public String getOsName() {
  return osName;
}
public void setOsName(String osName) {
  this.osName = osName;
}
public float getTotalMemorySize() {
  return totalMemorySize;
}
public void setTotalMemorySize(float totalMemorySize) {
  this.totalMemorySize = totalMemorySize;
}
public float getUsedMemory() {
  return usedMemory;
}
public void setUsedMemory(long usedMemory) {
  this.usedMemory = usedMemory;
}
public double getCpuRatio() {
  return cpuRatio;
}
public void setCpuRatio(double cpuRatio) {
  this.cpuRatio = cpuRatio;
}
public int compareTo(MonitorInfoBean m) {
  String stra = this.getDDateTime();
  String strb = m.getDDateTime();
  Timestamp a = Timestamp.valueOf(stra);
  Timestamp b = Timestamp.valueOf(strb);
  if (a.before(b)) {
   return -1;
  } else if (a.after(b)) {
   return 1;
  } else {
   return 0;
  }
}
public float getMemoryRatio() {
  return memoryRatio;
}
public void setMemoryRatio(float memoryRatio) {
  this.memoryRatio = memoryRatio;
}
public void setUsedMemory(float usedMemory) {
  this.usedMemory = usedMemory;
}
public void setBuffersMemory(float buffersMemory) {
  this.buffersMemory = buffersMemory;
}
public void setCachedMemory(float cachedMemory) {
  this.cachedMemory = cachedMemory;
}
}
分享到:
评论

相关推荐

    java在cpu的占有率

    * 获得CPU使用率. * * @return 返回cpu使用率 */ double getCpuRatioForWindows() { try { String procCmd = System.getenv("windir") + "\\system32\\wbem\\wmic.exe process get Caption,...

    java获取系统信息的类文件

    可能通过这几个类实现获取系统内存,CPU使用率,物理内存!

    oshi-dist-4.2.1.zip

    ·进程正常运行时间,CPU,内存使用率 ·已使用/可用的物理和虚拟内存 ·挂载的文件系统(类型,可用空间和总空间) ·磁盘驱动器(型号,序列号,大小)和分区 ·网络接口(IP,带宽输入/输出) ·电池状态...

    服务器性能监测

    本资源是一个完整的java工程,主要是用于服务器监测的;swing界面上输入要监测的服务器的ip,就能获取当前ip的cpu使用率、物理内存使用大小等;

    大一大学计算机基础课程知识点.doc

    硬件系统:是指构成计算机的看得见、摸得着的东西,如元器件、电路板、零部件等 物理实体和物理装置,称为计算机硬件;硬件系统是组成计算机系统的各个物理设备的 总称,是计算机系统的物理基础,又称为裸机。 8....

    GrafanaTest.zip

    利用JSON数据源插件,无论数据...源数据为通过SNMP协议采集的物理机的各项指标,包括CPU利用率、物理内存的利用率、虚拟内存的利用率、磁盘信息等。详见 https://blog.csdn.net/LANSHANZHUYAO/article/details/95888257

    服务器运维管理手册.doc

    进程监控 任务管理器——进程 查看进程的CPU使用率和内存使用率是否超阀值 3. CPU性能 任务管理器——性能 查看CPU最高峰值与一般使用率是否超阀值 4. 内存性能 任务管理器——性能 查看内存最高峰值与一般使用率...

    oshi:本机操作系统和硬件信息

    它不需要安装任何其他本机库,并且旨在提供一种跨平台的实现来检索系统信息,例如OS版本,进程,内存和CPU使用率,磁盘和分区,设备,传感器等。 支持平台 Windows•Linux•macOS•Unix(AIX,FreeBSD,OpenBSD,...

    jsr80 java 访问 usb

    因为它的低成本、高数据传输率、使用容易和灵活性,USB 在计算机行业里获得了广泛接受。今天,许多周边设备和装置都是通过 USB 接口连接到计算机上的。目前,大多数一般用途的操作系统都提供了对 USB 设备的支持,...

    JMeter操作手册大全.docx

    当物理内存接近崩溃时,将物理内存中最近一段时间最少频率使用到的页框移出物理内存,放进该存储空间,这段存储空间我们称之为交换空间(Swap) 1.7.磁盘吞吐量 Disk Throughput. 磁盘吞吐量是指在无磁盘故障的情况...

    学生成绩信息管理系统论文 JSP 完整版

    数据库技术是信息系统的核心和基础,它的出现极大地促进了计算机应用向各行各业的渗透数据库的建设规模、数据库信息量的大小和使用频度已成为衡量一个国家信息化程度的重要标志。 MySQL作为一种开放源码数据库,以其...

    Toad 使用快速入门

    直观的查看各个表空间的利用率、剩余空间、破碎情况等信息 iii. 可以进行各种alter操作:online, offline, 增加数据文件,改变数据文件大小,改变物理存储属性等 对其他数据库对象也有完备的操作支持。 2. SQL...

    Redis面试题50道(含答案)_.pdf

    45、Redis 是单线程的,如何提高多核 CPU 的利用率? 46、一个 Redis 实例最多能存放多少的 keys?List、Set、 Sorted Set 他们最多能存放多少元素? 47、Redis 常见性能问题和解决方案? 48、Redis 提供了哪几种...

    ORACLE9i_优化设计与系统调整

    §3.4.5 CPU个数(CPU_COUNT) 72 §3.4.6 数据缓冲区块数(DB_BLOCK_BUFFERS) 72 §3.4.7 数据块大小(DB_BLOCK_SIZE) 73 §3.4.8 读数据块数(DB_FILE_MULTIBLOCK_READ_COUNT) 73 §3.4.9 数据文件的数目(DB_...

    病房:服务器仪表板

    如果您希望看到美观的仪表板,而不是查看大量数字和图形,则它仅显示主要信息并且可以使用。 Ward在所有流行的操作系统上都可以正常运行,因为它使用了 。 测试所有功能: Windows Linux 预览图片 产品特点 ...

Global site tag (gtag.js) - Google Analytics