Browse Source

Add a set of support scripts used to import data into the noc database.

Joe Clarke 9 years ago
parent
commit
30949da4cb
6 changed files with 510 additions and 0 deletions
  1. 51 0
      scripts/build_ips.pl
  2. 61 0
      scripts/import3560s.pl
  3. 89 0
      scripts/import3750s.pl
  4. 242 0
      scripts/importsw.pl
  5. 20 0
      scripts/syslog-email.pl
  6. 47 0
      scripts/update_dns.pl

+ 51 - 0
scripts/build_ips.pl

@@ -0,0 +1,51 @@
+#!/usr/bin/perl
+
+use strict;
+use DBI;
+use vars qw(%VLANS $DB_USER $DB_PASS);
+require 'dbi.ph';
+
+%VLANS = (
+        'SDCC' =>
+            {'ip' => '10.101', 'vlan' => 'Vlan100', 'gw' => '10.101.0.1',},
+        'NODE' =>
+            {'ip' => '10.102', 'vlan' => 'Vlan100', 'gw' => '10.102.0.1',},
+        'HILTON' =>
+            {'ip' => '10.103', 'vlan' => 'Vlan100', 'gw' => '10.103.0.1',},
+        'HYATT' =>
+            {'ip' => '10.104', 'vlan' => 'Vlan100', 'gw' => '10.104.0.1',},
+);
+
+my $dsn = "DBI:mysql:database=noc";
+
+my $dbh = DBI->connect($dsn, $DB_USER, $DB_PASS, {PrintError => 0})
+    or die "ERROR: Failed to connect to the database.";
+
+foreach my $loc (keys %VLANS) {
+        my $lref = $VLANS{$loc};
+        my $vlan = $lref->{'vlan'};
+        my $ip   = $lref->{'ip'};
+        my $gw   = $lref->{'gw'};
+        for (my $i = 1 ; $i < 256 ; $i++) {
+                for (my $j = 1 ; $j < 255 ; $j++) {
+                        my $sql =
+                            'REPLACE INTO ADDRESSES (address, router, mask, mgmt_vlan, location, used) VALUES (?, ?, ?, ?, ?, ?)';
+                        my $sth = $dbh->prepare($sql);
+                        if (!$sth) {
+                                print STDERR "Failed to prepare query '$sql': "
+                                    . $dbh->errstr . "\n";
+                                next;
+                        }
+                        my @params = (
+                                $ip . ".$i.$j",
+                                $gw, '255.255.0.0', $vlan, $loc, '0'
+                        );
+                        my $res = $sth->execute(@params);
+                        if (!$res) {
+                                print STDERR "Failed to execute query '$sql': "
+                                    . $dbh->errstr . "\n";
+                                next;
+                        }
+                }
+        }
+}

+ 61 - 0
scripts/import3560s.pl

@@ -0,0 +1,61 @@
+#!/usr/bin/perl
+
+use strict;
+use DBI;
+use vars qw($PID $DB_USER $DB_PASS);
+require 'dbi.ph';
+
+sub trim($) {
+        my ($str) = shift;
+
+        $str //= '';
+
+        $str =~ s/^\s+//g;
+        $str =~ s/\s+$//g;
+
+        return $str;
+}
+
+$PID = 'WS-C3560CG-8PC-S';
+
+my $dsn = "DBI:mysql:database=noc";
+
+my $dbh = DBI->connect($dsn, $DB_USER, $DB_PASS, {PrintError => 0})
+    or die "ERROR: Failed to connect to the database.";
+
+open(IN, "/home/jclarke/inv3560.csv")
+    or die "ERROR: Failed to open /home/jclarke/inv3560.csv for reading.";
+
+my @contents = <IN>;
+close(IN);
+for (my $i = 1 ; $i < scalar(@contents) ; $i++) {
+        my $line = $contents[$i];
+        chomp $line;
+        my ($mac, $serial) = split(/,/, $line);
+        $mac    = trim($mac);
+        $serial = trim($serial);
+        if ($mac ne '') {
+                my @mac_parts = ($mac =~ /../g);
+                $mac = lc(join(':', @mac_parts));
+        }
+
+        if ($serial ne '') {
+                my $query =
+                    'REPLACE INTO DEVICE_MAP (serial_number, pid, mac, max_ports) VALUES (?, ?, ?, ?)';
+                my $sth = $dbh->prepare($query);
+                $serial = uc $serial;
+                if (!$sth) {
+                        print STDERR "Failed to insert switch record: "
+                            . $dbh->errstr . "\n";
+                        next;
+                }
+
+                my @params = ($serial, $PID, $mac, '8');
+                my $res = $sth->execute(@params);
+                if (!$res) {
+                        print STDERR "Failed to insert switch record: "
+                            . $dbh->errstr . "\n";
+                        next;
+                }
+        }
+}

