Class: Rbeapi::Api::Snmp

Inherits:
Entity
  • Object
show all
Defined in:
lib/rbeapi/api/snmp.rb

Overview

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.

Constant Summary collapse

DEFAULT_SNMP_LOCATION =
''
DEFAULT_SNMP_CONTACT =
''
DEFAULT_SNMP_CHASSIS_ID =
''
DEFAULT_SNMP_SOURCE_INTERFACE =
''
CFG_TO_STATE =
{ 'default' => 'default', 'no' => 'off', nil => 'on' }
STATE_TO_CFG =
{ 'default' => 'default', 'on' => nil, 'off' => 'no' }

Instance Attribute Summary

Attributes inherited from Entity

#config, #error, #node

Instance Method Summary collapse

Methods inherited from Entity

#command_builder, #configure, #configure_interface, #get_block, #initialize, instance

Constructor Details

This class inherits a constructor from Rbeapi::Api::Entity

Instance Method Details

#add_community(name, access = 'ro') ⇒ Boolean

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.

Parameters:

  • :name (String)

    The name of the snmp community to add to the nodes running configuration.

  • :access (String)

    Specifies the access level to assign to the new snmp community. Valid values are ‘rw’ or ‘ro’

Returns:

  • (Boolean)

    returns true if the command completed successfully

See Also:



346
347
348
# File 'lib/rbeapi/api/snmp.rb', line 346

def add_community(name, access = 'ro')
  set_community_access(name, access)
end

#getObject

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

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

Examples:

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


67
68
69
70
71
72
73
74
75
76
# File 'lib/rbeapi/api/snmp.rb', line 67

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) ⇒ Boolean

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

Parameters:

  • :name (String)

    The name of the snmp community to add to the nodes running configuration.

Returns:

  • (Boolean)

    returns true if the command completed successfully



364
365
366
# File 'lib/rbeapi/api/snmp.rb', line 364

def remove_community(name)
  configure "no snmp-server community #{name}"
end

#set_chassis_id(opts = {}) ⇒ Boolean

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.

Parameters:

  • opts (Hash) (defaults to: {})

    The configuration parameters

  • :opts (Hash)

    a customizable set of options

Options Hash (opts):

  • :value (string)

    The snmp chassis id value to configure

  • :default (Boolean)

    Configures the snmp chassis id value using the default keyword

Returns:

  • (Boolean)

    returns true if the command completed successfully



296
297
298
299
# File 'lib/rbeapi/api/snmp.rb', line 296

def set_chassis_id(opts = {})
  cmd = command_builder('snmp-server chassis-id', opts)
  configure(cmd)
end

#set_community_access(name, access) ⇒ Boolean

set_community_access configures snmp-server community with designated

name and access values.

Parameters:

  • :name (String)

    The snmp-server community name value

  • :access (String)

    The snmp-server community access value

Returns:

  • (Boolean)

    returns true if the command completed successfully



415
416
417
# File 'lib/rbeapi/api/snmp.rb', line 415

def set_community_access(name, access)
  configure "snmp-server community #{name} #{access}"
end

#set_community_acl(name, opts = {}) ⇒ Boolean

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.

Parameters:

  • :name (String)

    The name of the snmp community to add to the nodes running configuration.

  • opts (Hash) (defaults to: {})

    The configuration parameters

Options Hash (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.

  • :enable (Boolean)

    If false then the command is negated. Default is true.

  • :default (Boolean)

    Configure the snmp community name using the default keyword. Default takes precedence over enable.

Returns:

  • (Boolean)

    returns true if the command completed successfully



393
394
395
396
397
398
399
400
401
402
403
404
# File 'lib/rbeapi/api/snmp.rb', line 393

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 = {}) ⇒ Boolean

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.

Parameters:

  • opts (Hash) (defaults to: {})

    The configuration parameters

  • :opts (Hash)

    a customizable set of options

Options Hash (opts):

  • :value (string)

    The snmp contact value to configure

  • :default (Boolean)

    Configures the snmp contact value using the default keyword

Returns:

  • (Boolean)

    returns true if the command completed successfully



265
266
267
268
# File 'lib/rbeapi/api/snmp.rb', line 265

def set_contact(opts = {})
  cmd = command_builder('snmp-server contact', opts)
  configure(cmd)
end

#set_location(opts = {}) ⇒ Boolean

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.

Parameters:

  • opts (Hash) (defaults to: {})

    The configuration parameters

  • :opts (Hash)

    a customizable set of options

Options Hash (opts):

  • :value (string)

    The snmp location value to configure

  • :default (Boolean)

    Configure the snmp location value using the default keyword

Returns:

  • (Boolean)

    returns true if the command completed successfully



234
235
236
237
# File 'lib/rbeapi/api/snmp.rb', line 234

def set_location(opts = {})
  cmd = command_builder('snmp-server location', opts)
  configure(cmd)
end

#set_notification(opts = {}) ⇒ Object

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.

Parameters:

  • :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.

  • :state (String)

    The state to configure the trap notification. Valid values include ‘on’, ‘off’ or ‘default’



201
202
203
204
205
206
207
# File 'lib/rbeapi/api/snmp.rb', line 201

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 = {}) ⇒ Boolean

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.

Parameters:

  • opts (Hash) (defaults to: {})

    The configuration parameters

  • :opts (Hash)

    a customizable set of options

Options Hash (opts):

  • :value (string)

    The snmp source interface value to configure. This method will not ensure the interface is present in the configuration

  • :default (Boolean)

    Configures the snmp source interface value using the default keyword

Returns:

  • (Boolean)

    returns true if the command completed successfully



327
328
329
330
# File 'lib/rbeapi/api/snmp.rb', line 327

def set_source_interface(opts = {})
  cmd = command_builder('snmp-server source-interface', opts)
  configure(cmd)
end