Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- use lib '/home/guido/workspace';
- use Mail;
- use strict;
- use warnings;
- use POSIX qw(strftime);
- my @dtb=<*.db>;
- my $log="enlaces.log";
- my @lugar;
- my @IP;
- my @fallas;
- my @IPv6;
- my $c=0;
- my $dtb;
- my $max;
- my $v6="";
- sub hora;
- sub mandarmail;
- sub ping;
- sub sirena;
- (-e "/usr/bin/mpg123") or die "Falta instalar mpg123";
- #Toma input de .db existentes pero tambien acepta otros archivos como parametros del programa
- if (@ARGV){
- @dtb=(@dtb,@ARGV);
- }
- foreach $dtb (@dtb) {
- open (my $DBH,$dtb) or die "No se encontro ningun archivo .db";
- 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})/go) ||
- ( $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 ) )
- {
- #Guardo la IP en un hash
- push(@IP,"$&");
- #Guardo si es IPv6 en un hash
- push(@IPv6,"$v6");
- #Inicializo el hash de fallas
- push(@fallas, 0);
- #Puedo mostrar que es lo que cargo con esto
- #print "$lugar[$c] $IP[$c]\n";
- $c++;
- }
- }
- }
- }
- $max=$c-1;
- for(;;) {
- $c=$max;
- while ($c!=0) {
- if (ping($IPv6[$c],$IP[$c]) != 0 ) {
- $fallas[$c]++;
- my $hora=hora();
- if ($fallas[$c]) {
- open(LOGENLACE,">>$lugar[$c].log") or die "No se pudo abrir el log $!\n";
- print LOGENLACE "$hora: $lugar[$c] no contestó ping.\n";
- }
- if ($fallas[$c] == 5) {
- open(LOGENLACE,">>$lugar[$c].log") or die "No se pudo abrir el log $!\n";
- print LOGENLACE "$hora: $lugar[$c] falló $fallas[$c] veces.\n";
- }
- elsif ($fallas[$c] == 30) {
- open(LOG,">>$log") or die "No se pudo abrir el log $!\n";
- sirena();
- print LOG "$hora: $lugar[$c] estuvo caído por 30 segundos y sono la alarma.\n";
- }
- elsif ($fallas[$c] == 180) {
- sirena();
- open(LOG,">>$log") or die "No se pudo abrir el log $!\n";
- mandarmail($lugar[$c],$fallas[$c]) or print LOG "No esta andando el mail bien$!\n";
- print LOG "$hora: $lugar[$c] estuvo caído por 3 minutos, sono la alarma y se mando un mail.\n";
- }
- }
- else {
- my $hora=hora();
- if ($fallas[$c] > 4) {
- open(LOG,">>$log") or die "No se pudo abrir el log $!\n";
- $hora=hora();
- print LOG "$hora: $lugar[$c] está ahora activo, luego de estar $fallas[$c] segundos caido.\n";
- $fallas[$c] = 0;
- }
- elsif ($fallas[$c] > 180) {
- open(LOG,">>$log") or die "No se pudo abrir el log $!\n";
- $hora=hora();
- print LOG "$hora: $lugar[$c] está ahora activo, luego de estar $fallas[$c] segundos caido.\n";
- mandarmail($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 -W 1 -q $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";
- }
- sub mandarmail {
- my ($lugarm, $vfallas, $msg) = @_;
- my $muser = 'alerta@innova-red.net';
- my $mpass = 'mvelmgqh';
- my $mserver = '200.10.202.10';
- my $mto = 'gdevita@innova-red.net';
- my $mfrom_name = 'Alerta';
- my $mfrom_email = 'alerta@innova-red.net';
- my $msubject = "Enlace $lugarm";
- my $mmessage;
- if ($msg) {
- my $mmessage = $msg;
- }
- else {
- my $mmessage = "El enlace $lugarm esta caido hace $vfallas segundos, por favor revisar";
- }
- my $email=Mail->new($muser,$mpass,$mserver,$mfrom_email,$mto,$mfrom_name,$msubject,$mmessage);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement