update_wiki.pl 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. if (!$config_ref{wiki_path}) { exit; }
  17. my %content;
  18. find( \&wanted, $config_ref{wiki_path});
  19. foreach my $fname (keys %content) {
  20. open (FF,"<$content{$fname}") or die "unable to open file $content{$fname}!" ;
  21. my @tmp=<FF>;
  22. close(FF);
  23. chomp(@tmp);
  24. my @wiki_dev=();
  25. my $ip;
  26. foreach my $row (@tmp) {
  27. if ($row=~/\%META\:FIELD\{name\=\"Description\"/) { next; }
  28. if ($row=~/\%META\:FIELD\{name\=\"Parent\"/) { next; }
  29. if ($row=~/\%META\:FIELD\{name\=\"ParentPort\"/) { next; }
  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. if ($row=~/\%META\:FIELD\{name\=\"Mac\"/) { next; }
  34. push(@wiki_dev,$row);
  35. }
  36. if (!$ip) { next; }
  37. my $auth = get_record_sql($dbh,"SELECT * FROM User_auth WHERE deleted=0 and ip='".$ip."'");
  38. if (!$auth) { next; }
  39. 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};
  40. my $device = get_record_sql($dbh,$dSQL);
  41. if (!$device or !$device->{user_id}) { next; }
  42. $dSQL = "SELECT * FROM User_auth WHERE WikiName IS NOT NULL AND user_id=".$device->{user_id}." AND deleted=0 AND ip='".$device->{ip}."'";
  43. my $device_auth = get_record_sql($dbh,$dSQL);
  44. if (!$device_auth) { next; }
  45. my $device_name = $device_auth->{WikiName};
  46. my $device_port = $device->{port};
  47. if ($auth->{comments}) { push(@wiki_dev,'%META:FIELD{name="Description" title="Description" value="'.$auth->{comments}.'"}%'); }
  48. push(@wiki_dev,'%META:FIELD{name="Parent" title="Parent" value="'.$device_name.'"}%');
  49. push(@wiki_dev,'%META:FIELD{name="ParentPort" title="Parent Port" value="'.$device_port.'"}%');
  50. push(@wiki_dev,'%META:FIELD{name="Mac" title="Mac" value="'.$auth->{mac}.'"}%');
  51. print "Found: $auth->{ip} $auth->{mac} $device_name $device_port \n";
  52. open (LG,">$content{$fname}") || die("Error open file $content{$fname}!!! die...");
  53. foreach my $row (@wiki_dev) {
  54. next if (!$row);
  55. print LG $row."\n";
  56. }
  57. close (LG);
  58. }
  59. print "Done!\n";
  60. exit;
  61. sub wanted {
  62. my $filename = $File::Find::name;
  63. my $dev_name = basename($filename);
  64. if ($filename =~/\.txt$/ and $filename=~/Device/) {
  65. $dev_name=~s/\.txt$//;
  66. $content{$dev_name}=$filename;
  67. }
  68. return;
  69. }
  70. exit;