+ 89 - 0
scripts/import3750s.pl

@@ -0,0 +1,89 @@
+#!/usr/bin/perl
+
+use strict;
+use DBI;
+use vars qw($PID $DB_USER $DB_PASS);
+require 'dbi.ph';
+
+sub trim($) {
+        my ($str) = shift;
+
+        $str //= '';
+
+        $str =~ s/^\s+//g;
+        $str =~ s/\s+$//g;
+
+        return $str;
+}
+
+$PID = 'WS-C3750X';
+
+my $dsn = "DBI:mysql:database=noc";
+
+my $dbh = DBI->connect($dsn, $DB_USER, $DB_PASS, {PrintError => 0})
+    or die "ERROR: Failed to connect to the database.";
+
+open(IN, "/home/jclarke/inv3750.csv")
+    or die "ERROR: Failed to open /home/jclarke/inv3750.csv for reading.";
+
+my @contents = <IN>;
+close(IN);
+for (my $i = 1 ; $i < scalar(@contents) ; $i++) {
+        my $line = $contents[$i];
+        chomp $line;
+        my ($mac24, undef, $serial24, $serial48, $mac48, undef) =
+            split(/,/, $line);
+        $mac24    = trim($mac24);
+        $serial24 = trim($serial24);
+        $serial48 = trim($serial48);
+        $mac48    = trim($mac48);
+        if ($mac24 ne '') {
+                my @mac_parts = ($mac24 =~ /../g);
+                $mac24 = lc(join(':', @mac_parts));
+        }
+
+        if ($mac48 ne '') {
+                my @mac_parts = ($mac48 =~ /../g);
+                $mac48 = lc(join(':', @mac_parts));
+        }
+
+        if ($serial24 ne '') {
+                my $query =
+                    'REPLACE INTO DEVICE_MAP (serial_number, pid, mac, max_ports) VALUES (?, ?, ?, ?)';
+                my $sth = $dbh->prepare($query);
+                $serial24 = uc $serial24;
+                if (!$sth) {
+                        print STDERR "Failed to insert switch record: "
+                            . $dbh->errstr . "\n";
+                        next;
+                }
+
+                my @params = ($serial24, $PID, $mac24, 24);
+                my $res = $sth->execute(@params);
+                if (!$res) {
+                        print STDERR "Failed to insert switch record: "
+                            . $dbh->errstr . "\n";
+                        next;
+                }
+        }
+
+        if ($serial48 ne '') {
+                my $query =
+                    'REPLACE INTO DEVICE_MAP (serial_number, pid, mac, max_ports) VALUES (?, ?, ?, ?)';
+                my $sth = $dbh->prepare($query);
+                $serial48 = uc $serial48;
+                if (!$sth) {
+                        print STDERR "Failed to insert switch record: "
+                            . $dbh->errstr . "\n";
+                        next;
+                }
+
+                my @params = ($serial48, $PID, $mac48, 48);
+                my $res = $sth->execute(@params);
+                if (!$res) {
+                        print STDERR "Failed to insert switch record: "
+                            . $dbh->errstr . "\n";
+                        next;
+                }
+        }
+}

+ 242 - 0
scripts/importsw.pl

@@ -0,0 +1,242 @@
+#!/usr/bin/perl
+
+use strict;
+use DBI;
+use vars qw($PID_3560 $PID_3750 $DB_USER $DB_PASS);
+require 'dbi.ph';
+
+sub trim($) {
+        my ($str) = shift;
+
+        $str //= '';
+
+        $str =~ s/^\s+//g;
+        $str =~ s/\s+$//g;
+
+        return $str;
+}
+
+$PID_3560 = 'WS-C3560CG-8PC-S';
+$PID_3750 = 'WS-C3750X';
+
+my $dsn = "DBI:mysql:database=noc";
+
+my $dbh = DBI->connect($dsn, $DB_USER, $DB_PASS, {PrintError => 0})
+    or die "ERROR: Failed to connect to the database.";
+
+open(IN, "/home/jclarke/invsw.csv")
+    or die "ERROR: Failed to open /home/jclarke/invsw.csv for reading.";
+
+my @contents = <IN>;
+close(IN);
+for (my $i = 1 ; $i < scalar(@contents) ; $i++) {
+        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 '') {
+                        $query =
+                            'SELECT address FROM ADDRESSES WHERE address = ?';
+                        $sth = $dbh->prepare($query);
+                        if (!$sth) {
+                                print STDERR
+                                    "ERROR: Failed to prepare query '$query': "
+                                    . $dbh->errstr . "\n";
+                                next;
+                        }
+                        @params = ($ip);
+                        $res    = $sth->execute(@params);
+                        if (!$res) {
+                                print STDERR
+                                    "ERROR: Failed to execute query '$query': "
+                                    . $dbh->errstr . "\n";
+                                next;
+                        }
+                        $ref = $sth->fetchrow_hashref;
+                        if (!$ref) {
+                                print STDERR
+                                    "ERROR: No matching IP address record for $ip\n";
+                                print STDERR "XXX: Line is '$line'\n";
+                                next;
+                        }
+                        $query =
+                            'SELECT name FROM SWITCHES WHERE ip_address = ? AND name != ?';
+                        $sth = $dbh->prepare($query);
+                        if (!$sth) {
+                                print STDERR "Failed to prepare '$query': "
+                                    . $dbh->errstr . "\n";
+                                next;
+                        }
+                        @params = ($ref->{'address'}, $hostname);
+                        $res = $sth->execute(@params);
+                        if (!$res) {
+                                print STDERR
+                                    "Failed to execute query '$query': "
+                                    . $dbh->errstr . "\n";
+                                next;
+                        }
+                        if ($sth->rows != 0) {
+                                my $r = $sth->fetchrow_hashref;
+                                print STDERR
+                                    "Duplicate IP detected for IP $ip, hostname $hostname (current device is "
+                                    . $r->{'name'} . ")\n";
+                                $query =
+                                    'UPDATE ADDRESSES SET used=? WHERE address=?';
+                                $sth = $dbh->prepare($query);
+                                if (!$sth) {
+                                        print STDERR
+                                            "Failed to prepare query '$query': "
+                                            . $dbh->errstr . "\n";
+                                        next;
+                                }
+                                @params = ('1', $ip);
+                                $res = $sth->execute(@params);
+                                if (!$res) {
+                                        print STDERR
+                                            "Failed to execute query '$query': "
+                                            . $dbh->errstr . "\n";
+                                        next;
+                                }
+                                next;
+                        }
+                        $ip = $ref->{'address'};
+                } else {
+                        $query =
+                            'SELECT address FROM ADDRESSES WHERE location=? AND used=? LIMIT 1';
+                        $sth = $dbh->prepare($query);
+                        if (!$sth) {
+                                print STDERR
+                                    "Failed to prepare query '$query': "
+                                    . $dbh->errstr . "\n";
+                                next;
+                        }
+                        @params = ($mdf, '0');
+                        $res = $sth->execute(@params);
+                        if (!$res) {
+                                print STDERR
+                                    "Failed to execute query '$query': "
+                                    . $dbh->errstr . "\n";
+                                next;
+                        }
+                        my $r  = $sth->fetchrow_hashref;
+                        $ip = $r->{'address'};
+                }
+                $query = 'UPDATE ADDRESSES SET used=? WHERE address=?';
+                $sth   = $dbh->prepare($query);
+                if (!$sth) {
+                        print STDERR "Failed to prepare query '$query': "
+                            . $dbh->errstr . "\n";
+                        next;
+                }
+                @params = ('1', $ip);
+                $res = $sth->execute(@params);
+                if (!$res) {
+                        print STDERR "Failed to execute query '$query': "
+                            . $dbh->errstr . "\n";
+                        next;
+                }
+                $query =
+                    'UPDATE DEVICE_MAP SET assigned_switch = NULL, checked_out=? WHERE assigned_switch = ?';
+                $sth = $dbh->prepare($query);
+                if (!$sth) {
+                        print STDERR "Failed to prepare query '$query': "
+                            . $dbh->errstr . "\n";
+                        next;
+                }
+
+                @params = ('0', $hostname);
+                $res    = $sth->execute(@params);
+                if (!$res) {
+                        print STDERR "Failed to execute query '$query': "
+                            . $dbh->errstr . "\n";
+                        next;
+                }
+                $query =
+                    'REPLACE INTO SWITCHES (name, ip_address, pid, location, ports_required) VALUES (?, ?, ?, ?, ?)';
+                $sth = $dbh->prepare($query);
+                if (!$sth) {
+                        print STDERR "Failed to insert switch record: "
+                            . $dbh->errstr . "\n";
+                        next;
+                }
+
+                if ($model =~ /3750[xX]/) {
+                        $model = $PID_3750;
+                } else {
+                        $model    = $PID_3560;
+                        $port_req = '8';
+                }
+
+                @params = ($hostname, $ip, $model, $location, $port_req);
+                $res = $sth->execute(@params);
+                if (!$res) {
+                        print STDERR "Failed to insert switch record: "
+                            . $dbh->errstr . "\n";
+                        next;
+                }
+
+                if ($serial ne '') {
+                        $query =
+                            'SELECT serial_number, max_ports FROM DEVICE_MAP WHERE serial_number=?';
+                        $sth = $dbh->prepare($query);
+                        if (!$sth) {
+                                print STDERR "Failed to prepare query '$query':"
+                                    . $dbh->errstr . "\n";
+                                next;
+                        }
+
+                        @params = ($serial);
+                        $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 $serial in DEVICE_MAP\n";
+                                next;
+                        } else {
+                                my $r = $sth->fetchrow_hashref;
+                                if ($r->{'max_ports'} ne $port_req) {
+                                        print STDERR
+                                            "Ports required for $hostname do not match max ports provided by $serial (required: $port_req, provided: "
+                                            . $r->{'max_ports'} . ")\n";
+                                        next;
+                                }
+                        }
+                        $query =
+                            'UPDATE DEVICE_MAP SET assigned_switch=?, checked_out=? WHERE serial_number=?';
+                        $sth = $dbh->prepare($query);
+                        if (!$sth) {
+                                print STDERR
+                                    "Failed to prepare query '$query': "
+                                    . $dbh->errstr . "\n";
+                                next;
+                        }
+
+                        @params = ($hostname, '1', $serial);
+                        $res = $sth->execute(@params);
+                        if (!$res) {
+                                print STDERR
+                                    "Failed to execute query '$query' for ($hostname, $serial): "
+                                    . $dbh->errstr . "\n";
+                                next;
+                        }
+                }
+        }
+}

