mailer handler update

This commit is contained in:
Denis Zheleztsov 2015-07-17 15:08:19 +03:00
parent 7ea7866aa1
commit 3df80bd300

View File

@ -4,6 +4,8 @@ use strict;
use warnings; use warnings;
use JSON; use JSON;
use Data::Dumper;
my $json = <>; my $json = <>;
my $message = decode_json($json); my $message = decode_json($json);
@ -14,7 +16,6 @@ my $statuses = {
'0' => 'Ok' '0' => 'Ok'
}; };
# Get client values # Get client values
my $client_name = $message->{client}->{name}; my $client_name = $message->{client}->{name};
my $client_addr = $message->{client}->{address}; my $client_addr = $message->{client}->{address};
@ -28,6 +29,7 @@ my $check_interval = $message->{check}->{interval};
my $check_status = $statuses->{ $message->{check}->{status} }; my $check_status = $statuses->{ $message->{check}->{status} };
my @ch = $message->{check}->{history}; my @ch = $message->{check}->{history};
my @check_history; my @check_history;
foreach my $c (@ch) { foreach my $c (@ch) {
@check_history = @$c; @check_history = @$c;
} }
@ -70,7 +72,8 @@ CRITICAL: $crit_count
# Send email # Send email
my $mail_cmd = 'mail'; my $mail_cmd = 'mail';
my $mail_subg = "$client_name with address $client_addr status: $check_status"; my $mail_subg
= "$client_name with address $client_addr status: $check_status";
my $mail_to = 'admins@example.com'; my $mail_to = 'admins@example.com';
# Make text file # Make text file
@ -79,10 +82,70 @@ open my $mail_fh, '>', $mail_file or die "$!\n";
print $mail_fh $mail_body; print $mail_fh $mail_body;
close $mail_fh; close $mail_fh;
# Send it now! # Write current status to file
`cat $mail_file | $mail_cmd -s "'$mail_subg'" $mail_to`; sub write_status {
my ( $check_name, $status ) = @_;
my $check_run_file = '/etc/sensu/handlers/run/' . $check_name;
open my $run, '>', $check_run_file or die "Cannot open file: $!\n";
print $run $status;
close($run);
}
# Get previous status
sub get_previous_status {
my ( $check_name, $status ) = @_;
my $check_run_file = '/etc/sensu/handlers/run/' . $check_name;
my $previous_check_status;
open my $run, '<', $check_run_file or warn "Cannot open file: $!\n";
if ($run) {
while (<$run>) {
$previous_check_status = $_;
}
close($run);
return $previous_check_status;
}
# New checks hack
else {
write_status( $check_name, $status );
return -1;
}
}
# Check status
sub status_change {
my ($message) = @_;
my $check_name = $message->{check}->{name};
# Current check status
my $current_check_status = $message->{check}->{status};
# Get provious check status
my $previous_check_status
= get_previous_status( $check_name, $current_check_status );
if ( ( $previous_check_status == -1 )
or ( $previous_check_status eq $current_check_status ) )
{
return -1;
}
else {
return 0;
}
}
# Send it if status changed
if ( status_change == 0 ) {
`cat $mail_file | $mail_cmd -v -s "'$mail_subg'" $mail_to`;
}
# Remove temp file # Remove temp file
my @rm_cmd = ( 'rm', '-f', $mail_file ); my @rm_cmd = ( 'rm', '-f', $mail_file );
system(@rm_cmd) == 0 or die "$!\n"; system(@rm_cmd) == 0 or die "$!\n";