class Rbeapi::Api::Snmp

The Snmp class provides a class implementation for working with the nodes SNMP configuration entity. This class presents an abstraction of the node's snmp configuration from the running config.

@since eos_version 4.13.7M

Constants

CFG_TO_STATE
DEFAULT_SNMP_CHASSIS_ID
DEFAULT_SNMP_CONTACT
DEFAULT_SNMP_LOCATION
DEFAULT_SNMP_SOURCE_INTERFACE
STATE_TO_CFG

Public Instance Methods

add_community(name, access = 'ro') click to toggle source

add_community adds a new snmp community to the nodes running configuration. This function is a convenience function that passes the message to set_community_access.

@see set_community_access

@param name [String] The name of the snmp community to add to the

nodes running configuration.

@param access [String] Specifies the access level to assign to the

new snmp community. Valid values are 'rw' or 'ro'.

@return [Boolean] Returns true if the command completed successfully.

# File lib/rbeapi/api/snmp.rb, line 350
def add_community(name, access = 'ro')
  set_community_access(name, access)
end
get() click to toggle source

get returns the snmp resource Hash that represents the nodes snmp configuration abstraction from the running config.

@example

{
  location: <string>,
  contact: <string>,
  chassis_id: <string>,
  source_interface: <string>
}

@return[Hash<Symbol, Object>] Returns the snmp resource as a Hash.

# File lib/rbeapi/api/snmp.rb, line 69
def get
  response = {}
  response.merge!(parse_location)
  response.merge!(parse_contact)
  response.merge!(parse_chassis_id)
  response.merge!(parse_source_interface)
  response.merge!(parse_communities)
  response.merge!(parse_notifications)
  response
end
remove_community(name) click to toggle source

remove_community removes the specified community from the nodes running configuration. If the specified name is not configured, this method will still return successfully.

@since eos_version 4.13.7M

Commands

no snmp-server community <name>

@param name [String] The name of the snmp community to add to the

nodes running configuration.

@return [Boolean] Returns true if the command completed successfully.

# File lib/rbeapi/api/snmp.rb, line 368
def remove_community(name)
  configure "no snmp-server community #{name}"
end
set_chassis_id(opts = {}) click to toggle source

set_chassis_id updates the snmp chassis id value in the nodes running configuration. If enable is false in the opts Hash then the snmp chassis id value is negated using the no keyword. If the default keyword is set to true, then the snmp chassis id value is defaulted using the default keyword. The default keyword takes precedence over the enable keyword.

@since eos_version 4.13.7M

Commands

snmp-server chassis-id <value>
no snmp-server chassis-id
default snmp-server chassis-id

@param opts [Hash] The configuration parameters

@option opts value [string] The snmp chassis id value to configure

@option opts enable [Boolean] If false then the command is

negated. Default is true.

@option opts default [Boolean] Configures the snmp chassis id value

using the default keyword.

@return [Boolean] Returns true if the command completed successfully.

# File lib/rbeapi/api/snmp.rb, line 300
def set_chassis_id(opts = {})
  cmd = command_builder('snmp-server chassis-id', opts)
  configure(cmd)
end
set_community_access(name, access) click to toggle source

set_community_access configures snmp-server community with designated

name and access values.

@param name [String] The snmp-server community name value.

@param access [String] The snmp-server community access value.

@return [Boolean] Returns true if the command completed successfully.

# File lib/rbeapi/api/snmp.rb, line 421
def set_community_access(name, access)
  configure "snmp-server community #{name} #{access}"
end
set_community_acl(name, opts = {}) click to toggle source

set_community_acl configures the acl to apply to the specified community name. When enable is true, it will remove the the named community and then add the new acl entry.

@since eos_version 4.13.7M

Commands

no snmp-server <name> [ro|rw] <value>
snmp-server <name> [ro|rw] <value>

@param name [String] The name of the snmp community to add to the

nodes running configuration.

@param opts [Hash] The configuration parameters.

@option opts value [String] The name of the acl to apply to the snmp

community in the nodes config. If nil, then the community name
allows access to all objects.

@option opts enable [Boolean] If false then the command is

negated. Default is true.