+ 20 - 0
scripts/syslog-email.pl

@@ -0,0 +1,20 @@
+#!/usr/bin/perl -n
+# thanks to Brian Dowling for an example with security in mind.
+
+$TO = 'user@company.com';
+$FROM = 'yser@company.com';
+
+s/^<\d{1,2}>//;
+
+open(MAIL, "|/usr/sbin/sendmail -t");
+
+print MAIL <<"EOT";
+To: $TO
+From: $FROM
+Subject: Uplink Went Down: $_
+
+$_
+
+EOT
+
+close(MAIL);

+ 47 - 0
scripts/update_dns.pl

@@ -0,0 +1,47 @@
+#!/usr/bin/perl
+
+use strict;
+use DBI;
+use vars qw($DB_USER $DB_PASS);
+require 'dbi.ph';
+
+my $dsn = "DBI:mysql:database=noc";
+
+my $dbh = DBI->connect($dsn, $DB_USER, $DB_PASS, {PrintError => 0})
+	or die "ERROR: Failed to connect to the database.";
+
+my $sql = 'SELECT name, ip_address FROM SWITCHES WHERE ip_address IS NOT NULL';
+my $sth = $dbh->prepare($sql);
+if (!$sth) {
+	print STDERR "Failed to prepare query: " . $dbh->errstr . "\n";
+	exit(1);
+}
+my $res = $sth->execute();
+if (!$res) {
+	print STDERR "Failed to execute query: " . $dbh->errstr . "\n";
+	exit(1);
+}
+
+while (my $r = $sth->fetchrow_hashref) {
+	my $out = `ssh -2 root\@63.231.220.23 /opt/nwreg2/local/usrbin/nrcmd -N jclarke -P jclarke zone noc.ciscolive.com showHost $r->{'name'}`;
+	if ($out =~ /302 Not Found/) {
+		$out = `ssh -2 root\@63.231.220.23 /opt/nwreg2/local/usrbin/nrcmd -N jclarke -P jclarke zone noc.ciscolive.com addHost $r->{'name'} $r->{'ip_address'}`;
+		if ($out !~ /100 Ok/) {
+			print STDERR "Error adding entry for $r->{'name'}: '$out'\n";
+		}
+	} else {
+		my ($h, $a) = ($out =~ /([^:]+): addr=([^;]+)/);
+		if ($a ne $r->{'ip_address'}) {
+			$out = `ssh -2 root\@63.231.220.23 /opt/nwreg2/local/usrbin/nrcmd -N jclarke -P jclarke zone noc.ciscolive.com removeHost $r->{'name'}`;
+			if ($out !~ /100 Ok/) {
+				print STDERR "Failed to remove host $r->{'name'}: '$out'\n";
+				next;
+			}
+
+			$out = `ssh -2 root\@63.231.220.23 /opt/nwreg2/local/usrbin/nrcmd -N jclarke -P jclarke zone noc.ciscolive.com addHost $r->{'name'} $r->{'ip_address'}`;
+			if ($out !~ /100 Ok/) {
+				print STDERR "Error adding entry for $r->{'name'}: '$out'\n";
+			}
+		}
+	}
+}