sync_mikrotik_poe_monitor.pl 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #!/usr/bin/perl -w
  2. #
  3. # Copyright (C) Roman Dmitiriev, rnd@rajven.ru
  4. #
  5. use FindBin '$Bin';
  6. use lib "$Bin/";
  7. use strict;
  8. use Time::Local;
  9. use FileHandle;
  10. use Data::Dumper;
  11. use eyelib::config;
  12. use eyelib::main;
  13. use eyelib::cmd;
  14. use Net::Patricia;
  15. use Date::Parse;
  16. use eyelib::net_utils;
  17. use eyelib::mysql;
  18. use DBI;
  19. use utf8;
  20. use Fcntl qw(:flock);
  21. open(SELF,"<",$0) or die "Cannot open $0 - $!";
  22. flock(SELF, LOCK_EX|LOCK_NB) or exit 1;
  23. #exit;
  24. #$debug = 1;
  25. $|=1;
  26. if (IsNotRun($SPID)) { Add_PID($SPID); } else { die "Warning!!! $SPID already runnning!\n"; }
  27. my @poe_mikrotik = get_records_sql($dbh,"SELECT * FROM devices WHERE deleted=0 and device_model_id=12");
  28. foreach my $device (@poe_mikrotik) {
  29. next if (!$device);
  30. my $switch_name=$device->{device_name};
  31. my $switch_ip=$device->{ip};
  32. my @cmd_list=();
  33. my @auth_list = get_records_sql($dbh,"SELECT DP.port,AU.ip FROM `device_ports` AS DP, `User_auth` as AU, `connections` as C WHERE DP.id=C.port_id and C.auth_id=AU.id and AU.deleted=0 and AU.link_check=1 and AU.nagios=1 and C.device_id=".$device->{id}."");
  34. my %work_list;
  35. foreach my $auth (@auth_list) {
  36. next if (!$auth);
  37. $work_list{'ether'.$auth->{port}}=$auth->{ip};
  38. }
  39. $device = netdev_set_auth($device);
  40. $device->{login}.='+ct400w';
  41. db_log_verbose($dbh,"Sync link monitor at $switch_name [".$switch_ip."] started.");
  42. my $t = netdev_login($device);
  43. #/interface ethernet terse
  44. #/interface ethernet set [ find default-name=ether1 ] loop-protect=on power-cycle-ping-address=192.168.22.51 power-cycle-ping-enabled=yes power-cycle-ping-timeout=3m speed=100Mbps
  45. #/interface ethernet set [ find default-name=ether2 ] loop-protect=on speed=100Mbps
  46. #fetch current
  47. my @current_monitor=netdev_cmd($device,$t,'ssh','/interface ethernet export terse',1);
  48. @current_monitor=grep(/power/,@current_monitor);
  49. my %current_list;
  50. foreach my $poe (@current_monitor) {
  51. next if (!$poe);
  52. my $port_name;
  53. my $ping_enabled=0;
  54. my $ping_address;
  55. my @words=split(/ /,$poe);
  56. foreach my $item (@words) {
  57. if ($item=~/(ether\d{1,2})/) { $port_name=$1; }
  58. if ($item=~/power-cycle-ping-address=(.*)/) { $ping_address=$1; }
  59. if ($item=~/power-cycle-ping-enabled=yes/) { $ping_enabled=1; }
  60. }
  61. next if (!$ping_enabled);
  62. next if (!$port_name or !$ping_address);
  63. $current_list{$port_name}=$ping_address;
  64. }
  65. foreach my $current_port (keys %current_list) {
  66. if (defined $work_list{$current_port}) {
  67. if ($work_list{$current_port} ne $current_list{$current_port}) {
  68. db_log_info($dbh,"Change settings poe monitor at $switch_name [$current_port] to ip: $work_list{$current_port}");
  69. push(@cmd_list,'/interface ethernet set [ find default-name='.$current_port.' ] power-cycle-ping-address='.$work_list{$current_port}.' power-cycle-ping-enabled=yes power-cycle-ping-timeout=3m');
  70. }
  71. } else {
  72. db_log_info($dbh,"Disable poe monitor at $switch_name [$current_port]");
  73. push(@cmd_list,'/interface ethernet set [ find default-name='.$current_port.' ] power-cycle-ping-enabled=no');
  74. }
  75. }
  76. foreach my $work_port (keys %work_list) {
  77. if (!defined $current_list{$work_port}) {
  78. db_log_info($dbh,"Enable poe monitor at $switch_name [$work_port] for $work_list{$work_port}");
  79. push(@cmd_list,'/interface ethernet set [ find default-name='.$work_port.' ] power-cycle-ping-address='.$work_list{$work_port}.' power-cycle-ping-enabled=yes power-cycle-ping-timeout=3m');
  80. }
  81. }
  82. if (scalar(@cmd_list)) {
  83. netdev_cmd($device,$t,'ssh',\@cmd_list,1);
  84. if ($debug) { foreach my $cmd (@cmd_list) { db_log_debug($dbh,"$cmd"); } }
  85. }
  86. db_log_verbose($dbh,"Sync link monitor at $switch_name [".$switch_ip."] stopped.");
  87. }
  88. $dbh->disconnect();
  89. if (IsMyPID($SPID)) { Remove_PID($SPID); };
  90. do_exit 0;