-
Notifications
You must be signed in to change notification settings - Fork 50
Added SNMP Notification Receiver #129
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| # snmp_notification_receiver | ||
| --- | ||
| port: | ||
| config_get: "show running-config snmp all" | ||
| config_get_token: '/^snmp-server host %s.*version.* udp-port (\d+).*$/' | ||
| default_value: null | ||
|
|
||
| receivers: | ||
| config_get: "show running snmp all" | ||
| config_get_token: '/^snmp-server host (\S+) (traps|informs)/' | ||
| config_set: "<state> snmp-server host <ip> <type> version <version> <security> <username> <udp_port>" | ||
| multiple: | ||
|
|
||
| security: | ||
| config_get: "show running-config snmp all" | ||
| config_get_token: '/^snmp-server host %s.* version 3 (auth|noauth|priv).*$/' | ||
| default_value: null | ||
|
|
||
| source_interface: | ||
| config_get: "show running-config snmp all" | ||
| config_get_token: '/^snmp-server host %s.* source-interface (\S+).*$/' | ||
| config_set: "snmp-server host <ip> source-interface <source_interface> <port>" | ||
| default_value: null | ||
|
|
||
| type: | ||
| config_get: "show running-config snmp all" | ||
| config_get_token: '/^snmp-server host %s (traps|informs).*$/' | ||
| default_value: null | ||
|
|
||
| username: | ||
| config_get: "show running-config snmp all" | ||
| config_get_token: '/^snmp-server host %s.*version.* (\S+)$/' | ||
| default_value: null | ||
|
|
||
| username_with_port: | ||
| config_get: "show running-config snmp all" | ||
| config_get_token: '/^snmp-server host %s.*version.* (\S+) udp-port/' | ||
| default_value: null | ||
|
|
||
| version: | ||
| config_get: "show running-config snmp all" | ||
| config_get_token: '/^snmp-server host %s.* version (\S+).*$/' | ||
| default_value: null | ||
|
|
||
| vrf: | ||
| config_get: "show running-config snmp all" | ||
| config_get_token: '/^snmp-server host %s.* use-vrf (\S+).*$/' | ||
| config_set: "snmp-server host <ip> use-vrf <vrf> <port>" | ||
| default_value: null | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,158 @@ | ||
| # November 2015, Jonathan Tripathy | ||
| # | ||
| # Copyright (c) 2014-2015 Cisco and/or its affiliates. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| require_relative 'node_util' | ||
|
|
||
| module Cisco | ||
| # SnmpNotificationReceiver - node utility class for SNMP server management | ||
| class SnmpNotificationReceiver < NodeUtil | ||
| attr_reader :name | ||
|
|
||
| def initialize(name, | ||
| instantiate: true, | ||
| type: '', | ||
| version: '', | ||
| security: '', | ||
| username: '', | ||
| port: '', | ||
| vrf: '', | ||
| source_interface: '') | ||
|
|
||
| fail TypeError unless name.is_a?(String) | ||
| @name = name | ||
|
|
||
| fail TypeError unless type.is_a?(String) | ||
|
|
||
| fail TypeError unless version.is_a?(String) || version.is_a?(Integer) | ||
|
|
||
| fail TypeError unless security.is_a?(String) | ||
|
|
||
| fail TypeError unless username.is_a?(String) | ||
|
|
||
| fail TypeError unless port.is_a?(String) || port.is_a?(Integer) | ||
|
|
||
| fail TypeError unless vrf.is_a?(String) | ||
|
|
||
| fail TypeError unless source_interface.is_a?(String) | ||
|
|
||
| return unless instantiate | ||
|
|
||
| # Mandatory Properties | ||
| fail TypeError unless name.length > 0 | ||
| fail TypeError unless type.length > 0 | ||
|
|
||
| if version.is_a?(Integer) | ||
| fail TypeError if version <= 0 | ||
| else | ||
| fail TypeError if version.length <= 0 | ||
| end | ||
|
|
||
| fail TypeError unless username.length > 0 | ||
|
|
||
| config_set('snmp_notification_receiver', | ||
| 'receivers', | ||
| state: '', | ||
| ip: name, | ||
| type: type, | ||
| version: version, | ||
| security: security, | ||
| username: username, | ||
| udp_port: port.empty? ? '' : "udp-port #{port}") | ||
|
|
||
| unless source_interface.empty? | ||
| config_set('snmp_notification_receiver', | ||
| 'source_interface', | ||
| ip: name, | ||
| source_interface: source_interface, | ||
| port: port.empty? ? '' : "udp-port #{port}") | ||
| end | ||
|
|
||
| return if vrf.empty? | ||
| config_set('snmp_notification_receiver', | ||
| 'vrf', | ||
| ip: name, | ||
| vrf: vrf, | ||
| port: port.empty? ? '' : "udp-port #{port}") | ||
| end | ||
|
|
||
| def self.receivers | ||
| hash = {} | ||
|
|
||
| receivers_list = config_get('snmp_notification_receiver', 'receivers') | ||
| return hash if receivers_list.nil? | ||
|
|
||
| receivers_list.each do |arr| | ||
| next if !arr.is_a?(Array) || arr.empty? | ||
| id = arr[0] | ||
| hash[id] = SnmpNotificationReceiver.new(id, instantiate: false) | ||
| end | ||
|
|
||
| hash | ||
| end | ||
|
|
||
| def ==(other) | ||
| name == other.name | ||
| end | ||
|
|
||
| def destroy | ||
| config_set('snmp_notification_receiver', | ||
| 'receivers', | ||
| state: 'no', | ||
| ip: name, | ||
| type: type, | ||
| version: version, | ||
| security: security.nil? ? '' : "#{security}", | ||
| username: username, | ||
| udp_port: port.nil? ? '' : "udp-port #{port}") | ||
| end | ||
|
|
||
| def port | ||
| config_get('snmp_notification_receiver', 'port', @name) | ||
| end | ||
|
|
||
| def username | ||
| if !port.nil? | ||
| endpoint = 'username_with_port' | ||
| else | ||
| endpoint = 'username' | ||
| end | ||
|
|
||
| config_get('snmp_notification_receiver', endpoint, @name) | ||
| end | ||
|
|
||
| def version | ||
| config_get('snmp_notification_receiver', 'version', @name) | ||
| end | ||
|
|
||
| def type | ||
| config_get('snmp_notification_receiver', 'type', @name) | ||
| end | ||
|
|
||
| def security | ||
| config_get('snmp_notification_receiver', 'security', @name) | ||
| end | ||
|
|
||
| def vrf | ||
| config_get('snmp_notification_receiver', 'vrf', @name) | ||
| end | ||
|
|
||
| def source_interface | ||
| val = config_get('snmp_notification_receiver', 'source_interface', @name) | ||
| val = val.downcase unless val.nil? | ||
| val | ||
| end | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| # | ||
| # Minitest for SnmpNotificationReceiver class | ||
| # | ||
| # Copyright (c) 2014-2015 Cisco and/or its affiliates. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| require_relative 'ciscotest' | ||
| require_relative '../lib/cisco_node_utils/snmp_notification_receiver' | ||
|
|
||
| # TestSnmpNotificationReceiver - Minitest for SnmpNotificationReceiver | ||
| # node utility. | ||
| class TestSnmpNotificationReceiver < CiscoTestCase | ||
| def setup | ||
| # setup runs at the beginning of each test | ||
| super | ||
| no_snmpnotificationreceiver | ||
| config('vrf context red') | ||
| end | ||
|
|
||
| def teardown | ||
| # teardown runs at the end of each test | ||
| no_snmpnotificationreceiver | ||
| config('no vrf context red') | ||
| super | ||
| end | ||
|
|
||
| def no_snmpnotificationreceiver | ||
| # Turn the feature off for a clean test. | ||
| config('no snmp-server host 4.5.6.7 informs version 3 priv ab udp-port 45', | ||
| 'no snmp-server host 8.9.10.11 traps version 3 auth cd udp-port 46') | ||
| end | ||
|
|
||
| # TESTS | ||
|
|
||
| def test_create_destroy_single | ||
| id = '4.5.6.7' | ||
| refute_includes(Cisco::SnmpNotificationReceiver.receivers, id) | ||
|
|
||
| receiver = \ | ||
| Cisco::SnmpNotificationReceiver.new(id, | ||
| instantiate: true, | ||
| type: 'informs', | ||
| version: '3', | ||
| security: 'priv', | ||
| username: 'ab', | ||
| port: '45', | ||
| vrf: 'red', | ||
| source_interface: 'ethernet1/3') | ||
|
|
||
| assert_includes(Cisco::SnmpNotificationReceiver.receivers, id) | ||
| assert_equal(Cisco::SnmpNotificationReceiver.receivers[id], receiver) | ||
|
|
||
| assert_equal(Cisco::SnmpNotificationReceiver.receivers[id].source_interface, | ||
| 'ethernet1/3') | ||
| assert_equal(Cisco::SnmpNotificationReceiver.receivers[id].port, '45') | ||
| assert_equal(Cisco::SnmpNotificationReceiver.receivers[id].type, 'informs') | ||
| assert_equal(Cisco::SnmpNotificationReceiver.receivers[id].username, 'ab') | ||
| assert_equal(Cisco::SnmpNotificationReceiver.receivers[id].version, '3') | ||
| assert_equal(Cisco::SnmpNotificationReceiver.receivers[id].vrf, 'red') | ||
| assert_equal(Cisco::SnmpNotificationReceiver.receivers[id].security, 'priv') | ||
|
|
||
| receiver.destroy | ||
| refute_includes(Cisco::SnmpNotificationReceiver.receivers, id) | ||
| end | ||
|
|
||
| def test_create_destroy_multiple | ||
| id = '4.5.6.7' | ||
| id2 = '8.9.10.11' | ||
|
|
||
| refute_includes(Cisco::SnmpNotificationReceiver.receivers, id) | ||
| refute_includes(Cisco::SnmpNotificationReceiver.receivers, id2) | ||
|
|
||
| receiver = \ | ||
| Cisco::SnmpNotificationReceiver.new(id, | ||
| instantiate: true, | ||
| type: 'informs', | ||
| version: '3', | ||
| security: 'priv', | ||
| username: 'ab', | ||
| port: '45', | ||
| vrf: 'red', | ||
| source_interface: 'ethernet1/3') | ||
|
|
||
| receiver2 = \ | ||
| Cisco::SnmpNotificationReceiver.new(id2, | ||
| instantiate: true, | ||
| type: 'traps', | ||
| version: '3', | ||
| security: 'auth', | ||
| username: 'cd', | ||
| port: '46', | ||
| vrf: 'red', | ||
| source_interface: 'ethernet1/4') | ||
|
|
||
| assert_includes(Cisco::SnmpNotificationReceiver.receivers, id) | ||
| assert_equal(Cisco::SnmpNotificationReceiver.receivers[id], receiver) | ||
|
|
||
| assert_equal(Cisco::SnmpNotificationReceiver.receivers[id].source_interface, | ||
| 'ethernet1/3') | ||
| assert_equal(Cisco::SnmpNotificationReceiver.receivers[id].port, '45') | ||
| assert_equal(Cisco::SnmpNotificationReceiver.receivers[id].type, 'informs') | ||
| assert_equal(Cisco::SnmpNotificationReceiver.receivers[id].username, 'ab') | ||
| assert_equal(Cisco::SnmpNotificationReceiver.receivers[id].version, '3') | ||
| assert_equal(Cisco::SnmpNotificationReceiver.receivers[id].vrf, 'red') | ||
| assert_equal(Cisco::SnmpNotificationReceiver.receivers[id].security, 'priv') | ||
|
|
||
| assert_includes(Cisco::SnmpNotificationReceiver.receivers, id2) | ||
| assert_equal(Cisco::SnmpNotificationReceiver.receivers[id2], receiver2) | ||
|
|
||
| assert_equal( | ||
| Cisco::SnmpNotificationReceiver.receivers[id2].source_interface, | ||
| 'ethernet1/4') | ||
| assert_equal(Cisco::SnmpNotificationReceiver.receivers[id2].port, '46') | ||
| assert_equal(Cisco::SnmpNotificationReceiver.receivers[id2].type, 'traps') | ||
| assert_equal(Cisco::SnmpNotificationReceiver.receivers[id2].username, 'cd') | ||
| assert_equal(Cisco::SnmpNotificationReceiver.receivers[id2].version, '3') | ||
| assert_equal(Cisco::SnmpNotificationReceiver.receivers[id2].vrf, 'red') | ||
| assert_equal(Cisco::SnmpNotificationReceiver.receivers[id2].security, | ||
| 'auth') | ||
|
|
||
| receiver.destroy | ||
| receiver2.destroy | ||
| refute_includes(Cisco::SnmpNotificationReceiver.receivers, id) | ||
| refute_includes(Cisco::SnmpNotificationReceiver.receivers, id2) | ||
| end | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there no way to change any of the properties without doing a destroy and recreate? If I want to change the username for a receiver from 'ab' to 'cd', I have to call
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a limitation in the Cisco CLI that does not allow you to change properties independently of each other. This was discussed in the an email thread with the Cisco SNMP guys for the snmp_user provider.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fair enough - snmpuser does the same approach but I was mis-remembering that it had setters for individual properties too. This is fine. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi @mikewiebe - Is there anything you're needing to merge this PR?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi @ajjohnsonpdx @glennmatthews provided quite a few review comments which I believe are not yet resolved? |
||
| end | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For future work it would be preferable to use named keys in the config_get_token as well (
'/^snmp-server host <ip>.*version.* udp-port (\d+).*$/') but this isn't critical at this time.