cualsea/cualsea-server/lib/Cualsea/Server/Mail.pm

155 lines
4.4 KiB
Perl

package Cualsea::Server::Mail;
use v5.30.0;
use strict;
use warnings;
use Const::Fast;
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 $REPORT_TO_CONFIG_FILE => '/etc/cualsea/report_to';
const my $FROM_CONFIG_FILE => '/etc/cualsea/from';
sub new {
my $class = shift;
return bless {}, $class;
}
sub notify_service_down {
my $self = shift;
my $name = shift;
my $mime = Email::MIME->create(
header_str => [
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",
},
body_str => "$name is not up",
)
]
);
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 $email = <$fh>;
chomp $email;
close $fh;
$self->{report_to} = $email;
}
return $self->{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 $email = <$fh>;
chomp $email;
close $fh;
$self->{from} = $email;
}
return $self->{from};
}
sub 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];
}
$parameters{$key} = $value;
}
close $fh;
$self->{transport} = Email::Sender::Transport::SMTP->new(%parameters);
}
return $self->{transport};
}
1;