Compare commits

2 Commits
main ... main

Author SHA1 Message Date
0130019b59 fin du tp 2025-09-17 21:43:44 +02:00
7ba5104df5 TP2 2025-09-15 17:40:58 +02:00
5 changed files with 167 additions and 139 deletions

BIN
Get_ping_from_temp_log.pdf Normal file

Binary file not shown.

37
Rapport.txt Normal file
View File

@@ -0,0 +1,37 @@
Clémence DUCREUX
Clément JANNAIRE
Rapport TD2 :
daemon.c
create_daemon: complexité 3
ping_request: complexité 2
send_check: complexité 3
check_keep_working: complexité 4
daemon_work: complexité 3
db-sqlite.c
db_connect: complexité 1
db_disconnect: complexité 1
insert_hourly_report: complexité 1
ping-report.c
main: complexité 5
stats.c
get_ping_from_temp_log: complexité 14/15 (graph: Get_ping_from__temp_log.pdf)
write_ping_log: complexité 4
set_stats_ping: complexité 10 (graph: Set_stats_ping.pdf)
utils.c
write_pid_file: complexité 2
remove_file: complexité 1
Pour simplifier get_ping_from_temp_log, l'idée initiale à été de mettre tout de while dans une nouvelle fonction et dans la fonction
de base faire seulement appel à cette nouvelle fonction pour réduire la complixité.
Pour simplifier set_stats_ping, lidée a été de mettre tout le contenu du while dans une nouvelle
fonction process_ping_file, et dans la fonction de base ne garder que louverture du fichier,
lappel à cette sous-fonction et la fermeture, ce qui permet de réduire la complexité.

BIN
Set_stats_ping.pdf Normal file

Binary file not shown.

View File

@@ -22,86 +22,40 @@
Return value :
Ping value as a string or NULL if an error occured
*/
/*@null@*/char* get_ping_from_temp_log(){
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <regex.h>
/* Variables */
FILE* fd = NULL;
/* Sous-fonction : lit le fichier ligne par ligne et retourne le ping si trouvé */
static char* extract_ping_from_file(FILE* fd, regex_t *p_reg, regmatch_t* pmatch, size_t nmatch) {
char* read_line = NULL;
size_t n = 0;
size_t nmatch = 2;
regex_t *p_reg;
regmatch_t* pmatch;
char* ping = NULL;
int start;
int end;
int start, end;
size_t size_ping;
/* regex struct memory allocation */
p_reg = (regex_t *) malloc(sizeof(*p_reg));
if(p_reg == NULL){
return ping; /* NULL */
}
/* Open ping log file */
fd = fopen("/var/log/ping-report/last-ping.log","r");
if(fd == NULL){
free(p_reg);
return ping; /* NULL */
}
/* 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 */
}
/* match info memory allocation */
pmatch = malloc(sizeof(*pmatch) * nmatch);
if(pmatch == NULL){
(void) fclose(fd);
regfree(p_reg);
free(p_reg);
return ping; /* NULL */
}
/* Read file */
while(getline(&read_line,&n,fd) != -1){
if(read_line == NULL){
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 */
if(regexec(p_reg, read_line, nmatch, pmatch, 0) == 0) {
/* Extraire la position du ping */
start = (int) pmatch[1].rm_so;
end = (int) pmatch[1].rm_eo;
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);
read_line = NULL;
n = 0;
break;
/* Allouer mémoire pour le ping */
ping = malloc(sizeof(char) * (size_ping + 2));
if(ping != NULL) {
strncpy(ping, &read_line[start], size_ping);
ping[size_ping] = '\n';
ping[size_ping+1] = '\0';
}
/* 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;
break;
return ping; /* trouvé → on quitte */
}
free(read_line);
@@ -109,20 +63,63 @@
n = 0;
}
/* free allocated memory */
regfree(p_reg);
free(p_reg);
free(pmatch);
if(read_line != NULL){
if(read_line != NULL) {
free(read_line);
}
(void) fclose(fd);
/* ping may be null, then it must mean that the ping request was lost */
return ping;
return ping; /* NULL si non trouvé */
}
/* Fonction principale */
char* get_ping_from_temp_log() {
FILE* fd = NULL;
char* ping = NULL;
size_t nmatch = 2;
regex_t *p_reg;
regmatch_t* pmatch;
/* Allocation regex */
p_reg = (regex_t *) malloc(sizeof(*p_reg));
if(p_reg == NULL){
return NULL;
}
/* Ouvrir fichier log */
fd = fopen("/var/log/ping-report/last-ping.log","r");
if(fd == NULL){
free(p_reg);
return NULL;
}
/* Compiler regex */
if(regcomp(p_reg, "time=(.*) ms", REG_EXTENDED) != 0){
free(p_reg);
fclose(fd);
return NULL;
}
/* Allocation mémoire pour match */
pmatch = malloc(sizeof(*pmatch) * nmatch);
if(pmatch == NULL){
regfree(p_reg);
free(p_reg);
fclose(fd);
return NULL;
}
/* Extraction via la sous-fonction */
ping = extract_ping_from_file(fd, p_reg, pmatch, nmatch);
/* Libérations */
regfree(p_reg);
free(p_reg);
free(pmatch);
fclose(fd);
return ping; /* peut être NULL si pas trouvé */
}
/*
-- write_ping_log --
Desc :
@@ -171,79 +168,73 @@ void write_ping_log(char* new_ping){
Return value :
None
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* Déclaration fournie ailleurs */
void insert_hourly_report(double mean, double max, double min, int nb_high, int nb_loss, int nb_ping);
/* Sous-fonction qui lit le fichier et calcule les stats */
static void process_ping_file(FILE* fd,
double* mean, double* max, double* min,
int* nb_high, int* nb_loss, int* nb_ping)
{
double ping = 0.0;
double sum = 0.0;
char* read_line = NULL;
size_t n = 0;
*max = 0.0;
*min = 100.0;
*nb_high = 0;
*nb_loss = 0;
*nb_ping = 0;
while(getline(&read_line, &n, fd) != -1){
if(read_line == NULL){
break;
}
if(strcmp(read_line,"LOSS") == 0){
(*nb_loss)++;
}else{
ping = strtod(read_line,NULL);
if(ping >= 0.1){ /* on ignore les pings nuls */
(*nb_ping)++;
if(ping > *max) *max = ping;
if(ping < *min) *min = ping;
if(ping > 100.0) (*nb_high)++;
sum += ping;
}
}
free(read_line);
read_line = NULL;
n = 0;
}
if(read_line != NULL){
free(read_line);
}
*mean = (*nb_ping > 0) ? (sum / (double)*nb_ping) : 0.0;
}
void set_stats_ping(){
/* Variables */
FILE* fd;
/* Open log file */
fd = fopen("/var/log/ping-report/all-ping.log","r");
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;
double min = 100.0;
double mean = 0.0;
int nb_high = 0;
int nb_loss = 0;
int nb_ping = 0;
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;
}
double mean, max, min;
int nb_high, nb_loss, nb_ping;
/* 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;
}
}
free(read_line);
n = 0;
}
/* Mean calculation */
mean = sum / (double) nb_ping;
(void) fclose(fd);
/* Appel de la sous-fonction */
process_ping_file(fd, &mean, &max, &min, &nb_high, &nb_loss, &nb_ping);
insert_hourly_report(mean,max,min,nb_high,nb_loss,nb_ping);
fclose(fd);
if(read_line != NULL){
free(read_line);
}
insert_hourly_report(mean, max, min, nb_high, nb_loss, nb_ping);
}else{
perror("stats : ");
}
}