srikanth | 116e6e8 | 2014-08-19 07:22:37 -0700 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | # |
| 3 | # Copyright (c) 2013 Big Switch Networks, Inc. |
| 4 | # |
| 5 | # Licensed under the Eclipse Public License, Version 1.0 (the |
| 6 | # "License"); you may not use this file except in compliance with the |
| 7 | # License. You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.eclipse.org/legal/epl-v10.html |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 14 | # implied. See the License for the specific language governing |
| 15 | # permissions and limitations under the License. |
| 16 | # |
| 17 | # |
| 18 | # Python script for querying API and displaying connected hosts in a table |
| 19 | # |
| 20 | # |
| 21 | |
| 22 | #Importing modules |
| 23 | import re |
| 24 | import sys |
| 25 | import time |
| 26 | import datetime |
| 27 | import json |
| 28 | import urllib2 |
| 29 | import switchalias |
| 30 | from sdncon.rest.views import do_switches, do_model_list, do_instance, do_device |
| 31 | |
| 32 | def show_host_data(request): |
| 33 | |
| 34 | # Query JSON from API and load into dictionary |
| 35 | switches = json.loads(do_switches(request).content) |
| 36 | switchdevices = json.loads(do_device(request).content) |
| 37 | |
| 38 | # Dictionaries |
| 39 | sorteddict = [] |
| 40 | aliasDict = switchalias.aliasDict(request) |
| 41 | unsortdict = [] |
| 42 | |
| 43 | # Step through master 'device' list, extract entry for each dictionary. |
| 44 | for index_devices,value1_devices in enumerate(switchdevices): |
| 45 | tempdict = {} |
| 46 | tempswitchdict = {} |
| 47 | |
| 48 | # get needed entries in 'devices' |
| 49 | tempdict['mac'] = value1_devices.get('mac','') |
| 50 | tempdict['entityClass'] = value1_devices.get('entityClass','') |
| 51 | tempdict['vlan'] = value1_devices.get('vlan','') |
| 52 | tempdict['ipv4'] = value1_devices.get('ipv4','Unknown') |
| 53 | tempdict['switch'] = value1_devices.get('attachmentPoint','') |
| 54 | tempdict['lastSeen'] = value1_devices.get('lastSeen','') |
| 55 | |
| 56 | # append to final sorted output. |
| 57 | unsortdict.append(tempdict) |
| 58 | |
| 59 | |
| 60 | sorteddict = sorted(unsortdict, key=lambda elem: "%s" % (elem['mac'])) |
| 61 | |
| 62 | #print sorteddict |
| 63 | #print time.strftime('%Y-%m-%d %H:%M:%S %Z', time.gmtime(sorteddict[0]['connectedSince'] / float(1000))) |
| 64 | |
| 65 | result = '' |
| 66 | # Print table output |
| 67 | result += '<table id="showdeviceoutput" >' |
| 68 | result += '<tbody style="border-top: 0px;">' |
| 69 | result += '<tr><td>ID</td><td>MAC Address</td><td>Address Space</td><td>VLAN</td><td>IP</td><td>Switch</td><td>Last Seen</td></tr>' |
| 70 | for index_output,value_output in enumerate(sorteddict): |
| 71 | result += '<tr>' |
| 72 | result += ' <td>' + str(index_output + 1) + '</td>' |
| 73 | result += ' <td>' |
| 74 | for tmp_index,tmp_value in enumerate(value_output['mac']): |
| 75 | if tmp_index > 0: |
| 76 | result += ', ', |
| 77 | result += str(tmp_value) |
| 78 | result += '</td>' |
| 79 | result += ' <td>' + value_output.get('entityClass','') + '</td>' |
| 80 | result += ' <td>' |
| 81 | for tmp_index,tmp_value in enumerate(value_output['vlan']): |
| 82 | if tmp_index > 0: |
| 83 | result += ', ', |
| 84 | result += str(tmp_value) |
| 85 | result += '</td>' |
| 86 | result += ' <td>' |
| 87 | for tmp_index,tmp_value in enumerate(value_output['ipv4']): |
| 88 | if tmp_index > 0: |
| 89 | result += ', ', |
| 90 | result += str(tmp_value) |
| 91 | result += '</td>' |
| 92 | result += ' <td>' |
| 93 | for tmp_index,tmp_value in enumerate(value_output['switch']): |
| 94 | if tmp_index > 0: |
| 95 | result += ', ', |
| 96 | result += aliasDict.get(tmp_value.get('switchDPID', 'UNKNOWN'), tmp_value.get('switchDPID', 'UNKNOWN')) + ' Port ' + str(tmp_value.get('port', 'UNKNOWN')) |
| 97 | result += '</td>' |
| 98 | delta = round(time.time(),0) - (round(value_output.get('lastSeen',time.time()) / int(1000))) |
| 99 | if delta <= 0: |
| 100 | result += ' <td> Now </td>' |
| 101 | else: |
| 102 | result += ' <td>' + str(datetime.timedelta(seconds=delta)) + '</td>' |
| 103 | result += '</tr>' |
| 104 | result += '</tbody>' |
| 105 | result += '</table>' |
| 106 | return result |