update_wiki.pl 4.8 KB

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