FileSystem Alert

Publicado por Leonardo Berbert Gomes (última atualização em 29/07/2020)

[ Hits: 883 ]

Homepage: https://www.linkedin.com/in/leoberbert

Download fs_alert.pl




Script para monitoramento de diversos filesystems e envio de e-mail sem uso de autenticação.

  



Esconder código-fonte

#!/bin/perl
#
use Sys::Hostname;
use Data::Dumper;
use Time::Local;
#

($sec,$min,$hour,$day,$month,$year) = (localtime(time))[0,1,2,3,4,5]; $month +=1;$year +=1900;

if ($day =~ /^\d$/) { $day = "0" . $day;}
if ($min =~ /^\d$/) { $min = "0" . $min;}
if ($sec =~ /^\d$/) { $sec = "0" . $sec;}
if ($hour =~ /^\d$/) { $hour = "0" . $hour;}
if ($month =~ /^\d$/) { $month = "0" . $month;}

my $treshold = 85; ## Altere aqui pelo valor que irá gerar o alerta
my $fs = "/home,/var/log"; ## Inclua todos os FileSystem que deseja monitorar
my @fs = split /,/, $fs;

my @list = ();

for my $i (0 .. $#fs){
     push @list, qr/^$fs[$i]/;
}


open (COMMAND,"df -h |");
while ($line = <COMMAND>){ 
        chomp $line;
        if ($line =~ /^Filesystem/) {next};
        my @array = split /\s+/, $line;
        $used_perc = @array[4];
        $mount_point = @array[5];
        $used_perc =~s/\%//g;
        if ($mount_point ~~ @list) { 
                if ($used_perc >= $treshold) {
                        $hash{"FileSystem: $mount_point \t| used: $used_perc%"}++;
                }
        }
}

my $mail_file = "mail" .  "_" . $year . $month . $day . $hour . $min . $sec . ".tmp";
open(OUT,">$mail_file") or die "Can't open : $!";

foreach $I ( sort keys %hash) { 
        print OUT "$I\n";
} 
close(OUT);

if ( -z $mail_file ) {
        unlink $mail_file;
} else {
        my $emails="leoberbert\@uol.com.br,leoberbert\@gmail.com.br"; ## Inclua todos os email que irão receber o alerta
        my $hostname = hostname();
        my $server = "meuserver_smtp_sem_autenticacao.com.br:25"; ## Altere pelo seu servidor SMTP
        my $message = "Filesystem $mount_point acima do Treshold. ";
        system("cat $mail_file | mailx -S smtp=$server -s \"Alerta: Espaço em disco do servidor $hostname está no limite\" -v $emails");
        unlink $mail_file;
} 

Scripts recomendados

Diminuir ou aumentar o brilho de notebook

Viva o Linux for Torsmo

Wallpapers aleatórios no Fluxbox

Monitor Process

Beep-Media-Player for Torsmo


  

Comentários

Nenhum comentário foi encontrado.


Contribuir com comentário




Patrocínio

Site hospedado pelo provedor RedeHost.
Linux banner

Destaques

Artigos

Dicas

Tópicos

Top 10 do mês

Scripts