import-system-dev.pl 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #!/usr/bin/perl
  2. #
  3. # Copyright (C) Roman Dmitriev, rnd@rajven.ru
  4. #
  5. #Обновляем БД устрйств
  6. use utf8;
  7. use warnings;
  8. use Encode;
  9. use open qw(:std :encoding(UTF-8));
  10. no warnings 'utf8';
  11. use FindBin '$Bin';
  12. use lib "$Bin/";
  13. use Data::Dumper;
  14. use Rstat::config;
  15. use Rstat::main;
  16. use Rstat::database;
  17. use Rstat::net_utils;
  18. use strict;
  19. use warnings;
  20. print "Stage 0: Read system devices\n";
  21. my @user_devices=();
  22. if (-e "system-devs.csv") {
  23. my @nSQL=read_file("system-devs.csv");
  24. foreach my $row (@nSQL) {
  25. my ($dev_id,$dev_model,$dev_vendor) = split(/;/,$row);
  26. my $device;
  27. $device->{id}=$dev_id;
  28. $device->{model_name}=$dev_model;
  29. $device->{vendor_id}=$dev_vendor;
  30. push(@user_devices,$device);
  31. }
  32. }
  33. print "Stage 1: Read system vendors\n";
  34. my @user_vendors=();
  35. if (-e "system-vendors.csv") {
  36. my @nSQL=read_file("system-vendors.csv");
  37. foreach my $row (@nSQL) {
  38. my ($vendor_id,$vendor_name) = split(/;/,$row);
  39. my $vendor;
  40. $vendor->{id}=$vendor_id;
  41. $vendor->{name}=$vendor_name;
  42. push(@user_vendors,$vendor);
  43. }
  44. }
  45. print "Done!\n";
  46. my %vendor_migration;
  47. print "Import Vendors\n";
  48. foreach my $vendor (@user_vendors) {
  49. #seach exists vendor created by user
  50. my $vendor_exist = get_record_sql($dbh,"SELECT * FROM vendors WHERE id>=10000 and LOWER(name)= ?", lc(trim($vendor->{name})));
  51. print "Check: $vendor->{name} id: $vendor->{id} ...";
  52. if ($vendor_exist) {
  53. if ($vendor_exist->{id} == $vendor->{id}) { print "OK\n"; next; }
  54. print " created by user. Switch to system pool.";
  55. do_sql($dbh,"UPDATE vendors SET id=? WHERE id=?",$vendor->{id},$vendor_exist->{id});
  56. do_sql($dbh,"UPDATE device_models SET vendor_id=? WHERE vendor_id=?",$vendor->{id}, $vendor_exist->{id});
  57. do_sql($dbh,"UPDATE devices SET vendor_id=? WHERE vendor_id=?", $vendor->{id}, $vendor_exist->{id});
  58. print " Migrated.\n";
  59. next;
  60. }
  61. #check system pool
  62. $vendor_exist = get_record_sql($dbh,"SELECT * FROM vendors WHERE id<10000 and LOWER(name)=?",lc(trim($vendor->{name})));
  63. if ($vendor_exist) {
  64. if ($vendor_exist->{id} == $vendor->{id}) { print "OK\n"; next ; }
  65. print "Warning! System vendor mismatch! ";
  66. my $vendor2 = get_record_sql($dbh,"SELECT * FROM vendors WHERE id=?",$vendor->{id});
  67. if ($vendor2) {
  68. print "Found another vendor with this id =>".$vendor2->{name};
  69. my $last_id = get_record_sql($dbh,"SELECT MAX(id) as last FROM vendors");
  70. my $new_vendor_id = $last_id->{'last'}+1;
  71. if ($new_vendor_id <=10000 ) { $new_vendor_id = 10001; }
  72. print " Move vendor $vendor2->{name} to user custom block. Run script again\n";
  73. do_sql($dbh,"UPDATE vendors SET id=? WHERE id=?",$new_vendor_id, $vendor2->{id});
  74. do_sql($dbh,"UPDATE device_models SET vendor_id=? WHERE vendor_id=?",$new_vendor_id,$vendor2->{id});
  75. next;
  76. }
  77. do_sql($dbh,"UPDATE vendors SET id=? WHERE id=?",$vendor->{id},$vendor_exist->{id});
  78. do_sql($dbh,"UPDATE device_models SET vendor_id=? WHERE vendor_id=?",$vendor->{id},$vendor_exist->{id});
  79. do_sql($dbh,"UPDATE devices SET vendor_id=? WHERE vendor_id=?",$vendor->{id},$vendor_exist->{id});
  80. print "Fixed.\n";
  81. next;
  82. }
  83. #check another record with this id
  84. my $vendor2 = get_record_sql($dbh,"SELECT * FROM vendors WHERE id=?",$vendor->{id});
  85. if ($vendor2) {
  86. print "Found another vendor with this id =>".$vendor2->{name};
  87. my $last_id = get_record_sql($dbh,"SELECT MAX(id) as last FROM vendors");
  88. my $new_vendor_id = $last_id->{'last'}+1;
  89. if ($new_vendor_id <=10000 ) { $new_vendor_id = 10001; }
  90. print " Move vendor $vendor2->{name} to user custom block. Run script again\n";
  91. do_sql($dbh,"UPDATE vendors SET id=? WHERE id=?",$new_vendor_id,$vendor2->{id});
  92. do_sql($dbh,"UPDATE device_models SET vendor_id=? WHERE vendor_id=?",$new_vendor_id,$vendor2->{id});
  93. next;
  94. }
  95. insert_record($dbh,"vendors",$vendor);
  96. print " Imported.\n";
  97. }
  98. print "Done!\n";
  99. print "Import devices\n";
  100. foreach my $device (@user_devices) {
  101. #seach exists device created by user
  102. print "Check id: $device->{id} name: $device->{model_name}..";
  103. my $device_exist = get_record_sql($dbh,"SELECT * FROM device_models WHERE id>=10000 AND vendor_id=? AND LOWER(model_name)=?",$device->{vendor_id},lc(trim($device->{model_name})));
  104. if ($device_exist) {
  105. if ($device_exist->{id} == $device->{id}) { print "OK\n"; next; }
  106. do_sql($dbh,"UPDATE device_models SET id=? WHERE id=?",$device->{id},$device_exist->{id});
  107. do_sql($dbh,"UPDATE devices SET device_model_id=? WHERE device_model_id=?",$device->{id},$device_exist->{id});
  108. print "Migrated\n";
  109. next;
  110. }
  111. #system model table
  112. $device_exist = get_record_sql($dbh,"SELECT * FROM device_models WHERE id<10000 AND vendor_id=? AND LOWER(model_name)=?",$device->{vendor_id},lc(trim($device->{model_name})));
  113. if ($device_exist) {
  114. if ($device_exist->{id} == $device->{id}) { print "OK\n"; next; }
  115. print "Warning! System device model mismatch! ";
  116. my $device2 = get_record_sql($dbh,"SELECT * FROM device_models WHERE id=?",$device->{id});
  117. if ($device2) {
  118. print "Found another device model with this id =>".$device2->{model_name};
  119. my $last_id = get_record_sql($dbh,"SELECT MAX(id) as last FROM device_models");
  120. my $new_model_id = $last_id->{'last'}+1;
  121. if ($new_model_id <=10000 ) { $new_model_id = 10001; }
  122. print " Move device model $device2->{model_name} to user custom block. Run script again\n";
  123. do_sql($dbh,"UPDATE device_models SET id=? WHERE id=?",$new_model_id,$device2->{id});
  124. do_sql($dbh,"UPDATE devices SET device_model_id=? WHERE device_model_id=?",$new_model_id,$device2->{id});
  125. next;
  126. }
  127. do_sql($dbh,"UPDATE device_models SET id=? WHERE id=?",$device->{id},$device_exist->{id});
  128. do_sql($dbh,"UPDATE devices SET device_model_id=? WHERE device_model_id=?",$device->{id},$device_exist->{id});
  129. print "Migrated\n";
  130. next;
  131. }
  132. #check another record with this id
  133. my $device2 = get_record_sql($dbh,"SELECT * FROM device_models WHERE id=?",$device->{id});
  134. if ($device2) {
  135. print "Found another device model with this id =>".$device2->{model_name};
  136. my $last_id = get_record_sql($dbh,"SELECT MAX(id) as last FROM device_models");
  137. my $new_model_id = $last_id->{'last'}+1;
  138. if ($new_model_id <=10000 ) { $new_model_id = 10001; }
  139. print " Move device model $device2->{model_name} to user custom block. Run script again\n";
  140. do_sql($dbh,"UPDATE device_models SET id=? WHERE id=?",$new_model_id,$device2->{id});
  141. do_sql($dbh,"UPDATE devices SET device_model_id=? WHERE device_model_id=?",$new_model_id,$device2->{id});
  142. next;
  143. }
  144. insert_record($dbh,"device_models",$device);
  145. print " Imported.\n";
  146. }
  147. print "Done!\n";
  148. exit;