Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- use strict;
- use warnings;
- use POSIX qw(strftime);
- use Mail;
- my @dtb=glob "*.db";
- my $log="enlaces.log";
- my @lugar;
- my @IP;
- my @fallas;
- my @IPv6;
- my $c=0;
- my $max;
- my $v6="";
- (-e "/usr/bin/mpg123") or die "Falta instalar mpg123";
- #Toma input de .db existentes pero tambien acepta otros archivos como parametros del programa
- push @dtb, @ARGV;
- foreach my $dtb (@dtb) {
- open (my $DBH,$dtb) or warn "No se encontro ningun archivo .db, si no usaste argumentos corta";
- while (<$DBH>) {
- chomp;
- #Guardo el lugar en un array
- if ( m/\b\w+-*\w*/ ) {
- push(@lugar, "$&");
- }
- #Analizo los Strings de cada linea por IPs Validas, así no guardamos datos que no sirven.
- unless (m/^#/) {
- if ( ( m/(\b([0-9]{1,3}\.){3}[0-9]{1,3})/) ||
- ( $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*$/ ) )
- {
- push(@IP,$&);
- push(@IPv6,$v6);
- push(@fallas, 0);
- $c++;
- }
- }
- }
- }
- $max=$c-1;
- for(;;) {
- $c=$max;
- my @result;
- while ($c!=0) {
- $result[$c]=ping($IPv6[$c],$IP[$c]);
- $c--;
- }
- $c=$max;
- while ($c!=0) {
- my $hora=hora();
- if ($result[$c] != 0 ) {
- $fallas[$c]++;
- if ($fallas[$c]) {
- open(my $linklog,">>$lugar[$c].log") or die "No se pudo abrir el log $!\n";
- print $linklog "$hora: $lugar[$c] no contestó ping.\n";
- close($linklog);
- }
- if ($fallas[$c] == 5) {
- open(my $linklog,">>$lugar[$c].log") or die "No se pudo abrir el log $!\n";
- print $linklog "$hora: $lugar[$c] falló $fallas[$c] veces.\n";
- close($linklog);
- }
- elsif ($fallas[$c] == 30) {
- sirena();
- open(my $linklog,">>$lugar[$c].log") or die "No se pudo abrir el log $!\n";
- print $linklog "$hora: $lugar[$c] estuvo caído por 30 segundos y sono la alarma.\n";
- close($linklog);
- }
- elsif ($fallas[$c] == 180) {
- sirena();
- open(my $linklog,">>$lugar[$c].log") or die "No se pudo abrir el log $!\n";
- sendemail($lugar[$c],$fallas[$c]) or print $linklog "No esta andando el mail bien$!\n";
- print $linklog "$hora: $lugar[$c] estuvo caído por 3 minutos, sono la alarma y se mando un mail.\n";
- close($linklog);
- }
- }
- else {
- if ($fallas[$c] > 4) {
- open(my $linklog,">>$lugar[$c].log") or die "No se pudo abrir el log $!\n";
- print $linklog "$hora: $lugar[$c] está ahora activo, luego de estar $fallas[$c] segundos caido.\n";
- close($linklog);
- $fallas[$c] = 0;
- }
- elsif ($fallas[$c] > 180) {
- open(my $linklog,">>$lugar[$c].log") or die "No se pudo abrir el log $!\n";
- print $linklog "$hora: $lugar[$c] está ahora activo, luego de estar $fallas[$c] segundos caido.\n";
- close($linklog);
- sendemail($lugar[$c],$fallas[$c],"Ahora $lugar[$c] esta operativo");
- $fallas[$c] = 0;
- }
- }
- $c--;
- }
- # sleep(1);
- }
- sub ping {
- my ($prot, $add) = @_;
- my $ping = $prot ? "ping6" : "ping";
- return system("$ping -c 1 -i 1 -q -W 1 $add >> /dev/null");
- }
- sub hora {
- return strftime("%Y-%m-%d %H:%M:%S", localtime);
- }
- sub sirena {
- my @alarma = ("mpg123", "-q", "Sirens.mp3");
- system(@alarma) == 0
- or die "Perl falló al llamar a mpg321 $!\n";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement