基于SNMP和JRobin的监控

13 篇文章 0 订阅


SNMP4J模拟SNMPGET

[java]  view plain copy
  1. public class Snmp_Test_1 {  
  2.       
  3.     private static final Logger logger = Logger.getLogger(Snmp_Test_1.class);    
  4.     private static String protocol = "udp"// 监控时使用的协议    
  5.     private static String port = "161"// 监控时使用的端口    
  6.         public static String snmpGet(String ipAddress, String community, String oid) {  
  7.             String resultStat = null// 监控结果状态  
  8.               
  9.             StringBuffer address = new StringBuffer();  
  10.             address.append(protocol);  
  11.             address.append(":");  
  12.             address.append(ipAddress);  
  13.             address.append("/");  
  14.             address.append(port);  
  15.             //udp:xxx.xxx.xxx.xxx/161  
  16.               
  17.             Address targetAddress = GenericAddress.parse(address.toString());  
  18.             PDU pdu = new PDU();  
  19.             pdu.add(new VariableBinding(new OID(oid)));  
  20.             pdu.setType(PDU.GET);  
  21.             // 创建共同体对象CommunityTarget  
  22.             CommunityTarget target = new CommunityTarget();  
  23.             target.setCommunity(new OctetString(community));  
  24.             target.setAddress(targetAddress);  
  25.             target.setVersion(SnmpConstants.version1);  
  26.             target.setTimeout(2000); // 超时时间  
  27.             target.setRetries(1); // 重复次数  
  28.               
  29.             /**公共的SNMP首部包含:版本(已在共同体中),共同体,PDU*/  
  30.             DefaultUdpTransportMapping udpTransportMap = null;  
  31.             Snmp snmp = null;  
  32.             try {  
  33.                 // 发送同步消息  
  34.                 udpTransportMap = new DefaultUdpTransportMapping();  
  35.                 udpTransportMap.listen(); // 开启监听线程基于socket  
  36.                 snmp = new Snmp(udpTransportMap);  
  37.                 ResponseEvent response = snmp.send(pdu, target);  
  38.                 PDU resposePdu = response.getResponse();  
  39.                 if (resposePdu == null) {  
  40.                     logger.info(ipAddress + ": Request timed out.");  
  41.                 } else {  
  42.                     Object obj = resposePdu.getVariableBindings().firstElement();  
  43.                     VariableBinding variable = (VariableBinding) obj;  
  44.                     resultStat = variable.getVariable().toString();  
  45.                 }  
  46.             } catch (Exception e) {  
  47.                 e.printStackTrace();  
  48.             } finally {  
  49.                 if (snmp != null) {  
  50.                     try {  
  51.                         snmp.close();  
  52.                     } catch (IOException e) {  
  53.                         snmp = null;  
  54.                     }  
  55.                 }  
  56.                 if (udpTransportMap != null) {  
  57.                     try {  
  58.                         udpTransportMap.close();  
  59.                     } catch (IOException e) {  
  60.                         udpTransportMap = null;  
  61.                     }  
  62.                 }  
  63.             }  
  64.               
  65.             return resultStat;  
  66.         }  
  67.       
  68. }  

 

JRobin类:

