Skip to content
Snippets Groups Projects
Verified Commit bc9197d3 authored by David Beniamine's avatar David Beniamine
Browse files

Add mail test see #1

parent 526125d3
No related branches found
No related tags found
No related merge requests found
#!/usr/bin/perl
# Copyright (C) 2017 Tetras Libre <admin@tetras-libre.fr>
# Author: Beniamine, David <David@Beniamine.net>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
use strict;
use warnings;
use threads ('yield',
'stack_size' => 64*4096,
'exit' => 'threads_only',
'stringify');
use utf8;
use MIME::Lite;
sub do_sendmail($$$);
# Send subject, message by email to root
sub do_sendmail($$$){
my ($subject, $message, $retry) = @_;
utf8::encode($subject);
utf8::encode($message);
my $msg = MIME::Lite->new(
From => 'tetras-back',
To => 'root',
Subject => "[Tetras-back] $subject",
Type => 'multipart/mixed'
);
$msg->attr("content-type.charset" => "UTF-8");
$msg->attach(
Type => 'text/plain; charset=UTF-8',
Data => $message,
);
eval{ $msg->send() };
if($@){
if($retry == 0){
do_sendmail($subject,$message,1);
}
}else{
print("Etat envoyé par courriel");
}
}
do_sendmail("Courriel de test", "Ceci est un message de test du système de sauvegardes", 0);
...@@ -137,6 +137,19 @@ sub do_stop($$){ ...@@ -137,6 +137,19 @@ sub do_stop($$){
exit($error); exit($error);
} }
sub do_tests($){
my $test = shift;
if($test eq "mail"){
do_sendmail("Test", "This is a test message", 0);
}elsif($test eq "end"){
end_backup("Test success", 0);
end_backup("Test failure", 1);
}else{
$Logger->alert("Unknown test :'$test'");
exit(1);
}
}
# Notify clients of discs modifications # Notify clients of discs modifications
sub notify(){ sub notify(){
my $json = encode_json($CFG::CFG{DISKS}); my $json = encode_json($CFG::CFG{DISKS});
...@@ -381,6 +394,8 @@ sub start_daemon(){ ...@@ -381,6 +394,8 @@ sub start_daemon(){
notify(); notify();
}elsif ($line =~ /^STOP$/){ }elsif ($line =~ /^STOP$/){
do_stop("Arret demandé par la ligne de commande", 0); do_stop("Arret demandé par la ligne de commande", 0);
}elsif ($line =~ /^TEST (.*)$/){
do_tests($1);
}elsif ($line =~ /^REGISTER (.*)$/){ }elsif ($line =~ /^REGISTER (.*)$/){
$CFG::CFG{CLIENTS}{$1} = time(); $CFG::CFG{CLIENTS}{$1} = time();
save_config(); save_config();
...@@ -427,6 +442,8 @@ $ap->add_args( ...@@ -427,6 +442,8 @@ $ap->add_args(
help=>'Forget the given disc'], help=>'Forget the given disc'],
['--save', '-s', type=>'Pair', split=>' ', metavar=>'UUID=NAME', ['--save', '-s', type=>'Pair', split=>' ', metavar=>'UUID=NAME',
help=>'Mark disc UUID as known under name'], help=>'Mark disc UUID as known under name'],
['--test', '-t', metavar=>'mail',
help=>'Run a test, one of the following : [mail]'],
['--plug', '-p', type=>'Scalar', metavar=>"device", ['--plug', '-p', type=>'Scalar', metavar=>"device",
help=>"Notify $0 that a given device has been pluged, should be called by udev rule"], help=>"Notify $0 that a given device has been pluged, should be called by udev rule"],
['--unplug', '-u', type=>'Scalar', metavar=>"device", ['--unplug', '-u', type=>'Scalar', metavar=>"device",
...@@ -437,6 +454,8 @@ my $arguments = $ap->parse_args(); ...@@ -437,6 +454,8 @@ my $arguments = $ap->parse_args();
if ($arguments->get_attr('version')){ if ($arguments->get_attr('version')){
print "Tetras-back ".$version."\n"; print "Tetras-back ".$version."\n";
exit(0); exit(0);
}elsif($arguments->get_attr('test')){
send_command("TEST ".$arguments->get_attr('test'));
}elsif ($arguments->get_attr('start')){ }elsif ($arguments->get_attr('start')){
start_daemon(); start_daemon();
}elsif($arguments->get_attr('stop')){ }elsif($arguments->get_attr('stop')){
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment