Bladeren bron

Fix up with black and modernize.

Joe Clarke 4 jaren geleden
bovenliggende
commit
db5d8c56ac
2 gewijzigde bestanden met toevoegingen van 87 en 79 verwijderingen
  1. 69 70
      automation/network/poll_macs.py
  2. 18 9
      automation/network/poll_tcam.py

+ 69 - 70
automation/network/poll_macs.py

@@ -1,6 +1,6 @@
 #!/usr/bin/env python3
 #
-# Copyright (c) 2017-2019  Joe Clarke <jclarke@cisco.com>
+# Copyright (c) 2017-2020  Joe Clarke <jclarke@cisco.com>
 # All rights reserved.
 #
 # Redistribution and use in source and binary forms, with or without
@@ -25,6 +25,8 @@
 # SUCH DAMAGE.
 
 
+from builtins import str
+from builtins import range
 import os
 import re
 import sys
@@ -34,70 +36,60 @@ import paramiko
 import CLEUCreds
 
 
-CACHE_FILE = '/home/jclarke/mac_counts.dat'
-CACHE_FILE_TMP = CACHE_FILE + '.tmp'
+CACHE_FILE = "/home/jclarke/mac_counts.dat"
+CACHE_FILE_TMP = CACHE_FILE + ".tmp"
 
 commands = [
-  {
-    'command': 'show mac address-table count | inc Dynamic Address Count',
-    'pattern': r'Dynamic Address Count:\s+(\d+)',
-    'metric': 'totalMacs',
-    'devices': ['core1-l3c', 'core2-l3c']
-  },
-  {
-    'command': 'show mac address-table dynamic | inc Total',
-    'pattern': r'Total.*: (\d+)',
-    'metric': 'totalMacs',
-    'devicePatterns': [
-      {
-        'pattern': '10.127.0.{}',
-        'range': {
-          'min': 1,
-          'max': 60
-        }
-      }
-    ]
-  },
-  {
-    'command': 'show ip arp summary | inc IP ARP',
-    'pattern': r'(\d+) IP ARP entries',
-    'metric': 'arpEntries',
-    'devicePatterns': [
-      {
-        'pattern': '10.127.0.{}',
-        'range': {
-          'min': 1,
-          'max': 60
-        }
-      }
-    ]
-  }
+    {
+        "command": "show mac address-table count | inc Dynamic Address Count",
+        "pattern": r"Dynamic Address Count:\s+(\d+)",
+        "metric": "totalMacs",
+        "devices": ["core1-l3c", "core2-l3c"],
+    },
+    {
+        "command": "show mac address-table dynamic | inc Total",
+        "pattern": r"Total.*: (\d+)",
+        "metric": "totalMacs",
+        "devicePatterns": [{"pattern": "10.127.0.{}", "range": {"min": 1, "max": 60}}],
+    },
+    {
+        "command": "show ip arp summary | inc IP ARP",
+        "pattern": r"(\d+) IP ARP entries",
+        "metric": "arpEntries",
+        "devicePatterns": [{"pattern": "10.127.0.{}", "range": {"min": 1, "max": 60}}],
+    },
 ]
 
 
+def send_command(chan, command):
+    chan.sendall(command + "\n")
+    i = 0
+    output = ""
+    while i < 10:
+        if chan.recv_ready():
+            break
+        i += 1
+        time.sleep(i * 0.5)
+    while chan.recv_ready():
+        r = chan.recv(131070).decode("utf-8")
+        output = output + r
+
+    return output
+
+
 def get_results(ssh_client, ip, command, pattern, metric):
-    response = ''
+    response = ""
     try:
-        ssh_client.connect(ip, username=CLEUCreds.NET_USER, password=CLEUCreds.NET_PASS,
-                           timeout=5, allow_agent=False, look_for_keys=False)
+        ssh_client.connect(ip, username=CLEUCreds.NET_USER, password=CLEUCreds.NET_PASS, timeout=5, allow_agent=False, look_for_keys=False)
         chan = ssh_client.invoke_shell()
-        output = ''
+        output = ""
         try:
-            chan.sendall('term length 0\n')
-            chan.sendall('term width 0\n')
-            chan.sendall('{}\n'.format(command))
-            j = 0
-            while j < 10:
-                if chan.recv_ready():
-                    break
-                time.sleep(.5)
-                j += 1
-            while chan.recv_ready():
-                output += chan.recv(65535)
+            send_command(chan, "term length 0")
+            send_command(chan, "term width 0")
+            output = send_command(chan, command)
         except Exception as ie:
             response = '{}{{idf="{}"}}'.format(metric, ip)
-            sys.stderr.write(
-                'Failed to get MACs from {}: {}\n'.format(ip, ie))
+            sys.stderr.write("Failed to get MACs from {}: {}\n".format(ip, ie))
             return response
 
         m = re.search(pattern, output)
@@ -107,8 +99,8 @@ def get_results(ssh_client, ip, command, pattern, metric):
             response = '{}{{idf="{}"}} 0'.format(metric, ip)
     except Exception as e:
         ssh_client.close()
-        sys.stderr.write('Failed to connect to {}: {}\n'.format(ip, e))
-        return ''
+        sys.stderr.write("Failed to connect to {}: {}\n".format(ip, e))
+        return ""
 
     ssh_client.close()
 
@@ -123,26 +115,33 @@ def get_metrics():
     ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
 
     for command in commands:
-        if 'devices' in command:
-            for device in command['devices']:
-                response.append(get_results(ssh_client, device, command['command'], command['pattern'], command['metric']))
+        if "devices" in command:
+            for device in command["devices"]:
+                response.append(get_results(ssh_client, device, command["command"], command["pattern"], command["metric"]))
         else:
-            for pattern in command['devicePatterns']:
-                if 'range' in pattern:
-                    for i in range(pattern['range']['min'], pattern['range']['max']):
-                        response.append(get_results(ssh_client, pattern['pattern'].format(str(i)), command[
-                                        'command'], command['pattern'], command['metric']))
+            for pattern in command["devicePatterns"]:
+                if "range" in pattern:
+                    for i in range(pattern["range"]["min"], pattern["range"]["max"]):
+                        response.append(
+                            get_results(
+                                ssh_client, pattern["pattern"].format(str(i)), command["command"], command["pattern"], command["metric"]
+                            )
+                        )
                 else:
-                    for sub in pattern['subs']:
-                        response.append(get_results(ssh_client, pattern['pattern'].format(sub), command[
-                                        'command'], command['pattern'], command['metric']))
+                    for sub in pattern["subs"]:
+                        response.append(
+                            get_results(
+                                ssh_client, pattern["pattern"].format(sub), command["command"], command["pattern"], command["metric"]
+                            )
+                        )
 
     return response
 
-if __name__ == '__main__':
-    response=get_metrics()
 
-    fd=open(CACHE_FILE_TMP, 'w')
+if __name__ == "__main__":
+    response = get_metrics()
+
+    fd = open(CACHE_FILE_TMP, "w")
     json.dump(response, fd, indent=4)
     fd.close()
 

+ 18 - 9
automation/network/poll_tcam.py

@@ -1,6 +1,6 @@
 #!/usr/bin/env python3
 #
-# Copyright (c) 2017-2019  Joe Clarke <jclarke@cisco.com>
+# Copyright (c) 2017-2020  Joe Clarke <jclarke@cisco.com>
 # All rights reserved.
 #
 # Redistribution and use in source and binary forms, with or without
@@ -24,6 +24,8 @@
 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 # SUCH DAMAGE.
 
+from __future__ import division
+from past.utils import old_div
 import os
 import re
 import sys
@@ -44,11 +46,18 @@ CACHE_FILE = "/home/jclarke/tcam_util.json"
 spark = None
 
 
-def exec_command(chan, cmd, dev):
+def send_command(chan, command):
+    chan.sendall(command + "\n")
+    i = 0
     output = ""
-    chan.send(cmd + "\n")
-    time.sleep(1)
-    output += chan.recv(65535)
+    while i < 10:
+        if chan.recv_ready():
+            break
+        i += 1
+        time.sleep(i * 0.5)
+    while chan.recv_ready():
+        r = chan.recv(131070).decode("utf-8")
+        output = output + r
 
     return output
 
@@ -66,11 +75,11 @@ def get_results(dev, cache):
         ssh_client.connect(dev, username=CLEUCreds.NET_USER, password=CLEUCreds.NET_PASS, timeout=5, allow_agent=False, look_for_keys=False)
         chan = ssh_client.invoke_shell()
         try:
-            exec_command(chan, "term width 0", dev)
-            exec_command(chan, "term length 0", dev)
+            send_command(chan, "term width 0")
+            send_command(chan, "term length 0")
             for cmd in commands:
                 try:
-                    output = exec_command(chan, cmd, dev)
+                    output = send_command(chan, cmd)
                 except Exception as iie:
                     sys.stderr.write("Failed to get result for {} from {}: {}\n".format(cmd, dev, iie))
                     traceback.print_exc()
@@ -106,7 +115,7 @@ def get_results(dev, cache):
         used = float(m.group(1))
         m = re.search(r"(\d+)(/\d+)?", max)
         max = float(m.group(1))
-        perc = float(used / max) * 100.0
+        perc = float(old_div(used, max)) * 100.0
         # if metric == 'Directly or indirectly connected routes':
         #    perc = 76.0
         if perc >= 75.0: