Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.14 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3.  
  4. use strict;
  5. use warnings;
  6. use POSIX qw(strftime);
  7. use Mail;
  8.  
  9. my @dtb=glob "*.db";
  10.  
  11. my $log="enlaces.log";
  12. my @lugar;
  13. my @IP;
  14. my @fallas;
  15. my @IPv6;
  16. my $c=0;
  17. my $max;
  18. my $v6="";
  19.  
  20. (-e "/usr/bin/mpg123") or die "Falta instalar mpg123";
  21.  
  22. #Toma input de .db existentes pero tambien acepta otros archivos como parametros del programa
  23. push @dtb, @ARGV;
  24.  
  25. foreach my $dtb (@dtb) {
  26. open (my $DBH,$dtb) or warn "No se encontro ningun archivo .db, si no usaste argumentos corta";
  27. while (<$DBH>) {
  28. chomp;
  29. #Guardo el lugar en un array
  30. if ( m/\b\w+-*\w*/ ) {
  31. push(@lugar, "$&");
  32. }
  33. #Analizo los Strings de cada linea por IPs Validas, así no guardamos datos que no sirven.
  34. unless (m/^#/) {
  35. if ( ( m/(\b([0-9]{1,3}\.){3}[0-9]{1,3})/) ||
  36. ( $v6=m/\b((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$/ ) )
  37. {
  38. push(@IP,$&);
  39. push(@IPv6,$v6);
  40. push(@fallas, 0);
  41. $c++;
  42. }
  43. }
  44. }
  45. }
  46. $max=$c-1;
  47. for(;;) {
  48. $c=$max;
  49. my @result;
  50. while ($c!=0) {
  51. $result[$c]=ping($IPv6[$c],$IP[$c]);
  52. $c--;
  53. }
  54. $c=$max;
  55. while ($c!=0) {
  56. my $hora=hora();
  57. if ($result[$c] != 0 ) {
  58. $fallas[$c]++;
  59. if ($fallas[$c]) {
  60. open(my $linklog,">>$lugar[$c].log") or die "No se pudo abrir el log $!\n";
  61. print $linklog "$hora: $lugar[$c] no contestó ping.\n";
  62. close($linklog);
  63. }
  64. if ($fallas[$c] == 5) {
  65. open(my $linklog,">>$lugar[$c].log") or die "No se pudo abrir el log $!\n";
  66. print $linklog "$hora: $lugar[$c] falló $fallas[$c] veces.\n";
  67. close($linklog);
  68. }
  69. elsif ($fallas[$c] == 30) {
  70. sirena();
  71. open(my $linklog,">>$lugar[$c].log") or die "No se pudo abrir el log $!\n";
  72. print $linklog "$hora: $lugar[$c] estuvo caído por 30 segundos y sono la alarma.\n";
  73. close($linklog);
  74. }
  75. elsif ($fallas[$c] == 180) {
  76. sirena();
  77. open(my $linklog,">>$lugar[$c].log") or die "No se pudo abrir el log $!\n";
  78. sendemail($lugar[$c],$fallas[$c]) or print $linklog "No esta andando el mail bien$!\n";
  79. print $linklog "$hora: $lugar[$c] estuvo caído por 3 minutos, sono la alarma y se mando un mail.\n";
  80. close($linklog);
  81. }
  82. }
  83. else {
  84. if ($fallas[$c] > 4) {
  85. open(my $linklog,">>$lugar[$c].log") or die "No se pudo abrir el log $!\n";
  86. print $linklog "$hora: $lugar[$c] está ahora activo, luego de estar $fallas[$c] segundos caido.\n";
  87. close($linklog);
  88. $fallas[$c] = 0;
  89. }
  90. elsif ($fallas[$c] > 180) {
  91. open(my $linklog,">>$lugar[$c].log") or die "No se pudo abrir el log $!\n";
  92. print $linklog "$hora: $lugar[$c] está ahora activo, luego de estar $fallas[$c] segundos caido.\n";
  93. close($linklog);
  94. sendemail($lugar[$c],$fallas[$c],"Ahora $lugar[$c] esta operativo");
  95. $fallas[$c] = 0;
  96. }
  97. }
  98. $c--;
  99. }
  100. # sleep(1);
  101. }
  102.  
  103.  
  104. sub ping {
  105. my ($prot, $add) = @_;
  106. my $ping = $prot ? "ping6" : "ping";
  107. return system("$ping -c 1 -i 1 -q -W 1 $add >> /dev/null");
  108. }
  109.  
  110. sub hora {
  111. return strftime("%Y-%m-%d %H:%M:%S", localtime);
  112. }
  113.  
  114. sub sirena {
  115. my @alarma = ("mpg123", "-q", "Sirens.mp3");
  116. system(@alarma) == 0
  117. or die "Perl falló al llamar a mpg321 $!\n";
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement