sync_mikrotik_poe_monitor.pl 4.3 KB

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