@option opts default [Boolean] Configure the snmp community name

using the default keyword. Default takes precedence over enable.

@return [Boolean] Returns true if the command completed successfully.

# File lib/rbeapi/api/snmp.rb, line 399
def set_community_acl(name, opts = {})
  value = opts[:value]
  enable = opts.fetch(:enable, true)
  default = opts.fetch(:default, false)
  # Default is same as negate for this command
  enable = default ? false : enable
  communities = parse_communities[:communities]
  access = communities[name][:access] if communities.include?(name)
  cmds = ["no snmp-server community #{name}"]
  cmds << "snmp-server community #{name} #{access} #{value}" if enable
  configure cmds
end
set_contact(opts = {}) click to toggle source

set_contact updates the snmp contact value in the nodes running configuration. If enable is false in the opts Hash then the snmp contact value is negated using the no keyword. If the default keyword is set to true, then the snmp contact value is defaulted using the default keyword. The default parameter takes precedence over the enable keyword.

@since eos_version 4.13.7M

Commands

snmp-server contact <value>
no snmp-server contact
default snmp-server contact

@param opts [Hash] The configuration parameters.

@option opts value [string] The snmp contact value to configure.

@option opts enable [Boolean] If false then the command is

negated. Default is true.

@option opts default [Boolean] Configures the snmp contact value

using the default keyword.

@return [Boolean] Returns true if the command completed successfully.

# File lib/rbeapi/api/snmp.rb, line 269
def set_contact(opts = {})
  cmd = command_builder('snmp-server contact', opts)
  configure(cmd)
end
set_location(opts = {}) click to toggle source

set_location updates the snmp location value in the nodes running configuration. If enable is false, then the snmp location value is negated using the no keyword. If the default keyword is set to true, then the snmp location value is defaulted using the default keyword. The default parameter takes precedence over the enable keyword.

@since eos_version 4.13.7M

Commands

snmp-server location <value>
no snmp-server location
default snmp-server location

@param opts [Hash] The configuration parameters.

@option opts value [string] The snmp location value to configure.

@option opts enable [Boolean] If false then the command is

negated. Default is true.

@option opts default [Boolean] Configure the snmp location value

using the default keyword.

@return [Boolean] Returns true if the command completed successfully.

# File lib/rbeapi/api/snmp.rb, line 238
def set_location(opts = {})
  cmd = command_builder('snmp-server location', opts)
  configure(cmd)
end
set_notification(opts = {}) click to toggle source

set_notification configures the snmp trap notification for the specified trap. The name option accepts the snmp trap name to configure or the keyword all to globally enable or disable notifications. If the optional state argument is not provided then the default state is default.

@since eos_version 4.13.7M

Commands

snmp-server enable traps <name>
no snmp-server enable traps <name>
default snmp-server enable traps <name>

@param opts [Hash] The configuration parameters.

@option opts name [String] The name of the trap to configure or the

keyword all. If this option is not specified, then the value of
'all' is used as the default.

@option opts state [String] The state to configure the trap

notification. Valid values include 'on', 'off' or 'default'.
# File lib/rbeapi/api/snmp.rb, line 205
def set_notification(opts = {})
  name = opts[:name]
  name = nil if name == 'all'
  state = opts[:state] || 'default'
  state = STATE_TO_CFG[state]
  configure "#{state} snmp-server enable traps #{name}"
end
set_source_interface(opts = {}) click to toggle source

set_source_interface updates the snmp source interface value in the nodes running configuration. If enable is false in the opts Hash then the snmp source interface is negated using the no keyword. If the default keyword is set to true, then the snmp source interface value is defaulted using the default keyword. The default keyword takes precedence over the enable keyword.

@since eos_version 4.13.7M

Commands

snmp-server source-interface <value>
no snmp-server source-interface
default snmp-server source-interface

@param opts [Hash] The configuration parameters.

@option opts value [string] The snmp source interface value to

configure. This method will not ensure the interface is present
in the configuration.

@option opts enable [Boolean] If false then the command is

negated. Default is true.

@option opts default [Boolean] Configures the snmp source interface

value using the default keyword.

