SNMP
Since Camel 2.1
Both producer and consumer are supported
The SNMP component gives you the ability to poll SNMP capable devices or receiving traps
Maven users will need to add the following dependency to their pom.xml for this component:
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-snmp</artifactId>
<version>x.x.x</version>
<!-- use the same version as your Camel core version -->
</dependency> URI format
snmp://hostname[:port][?Options]
The component supports polling OID values from an SNMP enabled device and receiving traps.
Snmp Producer
It can also be used to request information using GET method: the producer with type=POLL, or without a type, sends a GET request of the oids of the endpoint. With type=GET_NEXT the producer walks the oids (a GET_NEXT request is for the producer only, the consumer supports the types TRAP and POLL). With type=TRAP the producer sends the org.snmp4j.PDU of the message body as a trap.
The response body type is org.apache.camel.component.snmp.SnmpMessage (a list of them for GET_NEXT), and it converts to String as the XML text shown in "The result of a poll" below.
Without a port in the endpoint, the default port is 162 for type=TRAP and 161 otherwise.
Configuring Options
Camel components are configured on two separate levels:
-
component level
-
endpoint level
Configuring Component Options
At the component level, you set general and shared configurations that are, then, inherited by the endpoints. It is the highest configuration level.
For example, a component may have security settings, credentials for authentication, urls for network connection and so forth.
Some components only have a few options, and others may have many. Because components typically have pre-configured defaults that are commonly used, then you may often only need to configure a few options on a component; or none at all.
You can configure components using:
-
the Component DSL.
-
in a configuration file (
application.properties,*.yamlfiles, etc). -
directly in the Java code.
Configuring Endpoint Options
You usually spend more time setting up endpoints because they have many options. These options help you customize what you want the endpoint to do. The options are also categorized into whether the endpoint is used as a consumer (from), as a producer (to), or both.
Configuring endpoints is most often done directly in the endpoint URI as path and query parameters. You can also use the Endpoint DSL and DataFormat DSL as a type safe way of configuring endpoints and data formats in Java.
A good practice when configuring options is to use Property Placeholders.
Property placeholders provide a few benefits:
-
They help prevent using hardcoded urls, port numbers, sensitive information, and other settings.
-
They allow externalizing the configuration from the code.
-
They help the code to become more flexible and reusable.
The following two sections list all the options, firstly for the component followed by the endpoint.
Component Options
The SNMP component supports the following options which are listed below.
| Name | Description | Default | Type |
|---|---|---|---|
Allows for bridging the consumer to the Camel routing Error Handler, which mean any exceptions (if possible) occurred while the Camel consumer is trying to pickup incoming messages, or the likes, will now be processed as a message and handled by the routing Error Handler. Important: This is only possible if the 3rd party component allows Camel to be alerted if an exception was thrown. Some components handle this internally only, and therefore bridgeErrorHandler is not possible. In other situations we may improve the Camel component to hook into the 3rd party component and make this possible for future releases. By default the consumer will use the org.apache.camel.spi.ExceptionHandler to deal with exceptions, that will be logged at WARN or ERROR level and ignored. | false | boolean | |
Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing. | false | boolean | |
Whether autowiring is enabled. This is used for automatic autowiring options (the option must be marked as autowired) by looking up in the registry to find if there is a single instance of matching type, which then gets configured on the component. This can be used for automatic configuring JDBC data sources, JMS connection factories, AWS Clients, etc. | true | boolean | |
Used for enabling or disabling all consumer based health checks from this component. | true | boolean | |
Used for enabling or disabling all producer based health checks from this component. Notice: Camel has by default disabled all producer based health-checks. You can turn on producer checks globally by setting camel.health.producersEnabled=true. | true | boolean |
Endpoint Options
The SNMP endpoint is configured using URI syntax:
snmp:host:port
With the following path and query parameters:
Path Parameters
| Name | Description | Default | Type |
|---|---|---|---|
Required Hostname or IP address of the SNMP agent that the producer and the POLL consumer send to, or the local address that the TRAP consumer listens on. | String | ||
Required Port of the SNMP agent, or the local port that the TRAP consumer listens on. Without a port, the endpoint uses 162 for the type TRAP and 161 otherwise. | Integer |
Query Parameters
| Name | Description | Default | Type |
|---|---|---|---|
The OIDs to request, as one OID or a comma-separated list in dotted notation, for example 1.3.6.1.2.1.1.3.0,1.3.6.1.2.1.1.5.0. The type POLL requests their values with a GET request and the type GET_NEXT walks the subtree of each. Not used by traps. | OIDList | ||
The transport of SNMP4J that the endpoint uses: udp or tcp. Enum values:
| udp | String | |
How many times SNMP4J sends a request again when no response arrives within the timeout, so a request is sent at most retries + 1 times. Must be 0 or more. Used by the requests of the producer and the POLL consumer, not by traps. | 2 | int | |
The community of the SNMPv1 and SNMPv2c requests and traps that the producer and the POLL consumer send. Not used with snmpVersion 3, and not by the TRAP consumer, which sets the community of a received trap in the securityName header. | public | String | |
The context engine ID of the scoped PDU of the SNMPv3 requests of the producer and the POLL consumer, as text whose characters are used as the octets of the ID (not hexadecimal). Used only with snmpVersion 3. When not set, SNMP4J uses the engine ID of the agent. | String | ||
The context name of the scoped PDU of the SNMPv3 requests of the producer and the POLL consumer. Used only with snmpVersion 3; not set by default (the default context). | String | ||
The SNMP version: 0 means SNMPv1, 1 means SNMPv2c and 3 means SNMPv3 (the version values of SnmpConstants in SNMP4J). The TRAP consumer receives only the traps of this version (with 0 or 1 the SNMPv1 and SNMPv2c traps). Enum values:
| 0 | int | |
Milliseconds that SNMP4J waits for the response of a request before it sends the request again (see retries) or gives up, so a request without response takes up to timeout x (retries + 1). Without a response, the producer fails with a TimeoutException and the POLL consumer sends no message. Not used by traps. | 1500 | int | |
Which operation to perform such as poll, trap, etc. The consumer supports TRAP (receive traps) and POLL (poll the oids). The producer supports TRAP (send the PDU of the body as a trap), POLL (a GET of the oids, also done when no type is set) and GET_NEXT (a walk of the oids, for the producer only). Enum values:
| SnmpActionType | ||
Milliseconds before the next poll. | 60000 | long | |
If the polling consumer did not poll any files, you can enable this option to send an empty message (no body) instead. | false | boolean | |
Whether the POLL consumer first walks the subtree of each OID (with the TreeUtils of SNMP4J) and then requests all the OIDs found in one GET request, instead of requesting the OIDs themselves. | false | boolean | |
Allows for bridging the consumer to the Camel routing Error Handler, which mean any exceptions (if possible) occurred while the Camel consumer is trying to pickup incoming messages, or the likes, will now be processed as a message and handled by the routing Error Handler. Important: This is only possible if the 3rd party component allows Camel to be alerted if an exception was thrown. Some components handle this internally only, and therefore bridgeErrorHandler is not possible. In other situations we may improve the Camel component to hook into the 3rd party component and make this possible for future releases. By default the consumer will use the org.apache.camel.spi.ExceptionHandler to deal with exceptions, that will be logged at WARN or ERROR level and ignored. | false | boolean | |
To let the consumer use a custom ExceptionHandler. Notice if the option bridgeErrorHandler is enabled then this option is not in use. By default the consumer will deal with exceptions, that will be logged at WARN or ERROR level and ignored. | ExceptionHandler | ||
Sets the exchange pattern when the consumer creates an exchange. Enum values:
| ExchangePattern | ||
A pluggable org.apache.camel.PollingConsumerPollingStrategy allowing you to provide your custom implementation to control error handling usually occurred during the poll operation before an Exchange have been created and being routed in Camel. | PollingConsumerPollStrategy | ||
Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing. | false | boolean | |
The number of subsequent error polls (failed due some error) that should happen before the backoffMultipler should kick-in. | int | ||
The number of subsequent idle polls that should happen before the backoffMultipler should kick-in. | int | ||
To let the scheduled polling consumer backoff if there has been a number of subsequent idles/errors in a row. The multiplier is then the number of polls that will be skipped before the next actual attempt is happening again. When this option is in use then backoffIdleThreshold and/or backoffErrorThreshold must also be configured. | int | ||
If greedy is enabled, then the ScheduledPollConsumer will run immediately again, if the previous run polled 1 or more messages. | false | boolean | |
How long a poll may run before the health check of the consumer reports DOWN. The health check is otherwise based on the polls that are done, so it stays UP while a poll hangs, for example on a read without timeout from a connection that went silent. A poll includes routing the messages it polled, so set this above the longest poll you expect. The poll is not interrupted. The value is in millis (not affected by timeUnit). The default 0 turns this off. | 0 | long | |
Milliseconds before the first poll starts. | 1000 | long | |
Specifies a maximum limit of number of fires. So if you set it to 1, the scheduler will only fire once. If you set it to 5, it will only fire five times. A value of zero or negative means fire forever. | 0 | long | |
The consumer logs a start/complete log line when it polls. This option allows you to configure the logging level for that. Enum values:
| TRACE | LoggingLevel | |
Allows for configuring a custom/shared thread pool to use for the consumer. By default each consumer has its own single threaded thread pool. | ScheduledExecutorService | ||
To use a cron scheduler from either camel-spring or camel-quartz component. Use value spring or quartz for built in scheduler. | none | Object | |
To configure additional properties when using a custom scheduler or any of the Quartz, Spring based scheduler. This is a multi-value option with prefix: scheduler. | Map | ||
Whether the scheduler should be auto started. | true | boolean | |
Time unit for initialDelay and delay options. Enum values:
| MILLISECONDS | TimeUnit | |
Controls if fixed delay or fixed rate is used. See ScheduledExecutorService in JDK for details. | true | boolean | |
The SNMPv3 authentication passphrase of the user, used with the authenticationProtocol. SNMP4J requires at least 8 bytes (RFC 3414 section 11.2) when an authenticationProtocol is set, and otherwise fails with an IllegalArgumentException when the endpoint starts. | String | ||
The SNMPv3 authentication protocol of the user: MD5 (HMAC-MD5-96) or SHA1 (HMAC-SHA-96). Without it, the user has no authentication. SNMP4J does not register these protocols by default: add them to the SecurityProtocols of SNMP4J, for example with SecurityProtocols.getInstance().addAuthenticationProtocol(new AuthSHA()). Enum values:
| String | ||
The SNMPv3 privacy passphrase of the user, used with the privacyProtocol. SNMP4J requires at least 8 bytes (RFC 3414 section 11.2) when a privacyProtocol is set, and otherwise fails with an IllegalArgumentException when the endpoint starts. | String | ||
The SNMPv3 privacy (encryption) protocol of the user: DES, TRIDES (Triple DES), AES128, AES192 or AES256. Without it, the user supports only unencrypted messages. | String | ||
The SNMPv3 security level: 1 (noAuthNoPriv) means no authentication and no privacy, 2 (authNoPriv) authentication without privacy, and 3 (authPriv) authentication and privacy. The producer and the POLL consumer send their requests with this level, and the TRAP consumer receives only the traps with at least this level. Used only with snmpVersion 3. Enum values:
| 3 | int | |
The SNMPv3 security name (the user of the USM) of the requests and traps. Required with snmpVersion 3, where the TRAP consumer receives only the traps of this user. | String |
Message Headers
The SNMP component supports the following message header(s), which is/are listed below:
| Name | Description | Default | Type |
|---|---|---|---|
| Constant: | The security name of a received trap: the community for an SNMPv1 or SNMPv2c trap, the user for an SNMPv3 trap. Set by the trap consumer. | OctetString | |
| Constant: | The transport address, such as the IP address and port, of the sender of a received trap. Set by the trap consumer. | Address |
Message Body
The message body at each side of the SNMP component: Consumer is the body of the messages the consumer creates, Consumer reply the bodies a consumer that answers its caller can reply with, Producer input the bodies the producer can send or write, and Producer output the body after the producer.
| Side | Body | Description |
|---|---|---|
Consumer |
| The variable bindings of the response of a poll or of a trap as XML text: an snmp element with an entry of an oid and a value for each, and the community of a trap in the securityName header. |
Producer input |
| Not used by a POLL or GET_NEXT, which requests the oids. A TRAP sends the body, which must be a PDU of SNMP4J for the snmpVersion: a PDUv1 for 0, the default, a PDU for 1 and a ScopedPDU for 3. Another body fails.
|
Producer output |
| The response of a POLL, or of a producer without type, as an SnmpMessage, a Camel message as the body, whose own body is the XML text of the consumer; it converts to that text. A List of them, one per step of the walk, for GET_NEXT. A TRAP leaves the body unchanged.
|
The bodies are the ones with the default options. A side that is not listed is not known, and tooling does not guess it. The carriers, the sides and the tools that use them are described in Message Body Types.
Usage
The result of a poll
Given the situation, that I poll for the following OIDs:
OIDs
1.3.6.1.2.1.1.3.0 1.3.6.1.2.1.25.3.2.1.5.1 1.3.6.1.2.1.25.3.5.1.1.1 1.3.6.1.2.1.43.5.1.1.11.1
The body is the result as XML text, written on one line without an XML declaration (indented here):
<snmp>
<entry>
<oid>1.3.6.1.2.1.1.3.0</oid>
<value>6 days, 21:14:28.00</value>
</entry>
<entry>
<oid>1.3.6.1.2.1.25.3.2.1.5.1</oid>
<value>2</value>
</entry>
<entry>
<oid>1.3.6.1.2.1.25.3.5.1.1.1</oid>
<value>3</value>
</entry>
<entry>
<oid>1.3.6.1.2.1.43.5.1.1.11.1</oid>
<value>6</value>
</entry>
<entry>
<oid>1.3.6.1.2.1.1.1.0</oid>
<value>My Very Special Printer Of Brand Unknown</value>
</entry>
</snmp> As you maybe recognized, there is one more result than requested: ….1.3.6.1.2.1.1.1.0. The device fills in this one automatically in this special case. So it may absolutely happen that you receive more than you requested. Be prepared.
OID starting with dot representation
.1.3.6.1.4.1.6527.3.1.2.21.2.1.50
As you may notice, default snmpVersion is 0, which means version1 in the endpoint if it is not set explicitly. Make sure you explicitly set snmpVersion which is not default value, in a case of where you are able to query SNMP tables with different versions. Other possible values are 1, which means version2c, and 3, which means version3.
Examples
Polling a remote device:
snmp:192.168.178.23:161?protocol=udp&type=POLL&oids=1.3.6.1.2.1.1.5.0
Setting up a trap receiver (Note that no OID info is needed here!):
snmp:127.0.0.1:162?protocol=udp&type=TRAP
You can get the community of SNMP TRAP with message header securityName (an org.snmp4j.smi.OctetString), peer address of the SNMP TRAP with message header peerAddress (an org.snmp4j.smi.Address).
The trap receiver receives only the traps of its snmpVersion:
-
With
snmpVersion0 (the default) or 1, it receives SNMPv1 and SNMPv2c traps, whatever their community. The route can check the community in thesecurityNameheader. -
With
snmpVersion3, it receives only the SNMPv3 traps of the user set insecurityName, with at least thesecurityLevelof the endpoint (the default is 3, authentication and privacy). The traps are checked and decrypted with theauthenticationProtocol,authenticationPassphrase,privacyProtocolandprivacyPassphraseof the endpoint. These protocols must be registered in theorg.snmp4j.security.SecurityProtocolsof SNMP4J, for example withSecurityProtocols.getInstance().addAuthenticationProtocol(new AuthSHA()).
snmp:0.0.0.0:162?protocol=udp&type=TRAP&snmpVersion=3&securityName=camel&securityLevel=3
&authenticationProtocol=SHA1&authenticationPassphrase=RAW(...)&privacyProtocol=AES128&privacyPassphrase=RAW(...) Routing example in Java: (the body of the consumer is the SNMP PDU as XML text)
-
Java
-
XML
-
YAML
from("snmp:192.168.178.23:161?protocol=udp&type=POLL&oids=1.3.6.1.2.1.1.5.0")
.convertBodyTo(String.class)
.to("activemq:snmp.states"); <route>
<from uri="snmp:192.168.178.23:161?protocol=udp&type=POLL&oids=1.3.6.1.2.1.1.5.0"/>
<convertBodyTo type="String"/>
<to uri="activemq:snmp.states"/>
</route> - route:
from:
uri: snmp:192.168.178.23:161
parameters:
protocol: udp
type: POLL
oids: "1.3.6.1.2.1.1.5.0"
steps:
- convertBodyTo:
type: String
- to:
uri: activemq:snmp.states