1
0

import-system-dev.pl 6.6 KB

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