Created
March 29, 2012 22:20
-
-
Save jjhop/2244327 to your computer and use it in GitHub Desktop.
send_usage_alert.pl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl -w | |
# | |
# Skrypt wysylajacy ostrzezenie do @ALERT_RECIPIENTS jesli | |
# zajetosc powierzchni urzadzenia $MAIL_DEV przekroczy $ALERT_LEVEL | |
# | |
# Last mod: 2007-01-08 | |
# 2007 (C) Copyleft by GDP. | |
use strict; | |
# path to dir with virtual main domains | |
my $MAIL_DIR = '/var/spool/mail/virtual'; | |
# command witch showing disk space usage of virtual domains | |
my $DU_CMD_STRING = "du --si --max-depth=1 $MAIL_DIR"; | |
# device with virtual mail directory | |
my $MAIL_DEV = '/dev/sdb1'; | |
# command witch showing percent usage disk with mail directory | |
my $DF_CMD_STRING = "df --si | grep $MAIL_DEV"; | |
# list of recipients mail with alerts and warnings | |
my @ALERT_RECIPIENTS = ('rafal@gdp.pl','it@gdp.pl','pawelm@gdp.pl','sn@gdp.pl'); | |
# mail command path | |
my $MAIL_CMD = '/usr/bin/mail'; | |
# percent uage to send alert | |
my $ALERT_LEVEL = 70; | |
# percent usage to send warning | |
my $WARNING_LEVEL = 90; | |
# START | |
print "begin..."; | |
my %domain_size; | |
my $df_res = `$DF_CMD_STRING`; | |
my $du_res = `$DU_CMD_STRING`; | |
my @df_res = split( /[\s]{1,}/, $df_res ); | |
my $percent_u = substr( $df_res[4], 0, length($df_res[4])-1); | |
if( $percent_u >= $WARNING_LEVEL ) { | |
my $subject = "[Alert] Kończy sie miejsce na pocztę."; | |
send_alert( $subject, $du_res ); | |
} elsif( $percent_u >= $ALERT_LEVEL ) { | |
my $subject = "[Warning] Kończy sie miejsce na pocztę."; | |
send_alert( $subject, $du_res ); | |
} | |
# END | |
print "end\n"; | |
# | |
# procedure sending mail to person from recipients list | |
# | |
sub send_alert { | |
my $msg; | |
$msg = "Uwaga! Zajęto¶ć miejsca na dysku zawierajacym poczta\n"; | |
$msg.= "powoli osiaga krytyczna wartosc. [poczta.gdp.pl]\n\n"; | |
$msg.= "Pozostało wolnego miejsa: $df_res[3]\n"; | |
$msg.= "Procentowa zajęto¶ć dysku: $df_res[4]\n\n"; | |
my ( $subject, $report ) = @_; | |
$msg.= prepare_usage_report( $report ); | |
foreach( @ALERT_RECIPIENTS ) { | |
print "Wysylam mail to $_\n"; | |
open( MAIL, "|$MAIL_CMD -s \"$subject\" $_" ); | |
print MAIL "$msg\n\n"; | |
close (MAIL) or warn $! ? "Blad: $!" : "Blad ze statusem $?"; | |
} | |
} | |
# | |
# procedure preparig report content | |
# | |
sub prepare_usage_report { | |
my ( $result ) = @_; | |
my $to_return = "Raport zużycia powierzchni dyskowej dla domen.\n\n"; | |
my @result = split( /\n/, $result ); | |
foreach( @result ) { | |
my @res_line = split( /[\s]{1,}/, $_ ); | |
my @domain = split( /\//, $res_line[1] ); | |
if( @domain == 6 ) { | |
$domain_size{$domain[5]} = $res_line[0]; | |
} | |
} | |
foreach my $key( reverse sort cmp_size keys %domain_size ) { | |
$to_return .= $key . " => " . $domain_size{$key} . "\n"; | |
} | |
return $to_return; | |
} | |
# | |
# compare procedure to sort hash with | |
# domains and disk space order by disk space | |
# | |
sub cmp_size { | |
my $a_value = $domain_size{ $a }; | |
my $b_value = $domain_size{ $b }; | |
my %unit_val = ( | |
"k" => "1000", | |
"M" => "1000000", | |
"G" => "1000000000" | |
); | |
my $left_unit = substr( $a_value, -1 ); | |
my $right_unit = substr( $b_value, -1 ); | |
my $left_num = substr( $a_value, 0, length( $a_value ) - 1 ); | |
my $right_num = substr( $b_value, 0, length( $b_value ) - 1 ); | |
$left_num =~ tr/,/\./; | |
$right_num=~ tr/,/\./; | |
my $l_val = $left_num * $unit_val{$left_unit}; | |
my $r_val = $right_num * $unit_val{$right_unit}; | |
return $l_val <=> $r_val; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment