Class: Cisco::Node
- Inherits:
-
Object
- Object
- Cisco::Node
- Defined in:
- lib/cisco_node_utils/node.rb
Overview
class Cisco::Node Singleton representing the network node (switch/router) that is running this code. The singleton is lazily instantiated, meaning that it doesn’t exist until some client requests it (with Node.instance())
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Here and below are implementation details and private APIs that most providers shouldn’t need to know about or use.
-
#cmd_ref ⇒ Object
readonly
Here and below are implementation details and private APIs that most providers shouldn’t need to know about or use.
Class Method Summary collapse
Instance Method Summary collapse
-
#boot ⇒ String
Such as “bootflash:///n3000-uk9-kickstart.6.0.2.U5.0.941.bin”.
- #cache_auto=(enable) ⇒ Object
- #cache_auto? ⇒ Boolean
- #cache_enable=(enable) ⇒ Object
- #cache_enable? ⇒ Boolean
-
#cache_flush ⇒ Object
Clear the cache of CLI output results.
-
#config_get(feature, property, *args) ⇒ String, ...
Convenience wrapper for get() Uses CommandReference to look up the given show command and key of interest, executes that command, and returns the value corresponding to that key.
-
#config_get_default(feature, property) ⇒ String?
Uses CommandReference to lookup the default value for a given feature and feature property.
-
#config_set(feature, property, *args) ⇒ Object
Uses CommandReference to look up the given config command(s) of interest and then applies the configuration.
-
#delete_yang(yang) ⇒ Object
Delete the specified JSON YANG config from the device.
-
#domain_name ⇒ String
Such as “example.com”.
-
#get(**kwargs) ⇒ Object
Send a show command to the device.
-
#get_yang(yang_path) ⇒ Object
Retrieve JSON YANG config from the device for the specified path.
-
#get_yang_oper(yang_path) ⇒ Object
Retrieve JSON YANG operational data for the specified path.
-
#host_name ⇒ String
Such as “bxb-oa-n3k-7”.
-
#initialize ⇒ Node
constructor
A new instance of Node.
- #inspect ⇒ Object
-
#last_reset_reason ⇒ String
Such as “Reset Requested by CLI command reload”.
-
#last_reset_time ⇒ String
Timestamp of last reset time.
-
#massage(value, ref) ⇒ Object
Attempt to massage the given value into the format specified by the given CmdRef object.
-
#merge_yang(yang) ⇒ Object
Merge the specified JSON YANG config with the running config on the device.
-
#os ⇒ String
Such as “Cisco Nexus Operating System (NX-OS) Software”.
-
#os_version ⇒ String
Such as “6.0(2)U5(1) [build 6.0(2)U5(0.941)]”.
-
#product_description ⇒ String
Such as “Nexus 3048 Chassis”.
-
#product_id ⇒ String
Such as “N3K-C3048TP-1GE”.
-
#product_serial_number ⇒ String
Such as “FOC1722R0ET”.
-
#product_version_id ⇒ String
Such as “V01”.
-
#replace_yang(yang) ⇒ Object
Replace the running config on the device with the specified JSON YANG config.
-
#set(**kwargs) ⇒ Object
Send a config command to the device.
-
#system ⇒ String
Such as “bootflash:///n3000-uk9.6.0.2.U5.0.941.bin”.
-
#system_cpu_utilization ⇒ Float
Combined user/kernel CPU utilization.
-
#system_uptime ⇒ Integer
System uptime, in seconds.
- #to_s ⇒ Object
Constructor Details
#initialize ⇒ Node
Returns a new instance of Node.
158 159 160 161 162 163 164 165 |
# File 'lib/cisco_node_utils/node.rb', line 158 def initialize @client = Cisco::Client.create @cmd_ref = nil @cmd_ref = CommandReference.new(product: product_id, platform: @client.platform, data_formats: @client.data_formats) cache_flush end |
Instance Attribute Details
#client ⇒ Object (readonly)
Here and below are implementation details and private APIs that most providers shouldn’t need to know about or use.
152 153 154 |
# File 'lib/cisco_node_utils/node.rb', line 152 def client @client end |
#cmd_ref ⇒ Object (readonly)
Here and below are implementation details and private APIs that most providers shouldn’t need to know about or use.
152 153 154 |
# File 'lib/cisco_node_utils/node.rb', line 152 def cmd_ref @cmd_ref end |
Class Method Details
.instance ⇒ Object
154 155 156 |
# File 'lib/cisco_node_utils/node.rb', line 154 def self.instance @instance ||= new end |
Instance Method Details
#boot ⇒ String
Returns such as “bootflash:///n3000-uk9-kickstart.6.0.2.U5.0.941.bin”.
323 324 325 |
# File 'lib/cisco_node_utils/node.rb', line 323 def boot config_get('show_version', 'boot_image') end |
#cache_auto=(enable) ⇒ Object
187 188 189 |
# File 'lib/cisco_node_utils/node.rb', line 187 def cache_auto=(enable) @client.cache_auto = enable end |
#cache_auto? ⇒ Boolean
183 184 185 |
# File 'lib/cisco_node_utils/node.rb', line 183 def cache_auto? @client.cache_auto? end |
#cache_enable=(enable) ⇒ Object
179 180 181 |
# File 'lib/cisco_node_utils/node.rb', line 179 def cache_enable=(enable) @client.cache_enable = enable end |
#cache_enable? ⇒ Boolean
175 176 177 |
# File 'lib/cisco_node_utils/node.rb', line 175 def cache_enable? @client.cache_enable? end |
#cache_flush ⇒ Object
Clear the cache of CLI output results.
If cache_auto is true (default) then this will be performed automatically whenever a config_set() is called, but providers may also call this to explicitly force the cache to be cleared.
145 146 147 |
# File 'lib/cisco_node_utils/node.rb', line 145 def cache_flush @client.cache_flush end |
#config_get(feature, property, *args) ⇒ String, ...
Convenience wrapper for get() Uses CommandReference to look up the given show command and key of interest, executes that command, and returns the value corresponding to that key.
53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/cisco_node_utils/node.rb', line 53 def config_get(feature, property, *args) ref = @cmd_ref.lookup(feature, property) # If we have a default value but no getter, just return the default return ref.default_value if ref.default_value? && !ref.getter? get_args = ref.getter(*args) massage(get(command: ref.get_command, data_format: get_args[:data_format], context: get_args[:context], value: get_args[:value]), ref) end |
#config_get_default(feature, property) ⇒ String?
Uses CommandReference to lookup the default value for a given feature and feature property.
114 115 116 117 |
# File 'lib/cisco_node_utils/node.rb', line 114 def config_get_default(feature, property) ref = @cmd_ref.lookup(feature, property) ref.default_value end |
#config_set(feature, property, *args) ⇒ Object
Uses CommandReference to look up the given config command(s) of interest and then applies the configuration.
134 135 136 137 138 |
# File 'lib/cisco_node_utils/node.rb', line 134 def config_set(feature, property, *args) ref = @cmd_ref.lookup(feature, property) set_args = ref.setter(*args) set(**set_args) end |
#delete_yang(yang) ⇒ Object
Delete the specified JSON YANG config from the device.
223 224 225 |
# File 'lib/cisco_node_utils/node.rb', line 223 def delete_yang(yang) @client.set(data_format: :yang_json, values: [yang], mode: :delete_config) end |
#domain_name ⇒ String
Returns such as “example.com”.
289 290 291 |
# File 'lib/cisco_node_utils/node.rb', line 289 def domain_name config_get('dnsclient', 'domain_name') end |
#get(**kwargs) ⇒ Object
Send a show command to the device. In general, clients should use config_get() rather than calling this function directly.
205 206 207 |
# File 'lib/cisco_node_utils/node.rb', line 205 def get(**kwargs) @client.get(**kwargs) end |
#get_yang(yang_path) ⇒ Object
Retrieve JSON YANG config from the device for the specified path.
228 229 230 |
# File 'lib/cisco_node_utils/node.rb', line 228 def get_yang(yang_path) @client.get(data_format: :yang_json, command: yang_path) end |
#get_yang_oper(yang_path) ⇒ Object
Retrieve JSON YANG operational data for the specified path.
233 234 235 |
# File 'lib/cisco_node_utils/node.rb', line 233 def get_yang_oper(yang_path) @client.get(data_format: :yang_json, command: yang_path, mode: :get_oper) end |
#host_name ⇒ String
Returns such as “bxb-oa-n3k-7”.
284 285 286 |
# File 'lib/cisco_node_utils/node.rb', line 284 def host_name config_get('show_version', 'host_name') end |
#inspect ⇒ Object
171 172 173 |
# File 'lib/cisco_node_utils/node.rb', line 171 def inspect "Node: client:'#{client.inspect}' cmd_ref:'#{cmd_ref.inspect}'" end |
#last_reset_reason ⇒ String
Returns such as “Reset Requested by CLI command reload”.
310 311 312 |
# File 'lib/cisco_node_utils/node.rb', line 310 def last_reset_reason config_get('show_version', 'last_reset_reason') end |
#last_reset_time ⇒ String
Returns timestamp of last reset time.
305 306 307 |
# File 'lib/cisco_node_utils/node.rb', line 305 def last_reset_time config_get('show_version', 'last_reset_time') end |
#massage(value, ref) ⇒ Object
Attempt to massage the given value into the format specified by the given CmdRef object.
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/cisco_node_utils/node.rb', line 69 def massage(value, ref) Cisco::Logger.debug "Massaging '#{value}' (#{value.inspect})" if value.is_a?(Array) && !ref.multiple fail "Expected zero/one value but got '#{value}'" if value.length > 1 value = value[0] end if (value.nil? || value.empty?) && ref.default_value? && ref.auto_default Cisco::Logger.debug "Default: #{ref.default_value}" return ref.default_value end return value unless ref.kind case ref.kind when :boolean if value.nil? || value.empty? value = false elsif /^no / =~ value value = false elsif /disable$/ =~ value value = false else value = true end when :int value = value.to_i unless value.nil? when :string value = '' if value.nil? value = value.to_s.strip when :symbol value = value.to_sym unless value.nil? end Cisco::Logger.debug "Massaged to '#{value}'" value end |
#merge_yang(yang) ⇒ Object
Merge the specified JSON YANG config with the running config on the device.
211 212 213 |
# File 'lib/cisco_node_utils/node.rb', line 211 def merge_yang(yang) @client.set(data_format: :yang_json, values: [yang], mode: :merge_config) end |
#os ⇒ String
Returns such as “Cisco Nexus Operating System (NX-OS) Software”.
238 239 240 241 242 |
# File 'lib/cisco_node_utils/node.rb', line 238 def os o = config_get('show_version', 'header') fail 'failed to retrieve operating system information' if o.nil? o.split("\n")[0] end |
#os_version ⇒ String
Returns such as “6.0(2)U5(1) [build 6.0(2)U5(0.941)]”.
245 246 247 |
# File 'lib/cisco_node_utils/node.rb', line 245 def os_version config_get('show_version', 'version') end |
#product_description ⇒ String
Returns such as “Nexus 3048 Chassis”.
250 251 252 |
# File 'lib/cisco_node_utils/node.rb', line 250 def product_description config_get('show_version', 'description') end |
#product_id ⇒ String
Returns such as “N3K-C3048TP-1GE”.
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 |
# File 'lib/cisco_node_utils/node.rb', line 255 def product_id if @cmd_ref return config_get('inventory', 'productid') else # We use this function to *find* the appropriate CommandReference if @client.platform == :nexus entries = get(command: 'show inventory', data_format: :nxapi_structured) return entries['TABLE_inv']['ROW_inv'][0]['productid'] elsif @client.platform == :ios_xr # No support for structured output for this command yet output = get(command: 'show inventory', data_format: :cli) return /NAME: .*\nPID: (\S+)/.match(output)[1] end end end |
#product_serial_number ⇒ String
Returns such as “FOC1722R0ET”.
279 280 281 |
# File 'lib/cisco_node_utils/node.rb', line 279 def product_serial_number config_get('inventory', 'serialnum') end |
#product_version_id ⇒ String
Returns such as “V01”.
274 275 276 |
# File 'lib/cisco_node_utils/node.rb', line 274 def product_version_id config_get('inventory', 'versionid') end |
#replace_yang(yang) ⇒ Object
Replace the running config on the device with the specified JSON YANG config.
217 218 219 220 |
# File 'lib/cisco_node_utils/node.rb', line 217 def replace_yang(yang) @client.set(data_format: :yang_json, values: [yang], mode: :replace_config) end |
#set(**kwargs) ⇒ Object
Send a config command to the device. In general, clients should use config_set() rather than calling this function directly.
196 197 198 |
# File 'lib/cisco_node_utils/node.rb', line 196 def set(**kwargs) @client.set(**kwargs) end |
#system ⇒ String
Returns such as “bootflash:///n3000-uk9.6.0.2.U5.0.941.bin”.
329 330 331 |
# File 'lib/cisco_node_utils/node.rb', line 329 def system config_get('show_version', 'system_image') end |
#system_cpu_utilization ⇒ Float
Returns combined user/kernel CPU utilization.
315 316 317 318 319 |
# File 'lib/cisco_node_utils/node.rb', line 315 def system_cpu_utilization output = config_get('system', 'resources') return output if output.nil? output['cpu_state_user'].to_f + output['cpu_state_kernel'].to_f end |
#system_uptime ⇒ Integer
Returns System uptime, in seconds.
294 295 296 297 298 299 300 301 302 |
# File 'lib/cisco_node_utils/node.rb', line 294 def system_uptime cache_flush t = config_get('show_system', 'uptime') fail 'failed to retrieve system uptime' if t.nil? # time units: t = ["0", "23", "15", "49"] t.map!(&:to_i) d, h, m, s = t (s + 60 * (m + 60 * (h + 24 * (d)))) end |
#to_s ⇒ Object
167 168 169 |
# File 'lib/cisco_node_utils/node.rb', line 167 def to_s client.to_s end |