Steve McIntyre | e5043dd | 2014-12-10 16:49:28 +0000 | [diff] [blame^] | 1 | #! /usr/bin/python |
| 2 | |
| 3 | # Copyright 2014 Linaro Limited |
| 4 | # |
| 5 | # This program is free software; you can redistribute it and/or modify |
| 6 | # it under the terms of the GNU General Public License as published by |
| 7 | # the Free Software Foundation; either version 2 of the License, or |
| 8 | # (at your option) any later version. |
| 9 | # |
| 10 | # This program is distributed in the hope that it will be useful, |
| 11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | # GNU General Public License for more details. |
| 14 | # |
| 15 | # You should have received a copy of the GNU General Public License |
| 16 | # along with this program; if not, write to the Free Software |
| 17 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
| 18 | # MA 02110-1301, USA. |
| 19 | # |
| 20 | # Main VLANd module |
| 21 | # |
| 22 | |
| 23 | import os, sys, types |
| 24 | import time |
| 25 | import logging |
| 26 | |
| 27 | vlandpath = os.path.abspath(os.path.normpath(os.path.dirname(sys.argv[0]))) |
| 28 | sys.path.insert(0, vlandpath) |
| 29 | |
| 30 | import drivers |
| 31 | from config.config import VlanConfig |
| 32 | from db.db import VlanDB |
| 33 | from errors import CriticalError, InputError, ConfigError |
| 34 | |
| 35 | version = "0.0.0-DEV" |
| 36 | banner = "Linaro VLANd version %s" % version |
| 37 | |
| 38 | def get_switch_driver(switch): |
| 39 | logging.debug("Trying to find a driver for %s" % switch) |
| 40 | driver = config.switches[switch].driver |
| 41 | logging.debug("Driver: %s" % driver) |
| 42 | module = __import__("drivers.%s" % driver, fromlist=[driver]) |
| 43 | class_ = getattr(module, driver) |
| 44 | return class_(switch) |
| 45 | |
| 46 | print '%s' % banner |
| 47 | |
| 48 | print 'Parsing Config...' |
| 49 | config = VlanConfig(filenames=('./vland.cfg',)) |
| 50 | print ' Config knows about %d switches' % len(config.switches) |
| 51 | |
| 52 | print 'Connecting to DB...' |
| 53 | db = VlanDB(db_name=config.database.dbname, username=config.database.username) |
| 54 | |
| 55 | switches = db.all_switches() |
| 56 | print ' DB knows about %d switches' % len(switches) |
| 57 | ports = db.all_ports() |
| 58 | print ' DB knows about %d ports' % len(ports) |
| 59 | vlans = db.all_vlans() |
| 60 | print ' DB knows about %d vlans' % len(vlans) |
| 61 | |
| 62 | for switch in sorted(config.switches): |
| 63 | print "Found switch %s:" % (switch) |
| 64 | |
| 65 | print " Probing:" |
| 66 | |
| 67 | s = get_switch_driver(switch) |
| 68 | s.switch_connect(config.switches[switch].username, config.switches[switch].password) |
| 69 | print " Found details of switch:" |
| 70 | s._dump_list(s._systemdata) |
| 71 | print " Switch has %d ports:" % len(s.switch_get_port_names()) |
| 72 | for port in s.switch_get_port_names(): |
| 73 | print " %s" % port |
| 74 | if 0 == 1: |
| 75 | mode = s.port_get_mode(port) |
| 76 | if mode == "trunk": |
| 77 | print " port %s is in trunk mode, VLAN(s):" % port |
| 78 | vlans = s.port_get_trunk_vlan_list(port) |
| 79 | for vlan in vlans: |
| 80 | name = s.vlan_get_name(vlan) |
| 81 | print " %d (%s)" % (vlan, name) |
| 82 | else: |
| 83 | vlan = s.port_get_access_vlan(port) |
| 84 | name = s.vlan_get_name(vlan) |
| 85 | print " port %s is in access mode, VLAN %d (%s):" % (port, vlan, name) |
| 86 | |
| 87 | s.switch_disconnect() |
| 88 | del(s) |
| 89 | |