Browse Source

Create a more robust way of importing logical switches.

Joe Clarke 9 years ago
parent
commit
f240740acb
1 changed files with 100 additions and 38 deletions
  1. 100 38
      scripts/importsw.pl

+ 100 - 38
scripts/importsw.pl

@@ -29,24 +29,33 @@ open(IN, "/home/jclarke/invsw.csv")
 
 my @contents = <IN>;
 close(IN);
+my $header = $contents[0];
+chomp $header;
+my @elements = split(/,/, $header);
 for (my $i = 1 ; $i < scalar(@contents) ; $i++) {
+        my %field = (
+                'Hostname' => '',
+                'Ports'    => '',
+                'Serial'   => '',
+                'Location' => '',
+                'MDF'      => '',
+                'IDF'      => '',
+                'Template' => '',
+                'IP'       => '',
+        );
+
         my $line = $contents[$i];
         chomp $line;
-        my ($hostname, $model, $location, $mdf, $serial, $port_req, $ip) =
-            split(/,/, $line);
-        $hostname = trim($hostname);
-        $model    = trim($model);
-        $location = trim($location);
-        $mdf      = trim($mdf);
-        $ip       = trim($ip);
-        $serial   = trim($serial);
-        $port_req = trim($port_req);
-
-	my $query = '';
-	my ($sth, $res, $ref, @params);
-
-        if ($hostname ne '') {
-                if ($ip ne '') {
+        my @fields = split(/,/, $line);
+        for (my $j = 0 ; $j < scalar(@elements) ; $j++) {
+                $field{$elements[$j]} = trim($fields[$j]);
+        }
+
+        my $query = '';
+        my ($sth, $res, $ref, @params);
+
+        if ($field{'Hostname'} ne '') {
+                if ($field{'IP'} ne '') {
                         $query =
                             'SELECT address FROM ADDRESSES WHERE address = ?';
                         $sth = $dbh->prepare($query);
@@ -56,7 +65,7 @@ for (my $i = 1 ; $i < scalar(@contents) ; $i++) {
                                     . $dbh->errstr . "\n";
                                 next;
                         }
-                        @params = ($ip);
+                        @params = ($field{'IP'});
                         $res    = $sth->execute(@params);
                         if (!$res) {
                                 print STDERR
@@ -67,7 +76,7 @@ for (my $i = 1 ; $i < scalar(@contents) ; $i++) {
                         $ref = $sth->fetchrow_hashref;
                         if (!$ref) {
                                 print STDERR
-                                    "ERROR: No matching IP address record for $ip\n";
+                                    "ERROR: No matching IP address record for $field{'IP'}\n";
                                 print STDERR "XXX: Line is '$line'\n";
                                 next;
                         }
@@ -79,7 +88,7 @@ for (my $i = 1 ; $i < scalar(@contents) ; $i++) {
                                     . $dbh->errstr . "\n";
                                 next;
                         }
-                        @params = ($ref->{'address'}, $hostname);
+                        @params = ($ref->{'address'}, $field{'Hostname'});
                         $res = $sth->execute(@params);
                         if (!$res) {
                                 print STDERR
@@ -90,7 +99,7 @@ for (my $i = 1 ; $i < scalar(@contents) ; $i++) {
                         if ($sth->rows != 0) {
                                 my $r = $sth->fetchrow_hashref;
                                 print STDERR
-                                    "Duplicate IP detected for IP $ip, hostname $hostname (current device is "
+                                    "Duplicate IP detected for IP $field{'IP'}, hostname $field{'Hostname'} (current device is "
                                     . $r->{'name'} . ")\n";
                                 $query =
                                     'UPDATE ADDRESSES SET used=? WHERE address=?';
@@ -101,7 +110,7 @@ for (my $i = 1 ; $i < scalar(@contents) ; $i++) {
                                             . $dbh->errstr . "\n";
                                         next;
                                 }
-                                @params = ('1', $ip);
+                                @params = ('1', $field{'IP'});
                                 $res = $sth->execute(@params);
                                 if (!$res) {
                                         print STDERR
@@ -111,10 +120,32 @@ for (my $i = 1 ; $i < scalar(@contents) ; $i++) {
                                 }
                                 next;
                         }
-                        $ip = $ref->{'address'};
+                        $field{'IP'} = $ref->{'address'};
                 } else {
                         $query =
                             'SELECT address FROM ADDRESSES WHERE location=? AND used=? LIMIT 1';
+                        $query =
+                            'SELECT ip_address FROM SWITCHES WHERE name = ?';
+                        $sth = $dbh->prepare($query);
+                        if (!$sth) {
+                                print STDERR
+                                    "Failed to prepare query '$query': "
+                                    . $dbh->errstr . "\n";
+                                next;
+                        }
+                        @params = ($field{'IDF'});
+                        $res    = $sth->execute(@params);
+                        if (!$res) {
+                                print STDERR
+                                    "Failed to execute query '$query': "
+                                    . $dbh->errstr . "\n";
+                                next;
+                        }
+                        my $r = $sth->fetchrow_hashref;
+                        my ($subnet) =
+                            ($r->{'ip_address'} =~ /^(\d+\.\d+\.\d+\.)/);
+                        $query =
+                            'SELECT address FROM ADDRESSES WHERE location = ? AND address LIKE ? AND used = ? AND reserved = ? ORDER BY INET_ATON(address) LIMIT 1';
                         $sth = $dbh->prepare($query);
                         if (!$sth) {
                                 print STDERR
@@ -122,7 +153,7 @@ for (my $i = 1 ; $i < scalar(@contents) ; $i++) {
                                     . $dbh->errstr . "\n";
                                 next;
                         }
-                        @params = ($mdf, '0');
+                        @params = ($field{'MDF'}, $subnet . '%', '0', '0');
                         $res = $sth->execute(@params);
                         if (!$res) {
                                 print STDERR
@@ -130,8 +161,8 @@ for (my $i = 1 ; $i < scalar(@contents) ; $i++) {
                                     . $dbh->errstr . "\n";
                                 next;
                         }
-                        my $r  = $sth->fetchrow_hashref;
-                        $ip = $r->{'address'};
+                        $r = $sth->fetchrow_hashref;
+                        $field{'IP'} = $r->{'address'};
                 }
                 $query = 'UPDATE ADDRESSES SET used=? WHERE address=?';
                 $sth   = $dbh->prepare($query);
@@ -140,7 +171,7 @@ for (my $i = 1 ; $i < scalar(@contents) ; $i++) {
                             . $dbh->errstr . "\n";
                         next;
                 }
-                @params = ('1', $ip);
+                @params = ('1', $field{'IP'});
                 $res = $sth->execute(@params);
                 if (!$res) {
                         print STDERR "Failed to execute query '$query': "
@@ -156,8 +187,8 @@ for (my $i = 1 ; $i < scalar(@contents) ; $i++) {
                         next;
                 }
 
-                @params = ('0', $hostname);
-                $res    = $sth->execute(@params);
+                @params = ('0', $field{'Hostname'});
+                $res = $sth->execute(@params);
                 if (!$res) {
                         print STDERR "Failed to execute query '$query': "
                             . $dbh->errstr . "\n";
@@ -172,14 +203,17 @@ for (my $i = 1 ; $i < scalar(@contents) ; $i++) {
                         next;
                 }
 
-                if ($model =~ /3750[xX]/) {
+                my $model;
+                if ($field{'Ports'} == 24 || $field{'Ports'} == 48) {
                         $model = $PID_3750;
                 } else {
-                        $model    = $PID_3560;
-                        $port_req = '8';
+                        $model = $PID_3560;
                 }
 
-                @params = ($hostname, $ip, $model, $location, $port_req);
+                @params = (
+                        $field{'Hostname'}, $field{'IP'}, $model,
+                        $field{'Location'}, $field{'Ports'}
+                );
                 $res = $sth->execute(@params);
                 if (!$res) {
                         print STDERR "Failed to insert switch record: "
@@ -187,7 +221,35 @@ for (my $i = 1 ; $i < scalar(@contents) ; $i++) {
                         next;
                 }
 
-                if ($serial ne '') {
+                if ($field{'Template'} ne '') {
+                        $query = 'SELECT path FROM EXCEPTIONS WHERE name = ?';
+                        $sth   = $dbh->prepare($query);
+                        if (!$sth) {
+                                print STDERR "Failed to prepare query '$query':"
+                                    . $dbh->errstr . "\n";
+                                next;
+                        }
+                        @params = ($field{'Template'});
+                        $res    = $sth->execute(@params);
+                        if (!$res) {
+                                print STDERR "Failed to execute query '$query':"
+                                    . $dbh->errstr . "\n";
+                                next;
+                        }
+                        if ($sth->rows == 0) {
+                                print STDERR
+                                    "Unable to find $field{'Template'} in EXCEPTIONS\n";
+                                next;
+                        }
+
+                        my $r = $sth->fetchrow_hashref;
+                        symlink($r->{'path'},
+                                      '/tftpboot/'
+                                    . $field{'Hostname'}
+                                    . '-ports.tmpl');
+                }
+
+                if ($field{'Serial'} ne '') {
                         $query =
                             'SELECT serial_number, max_ports FROM DEVICE_MAP WHERE serial_number=?';
                         $sth = $dbh->prepare($query);
@@ -197,7 +259,7 @@ for (my $i = 1 ; $i < scalar(@contents) ; $i++) {
                                 next;
                         }
 
-                        @params = ($serial);
+                        @params = ($field{'Serial'});
                         $res    = $sth->execute(@params);
                         if (!$res) {
                                 print STDERR
@@ -208,13 +270,13 @@ for (my $i = 1 ; $i < scalar(@contents) ; $i++) {
 
                         if ($sth->rows == 0) {
                                 print STDERR
-                                    "Unable to find $serial in DEVICE_MAP\n";
+                                    "Unable to find $field{'Serial'} in DEVICE_MAP\n";
                                 next;
                         } else {
                                 my $r = $sth->fetchrow_hashref;
-                                if ($r->{'max_ports'} ne $port_req) {
+                                if ($r->{'max_ports'} ne $field{'Ports'}) {
                                         print STDERR
-                                            "Ports required for $hostname do not match max ports provided by $serial (required: $port_req, provided: "
+                                            "Ports required for $field{'Hostname'} do not match max ports provided by $field{'Serial'} (required: $field{'Ports'}, provided: "
                                             . $r->{'max_ports'} . ")\n";
                                         next;
                                 }
@@ -229,11 +291,11 @@ for (my $i = 1 ; $i < scalar(@contents) ; $i++) {
                                 next;
                         }
 
-                        @params = ($hostname, '1', $serial);
+                        @params = ($field{'Hostname'}, '1', $field{'Serial'});
                         $res = $sth->execute(@params);
                         if (!$res) {
                                 print STDERR
-                                    "Failed to execute query '$query' for ($hostname, $serial): "
+                                    "Failed to execute query '$query' for ($field{'Hostname'}, $field{'Serial'}): "
                                     . $dbh->errstr . "\n";
                                 next;
                         }