[SNMP4J] Cannot retrieve the information from SNMP Host

Chris Kam chriscnkam at gmail.com
Mon Nov 21 02:53:02 CET 2005


Hi all,
 I am very new to SNMP4J and I want to use JAVA to help me getting the disk
space of each servers (over 70) each morning. You know it's hard to get
those information by checking if from the local console one by one. I did
read some reference and wrote a program as below, but I cannot get what i
want. Can canone help me to see what should I change and what's wrong with
it. I have no idea on my program after read all the materials that I can
search on the net. Thanks a lot.
 Chris Kam
 P.S. I can read the source text file sucessfully and get the correct ip
address, just donno what happened with the codes that related to SNMP4J.

import java.io.*;
import java.lang.*;
import java.util.*;
import java.net.*;

import org.snmp4j.*;
import org.snmp4j.smi.*;
import org.snmp4j.transport.*;
import org.snmp4j.security.*;
import org.snmp4j.event.*;
import org.snmp4j.log.*;
import org.snmp4j.mp.*;

public class diskspacechecker
{
//Variable Declaration
public static String community;
public static BufferedReader inputstream;
public static FileReader instream;
public static BufferedWriter outputstream;
public static FileWriter outstream;
public static boolean fileexist = true;
public static String[] source;
public static String[][] inputinfo;
public static String[][] outputinfo;
public static int no_of_servers;

//protected org.snmp4j.snmp msnmp = null;
protected String obj_identifier_oid = ".1.3.6.1.2.1.25.2.3.1.2.";
protected String display_string_oid = ".1.3.6.1.2.1.25.2.3.1.3.";
protected String storage_size_oid = ".1.3.6.1.2.1.25.2.3.1.5.";
protected String storage_used_oid = ".1.3.6.1.2.1.25.2.3.1.6.";

public static void main (String[] args) throws IOException
{
//Ask for the SNMP community string for connection use.
System.out.print("Please enter the community string: ");
community = EasyIn.getString();
System.out.println("Your community string is " + community);

//Open the source file of server list. (serverlist.txt in csv format)
try
{
File serverlist = new File("serverlist.txt");
instream = new FileReader(serverlist);
inputstream = new BufferedReader(instream);

String line = inputstream.readLine();
while(line != null)
{
no_of_servers++;
line = inputstream.readLine();
}
inputstream.close();
instream.close();
}
catch (FileNotFoundException e)
{
System.out.println();
System.out.println("The source file serverlist.txt does not exist, please
check.");
System.out.println();
System.exit(-1);
}

//System.out.println(no_of_servers);

source = new String[no_of_servers];
inputinfo = new String[no_of_servers][2];
outputinfo = new String[no_of_servers][4];

if (no_of_servers>0)
{
File serverlist = new File("serverlist.txt");
instream = new FileReader(serverlist);
inputstream = new BufferedReader(instream);

for(int i = 0; i < no_of_servers; i++)
{
source[i] = inputstream.readLine();
}
inputstream.close();
instream.close();
}
else
{
System.out.println();
System.out.println("The source file serverlist.txt does not contain any
information, please check.");
System.out.println();
System.exit(-1);
}


//Prepare input info for disk space information retrieval.
int comma_pos = 0;

for(int i = 0; i < no_of_servers; i++)
{
for(int j = 0; j < source[i].length(); j++)
{
if (source[i].charAt(j) == ',')
{
comma_pos = j;
break;
}
}
inputinfo[i][0] = source[i].substring(0,comma_pos);
inputinfo[i][1] = source[i].substring(comma_pos+1, source[i].length());
}

CommunityTarget target = new CommunityTarget();

for(int i = 0; i < 1; i++)
{

System.out.println("Looping");


Address targetAddress = GenericAddress.parse("udp:" + inputinfo[i][1] +
"/162");

System.out.println(targetAddress);

TransportMapping transport = new DefaultUdpTransportMapping();
Snmp snmp = new Snmp(transport);
MPv3 mpv3 = (MPv3)snmp.getMessageProcessingModel(MessageProcessingModel.MPv3
);
USM usm = new USM(SecurityProtocols.getInstance(), new OctetString(
mpv3.createLocalEngineID()), 0);
SecurityModels.getInstance().addSecurityModel(usm);
transport.listen();

System.out.println("Before Set Community.");

target.setCommunity(new OctetString(community));
target.setAddress(targetAddress);
target.setRetries(10);
target.setTimeout(10000);
target.setVersion(SnmpConstants.version1);

PDU pdu = new PDU();
pdu.add(new VariableBinding(new OID(".1.3.6.1.2.1.25.2.3.1.6.2")));
pdu.setType(PDU.GET);

ResponseListener listener = new ResponseListener()
{
public void onResponse(ResponseEvent e)
{
((Snmp) e.getSource()).cancel(e.getRequest(), this);
System.out.println("Recieved response PDU is: " + e.getResponse());
}
};

snmp.send(pdu,target,null,listener);
}

}

}



More information about the SNMP4J mailing list