使用JfreeChart与sigar,quartz绘制CPU使用率

Category:

/

Date:

在社区中看到有人通过 sigar.jar 包读出了操作系统的硬件信息。 于是产生了强烈的兴趣, 于是就有下面这段代码,利用JfreeChart的 DefaultTableXYDataset 图表做显示。 用quartz做定时刷新数据,sigar 读取系统信息。

Job定时调度

  1. package com.fzb.monitoring.job;
  2. import java.util.Date;
  3. import java.util.HashMap;
  4. import java.util.LinkedList;
  5. import java.util.List;
  6. import java.util.Map;
  7. import org.hyperic.sigar.CpuPerc;
  8. import org.hyperic.sigar.NetInterfaceConfig;
  9. import org.hyperic.sigar.NetInterfaceStat;
  10. import org.hyperic.sigar.Sigar;
  11. import org.hyperic.sigar.SigarException;
  12. import org.jfree.data.xy.DefaultTableXYDataset;
  13. import org.jfree.data.xy.XYDataItem;
  14. import org.jfree.data.xy.XYDataset;
  15. import org.jfree.data.xy.XYSeries;
  16. import org.quartz.Job;
  17. import org.quartz.JobExecutionContext;
  18. import org.quartz.JobExecutionException;
  19. public class CPUJob implements Job {
  20. private static List<Double> l=new LinkedList<Double>();
  21. @Override
  22. public void execute(JobExecutionContext context) throws JobExecutionException {
  23. Sigar sigar=(Sigar) context.getJobDetail().getJobDataMap().get("sigar");
  24. DefaultTableXYDataset dataset= (DefaultTableXYDataset) context.getJobDetail().getJobDataMap().get("dataset");
  25. CpuPerc cpu;
  26. try {
  27. cpu = sigar.getCpuPerc();
  28. XYSeries s1 = (XYSeries) context.getJobDetail().getJobDataMap().get("series");
  29. if(l.size()>30){
  30. l.remove(0);
  31. l.add(cpu.getCombined());
  32. for(int i=0;i<30;i++){
  33. s1.update(i, l.get(i));
  34. }
  35. }
  36. else{
  37. l.add(cpu.getCombined());
  38. s1.add(l.size(), l.get(l.size()-1));
  39. }
  40. //s1.update(key-60, );
  41. System.out.println("CPU用户使用率: " + CpuPerc.format(cpu.getUser()));// 用户使用率
  42. System.out.println("CPU系统使用率: " + CpuPerc.format(cpu.getSys()));// 系统使用率
  43. System.out.println("CPU当前等待率: " + CpuPerc.format(cpu.getWait()));// 当前等待率
  44. System.out.println("CPU当前错误率: " + CpuPerc.format(cpu.getNice()));//
  45. System.out.println("CPU当前空闲率: " + CpuPerc.format(cpu.getIdle()));// 当前空闲率
  46. System.out.println("CPU总的使用率: " + CpuPerc.format(cpu.getCombined()));// 总的使用率
  47. } catch (SigarException e1) {
  48. e1.printStackTrace();
  49. }
  50. /*String ifNames[];
  51. try {
  52. ifNames = sigar.getNetInterfaceList();
  53. for (int i = 0; i < ifNames.length; i++) {
  54. String name = ifNames[i];
  55. NetInterfaceConfig ifconfig = sigar.getNetInterfaceConfig(name);
  56. System.out.println("网络设备名: " + name);// 网络设备名
  57. System.out.println("IP地址: " + ifconfig.getAddress());// IP地址
  58. System.out.println("子网掩码: " + ifconfig.getNetmask());// 子网掩码
  59. if ((ifconfig.getFlags() & 1L) <= 0L) {
  60. System.out.println("!IFF_UP...skipping getNetInterfaceStat");
  61. continue;
  62. }
  63. NetInterfaceStat ifstat = sigar.getNetInterfaceStat(name);
  64. System.out.println(name + "接收的总包裹数:" + ifstat.getRxPackets());// 接收的总包裹数
  65. System.out.println(name + "发送的总包裹数:" + ifstat.getTxPackets());// 发送的总包裹数
  66. System.out.println(name + "接收到的总字节数:" + ifstat.getRxBytes());// 接收到的总字节数
  67. System.out.println(name + "发送的总字节数:" + ifstat.getTxBytes());// 发送的总字节数
  68. System.out.println(name + "接收到的错误包数:" + ifstat.getRxErrors());// 接收到的错误包数
  69. System.out.println(name + "发送数据包时的错误数:" + ifstat.getTxErrors());// 发送数据包时的错误数
  70. System.out.println(name + "接收时丢弃的包数:" + ifstat.getRxDropped());// 接收时丢弃的包数
  71. System.out.println(name + "发送时丢弃的包数:" + ifstat.getTxDropped());// 发送时丢弃的包数
  72. }
  73. }
  74. catch (SigarException e) {
  75. // TODO Auto-generated catch block
  76. e.printStackTrace();
  77. }*/
  78. }
  79. }

