1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
| package sNMP4JHelper;
import org.snmp4j.CommunityTarget;
import org.snmp4j.PDU;
import org.snmp4j.Snmp;
import org.snmp4j.TransportMapping;
import org.snmp4j.event.ResponseEvent;
import org.snmp4j.event.ResponseListener;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.smi.*;
import org.snmp4j.transport.DefaultUdpTransportMapping;
public class SNMP4JHelper {
public static final String READ_COMMUNITY = "public";
public static final String WRITE_COMMUNITY= "private";
public static final int mSNMPVersion =0; // 0 represents SNMP version=1
public static final String OID_UPS_OUTLET_GROUP1 =
"1.3.6.1.4.1.318.1.1.1.12.3.2.1.3.1";
public static final String OID_SYS_DESCR="1.3.6.1.2.1.1.1.0";
public static void main(String[] args)
{
try
{
String strIPAddress = "127.0.0.1";
SNMP4JHelper objSNMP = new SNMP4JHelper();
//objSNMP.snmpSet();
////////////////////////////////////////////
//Set Value=2 to trun OFF UPS OUTLET Group1
//Value=1 to trun ON UPS OUTLET Group1
//////////////////////////////////////////
int Value = 2;
//objSNMP.snmpSet(strIPAddress, WRITE_COMMUNITY,OID_UPS_OUTLET_GROUP1, Value);
//////////////////////////////////////////////////////////
//Get Basic state of UPS
/////////////////////////////////////////////////////////
String batteryCap =objSNMP.snmpGet(strIPAddress,READ_COMMUNITY,OID_SYS_DESCR);
}
catch (Exception e)
{
e.printStackTrace();
}
}
/*
* The following code valid only SNMP version1. This
* method is very useful to set a parameter on remote device.
*/
public void snmpSet(String strAddress, String community, String strOID, int Value)
{
strAddress= strAddress+"/"+161;
Address targetAddress = GenericAddress.parse(strAddress);
Snmp snmp;
try
{
TransportMapping transport = new DefaultUdpTransportMapping();
snmp = new Snmp(transport);
transport.listen();
CommunityTarget target = new CommunityTarget();
target.setCommunity(new OctetString(community));
target.setAddress(targetAddress);
target.setRetries(2);
target.setTimeout(5000);
target.setVersion(SnmpConstants.version1);
PDU pdu = new PDU();
pdu.add(new VariableBinding(new OID(strOID), new Integer32(Value)));
pdu.setType(PDU.SET);
ResponseListener listener = new ResponseListener() {
public void onResponse(ResponseEvent event) {
// Always cancel async request when response has been received
// otherwise a memory leak is created! Not canceling a request
// immediately can be useful when sending a request to a broadcast
// address.
((Snmp)event.getSource()).cancel(event.getRequest(), this);
System.out.println("Set Status is:"+event.getResponse().getErrorStatusText());
}
};
snmp.send(pdu, target, null, listener);
snmp.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
/*
* The code is valid only SNMP version1. SnmpGet method
* return Response for given OID from the Device.
*/
public String snmpGet(String strAddress, String community, String strOID)
{
String str="";
try
{
OctetString community1 = new OctetString(community);
strAddress= strAddress+"/" + 161;
Address targetaddress = new UdpAddress(strAddress);
TransportMapping transport = new DefaultUdpTransportMapping();
transport.listen();
CommunityTarget comtarget = new CommunityTarget();
comtarget.setCommunity(community1);
comtarget.setVersion(SnmpConstants.version1);
comtarget.setAddress(targetaddress);
comtarget.setRetries(2);
comtarget.setTimeout(5000);
PDU pdu = new PDU();
ResponseEvent response;
Snmp snmp;
pdu.add(new VariableBinding(new OID(strOID)));
pdu.setType(PDU.GET);
snmp = new Snmp(transport);
response = snmp.get(pdu,comtarget);
if(response != null)
{
if(response.getResponse().getErrorStatusText().equalsIgnoreCase("Success"))
{
PDU pduresponse=response.getResponse();
str=pduresponse.getVariableBindings().firstElement().toString();
if(str.contains("="))
{
int len = str.indexOf("=");
str=str.substring(len+1, str.length());
}
}
}
else
{
System.out.println("Feeling like a TimeOut occured ");
}
snmp.close();
} catch(Exception e) { e.printStackTrace(); }
System.out.println("Response="+str);
return str;
}
} |
Partager