Adding support to create mail reports.

This commit is contained in:
sergiotarxz 2021-11-13 21:26:42 +01:00
parent 91562a3ae3
commit ea44ffb8ff
4 changed files with 103 additions and 25 deletions

View File

@ -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) {

View File

@ -10,9 +10,9 @@ use Email::Sender::Transport::SMTP;
use Email::Sender::Simple qw/sendmail/; use Email::Sender::Simple qw/sendmail/;
use Email::MIME; use Email::MIME;
const my $MAIL_CONFIG_FILE => '/etc/cualsea/mail.conf'; const my $MAIL_CONFIG_FILE => '/etc/cualsea/mail.conf';
const my $REPORT_TO_CONFIG_FILE => '/etc/cualsea/report_to'; const my $REPORT_TO_CONFIG_FILE => '/etc/cualsea/report_to';
const my $FROM_CONFIG_FILE => '/etc/cualsea/from'; const my $FROM_CONFIG_FILE => '/etc/cualsea/from';
sub new { sub new {
my $class = shift; my $class = shift;
@ -25,29 +25,93 @@ sub notify_service_down {
my $mime = Email::MIME->create( my $mime = Email::MIME->create(
header_str => [ header_str => [
From => $self->from, From => $self->from,
To => [ $self->report_to ], To => [ $self->report_to ],
Subject => "Service $name down", Subject => "Service $name down",
], ],
parts => [ parts => [
Email::MIME->create( Email::MIME->create(
attributes => { attributes => {
content_type => 'text/plain', content_type => 'text/plain',
disposition => 'attachment', disposition => 'attachment',
charset => 'UTF-8', charset => 'UTF-8',
encoding => "8bit", encoding => "8bit",
}, },
body_str => "$name is not up", body_str => "$name is not up",
) )
] ]
); );
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;
@ -57,9 +121,10 @@ 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;
@ -69,14 +134,15 @@ sub from {
} }
sub transport { 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
while (my $line = <$fh>) { or die "Unable to get transport from config file";
my ($key, $value) = $line =~ /^(.*?)=(.*?)\s*$/; while ( my $line = <$fh> ) {
if ($key eq 'hosts') { my ( $key, $value ) = $line =~ /^(.*?)=(.*?)\s*$/;
$value = [ $value ]; if ( $key eq 'hosts' ) {
$value = [$value];
} }
$parameters{$key} = $value; $parameters{$key} = $value;
} }

View File

@ -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'],

View File

@ -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();