Class: Cisco::Platform

Inherits:
NodeUtil show all
Defined in:
lib/cisco_node_utils/platform.rb

Overview

Platform - class for gathering platform hardware and software information

Class Method Summary collapse

Methods inherited from NodeUtil

client, #client, config_get, #config_get, #config_get_default, config_get_default, config_set, #config_set, #get, #ios_xr?, #nexus?, #node, node, platform, #platform, supports?, #supports?

Class Method Details

.boardObject

Ex: ‘Processor Board ID FOC15430TEY’



68
69
70
71
72
73
74
# File 'lib/cisco_node_utils/platform.rb', line 68

def self.board
  config_get('show_version', 'board')
rescue RuntimeError => e
  # Some Nexus platforms may fail to report this value.
  return nil if /No key/ =~ e.message
  raise
end

.chassisObject

Returns chassis hash with keys “descr”, “pid”, “vid”, “sn” Ex: { ‘descr’ => ‘Nexus9000 C9396PX Chassis’,

'pid'   => 'N9K-C9396PX',
'vid'   => 'V02',
'sn'    => 'SAL1812NTBP' }


102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/cisco_node_utils/platform.rb', line 102

def self.chassis
  node.cache_flush # TODO: investigate why this is needed
  all = config_get('inventory', 'chassis')
  return nil if all.nil?

  # item['name'] value is "\"Chassis\"" on most platforms,
  # but n5k/n6k use "Chassis" so make the quotes optional.
  chas = all.find { |item| item['name'][/^"?Chassis"?$/] }
  return nil if chas.nil?
  {
    'descr' => chas['desc'].tr('"', ''),
    'pid'   => chas['productid'],
    'vid'   => chas['vendorid'],
    'sn'    => chas['serialnum'],
  }
end

.cpuObject

Ex: ‘Intel® Celeron® CPU P450’



51
52
53
# File 'lib/cisco_node_utils/platform.rb', line 51

def self.cpu
  config_get('show_version', 'cpu')
end

.fansObject

Returns array of hashes with keys “name”, “descr”, “pid”, “vid”, “sn”



171
172
173
# File 'lib/cisco_node_utils/platform.rb', line 171

def self.fans
  Platform.inventory_of('Fan')
end

.hardware_typeObject

Ex: ‘Cisco Nexus3064 Chassis (“48x10GE + 16x10G/4x40G Supervisor”)’



46
47
48
# File 'lib/cisco_node_utils/platform.rb', line 46

def self.hardware_type
  config_get('show_version', 'description')
end

.image_versionObject

NX: 7.0(3)I2(3) [build 7.0(3)I2(2.118)] XR: 6.1.1.04I



25
26
27
# File 'lib/cisco_node_utils/platform.rb', line 25

def self.image_version
  config_get('show_version', 'version')
end

.inventory_of(type) ⇒ Object

Returns hash of hashes with inner keys “name”, “descr”, “pid”, “vid”, “sn” Ex: { ‘Slot 1’ => { ‘descr’ => ‘1/10G SFP+ Ethernet Module’,

              'pid'   => 'N9K-C9396PX',
              'vid'   => 'V02',
              'sn'    => 'SAL1812NTBP' },
'Slot 2' => { ... }}


125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/cisco_node_utils/platform.rb', line 125

def self.inventory_of(type)
  node.cache_flush # TODO: investigate why this is needed
  inv = config_get('inventory', 'all')
  inv_hsh = {}
  return inv_hsh if inv.nil?
  if platform == :ios_xr
    # XR gets a string so we have to process it directly
    inv_arr = inv.scan(
      /NAME:\s+"(#{type}[^"]*)",\s+
       DESCR:\s+"([^"]*)"\s*
       \n
       PID:\s+(\S+)\s*,\s+
       VID:\s+(\S+)\s*,\s+
       SN:\s+(\S+)\s*/x).flatten
    inv_arr.each do |entry|
      inv_hsh[entry[0]] = { 'descr' => entry[1],
                            'pid'   => entry[2],
                            'vid'   => entry[3],
                            'sn'    => entry[4] }
    end
  else
    # Nexus gets structured output
    inv.select! { |x| x['name'].include? type }
    return inv_hsh if inv.empty?
    # match desired output format
    inv.each do |s|
      inv_hsh[s['name'].tr('"', '')] = { 'descr' => s['desc'].tr('"', ''),
                                         'pid'   => s['productid'],
                                         'vid'   => s['vendorid'],
                                         'sn'    => s['serialnum'] }
    end
  end
  inv_hsh
end

.last_resetObject

Ex: ‘23113 usecs after Mon Jul 1 15:24:29 2013’



84
85
86
87
88
89
90
# File 'lib/cisco_node_utils/platform.rb', line 84

