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,
date_execution TEXT,
is_up INTEGER
);'
);',
'ALTER TABLE monitor
ADD COLUMN name TEXT',
);
sub dbh {
@ -50,7 +52,6 @@ sub run_migrations {
my $dbh = shift;
my $current_migration = _get_current_migration_number($dbh);
say $current_migration;
say scalar @migrations;
if ($current_migration < scalar @migrations) {
my @needed_migrations = @migrations[$current_migration .. $#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::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 $FROM_CONFIG_FILE => '/etc/cualsea/from';
const my $FROM_CONFIG_FILE => '/etc/cualsea/from';
sub new {
my $class = shift;
@ -25,29 +25,93 @@ sub notify_service_down {
my $mime = Email::MIME->create(
header_str => [
From => $self->from,
To => [ $self->report_to ],
From => $self->from,
To => [ $self->report_to ],
Subject => "Service $name down",
],
parts => [
Email::MIME->create(
attributes => {
content_type => 'text/plain',
disposition => 'attachment',
charset => 'UTF-8',
encoding => "8bit",
disposition => 'attachment',
charset => 'UTF-8',
encoding => "8bit",
},
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 {
my $self = shift;
if (!defined $self->{report_to}) {
open my $fh, '<', $REPORT_TO_CONFIG_FILE or die "Unable to get mail to report from config file";
my $self = shift;
if ( !defined $self->{report_to} ) {
open my $fh, '<', $REPORT_TO_CONFIG_FILE
or die "Unable to get mail to report from config file";
my $email = <$fh>;
chomp $email;
close $fh;
@ -57,9 +121,10 @@ sub report_to {
}
sub from {
my $self = shift;
if (!defined $self->{from}) {
open my $fh, '<', $FROM_CONFIG_FILE or die "Unable to get mail From from config file";
my $self = shift;
if ( !defined $self->{from} ) {
open my $fh, '<', $FROM_CONFIG_FILE
or die "Unable to get mail From from config file";
my $email = <$fh>;
chomp $email;
close $fh;
@ -69,14 +134,15 @@ sub from {
}
sub transport {
my $self = shift;
if (!defined $self->{transport}) {
my $self = shift;
if ( !defined $self->{transport} ) {
my %parameters;
open my $fh, '<', $MAIL_CONFIG_FILE or die "Unable to get transport from config file";
while (my $line = <$fh>) {
my ($key, $value) = $line =~ /^(.*?)=(.*?)\s*$/;
if ($key eq 'hosts') {
$value = [ $value ];
open my $fh, '<', $MAIL_CONFIG_FILE
or die "Unable to get transport from config file";
while ( my $line = <$fh> ) {
my ( $key, $value ) = $line =~ /^(.*?)=(.*?)\s*$/;
if ( $key eq 'hosts' ) {
$value = [$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 = (
add => {
params => [
@ -190,6 +200,7 @@ my %COMMANDS = (
report => {
params => [],
validator => sub { },
handle => \&handle_report,
},
start => {
params => ['name'],

View File

@ -56,8 +56,8 @@ EOF
$started = !system 'ps', '-p', $pid;
close $fh;
}}
$dbh->do( 'INSERT INTO monitor (date_execution, is_up) VALUES (?, ?);',
undef, DateTime->now() . '', $started );
$dbh->do( 'INSERT INTO monitor (date_execution, name, is_up) VALUES (?, ?);',
undef, DateTime->now() . '', $name, $started );
if (!$started) {
say "$name stopped";
my $mailer = Cualsea::Server::Mail->new();