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'


71
72
73
74
75
76
77
# File 'lib/cisco_node_utils/platform.rb', line 71

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' }

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

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(R) Celeron(R) CPU P450'


54
55
56
# File 'lib/cisco_node_utils/platform.rb', line 54

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

.fansObject

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


174
175
176
# File 'lib/cisco_node_utils/platform.rb', line 174

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

.hardware_typeObject

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


49
50
51
# File 'lib/cisco_node_utils/platform.rb', line 49

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' => { ... }}

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
159
160
161
# File 'lib/cisco_node_utils/platform.rb', line 128

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'


87
88
89
90
91
92
93
# File 'lib/cisco_node_utils/platform.rb', line 87

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' }

62
63
64
65
66
67
68
# File 'lib/cisco_node_utils/platform.rb', line 62

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
44
45
46
# File 'lib/cisco_node_utils/platform.rb', line 37

def self.packages
  pkg_hsh = {}
  pkgs = config_get('images', 'packages')
  return {} if pkgs.nil?
  pkgs.each { |p| pkg_hsh[p[0]] = p[1].downcase }
rescue RuntimeError => e
  raise unless e.message[/Invalid command/]
ensure
  pkg_hsh
end

.power_suppliesObject

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


169
170
171
# File 'lib/cisco_node_utils/platform.rb', line 169

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

.reset_reasonObject

Ex: 'Reset Requested by CLI command reload'


96
97
98
# File 'lib/cisco_node_utils/platform.rb', line 96

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

.slotsObject

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


164
165
166
# File 'lib/cisco_node_utils/platform.rb', line 164

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)'


80
81
82
83
84
# File 'lib/cisco_node_utils/platform.rb', line 80

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' }},
{ ... }}

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
229
230
231
# File 'lib/cisco_node_utils/platform.rb', line 193

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