def self.last_reset
  config_get('show_version', 'last_reset_time')
rescue RuntimeError => e
  # Some Nexus platforms may fail to report this value.
  return nil if /No key/ =~ e.message
  raise
end

.memoryObject

Return hash with keys “total”, “used”, “free” Ex: { ‘total’ => ‘16402252K’,

'used'  => '5909004K',
'free'  => '10493248K' }


59
60
61
62
63
64
65
# File 'lib/cisco_node_utils/platform.rb', line 59

def self.memory
  {
    'total' => config_get('memory', 'total'),
    'used'  => config_get('memory', 'used'),
    'free'  => config_get('memory', 'free'),
  }
end

.packagesObject

Returns package hash with state values Ex: { ‘n3000-uk9.6.0.2.U1.1.CSCaa12345.bin’ => ‘inactive committed’,

'n3000-uk9.6.0.2.U1.1.CSCaa12346.bin' => 'active', }


37
38
39
40
41
42
43
# File 'lib/cisco_node_utils/platform.rb', line 37

def self.packages
  pkgs = config_get('images', 'packages')
  return {} if pkgs.nil?
  pkg_hsh = {}
  pkgs.each { |p| pkg_hsh[p[0]] = p[1].downcase }
  pkg_hsh
end

.power_suppliesObject

Returns array of hashes with keys “name”, “descr”, “pid”, “vid”, “sn”



166
167
168
# File 'lib/cisco_node_utils/platform.rb', line 166

def self.power_supplies
  Platform.inventory_of('Power Supply')
end

.reset_reasonObject

Ex: ‘Reset Requested by CLI command reload’



93
94
95
# File 'lib/cisco_node_utils/platform.rb', line 93

def self.reset_reason
  config_get('show_version', 'last_reset_reason')
end

.slotsObject

Returns array of hashes with keys “name”, “descr”, “pid”, “vid”, “sn”



161
162
163
# File 'lib/cisco_node_utils/platform.rb', line 161

def self.slots
  Platform.inventory_of('Slot')
end

.system_imageObject

ex: ‘n3500-uk9.6.0.2.A3.0.40.bin’



30
31
32
# File 'lib/cisco_node_utils/platform.rb', line 30

def self.system_image
  config_get('show_version', 'system_image')
end

.uptimeObject

Ex: ‘1 day(s), 21 hour(s), 46 minute(s), 54 second(s)’



77
78
79
80
81
# File 'lib/cisco_node_utils/platform.rb', line 77

def self.uptime
  u = config_get('show_version', 'uptime')
  fail 'failed to retrieve platform uptime' if u.nil?
  u
end

.virtual_servicesObject

Returns hash of hashes with inner keys “state”, “application”, … Ex: { ‘chef’ => {

  'package_info' => { 'name'     => 'n3k_chef.ova',
                      'path'     => 'bootflash:/n3k_chef.ova' },
  'application'  => { 'name'     => 'ChefAgent',
                      'version'  => '0.1',
                      'descr'    => 'Cisco Chef Agent' },
  'signing'      => { 'key_type' => 'Cisco development key',
                      'method'   => 'SHA-1' }
  'licensing'    => { 'name'     => 'none',
                      'version'  => 'none' }
  'reservation'  => { 'disk'     => '111 MB',
                      'memory'   => '0 MB',
                      'cpu'      => '0% system CPU' }},
{ ... }}


190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/cisco_node_utils/platform.rb', line 190

def self.virtual_services
  # If no virtual-services are installed, this will result
  # in a RuntimeError.  We need to rescue this specific case.
  begin
    virts = config_get('virtual_service', 'services')
  rescue RuntimeError => e
    return {} if e.message[/No key \"TABLE_detail\"/]
    raise
  end
  return {} if virts.nil?
  # NXAPI returns hash instead of array if there's only 1
  virts = [virts] if virts.is_a? Hash
  # convert to expected format
  virts_hsh = {}
  virts.each do |serv|
    # rubocop:disable Style/AlignHash
    virts_hsh[serv['name']] = {
      'package_info' => { 'name'     => serv['package_name'],
                          'path'     => serv['ova_path'],
      },
      'application'  => { 'name'     => serv['application_name'],
                          'version'  => serv['application_version'],
                          'descr'    => serv['application_description'],
      },
      'signing'      => { 'key_type' => serv['key_type'],
                          'method'   => serv['signing_method'],
      },
      'licensing'    => { 'name'     => serv['licensing_name'],
                          'version'  => serv['licensing_version'],
      },
      'reservation'  => { 'disk'     => serv['disk_reservation'],
                          'memory'   => serv['memory_reservation'],
                          'cpu'      => serv['cpu_reservation'],
      },
    }
    # rubocop:enable Style/AlignHash
  end
  virts_hsh
end