importsw.pl 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. #!/usr/bin/perl
  2. use strict;
  3. use DBI;
  4. use vars qw($PID_3560 $PID_3750 $DB_USER $DB_PASS);
  5. require 'dbi.ph';
  6. sub trim($) {
  7. my ($str) = shift;
  8. $str //= '';
  9. $str =~ s/^\s+//g;
  10. $str =~ s/\s+$//g;
  11. return $str;
  12. }
  13. $PID_3560 = 'WS-C3560CG-8PC-S';
  14. $PID_3750 = 'WS-C3750X';
  15. my $dsn = "DBI:mysql:database=noc";
  16. my $dbh = DBI->connect($dsn, $DB_USER, $DB_PASS, {PrintError => 0})
  17. or die "ERROR: Failed to connect to the database.";
  18. open(IN, "/home/jclarke/invsw.csv")
  19. or die "ERROR: Failed to open /home/jclarke/invsw.csv for reading.";
  20. my @contents = <IN>;
  21. close(IN);
  22. for (my $i = 1 ; $i < scalar(@contents) ; $i++) {
  23. my $line = $contents[$i];
  24. chomp $line;
  25. my ($hostname, $model, $location, $mdf, $serial, $port_req, $ip) =
  26. split(/,/, $line);
  27. $hostname = trim($hostname);
  28. $model = trim($model);
  29. $location = trim($location);
  30. $mdf = trim($mdf);
  31. $ip = trim($ip);
  32. $serial = trim($serial);
  33. $port_req = trim($port_req);
  34. my $query = '';
  35. my ($sth, $res, $ref, @params);
  36. if ($hostname ne '') {
  37. if ($ip ne '') {
  38. $query =
  39. 'SELECT address FROM ADDRESSES WHERE address = ?';
  40. $sth = $dbh->prepare($query);
  41. if (!$sth) {
  42. print STDERR
  43. "ERROR: Failed to prepare query '$query': "
  44. . $dbh->errstr . "\n";
  45. next;
  46. }
  47. @params = ($ip);
  48. $res = $sth->execute(@params);
  49. if (!$res) {
  50. print STDERR
  51. "ERROR: Failed to execute query '$query': "
  52. . $dbh->errstr . "\n";
  53. next;
  54. }
  55. $ref = $sth->fetchrow_hashref;
  56. if (!$ref) {
  57. print STDERR
  58. "ERROR: No matching IP address record for $ip\n";
  59. print STDERR "XXX: Line is '$line'\n";
  60. next;
  61. }
  62. $query =
  63. 'SELECT name FROM SWITCHES WHERE ip_address = ? AND name != ?';
  64. $sth = $dbh->prepare($query);
  65. if (!$sth) {
  66. print STDERR "Failed to prepare '$query': "
  67. . $dbh->errstr . "\n";
  68. next;
  69. }
  70. @params = ($ref->{'address'}, $hostname);
  71. $res = $sth->execute(@params);
  72. if (!$res) {
  73. print STDERR
  74. "Failed to execute query '$query': "
  75. . $dbh->errstr . "\n";
  76. next;
  77. }
  78. if ($sth->rows != 0) {
  79. my $r = $sth->fetchrow_hashref;
  80. print STDERR
  81. "Duplicate IP detected for IP $ip, hostname $hostname (current device is "
  82. . $r->{'name'} . ")\n";
  83. $query =
  84. 'UPDATE ADDRESSES SET used=? WHERE address=?';
  85. $sth = $dbh->prepare($query);
  86. if (!$sth) {
  87. print STDERR
  88. "Failed to prepare query '$query': "
  89. . $dbh->errstr . "\n";
  90. next;
  91. }
  92. @params = ('1', $ip);
  93. $res = $sth->execute(@params);
  94. if (!$res) {
  95. print STDERR
  96. "Failed to execute query '$query': "
  97. . $dbh->errstr . "\n";
  98. next;
  99. }
  100. next;
  101. }
  102. $ip = $ref->{'address'};
  103. } else {
  104. $query =
  105. 'SELECT address FROM ADDRESSES WHERE location=? AND used=? LIMIT 1';
  106. $sth = $dbh->prepare($query);
  107. if (!$sth) {
  108. print STDERR
  109. "Failed to prepare query '$query': "
  110. . $dbh->errstr . "\n";
  111. next;
  112. }
  113. @params = ($mdf, '0');
  114. $res = $sth->execute(@params);
  115. if (!$res) {
  116. print STDERR
  117. "Failed to execute query '$query': "
  118. . $dbh->errstr . "\n";
  119. next;
  120. }
  121. my $r = $sth->fetchrow_hashref;
  122. $ip = $r->{'address'};
  123. }
  124. $query = 'UPDATE ADDRESSES SET used=? WHERE address=?';
  125. $sth = $dbh->prepare($query);
  126. if (!$sth) {
  127. print STDERR "Failed to prepare query '$query': "
  128. . $dbh->errstr . "\n";
  129. next;
  130. }
  131. @params = ('1', $ip);
  132. $res = $sth->execute(@params);
  133. if (!$res) {
  134. print STDERR "Failed to execute query '$query': "
  135. . $dbh->errstr . "\n";
  136. next;
  137. }
  138. $query =
  139. 'UPDATE DEVICE_MAP SET assigned_switch = NULL, checked_out=? WHERE assigned_switch = ?';
  140. $sth = $dbh->prepare($query);
  141. if (!$sth) {
  142. print STDERR "Failed to prepare query '$query': "
  143. . $dbh->errstr . "\n";
  144. next;
  145. }
  146. @params = ('0', $hostname);
  147. $res = $sth->execute(@params);
  148. if (!$res) {
  149. print STDERR "Failed to execute query '$query': "
  150. . $dbh->errstr . "\n";
  151. next;
  152. }
  153. $query =
  154. 'REPLACE INTO SWITCHES (name, ip_address, pid, location, ports_required) VALUES (?, ?, ?, ?, ?)';
  155. $sth = $dbh->prepare($query);
  156. if (!$sth) {
  157. print STDERR "Failed to insert switch record: "
  158. . $dbh->errstr . "\n";
  159. next;
  160. }
  161. if ($model =~ /3750[xX]/) {
  162. $model = $PID_3750;
  163. } else {
  164. $model = $PID_3560;
  165. $port_req = '8';
  166. }
  167. @params = ($hostname, $ip, $model, $location, $port_req);
  168. $res = $sth->execute(@params);
  169. if (!$res) {
  170. print STDERR "Failed to insert switch record: "
  171. . $dbh->errstr . "\n";
  172. next;
  173. }
  174. if ($serial ne '') {
  175. $query =
  176. 'SELECT serial_number, max_ports FROM DEVICE_MAP WHERE serial_number=?';
  177. $sth = $dbh->prepare($query);
  178. if (!$sth) {
  179. print STDERR "Failed to prepare query '$query':"
  180. . $dbh->errstr . "\n";
  181. next;
  182. }
  183. @params = ($serial);
  184. $res = $sth->execute(@params);
  185. if (!$res) {
  186. print STDERR
  187. "Failed to execute query '$query': "
  188. . $dbh->errstr . "\n";
  189. next;
  190. }
  191. if ($sth->rows == 0) {
  192. print STDERR
  193. "Unable to find $serial in DEVICE_MAP\n";
  194. next;
  195. } else {
  196. my $r = $sth->fetchrow_hashref;
  197. if ($r->{'max_ports'} ne $port_req) {
  198. print STDERR
  199. "Ports required for $hostname do not match max ports provided by $serial (required: $port_req, provided: "
  200. . $r->{'max_ports'} . ")\n";
  201. next;
  202. }
  203. }
  204. $query =
  205. 'UPDATE DEVICE_MAP SET assigned_switch=?, checked_out=? WHERE serial_number=?';
  206. $sth = $dbh->prepare($query);
  207. if (!$sth) {
  208. print STDERR
  209. "Failed to prepare query '$query': "
  210. . $dbh->errstr . "\n";
  211. next;
  212. }
  213. @params = ($hostname, '1', $serial);
  214. $res = $sth->execute(@params);
  215. if (!$res) {
  216. print STDERR
  217. "Failed to execute query '$query' for ($hostname, $serial): "
  218. . $dbh->errstr . "\n";
  219. next;
  220. }
  221. }
  222. }
  223. }