ssh2+jrobin生成图片

好好的自我总结:

首先的通过hibernate操作获取数据生成RRd文件

  
  
  
  
  1. public RrdGraphDef createImageByRrd(String taskName,String osid,String nodeip,Integer time,String dayTime){      
  2.         String osName = System.getProperty("os.name"); 
  3.         String baseDir=System.getProperty("java.io.tmpdir");     
  4.         if(osName.indexOf("Windows")>-1){ 
  5.             baseDir="D:\\tomcat\\temp\\pps_monitor"
  6.         }else
  7.             baseDir="/usr/local/tomcat/temp/pps_monitor"
  8.         }        
  9.         String rrdPath=baseDir+File.separator+taskName+File.separator+taskName+"_"+osid+".rrd"
  10.         String pngName=baseDir+File.separator+taskName+File.separator+osid+".png"
  11.         //默认一天 
  12.         long startTime=0
  13.         long endTime=0
  14.         if(null != dayTime && !dayTime.equals("")){ 
  15.             String sd = dayTime+" 00:00:00"
  16.             String ed = dayTime+" 23:59:59"
  17.              
  18.             startTime = DateUtils.getDateToTimeStamp(DateUtils.toUtilDateFromString(sd, DateUtils.FORMAT1)); 
  19.             endTime = DateUtils.getDateToTimeStamp(DateUtils.toUtilDateFromString(ed, DateUtils.FORMAT1)); 
  20.         }else
  21.             if(time == 1){ 
  22.                 startTime = Util.getTime()-86400
  23.             }else if(time == 2){ 
  24.                 startTime = Util.getTime()-86400*6
  25.             }else if(time == 3){ 
  26.                 startTime = Util.getTime()-86400*30
  27.             }else if(time == 4){ 
  28.                 startTime = Util.getTime()-86400*360
  29.             } 
  30.             endTime = Util.getTime(); 
  31.         } 
  32.         IJrobinTool tools=new JrobinTools(); 
  33.         try { 
  34.             File file=new File(rrdPath); 
  35.             if(file.exists()){   
  36.                 JrobinGraphParam param=new JrobinGraphParam(); 
  37.                 param.setRrdPath(rrdPath); 
  38.                 param.setStartTime(startTime); 
  39.                 param.setEndTime(endTime); 
  40.                 param.setPngPath(pngName); 
  41.                 param.setGraphTitle(nodeip+" - "+taskName); 
  42.                 param.setY(taskName); 
  43.                 RrdGraphDef graphDef=tools.graphingObj(param); 
  44.                 SNUM = SNUM + 1
  45.                 return graphDef; 
  46.             } 
  47.             FNUM = FNUM + 1
  48.         } catch (Exception e) { 
  49.             e.printStackTrace(); 
  50.         } 
  51.         return null
  52.     } 