quartz和Jfreechart做数据的展示和读取。

  1. package fzb.zcc.my;
  2. import static org.quartz.CronScheduleBuilder.cronSchedule;
  3. import static org.quartz.JobBuilder.newJob;
  4. import static org.quartz.TriggerBuilder.newTrigger;
  5. import java.awt.Font;
  6. import java.util.Date;
  7. import org.hyperic.sigar.Sigar;
  8. import org.jfree.chart.ChartFactory;
  9. import org.jfree.chart.ChartFrame;
  10. import org.jfree.chart.JFreeChart;
  11. import org.jfree.chart.plot.PlotOrientation;
  12. import org.jfree.chart.plot.XYPlot;
  13. import org.jfree.chart.title.LegendTitle;
  14. import org.jfree.chart.title.TextTitle;
  15. import org.jfree.data.xy.DefaultTableXYDataset;
  16. import org.jfree.data.xy.XYDataset;
  17. import org.jfree.data.xy.XYSeries;
  18. import org.quartz.CronTrigger;
  19. import org.quartz.JobDetail;
  20. import org.quartz.ScheduleBuilder;
  21. import org.quartz.Scheduler;
  22. import org.quartz.SchedulerFactory;
  23. import org.quartz.SimpleScheduleBuilder;
  24. import org.quartz.SimpleTrigger;
  25. import org.quartz.Trigger;
  26. import org.quartz.impl.StdSchedulerFactory;
  27. import org.slf4j.Logger;
  28. import org.slf4j.LoggerFactory;
  29. import com.fzb.monitoring.job.CPUJob;
  30. public class FirstCron {
  31. private static DefaultTableXYDataset dataset = new DefaultTableXYDataset();
  32. private static XYSeries series = new XYSeries("CPU使用率", true, false);
  33. public void run() throws Exception{
  34. Logger log=LoggerFactory.getLogger(FirstCron.class);
  35. SchedulerFactory sf = new StdSchedulerFactory();
  36. Scheduler scher=sf.getScheduler();
  37. JobDetail job = newJob(CPUJob.class)
  38. .withIdentity("job1", "group1")
  39. .build();
  40. job.getJobDataMap().put("dataset", dataset);
  41. job.getJobDataMap().put("sigar", new Sigar());
  42. job.getJobDataMap().put("series", series);
  43. dataset.addSeries(series);
  44. JFreeChart chart=ChartFactory.createXYAreaChart("使用JfreeChart与sigar绘制CPU使用率","", "",(XYDataset)job.getJobDataMap().get("dataset") ,PlotOrientation.VERTICAL, true,true,true);
  45. //title(上部分)
  46. TextTitle title=chart.getTitle();
  47. title.setFont(new Font("幼圆", Font.BOLD, 20));
  48. //legend(底部)
  49. LegendTitle legend=chart.getLegend();
  50. legend.setItemFont(new Font("华文彩云", Font.ITALIC, 14));
  51. //plot(中部)
  52. XYPlot plot=chart.getXYPlot();
  53. //plot.set.setLabelFont(new Font("宋体",Font.BOLD,16));
  54. ChartFrame frame=new ChartFrame("JAVA绘制CPU使用率", chart);
  55. frame.pack();
  56. frame.setVisible(true);
  57. Trigger trigger = newTrigger()
  58. .withIdentity("trigger1", "group1")
  59. .withSchedule(SimpleScheduleBuilder.simpleSchedule().repeatSecondlyForever(2))
  60. .build();
  61. scher.scheduleJob(job, trigger);
  62. log.info(scher.getJobGroupNames()+"");
  63. scher.start();
  64. }
  65. public static void main(String[] args) {
  66. try {
  67. new FirstCron().run();
  68. } catch (Exception e) {
  69. e.printStackTrace();
  70. }
  71. }
  72. }

好像好久都没有做过图形化东西了。 这次总算是给我逮到机会了。上图一张

最后更新:2013-10-01 07:57:27.0

—— 原创内容,转载请注明:[ 文章转载自:小春 ' s blog   / ] ——