@return [Boolean] Returns true if the command completed successfully.

# File lib/rbeapi/api/snmp.rb, line 331
def set_source_interface(opts = {})
  cmd = command_builder('snmp-server source-interface', opts)
  configure(cmd)
end

Private Instance Methods

parse_chassis_id() click to toggle source

parse_chassis_id scans the running config from the node and parses the snmp chassis id value if it exists in the configuration. If the snmp chassis id is not configured, then the DEFAULT_SNMP_CHASSIS_ID value is returned. The Hash returned by this method is intended to be merged into the snmp resource Hash.

@api private

@return [Hash<Symbol,Object>] Returns the resource Hash attribute.

# File lib/rbeapi/api/snmp.rb, line 122
def parse_chassis_id
  mdata = /snmp-server chassis-id (.+)$/.match(config)
  { chassis_id: mdata.nil? ? DEFAULT_SNMP_CHASSIS_ID : mdata[1] }
end
parse_communities() click to toggle source

parse_communities scans the running config from the node and parses all of the configure snmp community strings. If there are no configured snmp community strings, the community value is set to an empty array. The returned hash is intended to be merged into the global snmp resource hash.

@api private

@return [Hash<Hash>] Returns the resource hash attribute.

# File lib/rbeapi/api/snmp.rb, line 155
def parse_communities
  values = config.scan(/snmp-server community (\w+) (ro|rw)[ ]?(.+)?$/)
  communities = values.each_with_object({}) do |value, hsh|
    name, access, acl = value
    hsh[name] = { access: access, acl: acl }
  end
  { communities: communities }
end
parse_contact() click to toggle source

parse_contact scans the running config form the node and parses the snmp contact value if it exists in the configuration. If the snmp contact is not configured, then the DEFAULT_SNMP_CONTACT value is returned. The Hash returned by this method is merged into the snmp resource Hash returned by the get method.

@api private

@return [Hash<Symbol,Object] Returns the resource Hash attribute.

# File lib/rbeapi/api/snmp.rb, line 106
def parse_contact
  mdata = /snmp-server contact (.+)$/.match(config)
  { contact: mdata.nil? ? DEFAULT_SNMP_CONTACT : mdata[1] }
end
parse_location() click to toggle source

parse_location scans the running config from the node and parses the snmp location value if it exists in the configuration. If the snmp location is not configure, then the DEFAULT_SNMP_LOCATION string is returned. The Hash returned by this method is merged into the snmp resource Hash returned by the get method.

@api private

@return [Hash<Symbol,Object>] Returns the resource Hash attribute.

# File lib/rbeapi/api/snmp.rb, line 90
def parse_location
  mdata = /snmp-server location (.+)$/.match(config)
  { location: mdata.nil? ? DEFAULT_SNMP_LOCATION : mdata[1] }
end
parse_notifications() click to toggle source

parse_notifications scans the running configuration and parses all of the snmp trap notifications configuration. It is expected the trap configuration is in the running config. The returned hash is intended to be merged into the resource hash.

# File lib/rbeapi/api/snmp.rb, line 170
def parse_notifications
  traps = config.scan(/(default|no)?[ ]?snmp-server enable traps (.+)$/)
  all = config.scan(/(default|no)?[ ]?snmp-server enable traps$/).first

  notifications = traps.map do |trap|
    state, name = trap
    { name: name, state: CFG_TO_STATE[state] }
  end
  notifications << { name: 'all', state: CFG_TO_STATE[all.first] }
  { notifications: notifications }
end
parse_source_interface() click to toggle source

parse_source_interface scans the running config from the node and parses the snmp source interface value if it exists in the configuration. If the snmp source interface is not configured, then the DEFAULT_SNMP_SOURCE_INTERFACE value is returned. The Hash returned by this method is intended to be merged into the snmmp resource Hash.

@api private

@return [Hash<Symbol, Object>] Returns the resource Hash attribute.

# File lib/rbeapi/api/snmp.rb, line 139
def parse_source_interface
  mdata = /snmp-server (?:vrf .+ )?source-interface (.+)$/.match(config)
  { source_interface: mdata.nil? ? '' : mdata[1] }
end