Add: Add file

This commit is contained in:
2025-09-18 13:19:43 +02:00
parent db5f6bd795
commit 3ee38258b6
5 changed files with 162 additions and 34 deletions

View File

@@ -30,5 +30,48 @@ write_pid_file : complixité cyclonique de 2
remove_file : complixité cyclonique de 1
## Get_ping_from_temp_log
![alt text](image.png)
![alt text](image-1.png)
![alt text](image-2.png)
## Set_stats_ping
## Objectif
- Découper les fonctions en unités simples et réutilisables.
- Réduire limbrication des conditions pour rendre le code plus clair.
- Sécuriser les calculs (éviter la division par zéro).
- Faciliter la maintenance et la lecture.
---
## 🔹 Découpage de `get_ping_from_temp_log`
### 1. `extract_ping_from_line`
- Rôle : soccuper uniquement de lextraction via regex.
- Prend une ligne en entrée.
- Retourne la valeur du ping si trouvée, sinon erreur.
### 2. `read_ping_from_file`
- Rôle : lecture du fichier et appel à `extract_ping_from_line`.
- Parcourt toutes les lignes.
- Retourne le premier ping trouvé ou `NULL`.
### 3. `get_ping_from_temp_log`
- Alias avec le chemin en dur :
```c
return read_ping_from_file("/var/log/ping-report/last-ping.log");
## 🔹 Inversion des conditions: `set_stats_ping`
- Plutot que d'imbriquer des if ... else , on fait if(...){ continue; } quand on veut ignorer une ligne
- Moins de niveaux de blocs if dans un if
- Sécurisation : si nb_ping == 0, la moyenne reste 0.0 => evite une division par zéro

BIN
image-1.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 29 KiB

BIN
image-2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 30 KiB

BIN
image.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

View File

@@ -57,12 +57,43 @@
return 0;
}
/*@null@*/char* read_ping_from_file(const char)
/*@null@*/char* read_ping_from_file(const char* filepath){
FILE* fd = fopen(filepath, "r");
if(fd == NULL) {
return NULL;
}
char* line = NULL;
size_t n = 0;
char* ping = NULL;
while(getline(&line, &n, fd) != -1) {
if(line != NULL) {
if(extract_ping_from_line(line, &ping) == 0) {
// On a trouvé un ping → on arrête
free(line);
line = NULL;
break;
}
}
free(line);
line = NULL;
n = 0;
}
fclose(fd);
return ping;
}
/*@null@*/ char* get_ping_from_temp_log() {
return read_ping_from_file("/var/log/ping-report/last-ping.log");
}
/*
char* get_ping_from_temp_log(){
/*@null@*/char* get_ping_from_temp_log(){
/* Variables */
FILE* fd = NULL;
char* read_line = NULL;
size_t n = 0;
@@ -74,54 +105,54 @@
int end;
size_t size_ping;
/* regex struct memory allocation */
p_reg = (regex_t *) malloc(sizeof(*p_reg));
if(p_reg == NULL){
return ping; /* NULL */
return ping;
}
/* Open ping log file */
fd = fopen("/var/log/ping-report/last-ping.log","r");
if(fd == NULL){
free(p_reg);
return ping; /* NULL */
return ping;
}
/* Construct regex to get ping from log file */
if(regcomp(p_reg,"time=(.*) ms",REG_EXTENDED) != 0){
if(p_reg != NULL){
free(p_reg);
}
(void) fclose(fd);
return ping; /* NULL */
return ping;
}
/* match info memory allocation */
pmatch = malloc(sizeof(*pmatch) * nmatch);
if(pmatch == NULL){
(void) fclose(fd);
regfree(p_reg);
free(p_reg);
return ping; /* NULL */
return ping;
}
/* Read file */
while(getline(&read_line,&n,fd) != -1){
if(read_line == NULL){
break;
}
/* Exec regex to find ping */
if(regexec(p_reg,read_line,nmatch,pmatch,0) == 0){
/* Extract ping position from read line */
start = (int) pmatch[1].rm_so;
end = (int) pmatch[1].rm_eo;
size_ping = (size_t) (end - start);
/* ping string memory allocation */
ping = malloc(sizeof(char) * (size_ping+2));
if(ping == NULL){
free(read_line);
@@ -130,12 +161,10 @@
break;
}
/* Create ping string */
(void) strncpy(ping, &read_line[start], size_ping);
ping[size_ping]='\n';
ping[size_ping+1]='\0';
/* Free memory */
free(read_line);
read_line = NULL;
n = 0;
@@ -147,7 +176,6 @@
n = 0;
}
/* free allocated memory */
regfree(p_reg);
free(p_reg);
free(pmatch);
@@ -157,9 +185,9 @@
(void) fclose(fd);
/* ping may be null, then it must mean that the ping request was lost */
return ping;
}
*/
@@ -211,15 +239,72 @@ void write_ping_log(char* new_ping){
Return value :
None
*/
void set_stats_ping(void) {
FILE* fd = fopen("/var/log/ping-report/all-ping.log", "r");
if (fd == NULL) {
perror("stats : ");
return;
}
double ping, sum = 0.0, max = 0.0, min = 100.0, mean = 0.0;
int nb_high = 0, nb_loss = 0, nb_ping = 0;
char* line = NULL;
size_t n = 0;
while (getline(&line, &n, fd) != -1) {
if (line == NULL) {
continue;
}
/* Cas "LOSS" */
if (strcmp(line, "LOSS") == 0) {
nb_loss++;
free(line);
line = NULL;
n = 0;
continue;
}
/* Conversion en double */
ping = strtod(line, NULL);
free(line);
line = NULL;
n = 0;
/* On ignore les valeurs trop petites */
if (ping < 0.1) {
continue;
}
nb_ping++;
if (ping > max) max = ping;
if (ping < min) min = ping;
if (ping > 100.0) nb_high++;
sum += ping;
}
if (nb_ping > 0) {
mean = sum / (double) nb_ping;
}
fclose(fd);
insert_hourly_report(mean, max, min, nb_high, nb_loss, nb_ping);
if (line != NULL) {
free(line);
}
}
/*
void set_stats_ping(){
/* Variables */
FILE* fd;
/* Open log file */
fd = fopen("/var/log/ping-report/all-ping.log","r");
if(fd != NULL){
/* Stats variables */
double ping = 0.0;
double sum = 0.0;
double max = 0.0;
@@ -231,39 +316,39 @@ void set_stats_ping(){
char* read_line = NULL;
size_t n = 0;
/* Read file */
while(getline(&read_line,&n,fd) != -1){
/* Check getline error */
if(read_line == NULL){
break;
}
/* Check if the ping is flagged as LOSS */
if(strcmp(read_line,"LOSS") == 0){
nb_loss++;
}else{
/* Evaluate the ping as a double */
ping = strtod(read_line,NULL);
/* Test null ping */
if(ping < 0.1){
/* Ignore null ping */
}else{
/* Number of ping readed (for mean calculation) */
nb_ping++;
/* Max ping */
if(ping > max){
max = ping;
}
/* Min ping */
if(ping < min){
min = ping;
}
/* Number of ping above 100 ms */
if(ping > 100.0){
nb_high++;
}
/* Sum (for mean calculation) */
sum += ping;
}
}
@@ -271,7 +356,7 @@ void set_stats_ping(){
n = 0;
}
/* Mean calculation */
mean = sum / (double) nb_ping;
(void) fclose(fd);