SNMP Server Connection - snmp

I am sending a SNMP trap to the SNMP server. But I have to print the log if the connection to the server is not established.
UdpAddress targetAddress = new UdpAddress("127.0.0.1/1985");
CommunityTarget target = new CommunityTarget();
target.setCommunity(new OctetString("public"));
target.setAddress(targetAddress);
target.setRetries(2);
target.setTimeout(1000);
target.setVersion(SnmpConstants.version1);
Snmp snmp = new Snmp(new DefaultUdpTransportMapping());
snmp.listen();
// prepare the PDU for sending
PDU command = new PDU();
command.setType(PDU.GET);
command.add(new VariableBinding(new
OID("1.3.6.1.4.1.1331.11.5.1.0")));
// now send the PDU
**// I HAVE TO CHECK WETHER CONNECTION IS ESTABLISHED OR NOT WITH SNMP SERVER. AS OF
NOW EVEN IF I DO NOT START SNMP SERVER THAN I AM NOT GETTING ANY EXCEPTION and IF I START
SNMP SERVER THEN MESSAGE HAS BEEN SENT TO SNMP SERVER.....MAY BE IN SEND METHOD..ITS
CONNECTS WITH SNMP SERVER....BUT I TRIED TO FIND OUT BUT COULDNT ABLE TO DO IT.....**
ResponseEvent responseEvent = snmp.send(pdu, target);

UDP is connectionless, so you have no way to know if the packet is received in this case.

as you been told UDP is connectionless, so you have no way to know if the packet is received in this case.
but if you really must to know that the SNMP manager is alive before you send the trap. you can create your own a handshake.
the logic is pretty simple and it goes something like that:
1) create in the MIB new leaf to indicates if the manager is connected and initial it to false.
2) send a trap to the manager.
2.1) if the manager is alive (receive the trap)
2.1.1) the manager send a set-request to the leaf with true.
the agent can read the value from it's on MIB and know if the manager is listening to traps
you can expend and improve the logic but the basic idea is clear I think