JrobinTools代码:

  
  
  
  
  1. package com.pps.maya.service; 
  2.  
  3. import java.awt.Color; 
  4. import java.awt.Font; 
  5. import java.io.File; 
  6. import java.io.IOException; 
  7. import java.sql.Connection; 
  8. import java.sql.PreparedStatement; 
  9. import java.sql.ResultSet; 
  10. import java.sql.SQLException; 
  11. import java.util.ArrayList; 
  12. import java.util.Date; 
  13. import java.util.List; 
  14. import java.util.Random; 
  15. import java.util.Timer; 
  16. import java.util.TimerTask; 
  17.  
  18. import org.jrobin.core.ConsolFuns; 
  19. import org.jrobin.core.DsDef; 
  20. import org.jrobin.core.DsTypes; 
  21. import org.jrobin.core.FetchData; 
  22. import org.jrobin.core.FetchRequest; 
  23. import org.jrobin.core.RrdDb; 
  24. import org.jrobin.core.RrdDef; 
  25. import org.jrobin.core.RrdException; 
  26. import org.jrobin.core.Sample; 
  27. import org.jrobin.core.Util; 
  28. import org.jrobin.graph.RrdGraph; 
  29. import org.jrobin.graph.RrdGraphDef; 
  30.  
  31. import com.pps.maya.util.JdbcTemplete; 
  32. import com.pps.tools.util.DateUtils; 
  33.  

  34. public class JrobinTools implements IJrobinTool {      
  35.  
  36.     // 默认间隔频率 
  37.     final static int step = 60
  38.      
  39.     // 默认从多个数据中取综合值的方式 
  40.     private static final String DEFAULT_CONSOL_FUN = ConsolFuns.CF_AVERAGE; 
  41.      
  42.     //默认数据库名 
  43.     private static final String DEFAULT_Data_NAME="input"
  44.      
  45.      
  46.     public JrobinTools() { 
  47.         super(); 
  48.     } 
  49.  
  50.     @Override 
  51.     public RrdDef createRrdDef(String rootPath, String rrdName, long beginTime) { 
  52.         RrdDef rrdDef = null
  53.         try { 
  54.             File file = new File(rootPath); 
  55.             if (!file.exists()) 
  56.                 file.mkdirs(); 
  57.  
  58.             String rrdPath = rootPath +File.separator+ rrdName + ".rrd"
  59.             rrdDef = new RrdDef(rrdPath,step); 

  60.             rrdDef.setStartTime(beginTime-step); 
  61.              
  62.             DsDef dsDef = new DsDef(DEFAULT_Data_NAME, DsTypes.DT_GAUGE,step*10,Double.NaN,Double.NaN); 
  63.             rrdDef.addDatasource(dsDef); 
  64.  
  65.             rrdDef.addArchive(DEFAULT_CONSOL_FUN, 0.51,86400/step);        // 输入给数据源的数据每一个都保存下来 
  66.             rrdDef.addArchive(DEFAULT_CONSOL_FUN,0.5,7,480);                //每7笔数据,取平均值,然后保存 
  67.             rrdDef.addArchive(DEFAULT_CONSOL_FUN,0.5,30,480);              //一月 
  68.             rrdDef.addArchive(DEFAULT_CONSOL_FUN,0.5,360,480);            //一年 
  69.  
  70.             return rrdDef; 
  71.         } catch (Exception e) { 
  72.             e.printStackTrace(); 
  73.             return null
  74.         } 
  75.     } 
  76.  
  77.  
  78.  
  79.     public void insertRrdData(String rrdPath,Double value,long collectTimeUnitsSecond) { 
  80.         RrdDb rrdDb = null
  81.         try { 
  82.             rrdDb = new RrdDb(rrdPath);  
  83.             Sample sample = rrdDb.createSample(); 
  84.             System.out.println(rrdDb.getLastUpdateTime()); 
  85.             sample.setTime(collectTimeUnitsSecond); 
  86.             if (value == null
  87.                 value = Double.NaN; 
  88.             sample.setValue(DEFAULT_Data_NAME, value); 
  89.             sample.update(); 
  90.         } catch (RrdException e) { 
  91.             e.printStackTrace(); 
  92.         } catch (IOException e) { 
  93.             e.printStackTrace(); 
  94.         } finally { 
  95.             if (rrdDb != null
  96.                 try { 
  97.                     rrdDb.close(); 
  98.                 } catch (IOException e) { 
  99.                     e.printStackTrace(); 
  100.                 } 
  101.         } 
  102.     } 
  103.  
  104.     public void insertRrdData(RrdDef rrdDef, Double value,long collectTimeUnitsSecond) { 
  105.         RrdDb rrdDb = null
  106.         try { 
  107.             rrdDb = new RrdDb(rrdDef);       
  108.             Sample sample = rrdDb.createSample(); 
  109.             sample.setTime(collectTimeUnitsSecond); 
  110.             if (value == null
  111.                 value = Double.NaN; 
  112.             sample.setValue(DEFAULT_Data_NAME, value); 
  113.             sample.update(); 
  114.         } catch (RrdException e) { 
  115.             e.printStackTrace(); 
  116.         } catch (IOException e) { 
  117.             e.printStackTrace(); 
  118.         } finally { 
  119.             if (rrdDb != null
  120.                 try { 
  121.                     rrdDb.close(); 
  122.                 } catch (IOException e) { 
  123.                     e.printStackTrace(); 
  124.                 } 
  125.         } 
  126.     }         
  127.      
  128.     public RrdGraphDef graphingObj(JrobinGraphParam params){ 
  129.         RrdGraphDef gDef=null
  130.         try { 
  131.             //图形定义 
  132.              gDef = new RrdGraphDef(); 
  133.             gDef.setFilename(params.getPngPath()); 
  134.             gDef.setWidth(500); 
  135.             gDef.setHeight(100); 
  136.             gDef.setImageFormat("png"); 
  137.             //指定数据的时间跨度 
  138.             gDef.setTimeSpan(params.getStartTime(),params.getEndTime());         
  139.               
  140.             // 不显示JRobin的签名   
  141.             gDef.setShowSignature(false); 
  142.          
  143.             //设置最少值 
  144.             gDef.setMinValue(0); 
  145.              
  146.             //设置Y轴 
  147.             gDef.setVerticalLabel(params.getY()); 
  148.              
  149.             //中文字体 
  150.           gDef.setSmallFont(new Font("Monospaced", Font.PLAIN, 11)); 
  151.           gDef.setLargeFont(new Font("SansSerif", Font.BOLD, 14)); 
  152.            
  153.             //标题 
  154.             gDef.setTitle(params.getGraphTitle()); 
  155.              
  156.             //注释     
  157.      
  158.             gDef.comment("From "+params.getStartDate()+" To "+params.getEndDate()+"\\c"); 
  159.              
  160.             gDef.datasource("input", params.getRrdPath(), "input",DEFAULT_CONSOL_FUN); 
  161.             gDef.area("input", Color.green, params.getY()); 
  162.             //聚合 
  163.             gDef.gprint("input""LAST""Current: %5.11f"); 
  164.             gDef.gprint("input""MIN""Min: %5.1lf"); 
  165.             gDef.gprint("input""AVERAGE"" Average: %5.1lf"); 
  166.             gDef.gprint("input""MAX"" Max: %5.1lf");         
  167.               
  168.             return gDef; 
  169.              
  170.         } catch (Exception e) { 
  171.             e.printStackTrace(); 
  172.         }  
  173.         return null
  174.     } 
  175.      
  176.     public void fetchRrdData(String filePath, long startTime, 
  177.             long endTime) { 
  178.         try { 
  179.             // open the file 
  180.             RrdDb rrd = new RrdDb(filePath); 
  181.  
  182.             // create fetch request using the database reference 
  183.             FetchRequest request = rrd.createFetchRequest(DEFAULT_CONSOL_FUN, startTime,endTime); 
  184.  
  185.             // execute the request 
  186.             FetchData fetchData = request.fetchData(); 
  187.             int columnCount = fetchData.getColumnCount(); 
  188.             int rowCount = fetchData.getRowCount(); 
  189.             long[] timestamps = fetchData.getTimestamps(); 
  190.             System.out.println("[data column count:]" + columnCount); 
  191.             System.out.println("[data row count:]" + rowCount); 
  192.  
  193.             // System.out.println("[fetch data dump:]" + fetchData.dump()); 
  194.             // 循环获取数据 
  195.             double[][] values = fetchData.getValues(); 
  196.             StringBuffer buffer = new StringBuffer(""); 
  197.             for (int row = 0; row < rowCount; row++) { 
  198.                 buffer.append(timestamps[row]); 
  199.                 buffer.append(" " 
  200.                         + Util.getDate(timestamps[row]).toLocaleString()); 
  201.                 buffer.append(":  "); 
  202.                 for (int dsIndex = 0; dsIndex < columnCount; dsIndex++) { 
  203.                     buffer.append((values[dsIndex][row])); 
  204.                     buffer.append("  "); 
  205.                 } 
  206.                 buffer.append("\n"); 
  207.             } 
  208.             System.out.println("[fetch data display :]\n" + buffer); 
  209.         } catch (Exception e) { 
  210.             e.printStackTrace(); 
  211.         } 
  212.  
  213.     }     
  214.  

action 生成图片方法:

  
  
  
  
  1. /** 
  2.      * 生成图片流 
  3.      */ 
  4.     public void creatImageByTask() { 
  5.         try { 
  6.             this.setNum(num); 
  7.             RrdGraphDef graphDef = webPGService.createImageByRrd(taskName,osid,nodeip,time,dayTime); 
  8.             RrdGraph graph = new RrdGraph(graphDef); 
  9.          
  10.             BufferedImage image = new BufferedImage(graph.getRrdGraphInfo().getWidth(), graph.getRrdGraphInfo().getHeight(),BufferedImage.TYPE_INT_RGB); 
  11.             graph.render(image.getGraphics()); 
  12.             ServletActionContext.getResponse().setContentType("image/png"); 
  13.             ServletOutputStream out = ServletActionContext.getResponse().getOutputStream(); 
  14.             ImageIO.write(image, "png",out); 
  15.             out.close(); 
  16.         } catch (Exception e) { 
  17.             e.printStackTrace(); 
  18.         } 
  19.     } 

页面显示图片方法:

  
  
  
  
  1. <img src="action名称!creatImageByTask?参数" /> 

 

你可能感兴趣的:(JavaScript,java,jsp,JRobin)