[java]  view plain copy
  1. public class CPU_Monitor_Test {  
  2.       
  3.     private static final Logger logger = Logger.getLogger(CPU_Monitor_Test.class);  
  4.     private int step = 30;  
  5.     private String rrdPath = "./test.rrd";  
  6.     private int width = 600;  
  7.     private int height = 150;  
  8.     private long begintime = org.jrobin.core.Util.getTimestamp();  
  9.       
  10.     public CPU_Monitor_Test() throws RrdException, IOException {  
  11.           
  12.         logger.info("now into CPU_Monitor_Test");  
  13.         RrdDb rrdDb = null;  
  14.         File file = new File(rrdPath);  
  15.         if(file.exists()){  
  16.             try {  
  17.                 rrdDb = new RrdDb(rrdPath);  
  18.             } catch (Exception e) {  
  19.             }  
  20.         }else{  
  21.             logger.info("the rrd is not exists! creat new rrd file .");  
  22.         }  
  23.           
  24.         if (rrdDb == null) {  
  25.             RrdDef rrdDef = new RrdDef(rrdPath , step);  
  26.             rrdDef.addDatasource("cpu""GAUGE"2 * step, 0, Double.NaN);  
  27.             rrdDef.addArchive("AVERAGE"0.5160);  
  28.             rrdDef.addArchive("AVERAGE"0.5670);  
  29.             rrdDef.addArchive("AVERAGE"0.52479);  
  30.             rrdDef.addArchive("AVERAGE"0.528877);  
  31.             rrdDef.addArchive("MAX"0.5160);  
  32.             rrdDef.addArchive("MAX"0.5670);  
  33.             rrdDef.addArchive("MAX"0.52479);  
  34.             rrdDef.addArchive("MAX"0.528877);  
  35.             rrdDb = new RrdDb(rrdDef); //create  
  36.         }  
  37.         if (rrdDb != null)  
  38.             rrdDb.close();    
  39.           
  40.         logger.info("CPU_Monitor_Test init cuccess!");  
  41.     }  
  42.       
  43.     public CPU_Monitor_Test updateData() throws Exception {  
  44.           
  45.         logger.info("begin updateData");  
  46.         RrdDbPool pool = RrdDbPool.getInstance();  
  47.         RrdDb rrdDb = null;  
  48.         try {  
  49.             rrdDb = pool.requestRrdDb(rrdPath);  
  50.             long lastUpdateTime = rrdDb.getLastUpdateTime();  
  51.             long nowTime = org.jrobin.core.Util.getTimestamp();  
  52.             if (nowTime > lastUpdateTime) {  
  53.             Sample sample = rrdDb.createSample(nowTime);  
  54.             sample.setValue("cpu", Double.valueOf(Snmp_Test_1.snmpGet("127.0.0.1""public",   
  55.                     "1.3.6.1.2.1.25.3.3.1.2.3")));  
  56.             sample.update();  
  57.             logger.info("updateData success");  
  58.             }  
  59.             return this;  
  60.         }   
  61.         finally {  
  62.             pool.release(rrdDb);   
  63.         }  
  64.     }  
  65.       
  66.     public void generateGraph() throws IOException, RrdException {  
  67.           
  68.         RrdGraphDef def = new RrdGraphDef();  
  69.         def.setTimeSpan(begintime,org.jrobin.core.Util.getTimestamp());  
  70.         def.setWidth(width);  
  71.         def.setHeight(height);  
  72.         def.setFilename("./test.png");  
  73.         def.setVerticalLabel("used %");  
  74.         def.setTitle("测试:监控CPU");  
  75.         def.datasource("cpu", rrdPath, "cpu""AVERAGE");  
  76.         def.area("cpu", Color.RED, "cpu traffic");  
  77.           
  78.         def.gprint("cpu""MIN""%3f Min");  
  79.         def.gprint("cpu""AVERAGE",  "%3f Avg");  
  80.         def.gprint("cpu""MAX""%3f Max");  
  81.         def.gprint("cpu""LAST""%3f Last//r");  
  82.         def.print("cpu""MIN""min cpu = %.3f");  
  83.         def.print("cpu""AVERAGE""avg cpu = %.3f");  
  84.         def.print("cpu""MAX""max cpu = %.3f");  
  85.         def.print("cpu""LAST""last cpu = %.3f");  
  86.           
  87.         def.setPoolUsed(false);  
  88.         def.setImageFormat("png");  
  89.           
  90.         def.setSmallFont(new Font("Monospaced", Font.PLAIN, 11));  
  91.         def.setLargeFont(new Font("SansSerif", Font.BOLD, 14));   
  92.         RrdGraph g = new RrdGraph(def);  
  93.         logger.info("generateGraph:"+g.getRrdGraphInfo().getFilename());  
  94.           
  95.     }  
  96.       
  97.     public void sleepAndRun(int num) throws Exception{  
  98.         try {  
  99.             while(--num >0){  
  100.                 TimeUnit.SECONDS.sleep(Long.valueOf(step));  
  101.                 this.updateData();                
  102.             }  
  103.         } catch (InterruptedException e) {  
  104.             e.printStackTrace();  
  105.         }  
  106.         this.generateGraph();   
  107.     }  
  108.       
  109.       
  110.       
  111.       
  112.     public static void main(String[] args) {  
  113.         try {  
  114.             new CPU_Monitor_Test().updateData().sleepAndRun(30);  
  115.         } catch (RrdException e) {  
  116.             e.printStackTrace();  
  117.         } catch (IOException e) {  
  118.             e.printStackTrace();  
  119.         } catch (Exception e) {  
  120.             e.printStackTrace();  
  121.         }  
  122.     }  
  123. }  

结果:

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值