Although you want to send SNMP trap/notification in code you are doing the following
PDU command = new PDU();
command.setType(PDU.GET);
The above will result in sending an SNMP get request which should ideally fetch you a response however the port number (source or your client's source address) is where you should be listening. The above code snippet has some basic flaws as a result you are not getting desired results.
Some links that you may want to read up on SNMP4j to send notifications
https://www.jayway.com/2010/05/21/introduction-to-snmp4j/
http://lists.agentpp.org/pipermail/snmp4j/2006-April/001219.html

Related

JMeter TCPSampler - how to handle a custom protocol with a periodic keep alive?

I am relatively new to JMeter however I have been doing Performance testing for almost a decade.
I am working with a proprietary TCP protocol that sends a keep alive periodically - through the existing TCP connection.
I am struggling to understand how I can fork the JMeter 'thread group' to handle a TCP Keep alive received over the same TCP session.
Any ideas?
Thank you brainstrust!
edit: I'm using the TCPsampler and have read the help page. I'll try to provide some more detail shortly about what's happening and how the protocol is written.
edit2: Unfortunately because it's a propriety protocol I cannot reveal the exact nature of the protocol itself but it's largely irrelevant to the problem I'm facing.
Basically, I use the 1st TCP sampler to 'start/authenticate' the session with the server. This is configured the following options:
1. TCPClient classname: LengthPrefixedBinaryTCPClientImpl (my protocol is implemented this standard way)
2. Re-use connection ON.
3. Close connection OFF.
4. Set NoDelay OFF.
5. SO_Linger: nothing
6. Text to send: my hex code for the protocol (this is correct)
I get the response from the first TCP request and then I want to start interacting, however in the session, the server sends a keep alive mid-stream, so occassionally when I send a request, I get an unexpected keep alive response instead (it's an open stream of data).
This is what I would like to solve.
I attempted to use a recursive test fragment, so that on KeepAlive response, it would send the request again however one cannot recurse the test fragments (it throws a Java error on Run attempt).
I hope this gives more context! Thank you for your patience (I'm a newbie SO user!)
Please check the below options if it helps with you sceario:-
If "Re-use connection" is selected, connections are shared between
Samplers in the same thread, provided that the exact same host name
string and port are used. Different hosts/port combinations will use
different connections, as will different threads. If both of "Re-use
connection" and "Close connection" are selected, the socket will be
closed after running the sampler. On the next sampler, another socket
will be created. You may want to close a socket at the end of each
thread loop.
If an error is detected - or "Re-use connection" is not selected - the
socket is closed. Another socket will be reopened on the next sample.
The following properties can be used to control its operation:
tcp.status.prefix text that precedes a status numbertcp.status.suffix
text that follows a status numbertcp.status.properties name of
property file to convert status codes to messagestcp.handler Name of
TCP Handler class (default TCPClientImpl) - only used if not specified
on the GUI
For more details:-https://jmeter.apache.org/usermanual/component_reference.html#TCP_Sampler

How should an snmp client receive responses from multiple snmp agents?

I require to send get requests/ receive responses to several snmp agents from a single client/manager process.
I have implemented client/agent based on below urls:
http://www.jitendrazaa.com/blog/java/snmp/create-snmp-client-in-java-using-snmp4j/ http://www.jitendrazaa.com/blog/java/snmp/creating-snmp-agent-server-in-java-using-snmp4j/
In order to send a request to an agent, the following is carried out for each server:
TransportMapping transport = new DefaultUdpTransportMapping();
snmp = new Snmp(transport);
transport.listen();
...
pdu.setType(PDU.GET);
// communityTarget contains server target address.
ResponseEvent event = snmp.send(pdu, communityTarget, null);
In order to receive responses from the servers, do I require to carry out a separate transport.listen() for each server? or create
a new TransportMapping and Snmp object for each server?
I don't understand how the client process can know which server returned the response? ...since
each time transport.listen() is called, the listening port address (transport.getListenAddress()) is the same.
and 1 extra udp port is listed (netstat).
I am intending to poll each server from a different background thread (in order that the polling interval can be configured).
Thank you
You should use the same transport mapping.
From the example you provided, it seems that you are using the synchronous API.
The response is just from the agent you sent the request too.
In any case, you can get the peer (agent) address from ResponseEvent.getPeerAddress
http://www.snmp4j.org/doc/org/snmp4j/event/ResponseEvent.html#getPeerAddress()

How to assign an SNMPv2c inform request from another manager to the proper agent?

The SNMPv2c specification states that "An InformRequest-PDU is generated and transmitted at the request of an application in a SNMPv2 entity acting in a manager role..." [RFC 1905, section 4.2.7]
If a manager receives an inform request from another manager, I wonder how it assigns the data to the proper agent.
When the inform request is received from an agent - like a trap - the IP address can be used to assign the data to the agent.
This case also seems to make more sense to me. Are there any real world applications where an inform is sent by a manager?
You can not change the IP address in INFORM PDU. This type of SNMP PDU does not have such field. So the receiver always uses src_ip of UDP datagram. The Agent Address is part of TrapV1 PDU only.

Windows UDP sending problems

I have a weird behaviour on windows. I have 2 processes that are talking to each other on UDP protocol.
Scenario: 1 of the proceeses is up and the other is not. The process try to send udp message towards the one that is down. The one that is up gets from OS or soemthing else a signal for the socket to read as it got message from the other process.
How come ?
It sends a signal on the same port, but it not a real message that was sent from the other application. When trying to read it u get an excpetion of number 10054, connection reset.

Receiving datagrams using Udp connection

In order to receive datagrams through an UDP connection I have created an object of type UDPClient.
receivedNotificationSock = new UdpClient();
However once done and on using the receive method:
receivedHostNameBuffer=receivedNotificationSock.Receive(ref receivedNotificationIP);
I am getting an exception saying that I must call the bind method.
But there is no bind method in the UDPClient class.
Could You guys please provide me with the code if possible as to what should be done to overcome this exception.
You need I think to know some more about sockets.
All sockets possess a port number. First, you create a socket - which is almost useless on its own. It just floats there. But then you bind it - you assign it a port number. Now it's useful - now you can send and receive data on it.
Remember, all UDP communications are defined by the quad data set of the IP and port of the source and the IP and port of the destination. A freshly created socket doesn't have an IP address or port; binding gives it an IP address and port.
Unfortunately, I'm not a C# programmer, so I can't properly answer your question. But at least you know why it's important.
Pass the port number into the constructor of your UDP client.
receivedNotificationSock = new UdpClient(21000);
You may need to change firewall settings to allow the bind, though a popup window normally opens when you first run this on your dev machine.
For Socket proramming you need to know the sequence of syscalls you need to do on client side and on the server side.
If you are writting a client :
you open a socket with a socket call.
you then connect to the server port with a connect call
once connect is successful
then you send the request to the server using either a send or sendto or a write
which results in reception of data that you can read using a receive or read
On Server Side
you create a socket
bind it to a port
start listening on the socket for incoming connections from various clients using a listen.
There is a non blocking way of listening for connections as well with a select syscall.
Once the you establish a connection you can essentially read the request and start processing it.
Here's an example in C# that may be useful to you.
http://www.developerfusion.com/article/3918/socket-programming-in-c-part-1/

Resources