update_wiki.pl 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #!/usr/bin/perl
  2. #
  3. # Copyright (C) Roman Dmitiriev, rnd@rajven.ru
  4. #
  5. use FindBin '$Bin';
  6. use lib "$Bin/";
  7. use Data::Dumper;
  8. use Rstat::config;
  9. use Rstat::main;
  10. use Rstat::mysql;
  11. use Rstat::net_utils;
  12. use strict;
  13. use warnings;
  14. use File::Find;
  15. use File::Basename;
  16. use Fcntl qw(:flock);
  17. open(SELF,"<",$0) or die "Cannot open $0 - $!";
  18. flock(SELF, LOCK_EX|LOCK_NB) or exit 1;
  19. if (!$config_ref{wiki_path}) { exit; }
  20. my %content;
  21. find( \&wanted, $config_ref{wiki_path});
  22. foreach my $fname (keys %content) {
  23. open (FF,"<$content{$fname}") or die "unable to open file $content{$fname}!" ;
  24. my @tmp=<FF>;
  25. close(FF);
  26. chomp(@tmp);
  27. my @wiki_dev=();
  28. my $ip;
  29. foreach my $row (@tmp) {
  30. if ($row=~/\%META\:FIELD\{name\=\"DeviceIP\"/) {
  31. if ($row=~/value\=\"([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})\"/) { $ip = $1; }
  32. }
  33. }
  34. if (!$ip) { next; }
  35. my $auth = get_record_sql($dbh,"SELECT * FROM User_auth WHERE deleted=0 and ip='".$ip."'");
  36. if (!$auth) { next; }
  37. if (!$auth->{WikiName}) { next; }
  38. if ($auth->{WikiName} =~/^Gateway/) { next; }
  39. my $device;
  40. my $device_name;
  41. my $device_port;
  42. print "Found: $auth->{ip} $auth->{mac} ";
  43. eval {
  44. if ($auth->{WikiName} =~/^(Switch|Router)/) {
  45. $device = get_record_sql($dbh,"SELECT * FROM devices WHERE IP='".$ip."'");
  46. if (!$device) { die "Unknown device"; }
  47. if ($device->{comment}) { $auth->{comments} = $device->{comment}; }
  48. my $parent_connect = get_record_sql($dbh,"SELECT * FROM device_ports DP WHERE DP.uplink=1 AND DP.device_id=".$device->{id});
  49. if (!$parent_connect) { die "Unknown connection"; }
  50. my $parent_port = get_record_sql($dbh,"SELECT * FROM device_ports DP WHERE id=".$parent_connect->{target_port_id});
  51. if (!$parent_port) { die "Unknown port connection"; }
  52. my $device_parent = get_record_sql($dbh,"SELECT * FROM devices WHERE id=".$parent_port->{device_id});
  53. if (!$parent_port) { die "Unknown parent device"; }
  54. my $auth_parent = get_record_sql($dbh,"SELECT * FROM User_auth WHERE deleted=0 AND ip='".$device_parent->{ip}."'");
  55. if (!$parent_port) { die "Unknown auth for device"; }
  56. $device_name = $auth_parent->{WikiName};
  57. $device_port = $parent_port->{port};
  58. } else {
  59. my $dSQL = "SELECT D.ip, D.building_id, D.user_id ,DP.port FROM devices AS D, device_ports AS DP, connections AS C WHERE D.deleted=0 and D.id = DP.device_id AND DP.id = C.port_id AND C.auth_id=".$auth->{id};
  60. $device = get_record_sql($dbh,$dSQL);
  61. if (!$device or !$device->{user_id}) { die "Unknown connection"; }
  62. $dSQL = "SELECT * FROM User_auth WHERE WikiName IS NOT NULL AND user_id=".$device->{user_id}." AND deleted=0 AND ip='".$device->{ip}."'";
  63. my $device_auth = get_record_sql($dbh,$dSQL);
  64. if (!$device_auth) { die "Unknown device auth"; }
  65. $device_name = $device_auth->{WikiName};
  66. $device_port = $device->{port};
  67. }
  68. };
  69. if ($@) { print "Error: $@\n"; next; }
  70. #add non-existent field
  71. my %empty_fields;
  72. $empty_fields{descr}=1;
  73. $empty_fields{parent}=1;
  74. $empty_fields{parent_port}=1;
  75. $empty_fields{mac}=1;
  76. #apply patch
  77. foreach my $row (@tmp) {
  78. if ($row=~/\%META\:FIELD\{name\=\"Description\"/) {
  79. $empty_fields{descr}=0;
  80. if ($auth->{comments}) { push(@wiki_dev,'%META:FIELD{name="Description" title="Description" value="'.$auth->{comments}.'"}%'); next; }
  81. }
  82. if ($row=~/\%META\:FIELD\{name\=\"Parent\"/) {
  83. $empty_fields{parent}=0;
  84. if ($device_name) { push(@wiki_dev,'%META:FIELD{name="Parent" title="Parent" value="'.$device_name.'"}%'); next; }
  85. }
  86. if ($row=~/\%META\:FIELD\{name\=\"ParentPort\"/) {
  87. $empty_fields{parent_port}=0;
  88. if ($device_port) { push(@wiki_dev,'%META:FIELD{name="ParentPort" title="Parent Port" value="'.$device_port.'"}%'); next; }
  89. }
  90. if ($row=~/\%META\:FIELD\{name\=\"Mac\"/) {
  91. $empty_fields{mac}=0;
  92. if ($auth->{mac}) { push(@wiki_dev,'%META:FIELD{name="Mac" title="Mac" value="'.$auth->{mac}.'"}%'); next; }
  93. }
  94. push(@wiki_dev,$row);
  95. }
  96. foreach my $field (keys %empty_fields) {
  97. next if (!$empty_fields{$field});
  98. if ($field eq 'descr' and $auth->{comments}) { push(@wiki_dev,'%META:FIELD{name="Description" title="Description" value="'.$auth->{comments}.'"}%'); next; }
  99. if ($field eq 'parent' and $device_name) { push(@wiki_dev,'%META:FIELD{name="Parent" title="Parent" value="'.$device_name.'"}%'); next; }
  100. if ($field eq 'parent_port' and $device_port) { push(@wiki_dev,'%META:FIELD{name="ParentPort" title="Parent Port" value="'.$device_port.'"}%'); next; }
  101. if ($field eq 'mac' and $auth->{mac}) { push(@wiki_dev,'%META:FIELD{name="Mac" title="Mac" value="'.$auth->{mac}.'"}%'); next; }
  102. }
  103. #print Dumper(\@wiki_dev);
  104. #next;
  105. if (!$device_name) { $device_name='None'; };
  106. if (!$device_port) { $device_port='None'; };
  107. print "at $device_name $device_port \n";
  108. open (LG,">$content{$fname}") || die("Error open file $content{$fname}!!! die...");
  109. foreach my $row (@wiki_dev) {
  110. if (!$row) { $row=''; }
  111. print LG $row."\n";
  112. }
  113. close (LG);
  114. }
  115. print "Done!\n";
  116. exit;
  117. sub wanted {
  118. my $filename = $File::Find::name;
  119. my $dev_name = basename($filename);
  120. if ($dev_name =~/\.txt$/ and $dev_name=~/^(Device|Switch|Ups|Sensor|Gateway|Router|Server|Bras)/) {
  121. $dev_name=~s/\.txt$//;
  122. $content{$dev_name}=$filename;
  123. }
  124. return;
  125. }
  126. exit;