mailer handler update
This commit is contained in:
parent
7ea7866aa1
commit
3df80bd300
@ -4,49 +4,51 @@ 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);
|
||||||
|
|
||||||
my $statuses = {
|
my $statuses = {
|
||||||
'2' => 'Critical',
|
'2' => 'Critical',
|
||||||
'1' => 'Warning',
|
'1' => 'Warning',
|
||||||
'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};
|
||||||
my $timestamp = localtime( $message->{client}->{timestamp} );
|
my $timestamp = localtime( $message->{client}->{timestamp} );
|
||||||
|
|
||||||
# Get check information
|
# Get check information
|
||||||
my $check_name = $message->{check}->{name};
|
my $check_name = $message->{check}->{name};
|
||||||
my $check_out = $message->{check}->{output};
|
my $check_out = $message->{check}->{output};
|
||||||
my $check_command = $message->{check}->{command};
|
my $check_command = $message->{check}->{command};
|
||||||
my $check_interval = $message->{check}->{interval};
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
# Count history
|
# Count history
|
||||||
my $ok_count = 0;
|
my $ok_count = 0;
|
||||||
my $warn_count = 0;
|
my $warn_count = 0;
|
||||||
my $crit_count = 0;
|
my $crit_count = 0;
|
||||||
my $total = @check_history;
|
my $total = @check_history;
|
||||||
foreach my $stat (@check_history) {
|
foreach my $stat (@check_history) {
|
||||||
if ( $stat eq '2' ) {
|
if ( $stat eq '2' ) {
|
||||||
$crit_count++;
|
$crit_count++;
|
||||||
}
|
}
|
||||||
elsif ( $stat eq '1' ) {
|
elsif ( $stat eq '1' ) {
|
||||||
$warn_count++;
|
$warn_count++;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
$ok_count++;
|
$ok_count++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
# Build mail body message
|
# Build mail body message
|
||||||
@ -69,20 +71,81 @@ 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
|
||||||
my $mail_to = 'admins@example.com';
|
= "$client_name with address $client_addr status: $check_status";
|
||||||
|
my $mail_to = 'admins@example.com';
|
||||||
|
|
||||||
# Make text file
|
# Make text file
|
||||||
my $mail_file = '/tmp/'.$message->{client}->{timestamp}.'mail.txt';
|
my $mail_file = '/tmp/' . $message->{client}->{timestamp} . 'mail.txt';
|
||||||
open my $mail_fh, '>', $mail_file or die "$!\n";
|
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";
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user