sync_mikrotik_poe_monitor.pl 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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::database;
  18. use eyelib::common;
  19. use DBI;
  20. use utf8;
  21. use Fcntl qw(:flock);
  22. open(SELF,"<",$0) or die "Cannot open $0 - $!";
  23. flock(SELF, LOCK_EX|LOCK_NB) or exit 1;
  24. #exit;
  25. #$debug = 1;
  26. $|=1;
  27. if (IsNotRun($SPID)) { Add_PID($SPID); } else { die "Warning!!! $SPID already runnning!\n"; }
  28. my @poe_mikrotik = get_records_sql($dbh,"SELECT * FROM devices WHERE deleted=0 and device_model_id=12");
  29. foreach my $device (@poe_mikrotik) {
  30. next if (!$device);
  31. my $switch_name=$device->{device_name};
  32. my $switch_ip=$device->{ip};
  33. my @cmd_list=();
  34. my @auth_list = get_records_sql($dbh,"SELECT DP.port,AU.ip,AU.dns_name 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.nagios=1 and C.device_id=".$device->{id}."");
  35. my %work_list;
  36. foreach my $auth (@auth_list) {
  37. next if (!$auth);
  38. $work_list{'ether'.$auth->{port}}{ip}=$auth->{ip};
  39. if ($auth->{dns_name}) {
  40. $work_list{'ether'.$auth->{port}}{dns_name}=$auth->{dns_name};
  41. } else {
  42. $work_list{'ether'.$auth->{port}}{dns_name}=$auth->{ip};
  43. }
  44. }
  45. $device = netdev_set_auth($device);
  46. $device->{login}.='+ct400w';
  47. db_log_verbose($dbh,"Sync link monitor at $switch_name [".$switch_ip."] started.");
  48. my $t = netdev_login($device);
  49. #/interface ethernet terse
  50. #/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
  51. #/interface ethernet set [ find default-name=ether2 ] loop-protect=on speed=100Mbps
  52. #fetch current
  53. my @current_monitor=netdev_cmd($device,$t,'/interface ethernet export terse',1);
  54. @current_monitor=grep(/power/,@current_monitor);
  55. my %current_list;
  56. foreach my $poe (@current_monitor) {
  57. next if (!$poe);
  58. my $port_name;
  59. my $ping_enabled=0;
  60. my $ping_address;
  61. my @words=split(/ /,$poe);
  62. foreach my $item (@words) {
  63. if ($item=~/(ether\d{1,2})/) { $port_name=$1; }
  64. if ($item=~/power-cycle-ping-address=(.*)/) { $ping_address=$1; }
  65. if ($item=~/power-cycle-ping-enabled=yes/) { $ping_enabled=1; }
  66. }
  67. next if (!$ping_enabled);
  68. next if (!$port_name or !$ping_address);
  69. $current_list{$port_name}=$ping_address;
  70. }
  71. foreach my $current_port (keys %current_list) {
  72. if (defined $work_list{$current_port}) {
  73. if ($work_list{$current_port}{ip} ne $current_list{$current_port}) {
  74. db_log_info($dbh,"Change settings poe monitor at $switch_name [$current_port] to ip: $work_list{$current_port}{ip}");
  75. push(@cmd_list,'/interface ethernet set [ find default-name='.$current_port.' ] power-cycle-ping-address='.$work_list{$current_port}{ip}.' power-cycle-ping-enabled=yes power-cycle-ping-timeout=5m');
  76. push(@cmd_list,'/interface ethernet set [ find default-name='.$current_port.' ] comment='.$work_list{$current_port}{dns_name});
  77. }
  78. } else {
  79. db_log_info($dbh,"Disable poe monitor at $switch_name [$current_port]");
  80. push(@cmd_list,'/interface ethernet set [ find default-name='.$current_port.' ] power-cycle-ping-enabled=no');
  81. push(@cmd_list,'/interface ethernet set [ find default-name='.$current_port.' ] comment=""');
  82. }
  83. }
  84. foreach my $work_port (keys %work_list) {
  85. if (!defined $current_list{$work_port}) {
  86. db_log_info($dbh,"Enable poe monitor at $switch_name [$work_port] for $work_list{$work_port}{ip}");
  87. push(@cmd_list,'/interface ethernet set [ find default-name='.$work_port.' ] power-cycle-ping-address='.$work_list{$work_port}{ip}.' power-cycle-ping-enabled=yes power-cycle-ping-timeout=5m');
  88. push(@cmd_list,'/interface ethernet set [ find default-name='.$work_port.' ] comment='.$work_list{$work_port}{dns_name});
  89. }
  90. }
  91. if (scalar(@cmd_list)) {
  92. netdev_cmd($device,$t,\@cmd_list,1);
  93. if ($debug) { foreach my $cmd (@cmd_list) { db_log_debug($dbh,"$cmd"); } }
  94. }
  95. db_log_verbose($dbh,"Sync link monitor at $switch_name [".$switch_ip."] stopped.");
  96. }
  97. $dbh->disconnect();
  98. if (IsMyPID($SPID)) { Remove_PID($SPID); };
  99. do_exit 0;