Class: Puppet::Util::NetworkDevice::Cisco::Facts

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet/util/network_device/cisco/facts.rb

Overview

this retrieves facts from a cisco device

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(transport) ⇒ Facts

Returns a new instance of Facts.



10
11
12
# File 'lib/puppet/util/network_device/cisco/facts.rb', line 10

def initialize(transport)
  @transport = transport
end

Instance Attribute Details

#transportObject (readonly)

Returns the value of attribute transport.



8
9
10
# File 'lib/puppet/util/network_device/cisco/facts.rb', line 8

def transport
  @transport
end

Instance Method Details

#ios_major_version(version) ⇒ Object



61
62
63
# File 'lib/puppet/util/network_device/cisco/facts.rb', line 61

def ios_major_version(version)
  version.gsub(/^(\d+)\.(\d+)\(.+\)([A-Z]+)([\da-z]+)?/, '\1.\2\3')
end

#parse_show_verObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/puppet/util/network_device/cisco/facts.rb', line 19

def parse_show_ver
  facts = {}
  out = @transport.command("sh ver")
  lines = out.split("\n")
  lines.shift; lines.pop
  lines.each do |l|
    case l
    # cisco WS-C2924C-XL (PowerPC403GA) processor (revision 0x11) with 8192K/1024K bytes of memory.
    # Cisco 1841 (revision 5.0) with 355328K/37888K bytes of memory.
    # Cisco 877 (MPC8272) processor (revision 0x200) with 118784K/12288K bytes of memory.
    # cisco WS-C2960G-48TC-L (PowerPC405) processor (revision C0) with 61440K/4088K bytes of memory.
    # cisco WS-C2950T-24 (RC32300) processor (revision R0) with 19959K bytes of memory.
    when /[cC]isco ([\w-]+) (?:\(([\w-]+)\) processor )?\(revision (.+)\) with (\d+[KMG])(?:\/(\d+[KMG]))? bytes of memory\./
      facts[:hardwaremodel] = $1
      facts[:processor] = $2 if $2
      facts[:hardwarerevision] = $3
      facts[:memorysize] = $4
    # uptime
    # Switch uptime is 1 year, 12 weeks, 6 days, 22 hours, 32 minutes
    # c2950 uptime is 3 weeks, 1 day, 23 hours, 36 minutes
    # c2960 uptime is 2 years, 27 weeks, 5 days, 21 hours, 30 minutes
    # router uptime is 5 weeks, 1 day, 3 hours, 30 minutes
    when /^\s*([\w-]+)\s+uptime is (.*?)$/
      facts[:hostname] = $1
      facts[:uptime] = $2
      facts[:uptime_seconds] = uptime_to_seconds($2)
      facts[:uptime_days] = facts[:uptime_seconds] / 86400
    # "IOS (tm) C2900XL Software (C2900XL-C3H2S-M), Version 12.0(5)WC10, RELEASE SOFTWARE (fc1)"=> { :operatingsystem => "IOS", :operatingsystemrelease => "12.0(5)WC10", :operatingsystemmajrelease => "12.0", :operatingsystemfeature => "C3H2S"},
    # "IOS (tm) C2950 Software (C2950-I6K2L2Q4-M), Version 12.1(22)EA8a, RELEASE SOFTWARE (fc1)"=> { :operatingsystem => "IOS", :operatingsystemrelease => "12.1(22)EA8a", :operatingsystemmajrelease => "12.1", :operatingsystemfeature => "I6K2L2Q4"},
    # "Cisco IOS Software, C2960 Software (C2960-LANBASEK9-M), Version 12.2(44)SE, RELEASE SOFTWARE (fc1)"=>{ :operatingsystem => "IOS", :operatingsystemrelease => "12.2(44)SE", :operatingsystemmajrelease => "12.2", :operatingsystemfeature => "LANBASEK9"},
    # "Cisco IOS Software, C870 Software (C870-ADVIPSERVICESK9-M), Version 12.4(11)XJ4, RELEASE SOFTWARE (fc2)"=>{ :operatingsystem => "IOS", :operatingsystemrelease => "12.4(11)XJ40", :operatingsystemmajrelease => "12.4XJ", :operatingsystemfeature => "ADVIPSERVICESK9"},
    # "Cisco IOS Software, 1841 Software (C1841-ADVSECURITYK9-M), Version 12.4(24)T4, RELEASE SOFTWARE (fc2)" =>{ :operatingsystem => "IOS", :operatingsystemrelease => "12.4(24)T4", :operatingsystemmajrelease => "12.4T", :operatingsystemfeature => "ADVSECURITYK9"},
    when /(?:Cisco )?(IOS)\s*(?:\(tm\) |Software, )?(?:\w+)\s+Software\s+\(\w+-(\w+)-\w+\), Version ([0-9.()A-Za-z]+),/
      facts[:operatingsystem] = $1
      facts[:operatingsystemrelease] = $3
      facts[:operatingsystemmajrelease] = ios_major_version(facts[:operatingsystemrelease])
      facts[:operatingsystemfeature] = $2
    end
  end
  facts
end

#retrieveObject



14
15
16
17
# File 'lib/puppet/util/network_device/cisco/facts.rb', line 14

def retrieve
  facts = {}
  facts.merge(parse_show_ver)
end

#uptime_to_seconds(uptime) ⇒ Object



65
66
67
68
69
70
# File 'lib/puppet/util/network_device/cisco/facts.rb', line 65

def uptime_to_seconds(uptime)
  captures = (uptime.match /^(?:(\d+) years?,)?\s*(?:(\d+) weeks?,)?\s*(?:(\d+) days?,)?\s*(?:(\d+) hours?,)?\s*(\d+) minutes?$/).captures
  captures.zip([31536000, 604800, 86400, 3600, 60]).inject(0) do |total, (x,y)|
    total + (x.nil? ? 0 : x.to_i * y)
  end
end