Skip to content

Commit

Permalink
Task for checking available space
Browse files Browse the repository at this point in the history
  • Loading branch information
lucatacconi committed Mar 22, 2024
1 parent 8396fc8 commit 9d83057
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ tasks/*
!tasks/.htaccess
!tasks/testTasks.php
!tasks/testAliveTasks.php
!tasks/chkFreeSpaceTasks.php

# Ignore all logs files everywhere
*.log
Expand Down
34 changes: 33 additions & 1 deletion routes/api/tools.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@
;

$messageObject->addTo(new Address($recipient_mail, $recipient_name));

$check_mail = $mailer->send($messageObject);

if( $check_mail == null ){
Expand All @@ -125,4 +125,36 @@
->withHeader("Content-Type", "application/json");
});

$group->get('/disk/free-space', function (Request $request, Response $response, array $args) {

$data = 0;

$app_configs = $this->get('configs')["app_configs"];
$base_path =$app_configs["paths"]["base_path"];

if(empty($_ENV["CRUNZ_BASE_DIR"])){
$crunz_base_dir = $base_path;
}else{
$crunz_base_dir = $_ENV["CRUNZ_BASE_DIR"];
}

if(empty($_ENV["LOGS_DIR"])) throw new Exception("ERROR - Logs directory configuration empty");

if(substr($_ENV["LOGS_DIR"], 0, 2) == "./"){
$LOGS_DIR = $base_path . "/" . $_ENV["LOGS_DIR"];
}else{
$LOGS_DIR = $_ENV["LOGS_DIR"];
}

$total_space = $total_space_disp = disk_total_space($LOGS_DIR);
$free_space = $free_space_disp = disk_free_space($LOGS_DIR);
$used_space = $used_space_disp = $total_space - $free_space;

$data = $free_space;

$response->getBody()->write(json_encode($data, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT));
return $response->withStatus(200)
->withHeader("Content-Type", "application/json");
});

});
101 changes: 101 additions & 0 deletions tasks/chkFreeSpaceTasks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php

use Crunz\Schedule;
use Symfony\Component\Yaml\Yaml;
use Symfony\Component\Mailer\Transport;
use Symfony\Component\Mailer\Mailer;
use Symfony\Component\Mime\Email;
use Symfony\Component\Mime\Address;

$schedule = new Schedule();

$task = $schedule->run(function() {

$aRESULT = [];

try {

$base_path = __DIR__."/..";

if(!file_exists ( $base_path."/crunz.yml" )) throw new Exception("ERROR - Crunz.yml configuration file not found");
$crunz_config_yml = file_get_contents($base_path."/crunz.yml");

if(empty($crunz_config_yml)) throw new Exception("ERROR - Crunz configuration file empty");

try {
$crunz_config = Yaml::parse($crunz_config_yml);
} catch (ParseException $exception) {
throw new Exception("ERROR - Crunz configuration file error");
}


$dotenv = Dotenv\Dotenv::createImmutable($base_path);
$dotenv->load();

if(empty($_ENV["LOGS_DIR"])) throw new Exception("ERROR - Logs directory configuration empty");

if(substr($_ENV["LOGS_DIR"], 0, 2) == "./"){
$LOGS_DIR = $base_path . "/" . $_ENV["LOGS_DIR"];
}else{
$LOGS_DIR = $_ENV["LOGS_DIR"];
}

$free_space = $free_space_disp = disk_free_space($LOGS_DIR);

$megabyte_alert = 300;

$subject = 'Low space warning';
$message = "The log partition is running out of space. ".number_format($free_space_disp / 1024 / 1024, 2)."Mb left.";

if($free_space < ($megabyte_alert * 1024 * 1024)){

if( !empty($crunz_config)
&& !empty($crunz_config["mailer"])
&& !empty($crunz_config["mailer"]["transport"]) && $crunz_config["mailer"]["transport"] == 'smtp'
&& !empty($crunz_config["mailer"]["recipients"])
&& !empty($crunz_config["mailer"]["sender_name"])
&& !empty($crunz_config["mailer"]["sender_email"])
&& !empty($crunz_config["smtp"])
&& !empty($crunz_config["smtp"]["host"])
&& !empty($crunz_config["smtp"]["port"])
){

$userPart = '';
if(!empty($crunz_config["smtp"]["username"])){
$userPart = $crunz_config["smtp"]["username"].":".$crunz_config["smtp"]["password"]."@";
}

$dsn = "smtp://".$userPart.$crunz_config["smtp"]["host"].":".$crunz_config["smtp"]["port"]."?verifyPeer=".$crunz_config["smtp"]["encryption"];

$transport = Transport::fromDsn($dsn);
$mailer = new Mailer($transport);

$from = new Address($crunz_config["mailer"]["sender_email"], $crunz_config["mailer"]["sender_name"]);

$messageObject = (new Email())
->from($from)
->subject($subject)
->text($message)
;

foreach ($crunz_config["mailer"]["recipients"] ?? [] as $recipient_name => $recipient_mail) {
$messageObject->addTo(new Address($recipient_mail, $recipient_name));
}

$mailer->send($messageObject);
}else{
throw new Exception("ERROR - Email configuration not present");
}
}

} catch (Exception $e) {
throw new Exception($e->getMessage());
}

});

$task
->description('Check available disk space. Possibly warns in case of running out of space.')
->daily();

return $schedule;

0 comments on commit 9d83057

Please sign in to comment.