Adding support to create mail reports.
This commit is contained in:
parent
91562a3ae3
commit
ea44ffb8ff
@ -30,7 +30,9 @@ my @migrations = (
|
|||||||
id INTEGER PRIMARY KEY,
|
id INTEGER PRIMARY KEY,
|
||||||
date_execution TEXT,
|
date_execution TEXT,
|
||||||
is_up INTEGER
|
is_up INTEGER
|
||||||
);'
|
);',
|
||||||
|
'ALTER TABLE monitor
|
||||||
|
ADD COLUMN name TEXT',
|
||||||
);
|
);
|
||||||
|
|
||||||
sub dbh {
|
sub dbh {
|
||||||
@ -50,7 +52,6 @@ sub run_migrations {
|
|||||||
my $dbh = shift;
|
my $dbh = shift;
|
||||||
my $current_migration = _get_current_migration_number($dbh);
|
my $current_migration = _get_current_migration_number($dbh);
|
||||||
say $current_migration;
|
say $current_migration;
|
||||||
say scalar @migrations;
|
|
||||||
if ($current_migration < scalar @migrations) {
|
if ($current_migration < scalar @migrations) {
|
||||||
my @needed_migrations = @migrations[$current_migration .. $#migrations];
|
my @needed_migrations = @migrations[$current_migration .. $#migrations];
|
||||||
for my $migration (@needed_migrations) {
|
for my $migration (@needed_migrations) {
|
||||||
|
@ -44,10 +44,74 @@ sub notify_service_down {
|
|||||||
sendmail( $mime, { transport => $self->transport } );
|
sendmail( $mime, { transport => $self->transport } );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sub report {
|
||||||
|
my $self = shift;
|
||||||
|
my $dbh = Cualsea::Server::DB->dbh();
|
||||||
|
my $current_date = DateTime->now;
|
||||||
|
my $message = "$current_date\n\n";
|
||||||
|
$message .= "@{[`uname -a`]} @{[`free -m`]} @{[`uptime`]}\n\n";
|
||||||
|
$message .= "List of services\n\n";
|
||||||
|
my $services = $dbh->selectall_arrayref( <<'EOF', { Slice => {} } );
|
||||||
|
SELECT name FROM services;
|
||||||
|
EOF
|
||||||
|
for my $service (@$services) {
|
||||||
|
$message .= "@{[$service->{name}]}\n";
|
||||||
|
}
|
||||||
|
$message .= "\nLast hour monitor\n\n";
|
||||||
|
my $monitor = $dbh->selectall_arrayref(
|
||||||
|
<<'EOF', { Slice => {} }, $current_date->clone->add( hours => -1 ) . '' );
|
||||||
|
SELECT date_execution, name, is_up FROM monitor where date_execution > ?;
|
||||||
|
EOF
|
||||||
|
|
||||||
|
for my $register (@$monitor) {
|
||||||
|
my $date_execution = $register->{date_execution};
|
||||||
|
my $name = $register->{name};
|
||||||
|
my $is_up = $register->{is_up};
|
||||||
|
|
||||||
|
$message .=
|
||||||
|
"$date_execution @{[ defined $name ? $name : '']} @{[$is_up ? 'started' : 'stopped']}\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
$message .= "\nLast 20 commands\n\n";
|
||||||
|
|
||||||
|
my $log = $dbh->selectall_arrayref( <<'EOF', { Slice => {} } );
|
||||||
|
SELECT date_execution, parameters, result FROM log ORDER BY id DESC LIMIT 20;
|
||||||
|
EOF
|
||||||
|
for my $register (@$log) {
|
||||||
|
my $date_execution = $register->{date_execution};
|
||||||
|
my $parameters = $register->{parameters};
|
||||||
|
my $result = $register->{result};
|
||||||
|
|
||||||
|
$message .= "$date_execution $parameters $result\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
my $mime = Email::MIME->create(
|
||||||
|
header_str => [
|
||||||
|
From => $self->from,
|
||||||
|
To => [ $self->report_to ],
|
||||||
|
Subject => "Service report.",
|
||||||
|
],
|
||||||
|
parts => [
|
||||||
|
Email::MIME->create(
|
||||||
|
attributes => {
|
||||||
|
content_type => 'text/plain',
|
||||||
|
disposition => 'attachment',
|
||||||
|
charset => 'UTF-8',
|
||||||
|
encoding => "8bit",
|
||||||
|
},
|
||||||
|
body_str => $message,
|
||||||
|
)
|
||||||
|
]
|
||||||
|
);
|
||||||
|
sendmail( $mime, { transport => $self->transport } );
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
sub report_to {
|
sub report_to {
|
||||||
my $self = shift;
|
my $self = shift;
|
||||||
if ( !defined $self->{report_to} ) {
|
if ( !defined $self->{report_to} ) {
|
||||||
open my $fh, '<', $REPORT_TO_CONFIG_FILE or die "Unable to get mail to report from config file";
|
open my $fh, '<', $REPORT_TO_CONFIG_FILE
|
||||||
|
or die "Unable to get mail to report from config file";
|
||||||
my $email = <$fh>;
|
my $email = <$fh>;
|
||||||
chomp $email;
|
chomp $email;
|
||||||
close $fh;
|
close $fh;
|
||||||
@ -59,7 +123,8 @@ sub report_to {
|
|||||||
sub from {
|
sub from {
|
||||||
my $self = shift;
|
my $self = shift;
|
||||||
if ( !defined $self->{from} ) {
|
if ( !defined $self->{from} ) {
|
||||||
open my $fh, '<', $FROM_CONFIG_FILE or die "Unable to get mail From from config file";
|
open my $fh, '<', $FROM_CONFIG_FILE
|
||||||
|
or die "Unable to get mail From from config file";
|
||||||
my $email = <$fh>;
|
my $email = <$fh>;
|
||||||
chomp $email;
|
chomp $email;
|
||||||
close $fh;
|
close $fh;
|
||||||
@ -72,7 +137,8 @@ sub transport {
|
|||||||
my $self = shift;
|
my $self = shift;
|
||||||
if ( !defined $self->{transport} ) {
|
if ( !defined $self->{transport} ) {
|
||||||
my %parameters;
|
my %parameters;
|
||||||
open my $fh, '<', $MAIL_CONFIG_FILE or die "Unable to get transport from config file";
|
open my $fh, '<', $MAIL_CONFIG_FILE
|
||||||
|
or die "Unable to get transport from config file";
|
||||||
while ( my $line = <$fh> ) {
|
while ( my $line = <$fh> ) {
|
||||||
my ( $key, $value ) = $line =~ /^(.*?)=(.*?)\s*$/;
|
my ( $key, $value ) = $line =~ /^(.*?)=(.*?)\s*$/;
|
||||||
if ( $key eq 'hosts' ) {
|
if ( $key eq 'hosts' ) {
|
||||||
|
@ -160,6 +160,16 @@ sub handle_restart {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sub handle_report {
|
||||||
|
my $mailer = Cualsea::Server::Mail->new();
|
||||||
|
$mailer->report;
|
||||||
|
return {
|
||||||
|
is_error => 0,
|
||||||
|
status => 200,
|
||||||
|
desc => 'Successfully sent report.',
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
my %COMMANDS = (
|
my %COMMANDS = (
|
||||||
add => {
|
add => {
|
||||||
params => [
|
params => [
|
||||||
@ -190,6 +200,7 @@ my %COMMANDS = (
|
|||||||
report => {
|
report => {
|
||||||
params => [],
|
params => [],
|
||||||
validator => sub { },
|
validator => sub { },
|
||||||
|
handle => \&handle_report,
|
||||||
},
|
},
|
||||||
start => {
|
start => {
|
||||||
params => ['name'],
|
params => ['name'],
|
||||||
|
@ -56,8 +56,8 @@ EOF
|
|||||||
$started = !system 'ps', '-p', $pid;
|
$started = !system 'ps', '-p', $pid;
|
||||||
close $fh;
|
close $fh;
|
||||||
}}
|
}}
|
||||||
$dbh->do( 'INSERT INTO monitor (date_execution, is_up) VALUES (?, ?);',
|
$dbh->do( 'INSERT INTO monitor (date_execution, name, is_up) VALUES (?, ?);',
|
||||||
undef, DateTime->now() . '', $started );
|
undef, DateTime->now() . '', $name, $started );
|
||||||
if (!$started) {
|
if (!$started) {
|
||||||
say "$name stopped";
|
say "$name stopped";
|
||||||
my $mailer = Cualsea::Server::Mail->new();
|
my $mailer = Cualsea::Server::Mail->new();
|
||||||
|
Loading…
Reference in New Issue
Block a user