`
dtt3401885
  • 浏览: 17879 次
文章分类
社区版块
存档分类
最新评论
阅读更多
get方法
1.3.6.1.2.1.1.1.0//基本信息
1.3.6.1.2.1.1.5.0//主机信息
walk方法(所有oid 都会得到一个数组值)
磁盘与内存
1.3.6.1.2.1.25.2.3.1.4 磁盘簇大小
1.3.6.1.2.1.25.2.3.1.5 磁盘总量
1.3.6.1.2.1.25.2.3.1.6 磁盘已用
1.3.6.1.2.1.25.2.3.1.3 查询磁盘名称
计算方法(循环数组,对应计算):
簇大小* 磁盘总量=总容量
簇大小* 磁盘已用=使用容量
使用容量/总容量=磁盘使用率
总容量-使用容量=可用容量
其中数组最后2个分别为虚拟内存与物理内存
网卡(网卡数据为开启snmp服务开始统计,所以计算方法为 第二次取值减去第一次取值再除以取值间隔时间为这段时间内的平均值)
.1.3.6.1.2.1.2.2.1.2 网卡名称
.1.3.6.1.2.1.2.2.1.10 接收字节
.1.3.6.1.2.1.2.2.1.16 发送字节
.1.3.6.1.2.1.2.2.1.11 接收数据包
.1.3.6.1.2.1.2.2.1.17 发送数据包
cpu负载
1.3.6.1.2.1.25.3.3.1.2 负载
系统进程
.1.3.6.1.2.1.25.4.2.1.2 系统进程
安装列表
.1.3.6.1.2.1.25.6.3.1.2 安装列表









采集方法 用的sping 定时采集 并且为groovy语言
package asiacomproject

import org.snmp4j.PDU;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.VariableBinding;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.TimerTask;
import groovy.sql.Sql;
import com.detao.dtma.utils.IpLocation;

import com.detao.dtma.database.DataBase;
import com.detao.dtma.utils.EmailUtils;
import  com.detao.dtma.utils.Configuration;
import cn.net.asiacom.snmp.SnmpWalk;
import cn.net.asiacom.snmp.SnmpGet;


class GetsnmpService extends TimerTask {

static transactional = false
static news = 0
protected DataBase dataBase;
public void setDataBase(DataBase dataBase) {
this.dataBase = dataBase;
}
def serviceMethod() {

}
/**
*每天收集SNMP信息
*/
private void sendMain(){

Connection conn = null
PreparedStatement pstmt = null
ResultSet rs = null
try {
conn = dataBase.getConnection()
if (conn != null) {
conn.setAutoCommit(false)

//要对那一台服务器进行采集
String finddevice="select t.id ,t.sn,t.ip1,t.snmpps from DEVICE t where t.snmp='1' and t.snmpps is not null and t.ip1 is not null";

pstmt = conn.prepareStatement(finddevice)

rs = pstmt.executeQuery()

   while(rs.next()){
   long snmpid ;
   Long deviceid=rs.getLong(1)
   String ip=rs.getString(3)
   String pas=rs.getString(4)
   String sn=rs.getString(2)
 
   String findsnmp="select t.id,t.repetition from SNMP t where t.device_id=?"
   pstmt = conn.prepareStatement(findsnmp);
   pstmt.setLong(1, deviceid);
   ResultSet rs1 = pstmt.executeQuery()
   SnmpGet gets=new SnmpGet();
   List<List<String>> jiben=gets.getSnmpget(ip,pas,"1.3.6.1.2.1.1.1.0")//基本信息
   List<List<String>> zhuji=gets.getSnmpget(ip,pas,"1.3.6.1.2.1.1.5.0")//主机信息
   //long aid
    //while(rs.next()){
  // aid = rs.getLong(1);
  // snmpid=aid
   // }
pstmt = conn.prepareStatement("SELECT HIBERNATE_SEQUENCE.NEXTVAL FROM DUAL");
ResultSet rssnmp = pstmt.executeQuery();
rssnmp.next();
long aid = rssnmp.getLong(1);
snmpid=aid
   if(!rs1.next()){
String insertsnmp="INSERT into SNMP (id,version,date_created, last_updated,sysname,sysdescribe,os,device_id,repetition)VALUES( ?, 0, SYSDATE, SYSDATE, ?, ?, ?, ?,0)"  
pstmt = conn.prepareStatement(insertsnmp);
pstmt.setLong(1,aid);
pstmt.setString(2, getstring(zhuji[0]));
pstmt.setString(3, getstring(jiben[1]));
pstmt.setString(4, getstring(zhuji[1]));
pstmt.setLong(5, deviceid);
    int rsu = pstmt.executeUpdate();
   }else if(rs1.getInt(2)==1){
     snmpid=rs1.getLong(1)
String insertsnmp="update  SNMP set sysname=?,sysdescribe=?,os=?,repetition=?"
pstmt = conn.prepareStatement(insertsnmp);
pstmt.setString(1, getstring(zhuji[0]));
pstmt.setString(2, getstring(jiben[1]));
pstmt.setString(3, getstring(zhuji[1]));
pstmt.setInt(4, 0);
int rsu = pstmt.executeUpdate();
   }else{
   snmpid=rs1.getLong(1)
   }

   //硬盘跟内存
    pstmt = conn.prepareStatement("SELECT HIBERNATE_SEQUENCE.NEXTVAL FROM DUAL");
    ResultSet rshd = pstmt.executeQuery();
    rshd.next();
    long hdid_temp = rshd.getLong(1);
SnmpWalk walk=new SnmpWalk();
String cuhdOid="1.3.6.1.2.1.25.2.3.1.4"
List<List<String>> cuhd=walk.getSnmp(ip, pas, cuhdOid);
String allhdOid="1.3.6.1.2.1.25.2.3.1.5"
List<List<String>> allhd=walk.getSnmp(ip, pas, allhdOid);
String usehdOid="1.3.6.1.2.1.25.2.3.1.6"
List<List<String>> usehd=walk.getSnmp(ip, pas, usehdOid);
String namehdOid="1.3.6.1.2.1.25.2.3.1.3"
List<List<String>> hdname=walk.getSnmp(ip, pas, namehdOid);
    String inserthd="INSERT into SNMP_HD (id,version,date_created, last_updated,hdname,hdcu,hdalldate,hdusedate,snmp_id,chinaname)VALUES( ?, 0, SYSDATE, SYSDATE, ?, ?, ?, ?,?,?)"
pstmt = conn.prepareStatement(inserthd);
pstmt.setLong(1, hdid_temp);
pstmt.setString(2, getstring(hdname[0]));
pstmt.setString(3, getstring(cuhd[0]));
pstmt.setString(4, getstring(allhd[0]));
pstmt.setString(5, getstring(usehd[0]));
pstmt.setLong(6, snmpid);
pstmt.setString(7, getstring(hdname[2]));
int rchd = pstmt.executeUpdate();//硬盘跟内存结束
//网卡

pstmt = conn.prepareStatement("SELECT HIBERNATE_SEQUENCE.NEXTVAL FROM DUAL");
ResultSet rsnet = pstmt.executeQuery();
rsnet.next();
long netid_temp = rsnet.getLong(1);
String bitgetOid=".1.3.6.1.2.1.2.2.1.10"
String bitsetOid=".1.3.6.1.2.1.2.2.1.16"
String pagegetOid=".1.3.6.1.2.1.2.2.1.11"
String pagesetOid=".1.3.6.1.2.1.2.2.1.17"
String netnameoid=".1.3.6.1.2.1.2.2.1.2"
List<List<String>> bitget=walk.getSnmp(ip, pas, bitgetOid);
List<List<String>> bitset=walk.getSnmp(ip, pas, bitsetOid);
List<List<String>> pageget=walk.getSnmp(ip, pas, pagegetOid);
List<List<String>> pageset=walk.getSnmp(ip, pas, pagesetOid);
List<List<String>> netname=walk.getSnmp(ip, pas, netnameoid);
String insertnet="INSERT into SNMP_Network (id,version,date_created, last_updated,networkname,networkdateset,networkdateget,networkset,networkget,snmp_id,repetition)VALUES( ?, 0, SYSDATE, SYSDATE, ?, ?, ?, ?,?,?,?)"
pstmt = conn.prepareStatement(insertnet);
pstmt.setLong(1, hdid_temp);
pstmt.setString(2, getstring(netname[2]));
pstmt.setString(3, getstring(bitget[0]));
pstmt.setString(4, getstring(bitset[0]));
pstmt.setString(5, getstring(pageget[0]));
pstmt.setString(6, getstring(pageset[0]));
pstmt.setLong(7, snmpid);
pstmt.setInt(8, news);
int rcnet = pstmt.executeUpdate();//网卡结束
//CPU负载
pstmt = conn.prepareStatement("SELECT HIBERNATE_SEQUENCE.NEXTVAL FROM DUAL");
ResultSet rsLoad = pstmt.executeQuery();
rsLoad.next();
long loadid_temp = rsLoad.getLong(1);
String loadOid="1.3.6.1.2.1.25.3.3.1.2"
List<List<String>> load=walk.getSnmp(ip, pas, loadOid);
String insertload="INSERT into SNMP_LOAD (id,version,date_created, last_updated,loadname,loaddate,snmp_id)VALUES( ?, 0, SYSDATE, SYSDATE, ?,?,?)"
pstmt = conn.prepareStatement(insertload);
pstmt.setLong(1, loadid_temp);
pstmt.setString(2, getstring(load[1]));
pstmt.setString(3, getstring(load[0]));
pstmt.setLong(4, snmpid);
int rcload= pstmt.executeUpdate(); //CPU负载结束
//系统进程
pstmt = conn.prepareStatement("SELECT HIBERNATE_SEQUENCE.NEXTVAL FROM DUAL");
ResultSet rssys = pstmt.executeQuery();
rssys.next();
long sysid_temp = rssys.getLong(1);
String sysOid=".1.3.6.1.2.1.25.4.2.1.2"
List<List<String>> sys=walk.getSnmp(ip, pas, sysOid);
String insertsys="INSERT into SNMP_SYSPRO (id,version,date_created, last_updated,sysproname,sysprodate,snmp_id)VALUES( ?, 0, SYSDATE, SYSDATE, ?,?,?)"
pstmt = conn.prepareStatement(insertsys);
pstmt.setLong(1, sysid_temp);
pstmt.setString(2, getstring(sys[1]));
pstmt.setString(3, getstring(sys[0]));
pstmt.setLong(4, snmpid);
int rcsys= pstmt.executeUpdate(); //系统进程结束
//软件安装列表
pstmt = conn.prepareStatement("SELECT HIBERNATE_SEQUENCE.NEXTVAL FROM DUAL");
ResultSet rsinstall = pstmt.executeQuery();
rsinstall.next();
long installid_temp = rsinstall.getLong(1);
String installOid=".1.3.6.1.2.1.25.6.3.1.2"
List<List<String>> install=walk.getSnmp(ip, pas, installOid);
String insertinstall="INSERT into SNMP_INSTALL (id,version,date_created, last_updated,installname,installdate,snmp_id)VALUES( ?, 0, SYSDATE, SYSDATE, ?,?,?)"
pstmt = conn.prepareStatement(insertinstall);
pstmt.setLong(1, installid_temp);
pstmt.setString(2, getstring(install[1]));
pstmt.setString(3, getstring(install[2]));
pstmt.setLong(4, snmpid);
int rcinstall= pstmt.executeUpdate(); //软件安装列表结束
//IO跟cpu没有存,磁盘跟内存在一个表
//磁盘查询公式
//hrStorageAllocationUnits是簇的大小
//hrStorageSize是有多少簇,两者想乘就是总容量
//hrStorageUsed是已使用多少,跟总容量相除就是占用率

}

   news=1


}
conn.commit()
} catch (Exception e) {
   if (conn != null) {
try {
conn.rollback();
} catch (SQLException e1) {
}
   }
e.printStackTrace()
}finally {
dataBase.close(conn, pstmt, rs);
}
}
private static String getstring(List snmplist){
String str="";
for(int i=0;i<snmplist.size();i++){
str+=snmplist[i].toString().trim()+","
}
return str
}
@Override
public void run() {
sendMain()

}

}

get 方法
package cn.net.asiacom.snmp;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

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.Address;
import org.snmp4j.smi.GenericAddress;
import org.snmp4j.smi.Integer32;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.Variable;
import org.snmp4j.smi.VariableBinding;
import org.snmp4j.transport.DefaultUdpTransportMapping;

public class SnmpGet {

       private static Snmp snmp = null;

       private static Address targetAddress = null;

       public void initComm(String ipStr) throws IOException {
              // 设置Agent方的IP和端口
             /// targetAddress = GenericAddress.parse("udp:"+ipStr+"/161");
             // TransportMapping transport = new DefaultUdpTransportMapping();
             // snmp = new Snmp(transport);
            //  transport.listen();
       }

       public ResponseEvent sendPDU(PDU pdu) throws IOException {
              // 设置 target
              CommunityTarget target = new CommunityTarget();
              target.setCommunity(new OctetString("public"));
              target.setAddress(targetAddress);
              // 通信不成功时的重试次数
              target.setRetries(2);
              // 超时时间
              target.setTimeout(1500);
              target.setVersion(SnmpConstants.version2c);
              //v3
              //target.setSecurityLevel(SecurityLevel.AUTH_PRIV);
              //target.setSecurityName(new OctetString("ray"));
              // 向Agent发送PDU,并返回Response
              return snmp.send(pdu, target);
       }
      
       public void setPDU() throws IOException {
              // set PDU
              PDU pdu = new PDU();
              pdu.add(new VariableBinding(new OID(".1.3.6.1.2.1.2.1.0"), new OctetString("CHENZHAOZHEN")));
              pdu.setType(PDU.SET);  
              //sendPDU(pdu);
              ResponseEvent resEvt = sendPDU(pdu);
              PDU response = resEvt.getResponse();
              System.out.println(resEvt.getResponse().toString());
              if(response.getErrorIndex()==PDU.noError&&response.getErrorStatus()==PDU.noError){//判断返回报文是否正确
                  VariableBinding vb = (VariableBinding) response.getVariableBindings().firstElement();
                  Variable var = vb.getVariable();
                  if(var.equals("CHENZHAOZHEN")){//比较返回值和设置值
                      System.out.println("SET操作成功 !");
                  }else{
                      System.out.println("SET操作失败 !");
                  }
              }else{
                  try {
throw new Exception("错误信息:"+response.getErrorStatusText());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
              }
             
       }
       static  List<List<String>> getSnmpget(final String ip, final String community, final String targetOid) throws IOException {
       ArrayList<List<String>> hashmap = new ArrayList<List<String>> ();
          List<String>  variable=new ArrayList<String> ();
          List<String>  snmpoid=new ArrayList<String> ();
       // 设置Agent方的IP和端口
           targetAddress = GenericAddress.parse("udp:"+ip+"/161");
           TransportMapping transport = new DefaultUdpTransportMapping();
           snmp = new Snmp(transport);
           transport.listen();
           PDU pdu = new PDU();
           pdu.add(new VariableBinding(new OID(targetOid)));
           pdu.setType(PDU.GET);
           // 设置 target
           CommunityTarget target = new CommunityTarget();
           target.setCommunity(new OctetString(community));
           target.setAddress(targetAddress);
           // 通信不成功时的重试次数
           target.setRetries(2);
           // 超时时间
           target.setTimeout(1500);
           target.setVersion(SnmpConstants.version2c);
           //v3
           //target.setSecurityLevel(SecurityLevel.AUTH_PRIV);
           //target.setSecurityName(new OctetString("ray"));
           // 向Agent发送PDU,并返回Response
           ResponseEvent respEvnt=snmp.send(pdu, target);
            if (respEvnt != null && respEvnt.getResponse() != null) {
                Vector<VariableBinding> recVBs = (Vector<VariableBinding>) respEvnt.getResponse().getVariableBindings();
                //System.out.println(recVBs);
                for (int i = 0; i < recVBs.size(); i++) {
                       VariableBinding recVB = recVBs.elementAt(i);
                       variable.add(recVB.getOid().toString());
                       snmpoid.add(recVB.getVariable().toString());
                      // System.out.println(recVB.getOid() + " : " + recVB.getVariable());
                }
         }
    
  
   hashmap.add(variable);
   hashmap.add(snmpoid);
   return  hashmap;
  
  
   }
       public void getPDU(String oidStr) throws IOException {
              // get PDU
              PDU pdu = new PDU();
              pdu.add(new VariableBinding(new OID(oidStr)));
              pdu.setType(PDU.GET);
              //pdu.add(new VariableBinding(new OID(".1.3.6.1.2.1.1.5.0"), new OctetString("CHENZHAOZHEN")));
              //pdu.setType(PDU.SET);
              readResponse(sendPDU(pdu));
       }
      
       public void readResponse(ResponseEvent respEvnt) {
              // 解析Response
              if (respEvnt != null && respEvnt.getResponse() != null) {
                     Vector<VariableBinding> recVBs = (Vector<VariableBinding>) respEvnt.getResponse().getVariableBindings();
                     System.out.println(recVBs);
                     for (int i = 0; i < recVBs.size(); i++) {
                            VariableBinding recVB = recVBs.elementAt(i);
                            System.out.println(recVB.getOid() + " : " + recVB.getVariable());
                     }
              }
       }
      
      
       public static void main(String[] args) {
              try {
                     SnmpGet util = new SnmpGet();
                     util.initComm("192.168.0.190");
                     //util.setPDU();
                     //获取机器名
                     //String oidStr = ".1.3.6.1.2.1.1.5.0";
                     //系统运行时间(SNMP客户端运行的时间,格式如下“时:分:秒.毫秒”)
                     //String oidStr1 = ".1.3.6.1.2.1.1.3.0";
                       String oidStr =  "1.3.6.1.2.1.1.1.0";
                     util.getPDU(oidStr);
              } catch (IOException e) {
                     e.printStackTrace();
              }
       }
}
walk方法
package cn.net.asiacom.snmp;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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.Address;
import org.snmp4j.smi.GenericAddress;
import org.snmp4j.smi.Integer32;
import org.snmp4j.smi.Null;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.Variable;
import org.snmp4j.smi.VariableBinding;
import org.snmp4j.transport.DefaultUdpTransportMapping;

/**
* 演示:WALK的方式获取值
*
* blog http://www.micmiu.com
*
* @author Michael
*/
public class SnmpWalk {

public static final int DEFAULT_VERSION = SnmpConstants.version2c;
public static final String DEFAULT_PROTOCOL = "udp";
public static final int DEFAULT_PORT = 161;
public static final long DEFAULT_TIMEOUT = 3 * 1000L;
public static final int DEFAULT_RETRY = 3;

/**
* 创建对象communityTarget
*
* @param targetAddress
* @param community
* @param version
* @param timeOut
* @param retry
* @return CommunityTarget
*/
public static CommunityTarget createDefault(String ip, String community) {
Address address = GenericAddress.parse(DEFAULT_PROTOCOL + ":" + ip
+ "/" + DEFAULT_PORT);
CommunityTarget target = new CommunityTarget();
target.setCommunity(new OctetString(community));
target.setAddress(address);
target.setVersion(DEFAULT_VERSION);
target.setTimeout(DEFAULT_TIMEOUT); // milliseconds
target.setRetries(DEFAULT_RETRY);
return target;
}

/**
* @param ip
* @param community
* @param oid
*/
public static void snmpWalk(String ip, String community, String targetOid) {

CommunityTarget target = SnmpWalk.createDefault(ip, community);
TransportMapping transport = null;
Snmp snmp = null;
try {
transport = new DefaultUdpTransportMapping();
snmp = new Snmp(transport);
transport.listen();

PDU pdu = new PDU();
OID targetOID = new OID(targetOid);
pdu.add(new VariableBinding(targetOID));

boolean finished = false;
System.out.println("----> demo start <----");
while (!finished) {
VariableBinding vb = null;
ResponseEvent respEvent = snmp.getNext(pdu, target);

PDU response = respEvent.getResponse();

if (null == response) {
System.out.println("responsePDU == null");
finished = true;
break;
} else {
vb = response.get(0);
}
// check finish
finished = checkWalkFinished(targetOID, pdu, vb);
if (!finished) {
//System.out.println("==== walk each vlaue :");
//System.out.println(vb.getOid() + " = "+ vb.getVariable().toString());
if(getChinese(vb.getVariable().toString())!=null){
System.out.println("111111111");
System.out.println(vb.getOid() + " = "+ getChinese(vb.getVariable().toString()));
}else{
System.out.println("2222222222");
System.out.println(vb.getOid() + " = "+ vb.getVariable().toString());
}

//System.out.println(vb.getOid() + " = " + vb.getVariable().toString());
//System.out.println(vb.getOid() + " = " + getChinese(vb.getVariable().toString()));

// Set up the variable binding for the next entry.
pdu.setRequestID(new Integer32(0));
pdu.set(0, vb);
} else {
System.out.println("SNMP walk OID has finished.");
snmp.close();
}
}
System.out.println("----> demo end <----");
} catch (Exception e) {
e.printStackTrace();
System.out.println("SNMP walk Exception: " + e);
} finally {
if (snmp != null) {
try {
snmp.close();
} catch (IOException ex1) {
snmp = null;
}
}
}

}



private static String getChinese(String octetString) {
try{
            String[] temps = octetString.split(":");
            byte[] bs = new byte[temps.length];
            for(int i=0;i<temps.length;i++)
                bs[i] = (byte)Integer.parseInt(temps[i],16);
          
            return new String(bs,"GB2312");
        }catch(Exception e){
            return null;
        }
}

/**
* 1)responsePDU == null<br>
* 2)responsePDU.getErrorStatus() != 0<br>
* 3)responsePDU.get(0).getOid() == null<br>
* 4)responsePDU.get(0).getOid().size() < targetOID.size()<br>
* 5)targetOID.leftMostCompare(targetOID.size(),responsePDU.get(0).getOid())
* !=0<br>
* 6)Null.isExceptionSyntax(responsePDU.get(0).getVariable().getSyntax())<br>
* 7)responsePDU.get(0).getOid().compareTo(targetOID) <= 0<br>
*
* @param resquestPDU
* @param targetOID
* @param responsePDU
* @param vb
* @return
*/
private static boolean checkWalkFinished(OID targetOID, PDU pdu,
VariableBinding vb) {
boolean finished = false;
if (pdu.getErrorStatus() != 0) {
//System.out.println("[true] responsePDU.getErrorStatus() != 0 ");
//System.out.println(pdu.getErrorStatusText());
finished = true;
} else if (vb.getOid() == null) {
//System.out.println("[true] vb.getOid() == null");
finished = true;
} else if (vb.getOid().size() < targetOID.size()) {
//System.out.println("[true] vb.getOid().size() < targetOID.size()");
finished = true;
} else if (targetOID.leftMostCompare(targetOID.size(), vb.getOid()) != 0) {
//System.out.println("[true] targetOID.leftMostCompare() != 0");
finished = true;
} else if (Null.isExceptionSyntax(vb.getVariable().getSyntax())) {
//System.out
// .println("[true] Null.isExceptionSyntax(vb.getVariable().getSyntax())");
finished = true;
} else if (vb.getOid().compareTo(targetOID) <= 0) {
//System.out.println("[true] Variable received is not "
// + "lexicographic successor of requested " + "one:");
//System.out.println(vb.toString() + " <= " + targetOID);
finished = true;
}
return finished;

}

static  List<List<String>> getSnmp(final String ip, final String community, final String targetOid) {

    ArrayList<List<String>> hashmap = new ArrayList<List<String>> ();
    List<String>  variable=new ArrayList<String> ();
    List<String>  chinavariable=new ArrayList<String> ();
    List<String>  snmpoid=new ArrayList<String> ();
CommunityTarget target = SnmpWalk.createDefault(ip, community);

TransportMapping transport = null;
Snmp snmp = null;
try {
transport = new DefaultUdpTransportMapping();
snmp = new Snmp(transport);
transport.listen();

PDU pdu = new PDU();
OID targetOID = new OID(targetOid);
pdu.add(new VariableBinding(targetOID));

boolean finished = false;

while (!finished) {
VariableBinding vb = null;
ResponseEvent respEvent = snmp.getNext(pdu, target);

PDU response = respEvent.getResponse();

if (null == response) {

finished = true;
break;
} else {
vb = response.get(0);
}
// check finish
finished = checkWalkFinished(targetOID, pdu, vb);
if (!finished) {
//System.out.println("==== walk each vlaue :");
    variable.add(vb.getVariable().toString());
snmpoid.add(vb.getOid().toString());
//System.out.println(vb.getOid() + " = "+ vb.getVariable().toString());
if(getChinese(vb.getVariable().toString())!=null){
chinavariable.add(getChinese(vb.getVariable().toString()));
//variable.add(getChinese(vb.getVariable().toString()));
    }else{
    chinavariable.add(vb.getVariable().toString());
    //variable.add(vb.getVariable().toString());
   }

//System.out.println(getSnmp);
//System.out.println(vb.getOid() + " = " + getChinese(vb.getVariable().toString()));

// Set up the variable binding for the next entry.
pdu.setRequestID(new Integer32(0));
pdu.set(0, vb);
} else {

snmp.close();
}
}

} catch (Exception e) {
e.printStackTrace();

} finally {
if (snmp != null) {
try {
snmp.close();
} catch (IOException ex1) {
snmp = null;
}
}
}
//System.out.println(chinavariable);
hashmap.add(variable);
hashmap.add(snmpoid);
hashmap.add(chinavariable);
return  hashmap;


}
/**
*
* @param args
*/
public static void main(String[] args) {
String ip = "192.168.0.109";
//String ip = "127.0.0.1";
String community = "public";
// 1.3.6.1.2.1.2.2.1.2
//查看进行列表
//String targetOid = ".1.3.6.1.2.1.25.4.2.1.2";
//查看软件安装列表
//String targetOid = ".1.3.6.1.2.1.25.6.3.1.2";
//CPU负载
//String targetOid = ". 1.3.6.1.2.1.25.3.3.1.2";
String targetOid="1.3.6.1.2.1.25.2.3.1.3";
SnmpWalk.snmpWalk(ip, community, targetOid);
//List<List<String>> aaa= getSnmp(ip, community, targetOid);
//System.out.println(aaa);
//GetSnmp ass=new GetSnmp();
//ass.snmpinto();
}

}
分享到:
评论

相关推荐

    F5 SNMP OID说明

    F5 SNMP OID 说明 本资源摘要信息涵盖了 F5 BIG-IP LTM V9.2.x SNMP 的详细信息,包括 SNMP 基础、F5 BIG-IP SNMP 系统概述、在 BIG-IP LTM 上配置 SNMP、BIG-IP LTM 的 SNMP MIB 库文件、通过 SNMP 采集 BIG-IP 的...

    windows上snmp自定义oid工具

    在Windows环境中,你可以通过自定义OID(对象标识符)来扩展SNMP服务,以便监控特定的应用程序或系统性能指标。本教程将深入介绍如何在Windows上使用SNMP自定义OID工具,以及与之相关的工具和文件。 首先,OID是...

    Juniper常用SNMP(OID)使用手册_Ver2.doc

    Juniper常用SNMP(OID)使用手册 本文档讲述了Juniper Networks常用SNMP(OID)使用手册,涵盖了ScreenOS、SRX、J-series和M-series、EX-series等多种设备的SNMP OID应用。 一、Juniper ScreenOS(通用) * 资源监控...

    Java实现SNMP获得OId值

    ### Java实现SNMP获取OID值:深入解析与代码示例 #### SNMP协议简介与OID概念 简单网络管理协议(SNMP)是一种应用层协议,用于收集和组织有关网络设备的信息,如路由器、交换机和服务器等,并对其进行管理。SNMP...

    SNMP_WALK获取SNMP协议oid的非常好用的工具

    "SNMP_WALK"是利用SNMP协议进行数据查询的一个功能,它能够遍历设备的MIB(Management Information Base)树,获取指定OID(Object Identifier,对象标识符)的所有子节点信息。 SNMP_WALK工具通常是一个命令行实用...

    SNMP常用OID,zabbix监控交换机

    总结来说,SNMP通过OID提供了一种标准化的方式来监控网络设备,而Zabbix利用这些OID进行网络监控,确保了网络运维的高效性和可靠性。理解并掌握常用OID的含义和用途,对于网络管理员来说是至关重要的,能够帮助他们...

    SNMP-OIDs:硬件描述的SNMP OID列表

    SNMP-OID 各种硬件的描述的SNMP OID列表。 从这些设备获取数据很有用。 使用net-snmp软件包可通过网络使用SNMP。 经测试的硬件: 思科SF200,SF300,SG300 =&gt; Dlink DGS1100-XX =&gt; Dlink DWR921 =&gt; 爱普生M2000,...

    SNMP常用OID合集.xlsx

    此合集包含200余个SNMP常用的OID。可以很方便的查找自己所需的OID,包括:系统参数、网络接口、CPU和负载、内存和磁盘及System Group等。

    hp手册show snmp alarm

    5. **`show snmp oid`**: OID是对象标识符,是SNMP中用来唯一标识网络管理对象的数字字符串。这个命令可以用来查看系统中定义的OID及其对应的管理对象。 6. **`show snmp trap`**: SNMP陷阱是设备主动发送到管理站...

    snmp 常用oid列表

    snmp 常用oid列表

    SNMP一些常用OID

    本文将详细介绍SNMP中的一些常用Object Identifier(OID),这些OID主要用于获取系统参数、网络接口信息以及CPU及负载数据。 1. **系统参数**(1.3.6.1.2.1.1) - **1.3.6.1.2.1.1.1.0**: 这个OID用于获取系统的...

    snmp监控设备时获取设备oid工具

    获取设备的OID通常需要使用SNMP工具。这些工具可以发送SNMP GET请求到目标设备,然后设备会返回对应的OID值。这些值可以用于监控设备状态,比如CPU和内存使用率。对于CPU利用率,可能的OID包括.1.3.6.1.2.1.25.3....

    基于C#编写的通过OID获取SNMP

    本篇将深入探讨如何利用C#编程语言通过OID(对象标识符)获取SNMP数据。 首先,OID是SNMP中的关键元素,它是一个有序的数字串,用于唯一地标识网络中的管理对象。理解OID对于正确查询网络设备的状态至关重要。例如...

    用snmp4j获取本地所有oid

    本篇文章将详细介绍如何在Windows环境下配置SNMP服务,并利用Snmp4j获取本地所有OID(对象标识符)。 首先,我们需要了解SNMP服务的安装与配置。在Windows系统中,SNMP服务通常不默认安装,因此我们需要手动进行...

    通过snmp的OID获取对方主机的内存利用率及CPU的使用率

    总结来说,通过SNMP获取远程主机的内存利用率和CPU使用率涉及SNMP协议的理解、OID的选择、以及使用像SNMP4J这样的库来构建SNMP请求和解析响应。在Java程序中,这通常包括建立SNMP会话、构造PDU、发送GET请求以及解析...

    常用SNMP_OID.txt

    列出了SNMP的MIB中常用的OID,根据这些OID可以很容易找到相关的被管对象。供初学SNMP协议的人使用,非常好!

    4-FortiGate防火墙SNMP状态监控OID值说明-v1.1归类.pdf

    4-FortiGate防火墙SNMP状态监控OID值说明-v1.1归类.pdf

    auto-avaya-weathermap:使用Avaya Switch拓扑SNMP OID自动创建php-Weathermap的Perl脚本

    SNMP(简单网络管理协议)是用于监控和管理网络设备的标准协议,通过SNMP OID(对象标识符),我们可以获取到设备的详细状态和配置信息。`auto-avaya-weathermap`脚本就是利用这一特性,通过SNMP查询交换机,获取...

    snmp查看工具

    下面将详细解释SNMP和OID,以及如何使用名为BPSNMPUtil的工具来查看OID。 SNMP(Simple Network Management Protocol)是一种应用层协议,基于TCP/IP协议族。它的主要任务是收集网络设备的状态信息,并允许对这些...

    snmp_SNMP_oid_snmp查内存oid_使用snmp_

    OID(Object Identifier,对象标识符)是SNMP中的核心概念,用于唯一地标识网络管理中的每一个对象。 在SNMP中,OID就像一种地址,它由一组数字组成,定义了网络资源的层级结构,使得网络管理系统能够定位并访问...

Global site tag (gtag.js) - Google Analytics