Faidon Liambotis has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/354084 )

Change subject: Rewrite the LLDP fact(s)
......................................................................

Rewrite the LLDP fact(s)

Rewrite the LLDP fact, removing all of the existing string facts and
adding the following instead:

* "lldp", a structured fact resulting in a hash that contains
interfaces, each of which contain a neighbor, a port and a VLAN. Note
that we are not returning port/id (lldpswportid) anymore, as this was of
limited usefulness (e.g. on Junipers it returned the internal port
number).

* "lldp_parent", a fact returning a string with the LLDP neighbor of the
primary interface (interface_primary, in our setup).

* "lldp_neighbors", a fact returning an array with the LLDP neighbors
across all interfaces.

Note that since "lldp" is a structured fact, it will only work with
Facter >= 2.0 and will be stringified by Puppet in our setup. Also note
that since "lldp_parent" and "lldp_neighbors" rely on "lldp", they
implicitly require Facter >= 2.0 as well (so >= jessie).

Change-Id: Ia156007346d7938a3f5a065fd8953f853748c314
---
M modules/base/lib/facter/lldp.rb
M modules/monitoring/manifests/host.pp
2 files changed, 66 insertions(+), 52 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/operations/puppet 
refs/changes/84/354084/1

diff --git a/modules/base/lib/facter/lldp.rb b/modules/base/lib/facter/lldp.rb
index 32abe04..0d5d956 100644
--- a/modules/base/lib/facter/lldp.rb
+++ b/modules/base/lib/facter/lldp.rb
@@ -1,58 +1,72 @@
 require 'facter'
 require 'rexml/document'
 
-if Facter.value('virtual') == 'physical' && File.exists?('/usr/sbin/lldpctl')
+Facter.add(:lldp) do
+  confine :kernel => %w{Linux FreeBSD OpenBSD}
+  confine :virtual => "physical" # TODO: actually needed?
+  confine do
+    File.exists?('/usr/sbin/lldpctl')
+  end
 
-    lldppeers = nil
-
+  setcode do
+    lldp = {}
     data = Facter::Util::Resolution.exec('/usr/sbin/lldpctl -f xml')
     document = REXML::Document.new(data)
-    document.elements.each('lldp/interface') do |iface|
-        eth = iface.attributes['name']
-        iface.elements.each('chassis/name') do |switch|
-            Facter.add('lldppeer_%s' % eth) do
-                confine :kernel => %w{Linux FreeBSD OpenBSD}
-                setcode do
-                    switch.text
-                end
-            end
-            if lldppeers
-                lldppeers = lldppeers + ',' + switch.text
-            else
-                lldppeers = switch.text
-            end
-        end
-        iface.elements.each('port/descr') do |port|
-            Facter.add('lldpswport_%s' % eth) do
-                confine :kernel => %w{Linux FreeBSD OpenBSD}
-                setcode do
-                    port.text
-                end
-            end
-        end
-        iface.elements.each('port/id') do |port|
-            Facter.add('lldpswportid_%s' % eth) do
-                confine :kernel => %w{Linux FreeBSD OpenBSD}
-                setcode do
-                    port.text
-                end
-            end
-        end
-        iface.elements.each('vlan') do |vlan|
-            Facter.add('lldpswport_vlan_%s' % eth) do
-                confine :kernel => %w{Linux FreeBSD OpenBSD}
-                setcode do
-                    vlan.text
-                end
-            end
-        end
+
+    document.elements.each('lldp/interface') do |interface|
+      eth = interface.attributes['name']
+      lldp[eth] = {}
+
+      interface.elements.each('chassis/name') do |switch|
+        lldp[eth]['neighbor'] = switch.text
+      end
+      interface.elements.each('port/descr') do |port|
+        lldp[eth]['port'] = port.text
+      end
+      interface.elements.each('vlan') do |vlan|
+        lldp[eth]['vlan'] = vlan.text
+      end
     end
 
-    # Aggregate all the lldp peers on one single variable
-    Facter.add('lldppeers') do
-        confine :kernel => %w{Linux FreeBSD OpenBSD}
-        setcode do
-            lldppeers
-        end
+    lldp
+  end
+end
+
+Facter.add(:lldp_parent) do
+  confine :kernel => %w{Linux FreeBSD OpenBSD}
+
+  setcode do
+    begin
+      # Facter 3
+      primary = Facter.value(:networking)['primary']
+    rescue
+      # fallback to our own implementation
+      primary = Facter.value(:interface_primary)
     end
+
+    begin
+      Facter.value(:lldp)[primary]['neighbor']
+    rescue
+      nil
+    end
+  end
+end
+
+Facter.add(:lldp_neighbors) do
+  confine :kernel => %w{Linux FreeBSD OpenBSD}
+  confine do
+    !Facter.value(:lldp).nil?
+  end
+
+  setcode do
+    neighbors = []
+    Facter.value(:lldp).each do |interface, values|
+      neighbor = values['neighbor']
+      if neighbor
+        neighbors.push(neighbor)
+      end
+    end
+
+    neighbors
+  end
 end
diff --git a/modules/monitoring/manifests/host.pp 
b/modules/monitoring/manifests/host.pp
index f121a4d..eec1e4b 100644
--- a/modules/monitoring/manifests/host.pp
+++ b/modules/monitoring/manifests/host.pp
@@ -54,10 +54,10 @@
         # functionality in the case of an exported host
         if $parents {
             $real_parents = $parents
-        } elsif $facts['lldppeer_eth0'] {
-            # TODO: Make this better by getting all LLDP peers on all physical 
(only!) interfaces
-            # map() would have been great for this.
-            $real_parents = $facts['lldppeer_eth0']
+        } elsif $facts['lldp_parent'] {
+            # we could use lldp_neighbors here, but not all of our neighbors
+            # are necessarily our parents, so just use the lldp_parent alone
+            $real_parents = $facts['lldp_parent']
         } else {
             $real_parents = undef
         }

-- 
To view, visit https://gerrit.wikimedia.org/r/354084
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ia156007346d7938a3f5a065fd8953f853748c314
Gerrit-PatchSet: 1
Gerrit-Project: operations/puppet
Gerrit-Branch: production
Gerrit-Owner: Faidon Liambotis <fai...@wikimedia.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to