Ca devrait repondre à ton besoin. Cette version intègre systématiquement une notif
// version bloc code
$version = '19/05/2024 14:00';
$scenario->setLog('┌──────────── Logs bloc code - version du ' . $version);
/**
* Class and Function List:
* Function list:
* - csv_to_array()
* Classes list:
*/
function csv_to_array($filename = '', $delimiter = ',') {
if (!file_exists($filename) || !is_readable($filename)) return false;
$header = array(
'taille',
'date',
'heure',
'file'
);
$data = array();
if (($handle = fopen($filename, 'r')) !== false) {
while (($row = fgetcsv($handle, 1000, $delimiter)) !== false) {
if (!$header) $header = $row;
else $data[] = array_combine($header, $row);
}
fclose($handle);
}
return $data;
}
// parametres
$tags = $scenario->getTags(); //récupère la liste des tags
$cmdIdListe = str_replace('#', '', $tags['#cmdListe#']);
$seuilTailleMax = $tags['#seuilTailleMax#'];
$seuilTailleMin = $tags['#seuilTailleMin#'];
$AlerteCentreMsg = $tags['#AlerteCentreMsg#'];
$AlerteNotifJC = $tags['#AlerteNotifJC#'];
$AlerteNotifTelegram = $tags['#AlerteNotifTelegram#'];
$fichierListe = '/var/www/html/log/cloudsyncpro.#' . $cmdIdListe;
if ($AlerteNotifJC) $cmdNotifJC = cmd::byId(str_replace('#', '', $tags['#cmdNotifJC#']));
if ($AlerteNotifTelegram) $cmdNotifTelegram = cmd::byId(str_replace('#', '', $tags['#cmdNotifTelegram#']));
$scenario->setLog('| Fichier log : ' . $fichierListe);
$scenario->setLog('| Taille min : ' . $seuilTailleMin . 'Mo');
$scenario->setLog('| Taille max : ' . $seuilTailleMax . 'Mo');
// mise à jour de la liste
cmd::byId($cmdIdListe)->execCmd();
if (!file_exists($fichierListe) || !is_readable($fichierListe)) {
$scenario->setLog('| /!\ le fichier ' . $fichierListe . ' n\'existe pas ou n\'est pas lisible');
if ($AlerteCentreMsg) message::add('Backup', 'Vérification des backups, ERREUR : le fichier ' . $fichierListe . ' n\'existe pas ou n\'est pas lisible');
if ($AlerteNotifJC) $cmdNotifJC->execCmd($options = array(
'title' => 'title=Defaut BACKUP ' . $site,
'message' => 'Vérification des backups, ERREUR : le fichier ' . $fichierListe . ' n\'existe pas ou n\'est pas lisible'
) , $cache = 0);
if ($AlerteNotifTelegram) $cmdNotifTelegram->execCmd($_options = array(
'message' => 'Vérification des backups, ERREUR : le fichier ' . $fichierListe . ' n\'existe pas ou n\'est pas lisible'
) , $cache = 0);
}
else {
// mise à jour de la liste
cmd::byId($cmdIdListe)->execCmd();
// Extraction des données du fichier dans un tableau
$data = csv_to_array($fichierListe, ' ');
// suppression des lignes vides
for ($n = 0;$n < count($data);$n++) {
if (empty($data[$n][taille])) {
array_splice($data, $n, 1);
}
}
// recherche des sites de backup concernés
for ($n = 0;$n < count($data);$n++) {
$file = explode('-', $data[$n][file]);
$sites[] = $file[1];
}
$sites = array_unique($sites);
foreach ($sites as $site) {
$nbBackup = 0;
$tailleTotale = 0;
$tailleMax = '';
$tailleMin = '';
$backupToday = 0;
$tailleFirstBackup = 0;
$tailleLastBackup = 0;
$timestampFirstBackup = '';
$timestampLastBackup = '';
$erreur = 0 ;
for ($n = 0;$n < count($data);$n++) {
$file = explode('-', $data[$n][file]);
if ($site == $file[1]) {
// $scenario->setLog($site.' - '.$file[1]) ;
$taille = round($data[$n][taille] / 1024 / 1024);
$file = explode('-', $data[$n][file]);
$jeedom = $file[1];
$version = $file[2];
// $scenario->setLog($jeedom.' version '.$version.' taille '.$taille.'Mo '.date("d/m/y", $date));
// nb de backup, taille totale et taille des backups
$nbBackup++;
$tailleTotale = $tailleTotale + $taille;
if ($taille > $tailleMax || empty($tailleMax)) $tailleMax = $taille;
if ($taille < $tailleMin || empty($tailleMin)) $tailleMin = $taille;
// date des backu^p
$timestampBackup = mktime(0, 0, 0, $file[4], $file[5], $file[3]); // timstamp du backup )à 0à:00
$timestampNow = mktime(0, 0, 0, date('m') , date('d') , date('Y'));
if ($timestampBackup == $timestampNow) $backupToday = 1;
if ($timestampBackup <= $timestampFirstBackup || empty($timestampFirstBackup)) {
$timestampFirstBackup = $timestampBackup;
$tailleFirstBackup = $taille ;
}
if ($timestampBackup >= $timestampLastBackup || empty($timestampLastBackup)) {
$timestampLastBackup = $timestampBackup;
$tailleLastBackup = $taille ;
}
}
}
$scenario->setLog('| Résumé : ' . $site . ' (Version : ' . $version . ')');
$scenario->setLog('| Nb backup presents : ' . $nbBackup);
$scenario->setLog('| 1er backup dispo : ' . date('d/m/Y', $timestampFirstBackup).' ('.$tailleFirstBackup.'Mo)');
$scenario->setLog('| dernier backup dispo : ' . date('d/m/Y', $timestampLastBackup).' ('.$tailleLastBackup.'Mo)');
$scenario->setLog('| Taille moyenne : ' . round($tailleTotale / $nbBackup) . 'Mo');
// gestion des erreurs
if ($tailleMax >= $seuilTailleMax || $tailleMin <= $seuilTailleMin) {
$erreur = 1 ;
$scenario->setLog('| /!\ Pb de taille sur un backup - merci de verifier (taille min : ' . $tailleMin . 'Mb, max :' . $tailleMax . 'Mb)');
if ($AlerteCentreMsg) message::add('Backup', $site . ' - Pb de taille sur une sauvegarde - merci de verifier (taille min : ' . $tailleMin . 'Mb, max :' . $tailleMax . 'Mb)');
if ($AlerteNotifJC) $cmdNotifJC->execCmd($options = array(
'title' => 'title=Defaut BACKUP ' . $site,
'message' => 'Attention : Pb de taille sur un backup<br>Taille min : ' . $tailleMin . 'Mb<br>Taille max :' . $tailleMax . 'Mb'
) , $cache = 0);
if ($AlerteNotifTelegram) $cmdNotifTelegram->execCmd($_options = array(
'message' => 'Attention : problème de taille sur un backup' ."\n" .'Taille min : ' . $tailleMin . 'Mb' ."\n" .'Taille max : ' . $tailleMax . 'Mb'
) , $cache = 0);
}
if ( !$backupToday) {
$erreur = 1 ;
$scenario->setLog('| /!\ Pas de backup aujourd\'hui');
if ($AlerteCentreMsg) message::add('Backup', $site . ' - Pas de backup aujourd\'hui, dernier backup : ' . date('d/m/Y', $timestampLastBackup));
if ($AlerteNotifJC) $cmdNotifJC->execCmd($options = array(
'title' => 'title=Defaut BACKUP ' . $site,
'message' => 'Pas de backup aujourd\'hui<br>Dernier backup : ' . date('d/m/Y', $timestampLastBackup)
) , $cache = 0);
if ($AlerteNotifTelegram) $cmdNotifTelegram->execCmd($_options = array(
'message' => 'Pas de backup aujourd\'hui Dernier backup : ' . date('d/m/Y', $timestampLastBackup)
) , $cache = 0);
}
if ( $erreur == 0 ) {
$scenario->setLog('| Aucun pb de backup aujourd\'hui');
//if ($AlerteCentreMsg) message::add('Backup', $site . ' - Pas de backup aujourd\'hui, dernier backup : ' . date('d/m/Y', $timestampLastBackup));
if ($AlerteNotifJC) $cmdNotifJC->execCmd($options = array(
'title' => 'title=BACKUP ' . $site.' OK',
'message' => 'Aucun pb de backup aujourd\'hui<br>Dernier backup : ' . date('d/m/Y', $timestampLastBackup)
) , $cache = 0);
if ($AlerteNotifTelegram) $cmdNotifTelegram->execCmd($_options = array(
'message' => 'Aucun pb de backup aujourd\'hui Dernier backup : ' . date('d/m/Y', $timestampLastBackup)
) , $cache = 0);
}
}
}
$scenario->setLog('└───────────────────────────');