Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
497
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.77 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3.  
  4. use lib '/home/guido/workspace';
  5. use Mail;
  6. use strict;
  7. use warnings;
  8. use POSIX qw(strftime);
  9.  
  10. my @dtb=<*.db>;
  11. my $log="enlaces.log";
  12. my @lugar;
  13. my @IP;
  14. my @fallas;
  15. my @IPv6;
  16. my $c=0;
  17. my $dtb;
  18. my $max;
  19. my $v6="";
  20. sub hora;
  21. sub mandarmail;
  22. sub ping;
  23. sub sirena;
  24.  
  25. (-e "/usr/bin/mpg123") or die "Falta instalar mpg123";
  26.  
  27. #Toma input de .db existentes pero tambien acepta otros archivos como parametros del programa
  28. if (@ARGV){
  29. @dtb=(@dtb,@ARGV);
  30. }
  31.  
  32. foreach $dtb (@dtb) {
  33. open (my $DBH,$dtb) or die "No se encontro ningun archivo .db";
  34. while (<$DBH>) {
  35. chomp;
  36. #Guardo el lugar en un array
  37. if ( m/\b\w+-*\w*/ ) {
  38. push(@lugar, "$&");
  39. }
  40. #Analizo los Strings de cada linea por IPs Validas, así no guardamos datos que no sirven.
  41. unless (m/^#/) {
  42. if ( ( m/(\b([0-9]{1,3}\.){3}[0-9]{1,3})/go) ||
  43. ( $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*$/go ) )
  44. {
  45. #Guardo la IP en un hash
  46. push(@IP,"$&");
  47. #Guardo si es IPv6 en un hash
  48. push(@IPv6,"$v6");
  49. #Inicializo el hash de fallas
  50. push(@fallas, 0);
  51. #Puedo mostrar que es lo que cargo con esto
  52. #print "$lugar[$c] $IP[$c]\n";
  53. $c++;
  54. }
  55. }
  56. }
  57. }
  58. $max=$c-1;
  59. for(;;) {
  60. $c=$max;
  61. while ($c!=0) {
  62. if (ping($IPv6[$c],$IP[$c]) != 0 ) {
  63. $fallas[$c]++;
  64. my $hora=hora();
  65. if ($fallas[$c]) {
  66. open(LOGENLACE,">>$lugar[$c].log") or die "No se pudo abrir el log $!\n";
  67. print LOGENLACE "$hora: $lugar[$c] no contestó ping.\n";
  68. }
  69. if ($fallas[$c] == 5) {
  70. open(LOGENLACE,">>$lugar[$c].log") or die "No se pudo abrir el log $!\n";
  71. print LOGENLACE "$hora: $lugar[$c] falló $fallas[$c] veces.\n";
  72. }
  73. elsif ($fallas[$c] == 30) {
  74. open(LOG,">>$log") or die "No se pudo abrir el log $!\n";
  75. sirena();
  76. print LOG "$hora: $lugar[$c] estuvo caído por 30 segundos y sono la alarma.\n";
  77. }
  78. elsif ($fallas[$c] == 180) {
  79. sirena();
  80. open(LOG,">>$log") or die "No se pudo abrir el log $!\n";
  81. mandarmail($lugar[$c],$fallas[$c]) or print LOG "No esta andando el mail bien$!\n";
  82. print LOG "$hora: $lugar[$c] estuvo caído por 3 minutos, sono la alarma y se mando un mail.\n";
  83. }
  84. }
  85. else {
  86. my $hora=hora();
  87. if ($fallas[$c] > 4) {
  88. open(LOG,">>$log") or die "No se pudo abrir el log $!\n";
  89. $hora=hora();
  90. print LOG "$hora: $lugar[$c] está ahora activo, luego de estar $fallas[$c] segundos caido.\n";
  91. $fallas[$c] = 0;
  92. }
  93. elsif ($fallas[$c] > 180) {
  94. open(LOG,">>$log") or die "No se pudo abrir el log $!\n";
  95. $hora=hora();
  96. print LOG "$hora: $lugar[$c] está ahora activo, luego de estar $fallas[$c] segundos caido.\n";
  97. mandarmail($lugar[$c],$fallas[$c],"Ahora $lugar[$c] esta operativo");
  98. $fallas[$c] = 0;
  99. }
  100. }
  101. $c--;
  102. }
  103. sleep(1);
  104. }
  105.  
  106. sub ping {
  107. my ($prot, $add) = @_;
  108. my $ping = $prot ? "ping6" : "ping";
  109. return system("$ping -c 1 -i 1 -W 1 -q $add >> /dev/null");
  110. }
  111.  
  112. sub hora() { return strftime("%Y-%m-%d %H:%M:%S", localtime); }
  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. }
  119. sub mandarmail {
  120. my ($lugarm, $vfallas, $msg) = @_;
  121. my $muser = 'alerta@innova-red.net';
  122. my $mpass = 'mvelmgqh';
  123. my $mserver = '200.10.202.10';
  124. my $mto = 'gdevita@innova-red.net';
  125. my $mfrom_name = 'Alerta';
  126. my $mfrom_email = 'alerta@innova-red.net';
  127. my $msubject = "Enlace $lugarm";
  128. my $mmessage;
  129. if ($msg) {
  130. my $mmessage = $msg;
  131. }
  132. else {
  133. my $mmessage = "El enlace $lugarm esta caido hace $vfallas segundos, por favor revisar";
  134. }
  135. my $email=Mail->new($muser,$mpass,$mserver,$mfrom_email,$mto,$mfrom_name,$msubject,$mmessage);
  136.  
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement