update_wiki.pl 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. if ($auth->{WikiName} =~/^(Switch|Router)/) {
  43. $device = get_record_sql($dbh,"SELECT * FROM devices WHERE IP='".$ip."'");
  44. if ($device->{comment}) { $auth->{comments} = $device->{comment}; }
  45. my $parent_connect = get_record_sql($dbh,"SELECT * FROM device_ports DP WHERE DP.uplink=1 AND DP.device_id=".$device->{id});
  46. my $parent_port = get_record_sql($dbh,"SELECT * FROM device_ports DP WHERE id=".$parent_connect->{target_port_id});
  47. my $device_parent = get_record_sql($dbh,"SELECT * FROM devices WHERE id=".$parent_port->{device_id});
  48. my $auth_parent = get_record_sql($dbh,"SELECT * FROM User_auth WHERE deleted=0 AND ip='".$device_parent->{ip}."'");
  49. $device_name = $auth_parent->{WikiName};
  50. $device_port = $parent_port->{port};
  51. } else {
  52. 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};
  53. $device = get_record_sql($dbh,$dSQL);
  54. if (!$device or !$device->{user_id}) { next; }
  55. # if ($ip eq '192.168.13.104') { print Dumper($device),Dumper(\@tmp); die; }
  56. $dSQL = "SELECT * FROM User_auth WHERE WikiName IS NOT NULL AND user_id=".$device->{user_id}." AND deleted=0 AND ip='".$device->{ip}."'";
  57. my $device_auth = get_record_sql($dbh,$dSQL);
  58. if (!$device_auth) { next; }
  59. $device_name = $device_auth->{WikiName};
  60. $device_port = $device->{port};
  61. }
  62. #add non-existent field
  63. my %empty_fields;
  64. $empty_fields{descr}=1;
  65. $empty_fields{parent}=1;
  66. $empty_fields{parent_port}=1;
  67. $empty_fields{mac}=1;
  68. #apply patch
  69. foreach my $row (@tmp) {
  70. if ($row=~/\%META\:FIELD\{name\=\"Description\"/) {
  71. $empty_fields{descr}=0;
  72. if ($auth->{comments}) { push(@wiki_dev,'%META:FIELD{name="Description" title="Description" value="'.$auth->{comments}.'"}%'); next; }
  73. }
  74. if ($row=~/\%META\:FIELD\{name\=\"Parent\"/) {
  75. $empty_fields{parent}=0;
  76. if ($device_name) { push(@wiki_dev,'%META:FIELD{name="Parent" title="Parent" value="'.$device_name.'"}%'); next; }
  77. }
  78. if ($row=~/\%META\:FIELD\{name\=\"ParentPort\"/) {
  79. $empty_fields{parent_port}=0;
  80. if ($device_port) { push(@wiki_dev,'%META:FIELD{name="ParentPort" title="Parent Port" value="'.$device_port.'"}%'); next; }
  81. }
  82. if ($row=~/\%META\:FIELD\{name\=\"Mac\"/) {
  83. $empty_fields{mac}=0;
  84. if ($auth->{mac}) { push(@wiki_dev,'%META:FIELD{name="Mac" title="Mac" value="'.$auth->{mac}.'"}%'); next; }
  85. }
  86. push(@wiki_dev,$row);
  87. }
  88. foreach my $field (keys %empty_fields) {
  89. next if (!$empty_fields{$field});
  90. if ($field eq 'descr' and $auth->{comments}) { push(@wiki_dev,'%META:FIELD{name="Description" title="Description" value="'.$auth->{comments}.'"}%'); next; }
  91. if ($field eq 'parent' and $device_name) { push(@wiki_dev,'%META:FIELD{name="Parent" title="Parent" value="'.$device_name.'"}%'); next; }
  92. if ($field eq 'parent_port' and $device_port) { push(@wiki_dev,'%META:FIELD{name="ParentPort" title="Parent Port" value="'.$device_port.'"}%'); next; }
  93. if ($field eq 'mac' and $auth->{mac}) { push(@wiki_dev,'%META:FIELD{name="Mac" title="Mac" value="'.$auth->{mac}.'"}%'); next; }
  94. }
  95. print "Found: $auth->{ip} $auth->{mac} $device_name $device_port \n";
  96. open (LG,">$content{$fname}") || die("Error open file $content{$fname}!!! die...");
  97. foreach my $row (@wiki_dev) {
  98. if (!$row) { $row=''; }
  99. print LG $row."\n";
  100. }
  101. close (LG);
  102. }
  103. print "Done!\n";
  104. exit;
  105. sub wanted {
  106. my $filename = $File::Find::name;
  107. my $dev_name = basename($filename);
  108. if ($dev_name =~/\.txt$/ and $dev_name=~/^(Device|Switch|Ups|Sensor|Gateway|Router|Server)/) {
  109. $dev_name=~s/\.txt$//;
  110. $content{$dev_name}=$filename;
  111. }
  112. return;
  113. }
  114. exit;