Skip to content

Commit 4a290ea

Browse files
committed
update readme
1 parent f83ba42 commit 4a290ea

File tree

1 file changed

+116
-3
lines changed

1 file changed

+116
-3
lines changed

README.md

+116-3
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,126 @@
22

33
[![Build Status](https://travis-ci.org/spider-gazelle/crystal-snmp.svg?branch=master)](https://travis-ci.org/spider-gazelle/crystal-snmp)
44

5+
NOTE:: I consider the project ready to use. Usage won't change significantly between now and v1.0.0
56

6-
## Writing to Sockets
7+
## Usage
8+
9+
This library can be used to build either an SNMP Agent or Client application.
10+
The examples below indicate how to use it as a client.
11+
12+
### SNMP v2c
13+
14+
```crystal
15+
# Connect to server
16+
socket = UDPSocket.new
17+
socket.connect("demo.snmplabs.com", 161)
18+
socket.sync = false
19+
20+
# Make request
21+
session = SNMP::Session.new
22+
socket.write_bytes session.get("1.3.6.1.2.1.1.4.0")
23+
socket.flush
24+
25+
# Process response
26+
response = session.parse(socket.read_bytes(ASN1::BER))
27+
response.value.get_string # "SNMP Laboratories, [email protected]"
28+
```
29+
30+
### SNMP v3
31+
32+
```crystal
33+
# Connect to server
34+
socket = UDPSocket.new
35+
socket.connect("demo.snmplabs.com", 161)
36+
socket.sync = false
37+
38+
# Setup session
39+
session = SNMP::V3::Session.new("usr-md5-aes", "authkey1", "privkey1", priv_protocol: SNMP::V3::Security::PrivacyProtocol::AES)
40+
41+
# This is required to get the engine ID, boot and tick times
42+
# You can read about it here: https://www.snmpsharpnet.com/?page_id=28
43+
if session.must_revalidate?
44+
socket.write_bytes session.engine_validation_probe
45+
socket.flush
46+
session.validate socket.read_bytes(ASN1::BER)
47+
end
48+
49+
# Make the request
50+
# NOTE:: with SNMPv3 you need to prepare the message for transmission
51+
unencrypted_message = session.get("1.3.6.1.2.1.1.4.0")
52+
socket.write_bytes session.prepare(unencrypted_message)
53+
socket.flush
54+
55+
# Process response
56+
response = session.parse(socket.read_bytes(ASN1::BER))
57+
response.value.get_string # "SNMP Laboratories, [email protected]"
58+
```
59+
60+
### Setting values
61+
62+
NOTE:: `set` currently supports:
63+
64+
* Strings
65+
* Integers
66+
* Boolean
67+
* Nil
68+
69+
More crystal classes will be added over time (such as `Float` and `Socket::IPAddress` etc)
70+
71+
```crystal
72+
# Setting a string
73+
session.set("1.3.6.1.2.1.1.3.0", "some string value")
74+
75+
# Setting an integer
76+
session.set("1.3.6.1.2.1.1.3.0", 34)
77+
```
78+
79+
For more complex or currently unsupported types you can build a custom ASN1.BER.
80+
81+
```crystal
82+
ber = ASN1::BER.new
83+
ber.tag_class = ASN1::BER::TagClass::Application
84+
ber.tag_number = 12
85+
ber.payload = Bytes[1,2,3,4,5]
86+
87+
session.set("1.3.6.1.2.1.1.3.0", ber)
88+
```
89+
90+
### Extracting response values
91+
92+
The response value is always an `ASN1::BER`
93+
94+
```crystal
95+
response = session.parse(socket.read_bytes(ASN1::BER))
96+
response.value
97+
```
98+
99+
You can extract common data types using helper methods:
100+
101+
* `.get_string`
102+
* `.get_object_id` for SNMP OIDs such as 1.3.6.1.2.1
103+
* `.get_hexstring` for a hex representation of the payload bytes
104+
* `.get_bytes` for the raw byte data
105+
* `.get_boolean`
106+
* `.get_integer` returning an `Int64`
107+
108+
109+
## Notes on IO
110+
111+
### Writing to Sockets
7112

8113
When writing SNMP messages to the socket, be aware that you should be buffering the write.
9114

10115
```crystal
11116
12117
session = SNMP::V3::Session.new
13-
message = session.engine_id_probe
118+
message = session.engine_validation_probe
14119
15-
# buffer the message
120+
# Ensure sync is false so the message is buffered
16121
socket.sync = false
17122
socket.write_bytes message
123+
124+
# This requires you to call `flush`
18125
socket.flush
19126
20127
```
@@ -23,3 +130,9 @@ This is because the call to `to_io` on message involves multiple writes to the I
23130
as the message is progressively constructed. However you don't want each write to
24131
be sending packets as this will result in a lot of overhead and most SNMP servers
25132
will not accept fragmented messages.
133+
134+
135+
### Reading from sockets
136+
137+
Whilst you'll probably be OK reading data like `socket.read_bytes(ASN1::BER)`
138+
you should probably be buffering requests based on SNMP PDU Max Size (defaulting to 65507 bytes) and throwing away any buffered data that can't be read after buffering or a short timeout.

0 commit comments

Comments
 (0)