浏览 3123 次
锁定老帖子 主题:snmp网络监控程序问题
该帖已经被评为隐藏帖
|
|
---|---|
作者 | 正文 |
发表时间:2008-06-13
最近公司有个项目,其中有一块要求如下:
hut
需要系统开 snmp 服务,
写个网关采集信息保存到数据库,web浏览数据库。 snmp采集可用 uk.co.westhawk.snmp 或者 snmp4j
感谢hut给的思路
我找个例子,各位帮我看看: package com.join.snmptext; import org.snmp4j.CommunityTarget; import org.snmp4j.PDU; import org.snmp4j.Snmp; import org.snmp4j.TransportMapping; import org.snmp4j.event.ResponseEvent; import org.snmp4j.mp.SnmpConstants; import org.snmp4j.smi.OID; import org.snmp4j.smi.OctetString; import org.snmp4j.smi.VariableBinding; import org.snmp4j.transport.DefaultUdpTransportMapping; import org.snmp4j.smi.*; import java.awt.*; import javax.swing.*; import java.net.*; public class SendSnmp extends JFrame { private static final long serialVersionUID = 1L; private JPanel jContentPane = null; private JLabel jLabel = null; private JLabel jLabel1 = null; private JLabel jLabel2 = null; private JLabel jLabel3 = null; private JTextField ipAddress = null; private JTextField Port = null; private JTextField Oid=null; private JTextArea responseText=null; private JScrollPane jsp=null; private JComboBox version=null; private JButton Get=null; private JButton GetNext=null; private JButton End=null; private CommunityTarget target=new CommunityTarget(); private static String ip="";//Ŀ��ip private int v;//�汾�� public SendSnmp() { super(); initialize(); } private void initialize() { this.setSize(428, 374); this.setContentPane(getJContentPane()); this.setTitle("Send_Receive"); } private JPanel getJContentPane() { if (jContentPane == null) { jLabel1 = new JLabel(); jLabel1.setBounds(new Rectangle(0, 20, 107, 30)); jLabel1.setText("����IP:"); jLabel = new JLabel(); jLabel.setBounds(new Rectangle(200, 20, 107, 30)); jLabel.setText("Port:"); jContentPane = new JPanel(); jContentPane.setLayout(null); jContentPane.add(jLabel, null); jContentPane.add(getIpAddress(), null); jContentPane.add(jLabel1, null); jContentPane.add(getport(), null); jLabel2 = new JLabel(); jLabel2.setBounds(new Rectangle(0, 60, 107, 30)); jLabel2.setText("snmp�汾��:"); jContentPane.add(jLabel2, null); jLabel3 = new JLabel(); jLabel3.setBounds(new Rectangle(0, 150, 107, 30)); jLabel3.setText("����OID:"); jContentPane.add(jLabel3, null); jContentPane.add(getVersion(), null); jContentPane.add(getOid(), null); jContentPane.add(getGetNext(), null); jContentPane.add(getEnd(), null); jContentPane.add(getGet(), null); jContentPane.add(getJScrollPane(),null); } return jContentPane; } private JTextField getIpAddress() { if (ipAddress == null) { ipAddress = new JTextField(); ipAddress.setBounds(new Rectangle(40, 20, 100, 20)); try{ InetAddress address=InetAddress.getLocalHost(); String IP_name=address.getHostAddress(); ipAddress.setText(IP_name); } catch(UnknownHostException e){ } } return ipAddress; } private JTextField getport() { if (Port == null) { Port = new JTextField(); Port.setBounds(new Rectangle(240, 20, 100, 20)); Port.setText("161"); } return Port; } private JTextField getOid() { if (Oid== null) { Oid = new JTextField(); Oid.setBounds(new Rectangle(100, 150, 200, 20)); } return Oid; } private JScrollPane getJScrollPane() { if (jsp == null) { jsp = new JScrollPane(); jsp.setBounds(new Rectangle(100, 200, 250, 100)); jsp.setViewportView(getReply()); } return jsp; } private JTextArea getReply() { if (responseText== null) { responseText= new JTextArea(); responseText.setLineWrap(true); } return responseText; } private JButton getGetNext() { if (GetNext == null) { GetNext = new JButton(); GetNext.setBounds(new Rectangle(0, 230, 80, 20)); GetNext.setText("GetNext"); GetNext.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { Set("GetNext"); }}); } return GetNext; } private JButton getEnd() { if (End == null) { End = new JButton(); End.setBounds(new Rectangle(0, 260, 80, 20)); End.setText("End"); End.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { System.exit(1); } }); } return End; } private JButton getGet() { if (Get == null) { Get = new JButton(); Get.setBounds(new Rectangle(0, 200, 80, 20)); Get.setText("Get"); Get.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { Set("Get"); } }); } return Get; } private JComboBox getVersion() { if (version== null) { String s[]={"SnmpV1","SnmpV2","SnmpV3"}; version = new JComboBox(s); version.setBounds(new Rectangle(100, 60, 107, 20)); version.addItemListener(new java.awt.event.ItemListener() { public void itemStateChanged(java.awt.event.ItemEvent e) { if(version.getSelectedIndex()==0) { v=1; } if(version.getSelectedIndex()==1) { v=2; } if(version.getSelectedIndex()==2) { v=3; } } }); } return version; } public void Set(String type) { try{ Snmp snmp = new Snmp(new DefaultUdpTransportMapping()); target = new CommunityTarget(); target.setCommunity(new OctetString("public")); if(v==1) { target.setVersion(SnmpConstants.version1); } if(v==2) { target.setVersion(SnmpConstants.version2c); } if(v==3) { target.setVersion(SnmpConstants.version3); } ip=ipAddress.getText(); String port=Port.getText(); target.setAddress(new UdpAddress(ip+"/"+port)); target.setRetries(1); target.setTimeout(1000); long l=System.currentTimeMillis(); int iii=0; snmp.listen(); PDU request = new PDU(); String OID=Oid.getText(); if(type.equals("Get")) { request.setType(PDU.GET); request.add(new VariableBinding(new OID(OID))); } if(type.equals("GetNext")) { request.setType(PDU.GETNEXT); request.add(new VariableBinding(new OID(OID))); } //request.setRequestID(new Integer32(-1111)); //request.add(new VariableBinding(new OID(OID),new Integer32(Integer.parseInt("01111000011110000",2))));//����Ӧ�������Ҫ���õ�ֵ System.out.println("���͵�UDP:" + request); PDU response = null; ResponseEvent responseEvent = snmp.send(request, target); iii=1; long ll=System.currentTimeMillis()-l; System.out.println("����ʱ��<=>:"+ll+" "+iii); response = responseEvent.getResponse(); ll=System.currentTimeMillis()-l; if(response!=null){ if(response.getErrorStatus()==response.noError){ String pause=responseEvent.getResponse().getVariableBindings().toString(); /*getvalue��ֵΪ��ӦOID��ֵ*/ String getvalue=pause.substring( pause.indexOf("= ")+2,pause.indexOf(']')); String oid=pause.substring(pause.indexOf("VBS[")+2,pause.indexOf("=")-1); System.out.println(oid+"::"); System.out.println(response); if(type.equals("Get")) { responseText.setText(""); responseText.append(change(getvalue)); } if(type.equals("GetNext")) { Oid.setText(oid); responseText.setText(""); responseText.append(change(getvalue)); } } } iii=0; } catch(Exception e){e.printStackTrace();} } public String change(String ss){ byte[] b=ss.getBytes(); if(b.length==1){ ss=String.valueOf(Integer.toHexString(b[0])); } return ss; } public static void main(String[] args) { SendSnmp ss=new SendSnmp(); ss.setVisible(true); } }
我的开发环境是winxp ,我配置snmp服务,运行上面的程序:
点击getnext按钮 在eclipse控制台显示结果如下:
RESPONSE[requestID=1884906071, errorStatus=Success(0), errorIndex=0, VBS[1.3.6.1.2.1.2.2.1.9.1 = 0:00:00.00]] ?????UDP:GETNEXT[requestID=0, errorStatus=Success(0), errorIndex=0, VBS[1.3.6.1.2.1.2.2.1.9.1 = Null]] ???????<=>:0 1 1.3.6.1.2.1.2.2.1.9.2:: RESPONSE[requestID=2037320774, errorStatus=Success(0), errorIndex=0, VBS[1.3.6.1.2.1.2.2.1.9.2 = 0:00:00.00]] ?????UDP:GETNEXT[requestID=0, errorStatus=Success(0), errorIndex=0, VBS[1.3.6.1.2.1.2.2.1.9.2 = Null]] ???????<=>:0 1 1.3.6.1.2.1.2.2.1.10.1:: RESPONSE[requestID=2119481164, errorStatus=Success(0), errorIndex=0, VBS[1.3.6.1.2.1.2.2.1.10.1 = 160092]] ?????UDP:GETNEXT[requestID=0, errorStatus=Success(0), errorIndex=0, VBS[1.3.6.1.2.1.2.2.1.10.1 = Null]] ???????<=>:0 1 1.3.6.1.2.1.2.2.1.10.2:: RESPONSE[requestID=2024271974, errorStatus=Success(0), errorIndex=0, VBS[1.3.6.1.2.1.2.2.1.10.2 = 8659471]] ?????UDP:GETNEXT[requestID=0, errorStatus=Success(0), errorIndex=0, VBS[1.3.6.1.2.1.2.2.1.10.2 = Null]] ???????<=>:16 1 1.3.6.1.2.1.2.2.1.11.1:: RESPONSE[requestID=802354818, errorStatus=Success(0), errorIndex=0, VBS[1.3.6.1.2.1.2.2.1.11.1 = 5319]] ?????UDP:GETNEXT[requestID=0, errorStatus=Success(0), errorIndex=0, VBS[1.3.6.1.2.1.2.2.1.11.1 = Null]] ???????<=>:0 1 1.3.6.1.2.1.2.2.1.11.2:: RESPONSE[requestID=1494985237, errorStatus=Success(0), errorIndex=0, VBS[1.3.6.1.2.1.2.2.1.11.2 = 8265]] ?????UDP:GETNEXT[requestID=0, errorStatus=Success(0), errorIndex=0, VBS[1.3.6.1.2.1.2.2.1.11.2 = Null]] ???????<=>:0 1 1.3.6.1.2.1.2.2.1.12.1:: RESPONSE[requestID=1804022706, errorStatus=Success(0), errorIndex=0, VBS[1.3.6.1.2.1.2.2.1.12.1 = 0]] ?????UDP:GETNEXT[requestID=0, errorStatus=Success(0), errorIndex=0, VBS[1.3.6.1.2.1.2.2.1.12.1 = Null]] ???????<=>:0 1 1.3.6.1.2.1.2.2.1.12.2:: RESPONSE[requestID=2093407011, errorStatus=Success(0), errorIndex=0, VBS[1.3.6.1.2.1.2.2.1.12.2 = 2983]] ?????UDP:GETNEXT[requestID=0, errorStatus=Success(0), errorIndex=0, VBS[1.3.6.1.2.1.2.2.1.12.2 = Null]] ???????<=>:0 1 1.3.6.1.2.1.2.2.1.13.1:: RESPONSE[requestID=1788293473, errorStatus=Success(0), errorIndex=0, VBS[1.3.6.1.2.1.2.2.1.13.1 = 0]]
如何解析上述的OID(对象标识符)和返回状态值,
如何获取: ApplicationProgram 记录应用程序运行状态
各位帮忙分析一下,我对这不太熟悉,非常谢谢 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |